Newbie Question

First thanks for taking the time to read this.

I have a Mega 2560 and I purchased 2 8 relay boards. My code is listed below, when run the program all my relays come on?

Here is the stranger thing, when digitalWrite(Relay1, HIGH) - it will turn it off!!!!!

I don't want all the relays one upon power up, I'd like all relays to stay LOW until told to go HIGH.It could be in these boards I guess, unless I'm not getting something correct in my program.

There is a jumper (on the relay board) that can let you move it to two of three pins. How it is in the photo the relays will click even though they are backwards (HIGH is off and Low is ON). If I take the jumper off the leds will come on and off following the code but the relays do not click (come on). If I put the jumper the other way it turns off the ardunio (shorting it from what I can tell).

Link to the board I'm using below:

VCC on the relay board is going to 5V on the Ardunio
GND on the relay board is going to GND on the Ardunio
IN1 on the relay board is going to pin 22 on the Ardunio
IN2 " 23
IN3 " 24
IN4 " 25
IN5 " 26
IN6 " 27
IN7 " 28
IN8 " 29

******************** ACTUAL CODE BELOW ********************


// put your setup code here, to run once:
//
//
// Ardunio Input Setup //
int Input1 = 1;
int Input2 = 2;
int Input3 = 3;
int Input4 = 4;
int Input5 = 5;
int Input6 = 6;
int Input7 = 7;
int Input8 = 8;
int Input9 = 9;
int Input10 = 10;
int Input11 = 11;
int Input12 = 12;
int Input13 = 13;
int Input14 = 14;
int Input15 = 15;
int Input16 = 16;
int Input17 = 17;
int Input18 = 18;
int Input19 = 19;
int Input20 = 20;
int Input21 = 21;

// Relay Board 1 Setup
//
int Relay1 = 22; //Relay #1 Relay Board #1
int Relay2 = 23; //Relay #2 Relay Board #1
int Relay3 = 24; //Relay #3 Relay Board #1
int Relay4 = 25; //Relay #4 Relay Board #1
int Relay5 = 26; //Relay #5 Relay Board #1
int Relay6 = 27; //Relay #6 Relay Board #1
int Relay7 = 28; //Relay #7 Relay Board #1
int Relay8 = 29; //Relay #8 Relay Board #1
//
// Relay Board 2 Setup
//
int Relay9 = 30; //Relay #9 Relay Board #2
int Relay10 = 31; //Relay #10 Relay Board #2
int Relay11 = 32; //Relay #11 Relay Board #2
int Relay12 = 33; //Relay #12 Relay Board #2
int Relay13 = 34; //Relay #13 Relay Board #2
int Relay14 = 35; //Relay #14 Relay Board #2
int Relay15 = 36; //Relay #15 Relay Board #2
int Relay16 = 37; //Relay #16 Relay Board #2

int FirstScan = 40;//Internal Bit Turned On During First Scan and Never Turned Off.
//
//
//
void setup() {
//
// Setup For Ardunio To Monitor Pins 1 through 21 as Inputs
//
pinMode(Input1, INPUT); // Sets The Ardunio Pin 1 As A Digital Input
pinMode(Input2, INPUT); // Sets The Ardunio Pin 2 As A Digital Input
pinMode(Input3, INPUT); // Sets The Ardunio Pin 3 As A Digital Input
pinMode(Input4, INPUT); // Sets The Ardunio Pin 4 As A Digital Input
pinMode(Input5, INPUT); // Sets The Ardunio Pin 5 As A Digital Input
pinMode(Input6, INPUT); // Sets The Ardunio Pin 6 As A Digital Input
pinMode(Input7, INPUT); // Sets The Ardunio Pin 7 As A Digital Input
pinMode(Input8, INPUT); // Sets The Ardunio Pin 8 As A Digital Input
pinMode(Input9, INPUT); // Sets The Ardunio Pin 9 As A Digital Input
pinMode(Input10, INPUT); // Sets The Ardunio Pin 10 As A Digital Input
pinMode(Input11, INPUT); // Sets The Ardunio Pin 11 As A Digital Input
pinMode(Input12, INPUT); // Sets The Ardunio Pin 12 As A Digital Input
pinMode(Input13, INPUT); // Sets The Ardunio Pin 13 As A Digital Input
pinMode(Input14, INPUT); // Sets The Ardunio Pin 14 As A Digital Input
pinMode(Input15, INPUT); // Sets The Ardunio Pin 15 As A Digital Input
pinMode(Input16, INPUT); // Sets The Ardunio Pin 16 As A Digital Input
pinMode(Input17, INPUT); // Sets The Ardunio Pin 17 As A Digital Input
pinMode(Input18, INPUT); // Sets The Ardunio Pin 18 As A Digital Input
pinMode(Input19, INPUT); // Sets The Ardunio Pin 19 As A Digital Input
pinMode(Input20, INPUT); // Sets The Ardunio Pin 20 As A Digital Input
pinMode(Input21, INPUT); // Sets The Ardunio Pin 21 As A Digital Input
//
// Setup For Ardunio To Control Relays 1 Through 8 On Relay Board #1.
//
pinMode(Relay1, OUTPUT); // Sets The Ardunio Pin 22 As A Digital Output
pinMode(Relay2, OUTPUT); // Sets The Ardunio Pin 23 As A Digital Output
pinMode(Relay3, OUTPUT); // Sets The Ardunio Pin 24 As A Digital Output
pinMode(Relay4, OUTPUT); // Sets The Ardunio Pin 25 As A Digital Output
pinMode(Relay5, OUTPUT); // Sets The Ardunio Pin 26 As A Digital Output
pinMode(Relay6, OUTPUT); // Sets The Ardunio Pin 27 As A Digital Output
pinMode(Relay7, OUTPUT); // Sets The Ardunio Pin 28 As A Digital Output
pinMode(Relay8, OUTPUT); // Sets The Ardunio Pin 29 As A Digital Output
//
// Setup For Ardunio To Control Relays 9 Through 16 On Relay Board #2.
//
pinMode(Relay9, OUTPUT); // Sets The Ardunio Pin 22 As A Digital Output
pinMode(Relay10, OUTPUT); // Sets The Ardunio Pin 23 As A Digital Output
pinMode(Relay11, OUTPUT); // Sets The Ardunio Pin 24 As A Digital Output
pinMode(Relay12, OUTPUT); // Sets The Ardunio Pin 25 As A Digital Output
pinMode(Relay13, OUTPUT); // Sets The Ardunio Pin 26 As A Digital Output
pinMode(Relay14, OUTPUT); // Sets The Ardunio Pin 27 As A Digital Output
pinMode(Relay15, OUTPUT); // Sets The Ardunio Pin 28 As A Digital Output
pinMode(Relay16, OUTPUT); // Sets The Ardunio Pin 29 As A Digital Output

pinMode(FirstScan, OUTPUT); //Sets The Ardunio Pin 40 As A Digital Output Wired To Nothing.

}

void loop()
{
// put your main code here, to run repeatedly:
digitalWrite(Relay1, HIGH);

}

Here is the stranger thing, when digitalWrite(Relay1, HIGH) - it will turn it off!!!!!

Nothing strange about it. That's the way most relay boards are designed.

I don't want all the relays one upon power up, I'd like all relays to stay LOW until told to go HIGH.

As do most people. Don't you suppose that someone has figured out how to make that happen, and maybe even published some code?

Of course, I doubt that you'll be able to find it, since you couldn't even find the stickies at the top of this section of the forum. Do it NOW.

take a look under learning and see how a "for" loop works it will save you lots of typing.

With PLC relay cards (Allen Bradly, Siemens, etc) this in far from that, I haven't seen any that are one when power is applied. When you turn a bit from 0 (OFF/LOW) to 1(ON/HIGH) the relay comes on.

From your statement I assume your referring the relay boards for the Arduinos " That's the way most relay boards are designed." What your saying is when designing a project, remember everything will come on upon the unit powering up and you need to turn it off before anything else?

From your statement I assume your referring the relay boards for the Arduinos

Wasn't that what we were talking about?

Not sure, you tell me. I asked simple question why my relays are coming on when there is no code telling them to go HIGH. When I set them high they turn off. Your reply to that was "That's the way most relay boards are designed."

I've been writing ladder logic for 25+ years (which I understand doesn't have anything to do with Ardunio) but when you refer to "most relay boards" those boards aren't designed that way, now when it come to Ardunio I only have 3 days experience, so you have me there.lol.

Your reply to that was "That's the way most relay boards are designed."

But, this is the Arduino forum, and you asked about a relay board for an Arduino. So, yes, I am talking specifically about relay boards for Arduinos.

If there was sufficient uprising, I'm sure manufacturers would change the design so that HIGH was on and LOW was off.

You can set the state of the pin immediately after setting the mode. The relay may still bang on momentarily. There are even ways of making that not happen.

I don't happen to have one of those shields. The relays I have require HIGH to activate and LOW to deactivate, so I haven't paid that much attention to exactly how to prevent the relay from activating momentarily. I just know that it can be done. You would need to do some searching on this forum.

billfloyd806:
Not sure, you tell me. I asked simple question why my relays are coming on when there is no code telling them to go HIGH. When I set them high they turn off. Your reply to that was "That's the way most relay boards are designed."

I've been writing ladder logic for 25+ years (which I understand doesn't have anything to do with Ardunio) but when you refer to "most relay boards" those boards aren't designed that way, now when it come to Ardunio I only have 3 days experience, so you have me there.lol.

welcome to ardunio

Ladder logic is a different beast that C++ but once you being to understand it you will start to see behind some of the command blocks used in ladder logic. You also have to remember you didn't pay allen Bradley 4k for a processor, 600 for a card, 1k for a back plane and 500 for a power supply.

Some of the hardware is quirky and you can learn to accept that or spend time and effort fighting it.

Heres a few hints

don't use pin 0 or 1 unless you have no other choose. These are used by serial/usb and you will need them later.

Arduino is single core so you can not see code running like you would in wonderware. To see what the processor is doing you will need to use serial prints (pin 0 and 1)

There is no watchdog timer on arduino unless you enable it with in the code. So avoid writing blocking code.

Delay(1000) means stop everything for 1000ms (1 second) that includes reading pins and everything else and should be avoided. look at how a millis timer works.

floating voltage is a pain especially on input pins look at the command INPUT_PULLUP . that will get you all excited as it will make the input code backwards just like the relays

always preview your post to get to the code tags as windows likes to turn certain brackets into smiley faces.

so much to learn......lmao.

#define on 0
#define off 1
#define xic  1
#define xio  0

#define Input1 1
//tag name input1 will be replaced in the
//code by the compiler with the number 1
#define Input2 2
#define Input3 3
#define Input4 4
#define Input5 5
#define Input6 6
#define Input7 7
#define Input8 8
#define Input9 9
#define Input10 10
#define Input11 11
#define Input12 12
#define Input13 13
#define Input14 14
#define Input15 15
#define Input16 16
#define Input17 17
#define Input18 18
#define Input19 19
#define Input20 20
#define Input21 21


// Relay Board 1 Setup
//
#define Relay1  22 //Relay #1 Relay Board #1
#define Relay2  23 //Relay #2 Relay Board #1
#define Relay3  24 //Relay #3 Relay Board #1
#define Relay4  25 //Relay #4 Relay Board #1
#define Relay5  26 //Relay #5 Relay Board #1
#define Relay6  27 //Relay #6 Relay Board #1
#define Relay7  28 //Relay #7 Relay Board #1
#define Relay8  29 //Relay #8 Relay Board #1
//
// Relay Board 2 Setup
//
#define Relay9  30  //Relay #9  Relay Board #2
#define Relay10  31 //Relay #10 Relay Board #2
#define Relay11  32 //Relay #11 Relay Board #2
#define Relay12  33 //Relay #12 Relay Board #2
#define Relay13  34 //Relay #13 Relay Board #2
#define Relay14  35 //Relay #14 Relay Board #2
#define Relay15  36 //Relay #15 Relay Board #2
#define Relay16  37 //Relay #16 Relay Board #2

#define FirstScan  40//Internal Bit Turned On During First Scan and Never Turned Off.
//
//
//
void setup() {
  //
  // Setup For Ardunio To Monitor Pins 1 through 21 as Inputs
  //
  Serial.begin(9600);
  for (int i = 1; i <= 21; i++) {//set pins 1-21 as inputs
    pinMode(i, INPUT);
  }
  for (int i = 22; i <= 37; i++) {//set up relays 22-37 and set to high (off)
    pinMode(i, OUTPUT);
    digitalWrite(i, HIGH);
  }
  pinMode(FirstScan, OUTPUT); //Sets The Ardunio Pin 40 As A Digital Output Wired To Nothing.
  digitalWrite(FirstScan, HIGH);

}

void loop()
{
  // put your main code here, to run repeatedly:

  
  digitalWrite(Relay1, on);//on equals 0 so it makes it seem the right way around
  delay (1500);
  digitalWrite(Relay1, off);
  delay (750);

//lol just in case you need a ladder logic fix you can do this
  if (digitalRead(Input1) == xio) {
    digitalWrite(Relay2, on);
  }
  if (digitalRead(Input1) == xic) {
    digitalWrite(Relay2, off);
  }
}

Most relays have a normally open and a normally closed output. Maybe you are looking at the wrong one?

PaulMurrayCbr:
Most relays have a normally open and a normally closed output.

Which is why it's dangerous (or at least ambiguous) to talk of a relay being "on". I prefer to say the relay is "activated" .

Thanks for the perfect explanation gpop1.

As for looking at the wrong one (Normally Open or Normally Closed). Normally electrical schematics (in the industrial controls side) are drawn with all devices in a shelf state (no powered applied). This relay board is switching from its shelf state to a powered state with zero code wrote telling it too.

My concerns with this is are I will be controlling 16 motor starters with these, the last thing I need is all of my motors all starting for a split second while these relays switch upon power up. Each motor pulls around 17 amps running, the starting current of 17 of all of them just flickering for a split second wouldn't be good.

I could just wire them to the other contact and not worry about it, that is until the power is lost only to the ardunio or the relay board but my 480VAC is still on. Then things will go south real bad, real fast, that's the logical side of me trying to idiot proof everything which sometimes is impossible.

Thanks to everyone for their posts!

You didn’t provide a link or any other information regarding the relay board you’re using, so I don’t have a schematic. But, maybe you could try putting external pull-up resistors on the relay control lines? I’m thinking the processor should power up with its GPIO pins set as inputs, so they will be high impedance. The pull-ups would prevent the relay board's input transistors (which I assume are there but can’t see since you didn’t provide a schematic) from turning on. Once the code configures the GPIOs as outputs, setting them LOW will overpower the pull-up and activate the relay.

This relay board is switching from its shelf state to a powered state with zero code wrote telling it too.

That is incorrect.

"no user code" might be correct.

The Arduino IDE manipulates your code, adding a main() function that calls an init() function and your setup() and loop() functions.

The init() function (provided) DOES set the mode of all digital pins to INPUT and it sets them all to LOW, and, bingo, your relay activates.

Plc5 rack including cards is dirt cheap used. A arduino next to 3 phase 480 contactors and a bunch of electric noise might not be the easiest or cheapest option

There is no pull-up when the input is INPUT; look at the datasheet of e.g. the 328 (figure 14-2, General Digital I/O). But your thinking is basically correct.

Looking at figure 14-2
After reset PUD is LOW ('global' pull-up functionality enabled), DDxn is LOW (pin is input) and PORTxn is LOW. As a result, the internal pull-up (FET and resistor at left) is disabled; and because DDxn is LOW, the output of the tri-state buffer (in the center of the figure) is in tri-state.

Writing a HIGH to DDxn (making the pin an output) after reset will not affect the pull-up as the PINxn is still LOW; it will however affect the output because the output of PORTxn is now passed through the tri-state buffer and as a result the pin goes LOW.

2a)
Writing a HIGH to PORTxn (e.g. using pinMode(pin, INPUT_PULLUP)), after a reset, has the effect that the output of the 3-input AND gate will go high and the internal pull-up will be enabled. It further has the effect that the input of the tri-state will also be HIGH. But as DDxn is still LOW, this will not affect the output.

2b)
Now writing a HIGH to DDxn (after 2a) will disable the internal pull-up (output of the 3-input AND goes low) but the output of PORTxn (which was set HIGH in 2a) will occur on the output.

To prevent undesired switching of the pin to LOW, one should first write a HIGH to PINxn (digitalWrite(pin, HIGH)) and next switch the pin to output.

sterretje:
To prevent undesired switching of the pin to LOW, one should first write a HIGH to PINxn (digitalWrite(pin, HIGH)) and next switch the pin to output.

While sterretje was checking the manual I was testing the same thing with an led on a pin and coming to the same conclusion. So theory and practice match...

There is no pull-up when the input is INPUT

Well, there is. It's just not turned on by default. It can be turned on by using digitalWrite() to write HIGH to the pin while it is in INPUT mode.

So, the arduino takes a bit of tme to turn on and during that time you want the outputs to be HIGH to turn the relays off while starting up.

To make this happen, the outputs need pullup resistors. To turn the relays on and off, you'd switch between pinMode INPUT for external pullup; and pinMode OUTPUT , digitalWrite LOW to pull the output down.