Issues driving 8-relay board

Hi! I'm trying to create a system that when one button is pressed, multiple relays trigger.
The project is for a set of doors that when certain doors open, certain ones lock, there's a total of 7 doors.

Logic Table
1- 2,4
2- 1,3,4
3- 2,7
4- 1,2,5
5- 4,6
6- 5,7
7- 6,3

(Ex. When door 1 activates, doors 2 and 4 lock)

THE ISSUE
When pressing certain buttons, either all, some, or none, of the relays trigger. Although whenever having the buttons and relays go 1 to 1 (One button activates one relay) it works fine.

HARDWARE
-Arduino MEGA2560
-8-Relay board (Has 817 Optoisolators)

CODE

const int Relay1 = 41;
const int Relay2 = 42;
const int Relay3 = 43;
const int Relay4 = 44;
const int Relay5 = 45;
const int Relay6 = 46;
const int Relay7 = 47;
int val = 0;
  //Divide
const int Door1 = 31;
const int Door2 = 32;
const int Door3 = 33;
const int Door4 = 34;
const int Door5 = 35;
const int Door6 = 36;
const int Door7 = 37;
void setup() {
  pinMode(Relay1, OUTPUT);
  pinMode(Relay2, OUTPUT);
  pinMode(Relay3, OUTPUT);
  pinMode(Relay4, OUTPUT);
  pinMode(Relay5, OUTPUT);
  pinMode(Relay6, OUTPUT);
  pinMode(Relay7, OUTPUT);

  pinMode(Door1, INPUT);
  pinMode(Door2, INPUT);
  pinMode(Door3, INPUT);
  pinMode(Door4, INPUT);
  pinMode(Door5, INPUT);
  pinMode(Door6, INPUT);
  pinMode(Door7, INPUT);

  digitalWrite(Relay1, HIGH);
  digitalWrite(Relay2, HIGH);
  digitalWrite(Relay3, HIGH);
  digitalWrite(Relay4, HIGH);
  digitalWrite(Relay5, HIGH);
  digitalWrite(Relay6, HIGH);
  digitalWrite(Relay7, HIGH);

}

void loop() {
  val = digitalRead(Door1);  //Set1
  if (val == HIGH) {
   digitalWrite(Relay2, HIGH);
   digitalWrite(Relay4, HIGH);
} else {
  digitalWrite(Relay2, LOW);
  digitalWrite(Relay4, LOW);
}
//Logic Break------------------------------------------------------
  val = digitalRead(Door2);  //Set 2
  if (val == HIGH) {
   digitalWrite(Relay1, HIGH);
   digitalWrite(Relay3, HIGH);
   digitalWrite(Relay4, HIGH);
} else {
  digitalWrite(Relay1, LOW);
  digitalWrite(Relay3, LOW);
  digitalWrite(Relay4, LOW);
}
//Logic Break------------------------------------------------------
  val = digitalRead(Door3);  //Set 3
  if (val == HIGH) {
   digitalWrite(Relay2, HIGH);
   digitalWrite(Relay7, HIGH);
} else {
  digitalWrite(Relay2, LOW);
  digitalWrite(Relay7, LOW);
}
//Logic Break------------------------------------------------------
  val = digitalRead(Door4);  //Set 4
  if (val == HIGH) {
   digitalWrite(Relay1, HIGH);
   digitalWrite(Relay2, HIGH);
   digitalWrite(Relay5, HIGH);
} else {
  digitalWrite(Relay1, LOW);
  digitalWrite(Relay2, LOW);
  digitalWrite(Relay5, LOW);
}
//Logic Break------------------------------------------------------
  val = digitalRead(Door5);  //Set 5
  if (val == HIGH) {
   digitalWrite(Relay4, HIGH);
   digitalWrite(Relay6, HIGH);
} else {
  digitalWrite(Relay4, LOW);
  digitalWrite(Relay6, LOW);
}
//Logic Break------------------------------------------------------
  val = digitalRead(Door6);  //Set 6
  if (val == HIGH) {
   digitalWrite(Relay5, HIGH);
   digitalWrite(Relay7, HIGH);
} else {
  digitalWrite(Relay5, LOW);
  digitalWrite(Relay7, LOW);
}
//Logic Break------------------------------------------------------
  val = digitalRead(Door7);  //Set 7
  if (val == HIGH) {
   digitalWrite(Relay6, HIGH);
   digitalWrite(Relay3, HIGH);
} else {
  digitalWrite(Relay6, LOW);
  digitalWrite(Relay3, LOW);
}
}

How are your switches wired?

2017-08-05_20-33-24.jpg

There's more wrong with your logic.

If e.g. switch 1 is active, you engage relays 2 and 4.

But if switch 2 is not active at the same time, you release door 4 again.

This is actually a rather complex problem. Also you have to think of what to do when multiple buttons are pressed: lock all doors based on all switches pressed? Or have one switch take priority, and if so, is that the one first pressed, or the one with the higher number?

Hi Boeing,

See my PM for options here. Interesting problem!

I would approach this by mapping the switch inputs to the relay outputs in a table instead of using logic. Makes it much easier to change. Then have a "scan/execute" loop where at the beginning of the pass, it updates the current input state and uses that table to update the output state at the end of each pass.

Second, as wvmarle said, what happens if multiple switches are active at once? Only respond to the first one, or respond to all of them?

Next, I assume that if you press, e.g., switch 1, then relays 2&4 close and the others open, is that correct? Not quite clear from your questions.

Are you looking for coding "help" or for someone to write all the code and provide as a finished, tested item? If so, PM me and I can get it done pretty quickly.

I believe my problem is in the interface from the OUTPUTS to the RELAY board.
Step by step…

  1. My inputs are momentary contact switches wired with a PULL UP resistor to VCC (5V). The switch connects to GRD when pressed (activated).
  2. The output is to a RELAY board (the 8 relay board from Amazon with opto-isolation and from what I can find, 5ma/relay trigger.

When I wire everything up this is what happens…
When I write program to trigger relay one from switch 1, relay 2 from switch 2, etc… everything works fine.
When I write the program to trigger the relays via my truth table…
Some switches work correctly and trigger multiple relays.
Some switches trigger one relay while ‘half’ triggering the second. Both leds’s on the relay board light up a little less than full bright and one of the relays seems to not have sufficient pull down current.
When I erase half the program, that half works. Seems everything works until a add a forth switch to the program. 1-3, ok. 5-7, ok.
I am not pulling much current to drive the relay board (5ma/relay trigger (optoisolation), max 3 at a time.).
I have tried pull up resistors and pull down resistors on the outputs as well as no resistor.
I have multiple Arduino’s (including a mega) and all respond the same.
Under the program, multiple switches can be activated at the same time.
Imagine 5 doors.
Door one opens and doors 2 & 5 lock.
At the same time, door 3 opens and doors 2 and 4 lock.
In general, the door on either side of the open one locks. So it is possible to have multiple doors open and triggering the same doors to lock.
An incredibly easy circuit with diodes but the assignment is to use the Arduino.
I am 76% sure it is hardware, 24% maybe software. (first project).
Truth table;
1 triggers 2,4
2 triggers 1,3,4
3 triggers 2,7
4 triggers 1,2,5
5 triggers 4,6
6 triggers 5,7
7 triggers 6,3

As you can see, multiple doors can open at the same time locking common doors.
Should be very easy but something obvious I am missing.

OK.
Let's get right to the heart of this. Forget your pushbuttons/switches. What happens if you turn all relays on in the code? What happens if you turn them on one at a time until they are all on?

You either have a code problem or an electrical problem. Step 1 is to figure out which it is.

Boeing7487:
I am 76% sure it is hardware, 24% maybe software. (first project).
Truth table;
1 triggers 2,4
2 triggers 1,3,4
3 triggers 2,7
4 triggers 1,2,5
5 triggers 4,6
6 triggers 5,7
7 triggers 6,3

As you can see, multiple doors can open at the same time locking common doors.
Should be very easy but something obvious I am missing.

I pointed out a major software issue in #2: you are very quickly turning on and off the relays all the time, instead of keeping on the ones that have to be on. This as unpressed buttons actively switch off relays that the pressed button switched on. Did you address this issue already? If not here's no chance of making it work; if yes please post updated sketch.

You also didn't answer: what to do when multiple buttons are pressed at the same time?

When a program ignores all the switches and only cycles all the relays on and off, all the relays work correctly.
So.... going with software problem.

I tried to explain multiple switches can be activated at the same time. By the nature of the design, when a door is locked via a triggered relay, that door switch cannot be activated as the door is locked. As someone suggested, I see where the 'else' potion of the program is unlocking doors that maybe should be locked. That is a problem. As this is a first project, I am struggling with what options are available to me (table?). Looking at this more closely, I can see it is more complicated than I first though. IF / ELSE is not going to work. If someone thinks a 'table' will work, please direct me to where I can read what that is and how it works? What key words should I search for?

Thanks.

Logic is;

When a door opens, that door switch goes low, the associated relay outputs go high and lock the door. At the time, the associated door switches cannot go low as they are locked.

Another unlocked door can be opened at the same time and lock additional doors, including a door that is already locked because of another door. There is no case that a unlocked door can try to lock a door that is open.

truth table

S1 LOW, Out 2, 4 HIGH
S2 LOW, Out 1, 3, 4 HIGH
S3 LOW, Out 2, 7 HIGH
S4 LOW, Out 1, 2, 5 HIGH
S5 LOW, Out 4 ,6 HIGH
S6 LOW, Out 5, 7 HIGH
S7 LOW, Out 3,6 HIGH

So... if S1 is LOW, relays 2 and 4 are triggered.
At the same time, S3 could go low and trigger out 7 and also out 2 ( out 2 is already HIGH because of S1)

There should not be an instance where a door opening will try to lock an open door.

In general, two doors next to each other cannot open but every other door could open at the same time. Confusing?

Pushing my ability but happily.

I did design a circuit that uses only diodes that works perfectly. Trying to use the Arduino though. Just because I want to learn it.

I will try to upload the diode schematic when I have it drawn out if anybody thinks that will help to understand what I am doing.

Thank everybody for responding.

To be clear, more than one switch can be low at the same time.

some random combinations to maybe help explain what happens....

Door 1 opens (S1 LOW) and locks Doors 2 and 4 (Relays 2 and 4 go HIGH)

During that, Doors 2 and 4 cannot open.

At the SAME time, Door 5 opens (S5 LOW) and locks doors 4 and 6 (Relays 4 and 6 go HIGH, 4 is already HIGH because of S1)

At the SAME time, Door 7 opens (S7 LOW) and lockes doors 6 and 3 (Relays 6 and 3 go HIGH, 6 is already HIGH because of S5)

The logic follows ALL switched, NOT only the first pressed

That is per the logic table i posted previously.

Can you please give me an example or mapping pins and scan / execute?

I though I was mapping the pins when i assigned what they were (input/output).

trying to code myself.

I am not smart, I am stubborn.

S1 LOW, Out 2, 4 HIGH
S2 LOW, Out 1, 3, 4 HIGH

Have a look at those two first. You better look at which button(s) switches on each relay, as multiple buttons may switch on each individual relay. So:

byte RELAY_PIN[2] = {41, 42};
byte BUTTON_PIN[2] = {  ,  };

void loop() {
  S1 = digitalRead(BUTTON_PIN[0]);
  S2 = digitalRead[BUTTON_PIN[1]);

  if (S1 == LOW) digitalWrite([RELAY_PIN[0], HIGH) else digitalWrite([RELAY_PIN[0], LOW);
  if (S2 == LOW) digitalWrite([RELAY_PIN[1], HIGH) else digitalWrite([RELAY_PIN[1], LOW);
  if (S1 == LOW) digitalWrite([RELAY_PIN[2], HIGH) else digitalWrite([RELAY_PIN[2], LOW);
  if (S1 == LOW || S2 == LOW) digitalWrite([RELAY_PIN[3], HIGH) else digitalWrite([RELAY_PIN[3], LOW);
}

Boeing, are you in Seattle?

-jim lee

Thanks everyone....

I am currently trying to play with the code you suggested...

takes time.

I will post if it worked.

Thanks again.

Thanks everyone...

Got everything working.

Just changed to an OR statement and came at the problem from the doors instead of the switches.

Was very obvious once I figured it out.

Not pretty but it works.

Thanks again....

const int Relay1 = 41;
const int Relay2 = 42;
const int Relay3 = 43;
const int Relay4 = 44;
const int Relay5 = 45;
const int Relay6 = 46;
const int Relay7 = 47;

int D1 = 0;
int D2 = 0;
int D3 = 0;
int D4 = 0;
int D5 = 0;
int D6 = 0;
int D7 = 0;

//Divide
const int Door1 = 31;
const int Door2 = 32;
const int Door3 = 33;
const int Door4 = 34;
const int Door5 = 35;
const int Door6 = 36;
const int Door7 = 37;

void setup() {
pinMode(Relay1, OUTPUT);
pinMode(Relay2, OUTPUT);
pinMode(Relay3, OUTPUT);
pinMode(Relay4, OUTPUT);
pinMode(Relay5, OUTPUT);
pinMode(Relay6, OUTPUT);
pinMode(Relay7, OUTPUT);

pinMode(Door1, INPUT_PULLUP);
pinMode(Door2, INPUT_PULLUP);
pinMode(Door3, INPUT_PULLUP);
pinMode(Door4, INPUT_PULLUP);
pinMode(Door5, INPUT_PULLUP);
pinMode(Door6, INPUT_PULLUP);
pinMode(Door7, INPUT_PULLUP);

digitalWrite(Relay1, HIGH);
digitalWrite(Relay2, HIGH);
digitalWrite(Relay3, HIGH);
digitalWrite(Relay4, HIGH);
digitalWrite(Relay5, HIGH);
digitalWrite(Relay6, HIGH);
digitalWrite(Relay7, HIGH);

}
void loop() {
D1 = digitalRead(Door1);
D2 = digitalRead(Door2);
D3 = digitalRead(Door3);
D4 = digitalRead(Door4);
D5 = digitalRead(Door5);
D6 = digitalRead(Door6);
D7 = digitalRead(Door7);

if (D2 == LOW || D4 == LOW) {
digitalWrite(Relay1, LOW);
} else {
digitalWrite(Relay1, HIGH);}
if (D1 == LOW || D3 == LOW || D4 == LOW) {
digitalWrite(Relay2, LOW);
} else {
digitalWrite(Relay2, HIGH);}
if (D2 == LOW || D7 == LOW) {
digitalWrite(Relay3, LOW);
} else {
digitalWrite(Relay3, HIGH);}
if (D1 == LOW || D2 == LOW || D5 == LOW) {
digitalWrite(Relay4, LOW);
} else {
digitalWrite(Relay4, HIGH);}
if (D4 == LOW || D6 == LOW) {
digitalWrite(Relay5, LOW);
} else {
digitalWrite(Relay5, HIGH);}
if (D5 == LOW || D7 == LOW) {
digitalWrite(Relay6, LOW);
} else {
digitalWrite(Relay6, HIGH);}
if (D3 == LOW || D6 == LOW) {
digitalWrite(Relay7, LOW);
} else {
digitalWrite(Relay7, HIGH);}
}