What's the best way to set up lots of binary sensors on a single Arduino?
-
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; } } } -
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; } } }@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 } -
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 }@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.
-
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.
-
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.
-
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.
@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