Hi guys
I am new to the world of arduino and working on relays.
I have just experienced that relays work opposite means when the pin is high relay is off and vice versa.
What changes I need to make in my code so that when the arduino reboot all the relays are off on default.
Thanks
You’ll need to post some code if you want an exact answer.
But in setup....
Set all relay pins to output.
Write HIGH to those pins.
There is no way to have reset automatically do that, you must write it in your code. So there will very likely be a glitch as you reset, or if you hold the Arduino in reset.
There are SPDT relays that can switch both ways. Perhaps better select one of them if that is your requirement.
Edit: Alternatively, if you are already using one of them, either:
a) connect to the other output instead
b) accept this is how relays work and invert the logic in your program so HIGH=OFF, LOW=ON
c6Asif:
I have just experienced that relays work opposite means when the pin is high relay is off and vice versa.
both inputs and outputs can be active low or active high. it's common for switches connected to an input pin with a pull-up resistor to pull the output low when pressed. The "ON" state of an output can be either high or low. The code below used a #define to specify whether ON/OFF is HIGH or LOW, making the code easier to read as well as change
c6Asif:
What changes I need to make in my code so that when the arduino reboot all the relays are off on default.
before a pin is configured as an OUTPUT, it doesn't have enough power to driver a relay. So simply programming the output state of the pin before configuring it as an OUTPUT will initialize it.
#define ON LOW
#define OFF HIGH
byte sensors [] = { A1, A2, A3 };
byte outputs [] = { 10, 11, 12 };
// -----------------------------------------------------------------------------
void setup (void)
{
Serial.begin (115200);
for (unsigned i = 0; i < sizeof(sensors); i++)
pinMode (sensors [i], INPUT_PULLUP);
for (unsigned i = 0; i < sizeof(outputs); i++) {
digitalWrite (outputs [i], OFF);
pinMode (outputs [i], OUTPUT);
}
}
// -----------------------------------------------------------------------------
void
loop (void)
{
}
gcjr:
before a pin is configured as an OUTPUT, it doesn't have enough power to driver a relay. So simply programming the output state of the pin before configuring it as an OUTPUT will initialize it.
You may need to put a pull-up resistor on the input to the relay module if it doesn't have one already. When the processor resets and all of the pins turn to inputs you don't want to have any of those pins float LOW and turn on your relay.
You can tell if the input pin has a pull-up resistor by measuring the current from the pin to Ground. If it's nearly zero the it doesn't. If its above about 0.2 mA then you should be OK. I'd expect 0.5 mA (10k Ohm pull-up to 5V).
You may have to avoid the LED_BUILTIN pin (Pin 13 on the UNO) because the bootloader might blink the LED.
Set the pin HIGH first, then set to output:
void setup()
{
digitalWrite(relayPin,HIGH);
pinMode(relayPin,OUTPUT);
If relay still clicks on at startup, then, as @johnwasser said, put a 10k pullup resistor from pin to 5V.