Go Down

Topic: Code for relay  (Read 130 times) previous topic - next topic

c6Asif

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

pcbbc

#1
Jun 10, 2020, 08:11 am Last Edit: Jun 10, 2020, 08:14 am by pcbbc
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

gcjr

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


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.

Code: [Select]

#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)
{

}
greg - somerset, nj

johnwasser

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.
Send Bitcoin tips to: 1G2qoGwMRXx8az71DVP1E81jShxtbSh5Hp
See who has no social life: https://forum.arduino.cc/index.php?action=stats :)

JCA34F

Set the pin HIGH first, then set to output:
Code: [Select]
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.

Go Up