<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0"><channel><title><![CDATA[What&#x27;s the best way to set up lots of binary sensors on a single Arduino?]]></title><description><![CDATA[<p dir="auto">Hello,</p>
<p dir="auto">I have an Arduino device where I need to monitor status of 32 pins for reed sensors. To make things easier, I put everything in for loop as much as I can, but I'm running into some roadblocks and I could use a little help.</p>
<p dir="auto">I started off with this example (<a href="https://github.com/mysensors/MySensorsArduinoExamples/blob/master/examples/BinarySwitchSensor/BinarySwitchSensor.ino" rel="nofollow ugc">https://github.com/mysensors/MySensorsArduinoExamples/blob/master/examples/BinarySwitchSensor/BinarySwitchSensor.ino</a>) and examples from forums on this site (<a href="https://forum.mysensors.org/topic/264/help-with-arrays-and-debouncing">https://forum.mysensors.org/topic/264/help-with-arrays-and-debouncing</a>). Second one was good for helping me simplify, but still would be too cluttered for 32 pins. I tried my modifications seen below:</p>
<pre><code>#define MY_DEBUG
#define MY_GATEWAY_SERIAL
#define MY_BAUD_RATE 9600
#include &lt;MySensors.h&gt;
#include &lt;Bounce2.h&gt;

Bounce debouncer[32];
int oldValue[32];
MyMessage msg[32];

void setup() {
  for (byte i = 22; i &lt;= 54; i++) {
    debouncer[i] = Bounce();
    oldValue[i] = -1;
  }
  for (byte i = 22; i &lt;= 54; i++) {
    pinMode(i, INPUT);
    digitalWrite(i, HIGH);
    debouncer[i].attach(i);
    debouncer[i].interval(5);
  }
}

void presentation() {
  for (byte i = 22; i &lt;= 54; i++) {
    present(i, S_DOOR);
  }
}

void loop() {
  for (byte i = 22; i &lt;= 54; i++) {
    debouncer[i].update();
    int value = debouncer[i].read();
    if (value != oldValue[i]) {
      send(msg[i].set(value == HIGH ? 1 : 0));
      oldValue[i] = value;
    }
  }
}
</code></pre>
<p dir="auto">I understand that since MyMessage objects are created at definition, I can't use "msg (i, V_TRIPPED)" in setup(). I can't seem to use constructor methods from MyMessage.cpp properly either. I tried code below in setup() but that threw error message 'unresolved overloaded function type' for both lines.</p>
<pre><code>msg[i].setSensor[i];
msg[i].setType[V_TRIPPED];
</code></pre>
<p dir="auto">Is there a better way to set up 32 instances of the message object with a for loop? I plan to add more sensors of different types once I get these working, so I would like to keep this in a for loop to make it maintainable.</p>
<p dir="auto">Thank you.</p>
]]></description><link>https://forum.mysensors.org/topic/11577/what-s-the-best-way-to-set-up-lots-of-binary-sensors-on-a-single-arduino</link><generator>RSS for Node</generator><lastBuildDate>Fri, 16 Oct 2026 08:40:55 GMT</lastBuildDate><atom:link href="https://forum.mysensors.org/topic/11577.rss" rel="self" type="application/rss+xml"/><pubDate>Tue, 16 Feb 2021 04:27:09 GMT</pubDate><ttl>60</ttl><item><title><![CDATA[Reply to What&#x27;s the best way to set up lots of binary sensors on a single Arduino? on Fri, 19 Feb 2021 20:55:55 GMT]]></title><description><![CDATA[<p dir="auto">Thanks ordered these from amazon, hoping to get a functional gateway out of them. I'll try to build it tomorrow according to specs and see if I can finally get some results.</p>
<p dir="auto">50pcs 100uf 50V Radial Electrolytic Capacitor 8x12mm<br />
ENC28J60 Ethernet LAN Network Module for Arduino SPI 51 AVR PIC LPC STM32<br />
Makerfire 10pcs Arduino NRF24L01+ 2.4GHz Wireless RF Transceiver Module New<br />
ELEGOO UNO Project Basic Starter Kit with UNO R3</p>
]]></description><link>https://forum.mysensors.org/post/109234</link><guid isPermaLink="true">https://forum.mysensors.org/post/109234</guid><dc:creator><![CDATA[Melih Kulig]]></dc:creator><pubDate>Fri, 19 Feb 2021 20:55:55 GMT</pubDate></item><item><title><![CDATA[Reply to What&#x27;s the best way to set up lots of binary sensors on a single Arduino? on Fri, 19 Feb 2021 17:49:16 GMT]]></title><description><![CDATA[<p dir="auto"><a class="plugin-mentions-user plugin-mentions-a" href="/user/melih-kulig" aria-label="Profile: Melih-Kulig">@<bdi>Melih-Kulig</bdi></a> Don't forget to order some 100uf capacitors, if you don't have them. It helps getting the NRF24L01+ working smoothly</p>
]]></description><link>https://forum.mysensors.org/post/109232</link><guid isPermaLink="true">https://forum.mysensors.org/post/109232</guid><dc:creator><![CDATA[TheoL]]></dc:creator><pubDate>Fri, 19 Feb 2021 17:49:16 GMT</pubDate></item><item><title><![CDATA[Reply to What&#x27;s the best way to set up lots of binary sensors on a single Arduino? on Fri, 19 Feb 2021 17:47:59 GMT]]></title><description><![CDATA[<p dir="auto">Thank you <a class="plugin-mentions-user plugin-mentions-a" href="/user/theol" aria-label="Profile: TheoL">@<bdi>TheoL</bdi></a>  and <a class="plugin-mentions-user plugin-mentions-a" href="/user/nca78" aria-label="Profile: Nca78">@<bdi>Nca78</bdi></a> I ordered a bunch of 1N4148 diodes and I'll see about integrating them sometime. Right now I'm trying to get any sort of output to a gateway and then to home assistant over the network, and once I have a proof of concept, I intend to clean up my hardware work and maybe even look into integrating PCA9555 as Nca78 recommended.</p>
<p dir="auto">I ordered a bunch of those NRF24L01 modules too, I will try that as transport and see if I can get a functional gateway.</p>
]]></description><link>https://forum.mysensors.org/post/109231</link><guid isPermaLink="true">https://forum.mysensors.org/post/109231</guid><dc:creator><![CDATA[Melih Kulig]]></dc:creator><pubDate>Fri, 19 Feb 2021 17:47:59 GMT</pubDate></item><item><title><![CDATA[Reply to What&#x27;s the best way to set up lots of binary sensors on a single Arduino? on Fri, 19 Feb 2021 13:11:06 GMT]]></title><description><![CDATA[<p dir="auto">Hello, if you want to keep it simple and you have no problem soldering SMD components (I don't think it's available as module...), you can use PCA9555, you can use up to 8 of them so 128 inputs/outputs.<br />
Advantages:</p>
<ul>
<li>no need to use an arduino mega any more, a simple atmega328 will have enough pins</li>
<li>it has an interrupt pin that will switch on when value of any input changes, then you can just check the values of input and check which one has changed. This also solves the problem of debouncing, interrupt will only be triggered once before you read the value of inputs, so you just need to wait a bit before you read the values and you have no problem.</li>
<li>low power, only 1uA/IC. If you switch from arduino mega to atmega328 you will probably save more power than what the PCA9555s will use</li>
</ul>
]]></description><link>https://forum.mysensors.org/post/109224</link><guid isPermaLink="true">https://forum.mysensors.org/post/109224</guid><dc:creator><![CDATA[Nca78]]></dc:creator><pubDate>Fri, 19 Feb 2021 13:11:06 GMT</pubDate></item><item><title><![CDATA[Reply to What&#x27;s the best way to set up lots of binary sensors on a single Arduino? on Fri, 19 Feb 2021 10:35:37 GMT]]></title><description><![CDATA[<p dir="auto">It's called a flyback diode. And probably most won't use it on a reed switch. I've discovered that if I open and close a reed switch fast. It sometimes caused my arduino to reset. Since then I use a flyback per read sensor. This occured when I was building a wind speed sensor.<br />
But I'm more a software guy. The hardware specialists can help you much more with this.</p>
<p dir="auto">I have a very old gateway. I believe it's still on MySensors 1.5 -  but nor sure. It's just an Arduino Uno, which is connected to through USB to a Raspberry PI running Domoticz. All nodes have a NRF24L01+ radio. For me that works perfect. I have no issues with the setup.</p>
]]></description><link>https://forum.mysensors.org/post/109220</link><guid isPermaLink="true">https://forum.mysensors.org/post/109220</guid><dc:creator><![CDATA[TheoL]]></dc:creator><pubDate>Fri, 19 Feb 2021 10:35:37 GMT</pubDate></item><item><title><![CDATA[Reply to What&#x27;s the best way to set up lots of binary sensors on a single Arduino? on Fri, 19 Feb 2021 05:06:00 GMT]]></title><description><![CDATA[<p dir="auto"><a class="plugin-mentions-user plugin-mentions-a" href="/user/theol" aria-label="Profile: TheoL">@<bdi>TheoL</bdi></a> Do you recommend putting a diode on each pin that's connected to reed sensor so if multiple reed sensors are on they wouldn't short circuit? Many examples I've seen for reed sensor usage on internet just connect them all together, but none of the examples connect more than a few of them at the same time anyway.</p>
<p dir="auto">Also how do you connect your Arduino to your gateway? I posted another thread about my gateway not picking up anything over serial, but I guess it doesn't work over plain serial ports without an RS485 module, so I need to figure out a reliable way to connect a gateway.</p>
]]></description><link>https://forum.mysensors.org/post/109219</link><guid isPermaLink="true">https://forum.mysensors.org/post/109219</guid><dc:creator><![CDATA[Melih Kulig]]></dc:creator><pubDate>Fri, 19 Feb 2021 05:06:00 GMT</pubDate></item><item><title><![CDATA[Reply to What&#x27;s the best way to set up lots of binary sensors on a single Arduino? on Wed, 17 Feb 2021 11:22:54 GMT]]></title><description><![CDATA[<p dir="auto"><a class="plugin-mentions-user plugin-mentions-a" href="/user/melih-kulig" aria-label="Profile: Melih-Kulig">@<bdi>Melih-Kulig</bdi></a> I can't help you with Home Assistant. I use Domoticz. But in domoticz a Sensor only appears after it has send a value to domoticz. So you could try top open and close one the read sensors just to check if that works for you as well.</p>
<p dir="auto">Don't forget to use protective diodes (sorry forget the English names) in each reed sensor. Not doing it could damage your Arduino mega.</p>
<p dir="auto">Personally I'd split the sensors over multiple nodes. Right now no sensor will work if there's a problem with the arduino or when it's not powered. Using multiples will only result in some not working when there's a problem with on of your Arduinos</p>
]]></description><link>https://forum.mysensors.org/post/109192</link><guid isPermaLink="true">https://forum.mysensors.org/post/109192</guid><dc:creator><![CDATA[TheoL]]></dc:creator><pubDate>Wed, 17 Feb 2021 11:22:54 GMT</pubDate></item><item><title><![CDATA[Reply to What&#x27;s the best way to set up lots of binary sensors on a single Arduino? on Wed, 17 Feb 2021 08:11:50 GMT]]></title><description><![CDATA[<p dir="auto">Thank you <a class="plugin-mentions-user plugin-mentions-a" href="/user/theol" aria-label="Profile: TheoL">@<bdi>TheoL</bdi></a>, I agree, the way you put it makes it look cleaner, only one place to add +22 instead of bunch of -22's. I have one more issue, possibly not the correct section for it, but on Home Assistant I'm not getting any of my sensors. I'm wondering if it has anything to do with the way I upload software onto Arduino. Arduino is in a storage closet, so I use the same Raspberry Pi to both upload software and run as gateway. When I upload, I have to kill the gateway process. Once Arduino upload is complete, Arduino reboots, presentation() goes through before I can start running the gateway process. I doubt this is the issue, I added a presentation() call on a 30 second timer on loop(), didn't seem to change anything. I end up with a Home Assistant json file like below:</p>
<pre><code>{
    "0": {
        "sensor_id": 0,
        "children": {},
        "type": 18,
        "sketch_name": null,
        "sketch_version": null,
        "battery_level": 0,
        "protocol_version": "2.3.2",
        "heartbeat": 0
    }
</code></pre>
]]></description><link>https://forum.mysensors.org/post/109191</link><guid isPermaLink="true">https://forum.mysensors.org/post/109191</guid><dc:creator><![CDATA[Melih Kulig]]></dc:creator><pubDate>Wed, 17 Feb 2021 08:11:50 GMT</pubDate></item><item><title><![CDATA[Reply to What&#x27;s the best way to set up lots of binary sensors on a single Arduino? on Wed, 17 Feb 2021 07:35:44 GMT]]></title><description><![CDATA[<p dir="auto"><a class="plugin-mentions-user plugin-mentions-a" href="/user/melih-kulig" aria-label="Profile: Melih-Kulig">@<bdi>Melih-Kulig</bdi></a> for your for loops in presentation and the loop I'd change some things. right now you are accessing array elements that are out of the array bounds. I don't have much time but the general idea would be:</p>
<pre><code>  for (byte i = 0; i &lt; 22; i++) { // access all 22 elements
    present(i, S_DOOR);
  }
</code></pre>
<p dir="auto">The i is good for accessing the array. The only thing where you want something to change is where you access a pin:</p>
<pre><code>void setup() {
  for (byte i = 0; i &lt;22 i++) {
    debouncer[ i ] = Bounce();
    oldValue[ i  = -1;
    // Setup the button
    pinMode( i + 22, INPUT_PULLUP);// &lt;---- here you want add 22 as the pin offset
    // After setting up the button, setup debouncer
    debouncer[ i ].attach( i + 22 ); // &lt;---- here you want add 22 as the pin offset
    debouncer[ i ].interval(5);
  }
}
</code></pre>
<p dir="auto">This way you don't go out of the bounds. And only for the pins you need to add the offset. For the rest off the code the way I propose makes it much more easy to read.</p>
<p dir="auto">Hope it makes sense</p>
]]></description><link>https://forum.mysensors.org/post/109190</link><guid isPermaLink="true">https://forum.mysensors.org/post/109190</guid><dc:creator><![CDATA[TheoL]]></dc:creator><pubDate>Wed, 17 Feb 2021 07:35:44 GMT</pubDate></item><item><title><![CDATA[Reply to What&#x27;s the best way to set up lots of binary sensors on a single Arduino? on Wed, 17 Feb 2021 02:56:09 GMT]]></title><description><![CDATA[<p dir="auto">Thanks to help from everyone, this compiled correctly and uploaded to Arduino. I will test further with the gateway and see if I run into any issues. Array size of 32 was giving errors on the 32nd element so I increased it to 48 to leave some room for future sensors as well.</p>
<pre><code>#define MY_DEBUG
#define MY_GATEWAY_SERIAL
#define MY_BAUD_RATE 9600
#include &lt;MySensors.h&gt;
#include &lt;Bounce2.h&gt;

Bounce debouncer[48];
int oldValue[48];
MyMessage msg(0,V_TRIPPED);

void setup() {
  for (byte i = 22; i &lt;= 54; i++) {
    uint8_t arrayIndex=i-22;
    debouncer[arrayIndex] = Bounce();
    oldValue[arrayIndex] = -1;
    // Setup the button
    pinMode(i, INPUT_PULLUP);
    // After setting up the button, setup debouncer
    debouncer[arrayIndex].attach(i);
    debouncer[arrayIndex].interval(5);
  }
}

void presentation() {
  for (byte i = 22; i &lt;= 54; i++) {
    present(i, S_DOOR);
  }
}

void loop() {
  for (byte i = 22; i &lt;= 54; i++) {
    uint8_t arrayIndex=i-22;
    debouncer[arrayIndex].update();
    // Get the update value
    int value = debouncer[arrayIndex].read();
    if (value != oldValue[arrayIndex]) {
      // Send in the new value
      send(msg.setSensor(i).set(value == HIGH ? 1 : 0));
      oldValue[arrayIndex] = value;
    }
  }
}
</code></pre>
]]></description><link>https://forum.mysensors.org/post/109187</link><guid isPermaLink="true">https://forum.mysensors.org/post/109187</guid><dc:creator><![CDATA[Melih Kulig]]></dc:creator><pubDate>Wed, 17 Feb 2021 02:56:09 GMT</pubDate></item><item><title><![CDATA[Reply to What&#x27;s the best way to set up lots of binary sensors on a single Arduino? on Wed, 17 Feb 2021 01:29:37 GMT]]></title><description><![CDATA[<p dir="auto">Thank you for all your responses. <a class="plugin-mentions-user plugin-mentions-a" href="/user/nca78" aria-label="Profile: Nca78">@<bdi>Nca78</bdi></a> I didn't miss the public attributes, I just didn't see that api documentation until you linked it, and even then I'm at best mediocre at C, I can read quite a bit of code but I'm poor at coming up with complete solutions myself. I will go through the apidocs, I would love to be able to contribute back if I'm able. Thank you for the complete code line <a class="plugin-mentions-user plugin-mentions-a" href="/user/cabat" aria-label="Profile: cabat">@<bdi>cabat</bdi></a>, I will definitely try it out.</p>
<p dir="auto"><a class="plugin-mentions-user plugin-mentions-a" href="/user/theol" aria-label="Profile: TheoL">@<bdi>TheoL</bdi></a> I'm using an Arduino Mega, thank you so much for pointing out issues I'm likely to have. I've been trying to fix errors one line at a time, and I'm nowhere near a C expert. I'm good at copy/paste and hacking code together, but not so good at fixing harder to see issues such as those. My home has wired reed sensors, there's about 32 wired reed sensors, a few wired motion sensors which I haven't tried connecting yet, and audible alarm. I didn't want to subscribe to a monthly service, wanted to come up with a custom solution that can integrate with other smart solutions in my house. Originally this was going to be a programming project for me, but the scope increased, and now I want to integrate this with Home Assistant to have useful solutions instead of pet projects.</p>
<p dir="auto">Wiring is extremely straightforward, I have 32 of the reed sensors. One wire from each sensor is connected to Arduino ground, and I used the double row of pins (22-54) to connect the other wire from the sensors. Arduino is connected to a Raspberry Pi through USB, which is the gateway, and I have Home Assistant running on an ESXi VM on my home server which will function as the controller. Once I get these running, I intend to add 2-3 fixed wired reed sensors to the same Arduino, and after those another 10-15 pir sensors through a wireless connection to the gateway, if I can figure out how to compile the gateway for both r485 and some flavor of rf.</p>
]]></description><link>https://forum.mysensors.org/post/109188</link><guid isPermaLink="true">https://forum.mysensors.org/post/109188</guid><dc:creator><![CDATA[Melih Kulig]]></dc:creator><pubDate>Wed, 17 Feb 2021 01:29:37 GMT</pubDate></item><item><title><![CDATA[Reply to What&#x27;s the best way to set up lots of binary sensors on a single Arduino? on Tue, 16 Feb 2021 23:14:47 GMT]]></title><description><![CDATA[<p dir="auto">I'm sure you have a good reason for hooking up that many devices. Just wanted to point out that the soft debounce is kind of tricky. Since it's blocking your code for 32 * 5 ms. Which is pretty long for a micro controller.</p>
<p dir="auto">Also curious how you get that many pins? Which board are you using?</p>
<pre><code>Bounce debouncer[32];
int oldValue[32];
MyMessage msg[32];

void setup() {
  for (byte i = 22; i &lt;= 54; i++) {
    debouncer[i] = Bounce();
    oldValue[i] = -1;
  }
  for (byte i = 22; i &lt;= 54; i++) {
    pinMode(i, INPUT);
    digitalWrite(i, HIGH);
    debouncer[i].attach(i);
    debouncer[i].interval(5);
  }
}
</code></pre>
<p dir="auto">You have a possible memory allocation problems with this part. The index of the first element in an array is always 0. You start at index 22 and end at 54. And the two for loops could be combined.</p>
<p dir="auto">like so.</p>
<pre><code>Bounce debouncer[32];
int oldValue[32];
MyMessage msg[32];

void setup() {
  for (byte i = 22; i &lt;= 54; i++) { // I assume you start at 22 for an offset
    debouncer[i - 22 ] = Bounce();
    oldValue[ i - 22] = -1;

    pinMode( i, INPUT); // &lt;------ pin 22? That one is not available on any uno, nano or micro
    digitalWrite( i, HIGH); // &lt;--- I think you want to use INPUT_PULLUP in the pinMode
    debouncer[i - 22].attach(i);
    debouncer[i - 22].interval(5);
  }
}
</code></pre>
<p dir="auto">All I did was fixing the wrong Array indexes. Maybe it's a good idea to post a wiring diagram of how you attach things. I might be able to help you more</p>
<p dir="auto">I also think you want to use () instead of []</p>
<pre><code>msg[i].setSensor[i];    // &lt;--- you are trying to call an element of the array setSensor
msg[i].setType[V_TRIPPED]; // &lt;--- same, calling an aray
</code></pre>
<p dir="auto">These are functions and functions use ()</p>
<pre><code>msg[i].setSensor( i );
msg[i].setType( V_TRIPPED ); 
</code></pre>
]]></description><link>https://forum.mysensors.org/post/109186</link><guid isPermaLink="true">https://forum.mysensors.org/post/109186</guid><dc:creator><![CDATA[TheoL]]></dc:creator><pubDate>Tue, 16 Feb 2021 23:14:47 GMT</pubDate></item><item><title><![CDATA[Reply to What&#x27;s the best way to set up lots of binary sensors on a single Arduino? on Tue, 16 Feb 2021 17:10:23 GMT]]></title><description><![CDATA[<p dir="auto">like this</p>
<pre><code>send(msgPir.setSensor(Number).set(State?"1":"0"));
</code></pre>
]]></description><link>https://forum.mysensors.org/post/109181</link><guid isPermaLink="true">https://forum.mysensors.org/post/109181</guid><dc:creator><![CDATA[cabat]]></dc:creator><pubDate>Tue, 16 Feb 2021 17:10:23 GMT</pubDate></item><item><title><![CDATA[Reply to What&#x27;s the best way to set up lots of binary sensors on a single Arduino? on Tue, 16 Feb 2021 11:10:26 GMT]]></title><description><![CDATA[<p dir="auto">Hello, did you miss the <em>MyMessage.sensor</em> in the public attributes of <em>MyMessage</em> ?<br />
<a href="https://www.mysensors.org/apidocs/classMyMessage.html" rel="nofollow ugc">https://www.mysensors.org/apidocs/classMyMessage.html</a></p>
<p dir="auto">You can just create one message, then before you send it set the <em>sensor</em> attribute then set the value and send. No need to create 32 instances of the message object.</p>
]]></description><link>https://forum.mysensors.org/post/109179</link><guid isPermaLink="true">https://forum.mysensors.org/post/109179</guid><dc:creator><![CDATA[Nca78]]></dc:creator><pubDate>Tue, 16 Feb 2021 11:10:26 GMT</pubDate></item></channel></rss>