Skip to content
  • MySensors
  • OpenHardware.io
  • Categories
  • Recent
  • Tags
  • Popular
Skins
  • Light
  • Brite
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Brand Logo
  1. Home
  2. Development
  3. What's the best way to set up lots of binary sensors on a single Arduino?

What's the best way to set up lots of binary sensors on a single Arduino?

Scheduled Pinned Locked Moved Development
15 Posts 4 Posters 229 Views 4 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • C Offline
    C Offline
    cabat
    wrote on last edited by
    #3

    like this

    send(msgPir.setSensor(Number).set(State?"1":"0"));
    
    1 Reply Last reply
    0
    • TheoLT Offline
      TheoLT Offline
      TheoL
      Contest Winner
      wrote on last edited by TheoL
      #4

      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.

      Also curious how you get that many pins? Which board are you using?

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

      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.

      like so.

      Bounce debouncer[32];
      int oldValue[32];
      MyMessage msg[32];
      
      void setup() {
        for (byte i = 22; i <= 54; i++) { // I assume you start at 22 for an offset
          debouncer[i - 22 ] = Bounce();
          oldValue[ i - 22] = -1;
      
          pinMode( i, INPUT); // <------ pin 22? That one is not available on any uno, nano or micro
          digitalWrite( i, HIGH); // <--- I think you want to use INPUT_PULLUP in the pinMode
          debouncer[i - 22].attach(i);
          debouncer[i - 22].interval(5);
        }
      }
      

      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

      I also think you want to use () instead of []

      msg[i].setSensor[i];    // <--- you are trying to call an element of the array setSensor
      msg[i].setType[V_TRIPPED]; // <--- same, calling an aray
      

      These are functions and functions use ()

      msg[i].setSensor( i );
      msg[i].setType( V_TRIPPED ); 
      
      Melih KuligM 1 Reply Last reply
      0
      • TheoLT TheoL

        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.

        Also curious how you get that many pins? Which board are you using?

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

        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.

        like so.

        Bounce debouncer[32];
        int oldValue[32];
        MyMessage msg[32];
        
        void setup() {
          for (byte i = 22; i <= 54; i++) { // I assume you start at 22 for an offset
            debouncer[i - 22 ] = Bounce();
            oldValue[ i - 22] = -1;
        
            pinMode( i, INPUT); // <------ pin 22? That one is not available on any uno, nano or micro
            digitalWrite( i, HIGH); // <--- I think you want to use INPUT_PULLUP in the pinMode
            debouncer[i - 22].attach(i);
            debouncer[i - 22].interval(5);
          }
        }
        

        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

        I also think you want to use () instead of []

        msg[i].setSensor[i];    // <--- you are trying to call an element of the array setSensor
        msg[i].setType[V_TRIPPED]; // <--- same, calling an aray
        

        These are functions and functions use ()

        msg[i].setSensor( i );
        msg[i].setType( V_TRIPPED ); 
        
        Melih KuligM Offline
        Melih KuligM Offline
        Melih Kulig
        wrote on last edited by
        #5

        Thank you for all your responses. @Nca78 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 @cabat, I will definitely try it out.

        @TheoL 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.

        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.

        1 Reply Last reply
        0
        • Melih KuligM Offline
          Melih KuligM Offline
          Melih Kulig
          wrote on last edited by
          #6

          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.

          #define MY_DEBUG
          #define MY_GATEWAY_SERIAL
          #define MY_BAUD_RATE 9600
          #include <MySensors.h>
          #include <Bounce2.h>
          
          Bounce debouncer[48];
          int oldValue[48];
          MyMessage msg(0,V_TRIPPED);
          
          void setup() {
            for (byte i = 22; i <= 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 <= 54; i++) {
              present(i, S_DOOR);
            }
          }
          
          void loop() {
            for (byte i = 22; i <= 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;
              }
            }
          }
          
          TheoLT 1 Reply Last reply
          0
          • Melih KuligM Melih Kulig

            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.

            #define MY_DEBUG
            #define MY_GATEWAY_SERIAL
            #define MY_BAUD_RATE 9600
            #include <MySensors.h>
            #include <Bounce2.h>
            
            Bounce debouncer[48];
            int oldValue[48];
            MyMessage msg(0,V_TRIPPED);
            
            void setup() {
              for (byte i = 22; i <= 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 <= 54; i++) {
                present(i, S_DOOR);
              }
            }
            
            void loop() {
              for (byte i = 22; i <= 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;
                }
              }
            }
            
            TheoLT Offline
            TheoLT Offline
            TheoL
            Contest Winner
            wrote on last edited by TheoL
            #7

            @Melih-Kulig 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:

              for (byte i = 0; i < 22; i++) { // access all 22 elements
                present(i, S_DOOR);
              }
            

            The i is good for accessing the array. The only thing where you want something to change is where you access a pin:

            void setup() {
              for (byte i = 0; i <22 i++) {
                debouncer[ i ] = Bounce();
                oldValue[ i  = -1;
                // Setup the button
                pinMode( i + 22, INPUT_PULLUP);// <---- here you want add 22 as the pin offset
                // After setting up the button, setup debouncer
                debouncer[ i ].attach( i + 22 ); // <---- here you want add 22 as the pin offset
                debouncer[ i ].interval(5);
              }
            }
            

            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.

            Hope it makes sense

            1 Reply Last reply
            0
            • Melih KuligM Offline
              Melih KuligM Offline
              Melih Kulig
              wrote on last edited by
              #8

              Thank you @TheoL, 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:

              {
                  "0": {
                      "sensor_id": 0,
                      "children": {},
                      "type": 18,
                      "sketch_name": null,
                      "sketch_version": null,
                      "battery_level": 0,
                      "protocol_version": "2.3.2",
                      "heartbeat": 0
                  }
              
              TheoLT 1 Reply Last reply
              0
              • Melih KuligM Melih Kulig

                Thank you @TheoL, 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:

                {
                    "0": {
                        "sensor_id": 0,
                        "children": {},
                        "type": 18,
                        "sketch_name": null,
                        "sketch_version": null,
                        "battery_level": 0,
                        "protocol_version": "2.3.2",
                        "heartbeat": 0
                    }
                
                TheoLT Offline
                TheoLT Offline
                TheoL
                Contest Winner
                wrote on last edited by
                #9

                @Melih-Kulig 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.

                Don't forget to use protective diodes (sorry forget the English names) in each reed sensor. Not doing it could damage your Arduino mega.

                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

                1 Reply Last reply
                0
                • Melih KuligM Offline
                  Melih KuligM Offline
                  Melih Kulig
                  wrote on last edited by
                  #10

                  @TheoL 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.

                  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.

                  1 Reply Last reply
                  0
                  • TheoLT Offline
                    TheoLT Offline
                    TheoL
                    Contest Winner
                    wrote on last edited by TheoL
                    #11

                    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.
                    But I'm more a software guy. The hardware specialists can help you much more with this.

                    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.

                    1 Reply Last reply
                    0
                    • Nca78N Offline
                      Nca78N Offline
                      Nca78
                      Hardware Contributor
                      wrote on last edited by Nca78
                      #12

                      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.
                      Advantages:

                      • no need to use an arduino mega any more, a simple atmega328 will have enough pins
                      • 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.
                      • 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
                      1 Reply Last reply
                      1
                      • Melih KuligM Offline
                        Melih KuligM Offline
                        Melih Kulig
                        wrote on last edited by
                        #13

                        Thank you @TheoL and @Nca78 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.

                        I ordered a bunch of those NRF24L01 modules too, I will try that as transport and see if I can get a functional gateway.

                        TheoLT 1 Reply Last reply
                        0
                        • Melih KuligM Melih Kulig

                          Thank you @TheoL and @Nca78 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.

                          I ordered a bunch of those NRF24L01 modules too, I will try that as transport and see if I can get a functional gateway.

                          TheoLT Offline
                          TheoLT Offline
                          TheoL
                          Contest Winner
                          wrote on last edited by
                          #14

                          @Melih-Kulig Don't forget to order some 100uf capacitors, if you don't have them. It helps getting the NRF24L01+ working smoothly

                          1 Reply Last reply
                          1
                          • Melih KuligM Offline
                            Melih KuligM Offline
                            Melih Kulig
                            wrote on last edited by
                            #15

                            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.

                            50pcs 100uf 50V Radial Electrolytic Capacitor 8x12mm
                            ENC28J60 Ethernet LAN Network Module for Arduino SPI 51 AVR PIC LPC STM32
                            Makerfire 10pcs Arduino NRF24L01+ 2.4GHz Wireless RF Transceiver Module New
                            ELEGOO UNO Project Basic Starter Kit with UNO R3

                            1 Reply Last reply
                            0
                            Reply
                            • Reply as topic
                            Log in to reply
                            • Oldest to Newest
                            • Newest to Oldest
                            • Most Votes


                            41

                            Online

                            11.7k

                            Users

                            11.2k

                            Topics

                            113.1k

                            Posts


                            Copyright 2025 TBD   |   Forum Guidelines   |   Privacy Policy   |   Terms of Service
                            • Login

                            • Don't have an account? Register

                            • Login or register to search.
                            • First post
                              Last post
                            0
                            • MySensors
                            • OpenHardware.io
                            • Categories
                            • Recent
                            • Tags
                            • Popular