Inconsistency between builtin led and led from relay

Hi there
I am new here. I am trying to control an 8 channel relay but find a problem I am still unable to understand. When programming the builtin led and one of the leds from the relay telling them both to go LOW, the builtin works as intended, but the one from the relay does just the opposite. Here's my code:

int relay_1 = 2;

void setup() {
  // initialize digital pin LED_BUILTIN as an output.
  pinMode(LED_BUILTIN, OUTPUT);
  pinMode(relay_1, OUTPUT);
  Serial.begin(9600);
}

void loop() {
  digitalWrite(LED_BUILTIN, HIGH);  // turn the LED on (HIGH is the voltage level)
  digitalWrite(relay_1, HIGH);
  Serial.println("Relays on board ON");
  delay(5000);                      // wait for 5 seconds
  
  digitalWrite(LED_BUILTIN, LOW);   // turn the LED off by making the voltage LOW
  digitalWrite(relay_1, LOW);
  Serial.println("Relays on board OFF");
  delay(10000);                      // wait for 10 seconds
}

So builtin led turns on and external led turns off, and viceversa.
Any suggestions?

Perfectly normal. That's just how the LEDs are wired!

For confirmation, please let us know what model of arduino, and give a link to the relay board.

Yes, most relay boards work by putting logic low on the output, and depending on how you wire up the LED, then the most common way here is to wire them so a high turns them on.

This is how a 4-channel relay schematic looks like. It should solve your doubts

Arduino is a Uno R4 Wifi. Relay board is unknown (had it lying around, worked as intended with a Pi). Single relays are Songle or Soncle, SRD-5VDC-SL-C.
So changing

  digitalWrite(LED_BUILTIN, HIGH);  // turn the LED on (HIGH is the voltage level)
  digitalWrite(relay_1, HIGH);

to

  digitalWrite(LED_BUILTIN, HIGH);  // turn the LED on (HIGH is the voltage level)
  digitalWrite(relay_1, LOW);

does the trick.
Thanks!

I find activating relays (or anything) with LOW as counterintuitive , and that keeps playing tricks with my mind, so I prefer something like

#define RELAY_ON LOW
#define RELAY_OFF HIGH
...
digitalWrite(relayPin,RELAY_ON); 

and also

turnOn(relayPin);
...
void turnOn(pin) {
  digitalWrite(pin,LOW); // this relay module works like this
}

You're welcome.

As far say I can tell, the schematic for your board shows that the LED_BUILTIN pin drives the gate of transistor Q3. When the pin is HIGH, Q3 turns on, current can flow from 5V through the yellow led, R25 and Q3 to ground. So a HIGH signal from the pin will light the led.

On the relay board, if the pin connected to IN1 is LOW, current can flow from Vcc, through R1, the opto-isolator U1, through led IN1 into the Arduino pin to ground. So when the pin is LOW, the led is on.

One thing to watch out for with LOW true relays. On startup all pins are set to 0 (LOW) by default, so when you set pinMode to OUTPUT in setup(), the relay will come ON until you digitalWrite it HIGH in the program.
To avoid that, do this in setup():

digitalWrite(relayPin,HIGH; // set pin HIGH before setting to OUTPUT
pinMode(relayPin,OUTPUT); // relay is OFF on program entry

What are you talking about? Relay boards have inputs not outputs.
An on an Arduino all pin are set to input by default.

2 Likes

Let me rephrase that:

On startup all MCU pins are set to input and (LOW) by default.

Happy now Grump? :grin:

No not at all. That is not true.
On start up ALL pins are set to be inputs. Inputs are not low.

They float.

Where do you get this wired miss information from?

Keep it polite and respectful please.

2 Likes

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.