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



  • Hello,

    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.

    I started off with this example (https://github.com/mysensors/MySensorsArduinoExamples/blob/master/examples/BinarySwitchSensor/BinarySwitchSensor.ino) and examples from forums on this site (https://forum.mysensors.org/topic/264/help-with-arrays-and-debouncing). Second one was good for helping me simplify, but still would be too cluttered for 32 pins. I tried my modifications seen below:

    #define MY_DEBUG
    #define MY_GATEWAY_SERIAL
    #define MY_BAUD_RATE 9600
    #include <MySensors.h>
    #include <Bounce2.h>
    
    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);
      }
    }
    
    void presentation() {
      for (byte i = 22; i <= 54; i++) {
        present(i, S_DOOR);
      }
    }
    
    void loop() {
      for (byte i = 22; i <= 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;
        }
      }
    }
    

    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.

    msg[i].setSensor[i];
    msg[i].setType[V_TRIPPED];
    

    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.

    Thank you.


  • Hardware Contributor

    Hello, did you miss the MyMessage.sensor in the public attributes of MyMessage ?
    https://www.mysensors.org/apidocs/classMyMessage.html

    You can just create one message, then before you send it set the sensor attribute then set the value and send. No need to create 32 instances of the message object.



  • like this

    send(msgPir.setSensor(Number).set(State?"1":"0"));
    

  • Contest Winner

    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 ); 
    


  • 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.



  • 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;
        }
      }
    }
    

  • Contest Winner

    @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



  • 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
        }
    

  • Contest Winner

    @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



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


  • Contest Winner

    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.


  • Hardware Contributor

    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


  • 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.


  • Contest Winner

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



  • 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


Log in to reply
 

Suggested Topics

  • 7
  • 6
  • 2
  • 5
  • 9
  • 79
  • 5
  • 1

276
Online

10.1k
Users

10.5k
Topics

108.8k
Posts