RELAY_ON 0 or 1?
-
I am running the relay sketch and I am having an issue with checking if the relay is HIGH or LOW. I don't have a whole lot of experience with these arduinos but shouldn't RELAY_ON == 1 (LOW) and off == 0 (HIGH)
#define RELAY_ON 0 #define RELAY_OFF 1
When using the example relay state, RELAY_OFF sets the relay to off but when I check the PIN state via digitalRead(RELAY_1) I get the opposite of what I expect.
digitalRead(RELAY_1) returns HIGH when the relay is OFF and LOW when the relay is ON. I have tried changing the state definitions to the reverse of what is in the example but this results in the relay turning ON when it should be OFF. Anyone know what might be causing this?
#define RELAY_ON 1 #define RELAY_OFF 0
-
If i think correct some relays are activated by high state, but some by low state. If i'm wrong, please correct me.
-
After more searching, I see the relay I am using is ACTIVE LOW. Which means it acts in reverse of an ACTIVE HIGH relay. I guess I need to reverse the value from digitalread before updating the GW.
digitalRead(RELAY_1+i)==1?0:1
-
Yes you got it, some devices are active low.
(Just to confuse things, some relays have double throw outputs, meaning that one pair of conductors is shorted when the relay is powered - "Normally Open/NO" - and one when it's not powered - "Normally Closed/NC" (with one of those contact being common to both).
So sometimes you might power a device with the NC connection of the relay output, meaning the device loses power when the relay is powered. One reason do so this would be if you want the device to be powered even if the microcontroller was disabled.
Anyway, take that into account too, and return 1 if after all the possible inversions, the hardware device is powered on.
For code, you could just return !digitalRead(...), where the NOT operator ! will convert 0 into 1, and 1 into 0 more simply than the ?: syntax (actually it will change any non-zero value to zero, not just 1).
Suggested Topics
-
Day 1 - Status report
Announcements • 23 Mar 2014, 22:45 • hek 24 Mar 2014, 20:12 -
Raspberry Pi 5: invalid GPIO 9
Troubleshooting • 27 Aug 2024, 13:20 • igo 27 Aug 2024, 13:20 -
Arduino nano and Uno in same My sensors network with NRF24L01+
Troubleshooting • 26 Jul 2024, 10:30 • DIY89 27 Aug 2024, 09:59 -
Forum Search not working?
Troubleshooting • 4 Oct 2023, 23:33 • Gibber 2 Sept 2024, 20:28 -
Compiling Sensor code using BME280 and ESP8266
Troubleshooting • 12 days ago • dpcons 12 days ago -
Can not compile MySensors on esp8266
Troubleshooting • 24 Aug 2024, 15:35 • TheoL 29 Aug 2024, 20:47