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.


  • Hero Member


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

Log in to reply

Suggested Topics