Unable to blink one relay and keep the other off

I am wondering if I have a faulty relay module. Any help would be greatly appreciated.

IN1 and IN2 on the relay connected to pin 9 and 10 vcc connected to VCC on the Arduino GND connected to GND on the Arduino

This code lets me alternate between relay1 and relay2 and it works just fine.


define red_light 10

define green_light 9

int ledState = LOW; int ledState2 = LOW;

void setup() { pinMode(green_light, OUTPUT); pinMode(red_light, OUTPUT); }

void loop() { if (ledState == LOW){ ledState = HIGH; ledState2 = LOW; } else{ ledState = LOW; ledState2 = HIGH; } delay(5000); digitalWrite(green_light, ledState2); digitalWrite(red_light, ledState);

delay(300); }


The problem is when I try to blink red and keep green off.

if (ledState == LOW){ ledState = HIGH; ledState2 = LOW; } else{ ledState = LOW; ledState2 = LOW; } delay(5000); digitalWrite(green_light, ledState2); digitalWrite(red_light, ledState);


Green remains on. I even did Serial.println(ledState2); and it showed "0" but green remains on no matter what I do. Red blinks as designed but green stays on.

Thanks in advance for the help.

Are you sure that a LOW turns it off? Try this:

#define red_light 10
#define green_light 9
int ledState = HIGH;
int ledState2 = HIGH;

void setup() {
  pinMode(green_light, OUTPUT);
  pinMode(red_light, OUTPUT);
}

void loop() {
   if (ledState == LOW){
      ledState = HIGH;
      ledState2 = HIGH;
    }
    else{
      ledState = LOW;
      ledState2 = HIGH;
    }
    delay(5000);
    digitalWrite(green_light, ledState2);
    digitalWrite(red_light, ledState);


  delay(300);
}

Thank you so much !!! you rock it works now.

Just can't figure out how it partially worked with the trigger backwards

Basically, your relay board is wired so that LOW turns a relay on, and HIGH turns it off. That's it.