question about using an 8 relay board

I have set up a relay board (code below). The problem I have is when I reset the Arduino to run, it turns on all the relays first and then shuts each one off (per code) and runs correctly. I have the relay board wired to the Arduino 5v and ground.

How to I stop the initial turn on of all relays?

const int relay1=4;  // incoming turnout straight
const int relay2=5;  // incoming turnout curve
const int relay3=6;  // outgoing turnout straight
const int relay4=7;  // outgoing turnout curve
#define RELAY_ON 0
#define RELAY_OFF 1
   
void setup() {
  
  pinMode(relay1, OUTPUT);
  pinMode(relay2, OUTPUT);
  pinMode(relay3, OUTPUT);
  pinMode(relay4, OUTPUT);
  
}
void loop() {
 
  digitalWrite(relay1, RELAY_OFF);// set the Relay OFF
  delay (500);
digitalWrite(relay2, RELAY_OFF);// set the Relay OFF
  delay (500);
digitalWrite(relay3, RELAY_OFF);// set the Relay OFF
  delay (500);
digitalWrite(relay4, RELAY_OFF);// set the Relay OFF
  delay (500);
  
  
  
  digitalWrite(relay1, RELAY_ON);// set the Relay ON
  delay(3000);              // wait for a second
  digitalWrite(relay1, RELAY_OFF);// set the Relay OFF
  delay (3000);
  digitalWrite(relay3, RELAY_ON);// set the Relay ON
  delay(3000);              // wait for a second
  digitalWrite(relay3, RELAY_OFF);// set the Relay OFF
  delay (3000);
  digitalWrite(relay2, RELAY_ON);// set the Relay ON
  delay(3000);              // wait for a second
  digitalWrite(relay2, RELAY_OFF);// set the Relay OFF
  delay (3000);
  digitalWrite(relay4, RELAY_ON);// set the Relay ON
  delay(3000);              // wait for a second
  digitalWrite(relay4, RELAY_OFF);// set the Relay OFF 
  delay (3000);
  }
//--(end main loop )---

Two options:
a) Don’t use a stupic opto relay board
b) Use a pull down resistor on the output of the Arduino.

And some tips:
a) And have a look at arrays and bytes :wink:

const byte RelayPins[] = {4, 5, 6, 7};

#define RELAY_ON 0
#define RELAY_OFF 1
   
void setup() {
  for(byte i = 0; i < sizeof(RelayPins); i++){
    pinMode(RelayPins[i], OUTPUT);
    digitalWrite(RelayPins[i], RELAY_OFF);
  }
}

b) Straight and cruved sounds like model railroad switches. It’s even simpler to just use a mosfet.

c) Avoid delay() if you want to be able to respond to something. Aka, after the Blink-sketch you never ever want to use it again :wink:

I'm very new at this. When you say use a pull down resistor on the output, how is that wired up? Right now I have the output pins going directly to the relay inputs and the 5v and ground going from the Arduino directly to the + and ground on the relay board.

  1. Get yourself 8 10k resistor (or 1k or anything in between 1k and 100k)
  2. One end to the 5V rail
  3. Other end to the output pin of the Arduino
  4. Repeat for all 8 outputs

The way it works:
Because those stupid opto relay boards turn on the relay when the output is low and off when high, what happens is as long as the Arduino did not turn the pin into an output and turned it on it's a input pin. And the relay board pulls this pin low thus turning it on. The resistor will pull the pin high even when the Arduino still has it set as input thus turning off the relay.

I'm confused. Is this the correct wiring:

+5v on arduino to +5v on relay board
GND on arduino to GND on relay board
Pin output on arduino to input (1 to 8) on relay board
Resistor from same output pin to +5v on either relay board or Arduino.

if you do a digitatWrite HIGH to the pin before you pinMode. I had the same problem and this worked well. If the pin is already set to HIGH before pinMode sets it to OUTPUT, the pin will still be in a high impedance state and will not engage the relay. The relay actuates when driven to ground.

digitalWrite (4, HIGH);  // Still High impedance -  no output or ground
digitalWrite (5, HIGH);  // Still High impedance -  no output or ground      
digitalWrite (6, HIGH);  // Still High impedance -  no output or ground
digitalWrite (7, HIGH);  // Still High impedance -  no output or ground     
pinMode(4, OUTPUT);   // Goes HIGH - not driving the relay            
pinMode(5, OUTPUT);   // Goes HIGH - not driving the relay              
pinMode(6, OUTPUT);   // Goes HIGH - not driving the relay              
pinMode(7, OUTPUT)    // Goes HIGH - not driving the relay; 
// more code
// more code
digitalWrite (4, LOW);  //Relay ON
digitalWrite (4, HIGH); //Relay OFF

you do not need pullup resistors.
In the above example, I have run 16 relays with no problem.

In the beginning I had the same problem as you had.

I didn’t know you could digitalWrite before you do a pinMode but I do now.

If you do digitalWrite (pinX, HIGH);
when the pin is an Input I believe it will enable the internal pullup resistor with its nominal resistance of 30K to 50K.
You can put a 50K from the pin to Gnd and see if it measures around 2.5V to confirm, forming a simple resistor divider from 5V thru internal pullup thru external pulldown to Gnd.
If it measures closer to 0V, then you can surmise the pullup has Not been enabled.

No need to test that, digitalWrite(pin, HIGH) when a pin is input indeed enables the pullup.

But I thought that those opto relay boards had there outputs on by default. So even when disconnected/floating and not only when driven LOW. Can you test that? So only connect +5V and GND to the relay board, leaf the rest disconnected. Are the relays energized?

If not, setting the pin high before changing the pinmode can solve the problem. But if they are energized you use a external pullup resistor just like you described, between the Arduino output (that's connected to the relay board) and +5V. That way the relays stay off even when you reset the Arduino (open Serial Monitor for example) or power off the Arduino.

Yes, as Nasa says, if you do a digitalWrite HIGH to the pin before you set it as an output with pinMode, the output will remain HIGH. The difference is that it will be driven HIGH rather than floating or pulled up. Note that floating, pulled up or driven HIGH all keep the relay in a de-energised state (OFF), which is the desired outcome at startup.

Your connections are not correct ... no resistors are required (especially in series with the 5V line), you're not using the board's opto-isolation and you'll need a separate 5V DC supply with at least 1A capacity.

Here is how to connect it (note that there's no GND connection to the Arduino and the relay board's jumper is removed):