16 Relay board with a reset button

Hello, I am an arduino newbie and I am doing my first projects, one of them is to control a board with 16 relays, which I make work perfectly with 16 buttons.
https://www.az-delivery.de/es/products/16-relais-modul
My goal is that when I press a switch, the relay stays active indefinitely, which I achieve by doing "while", but the only way I can reset the entire board is by resetting the arduino.
My question is, would there be a way to reset just the relay without resetting the entire arduino?
I'm trying to put a button in A0

// Pines de los Reles
const int RELE1 = 23; // Arduino Pin 23
const int RELE2 = 25; // Arduino Pin 25
const int RELE3 = 27; // Arduino Pin 27
const int RELE4 = 29; // Arduino Pin 29
const int RELE5 = 31; // Arduino Pin 31
const int RELE6 = 33; // Arduino Pin 33
const int RELE7 = 35; // Arduino Pin 35
const int RELE8 = 37; // Arduino Pin 37
const int RELE9 = 39; // Arduino Pin 39
const int RELE10 = 41; // Arduino Pin 41
const int RELE11 = 43; // Arduino Pin 43
const int RELE12 = 45; // Arduino Pin 45
const int RELE13 = 47; // Arduino Pin 47
const int RELE14 = 49; // Arduino Pin 49
const int RELE15 = 51; // Arduino Pin 51
const int RELE16 = 53; // Arduino Pin 53

// Pines de los pulsadores
const int PULSADOR1 = 50; // Pin 50
const int PULSADOR2 = 52; // Pin 52
const int PULSADOR3 = 46; // Pin 46
const int PULSADOR4 = 48; // Pin 48
const int PULSADOR5 = 42; // Pin 42
const int PULSADOR6 = 44; // Pin 44
const int PULSADOR7 = 38; // Pin 38
const int PULSADOR8 = 40; // Pin 40
const int PULSADOR9 = 34; // Pin 34
const int PULSADOR10 = 36; // Pin 36
const int PULSADOR11 = 30; // Pin 30
const int PULSADOR12 = 32; // Pin 32
const int PULSADOR13 = 26; // Pin 26
const int PULSADOR14 = 28; // Pin 28
const int PULSADOR15 = 22; // Pin 22
const int PULSADOR16 = 24; // Pin 24

// Pin reset
const int BOTONRESET = A0; // Pin A0

void setup() {
  Serial.begin(9600);
  pinMode(BOTONRESET, INPUT_PULLUP);
  pinMode(PULSADOR1, INPUT_PULLUP);
  pinMode(PULSADOR2, INPUT_PULLUP);
  pinMode(PULSADOR3, INPUT_PULLUP);
  pinMode(PULSADOR4, INPUT_PULLUP);
  pinMode(PULSADOR5, INPUT_PULLUP);
  pinMode(PULSADOR6, INPUT_PULLUP);
  pinMode(PULSADOR7, INPUT_PULLUP);
  pinMode(PULSADOR8, INPUT_PULLUP);
  pinMode(PULSADOR9, INPUT_PULLUP);
  pinMode(PULSADOR10, INPUT_PULLUP);
  pinMode(PULSADOR11, INPUT_PULLUP);
  pinMode(PULSADOR12, INPUT_PULLUP);
  pinMode(PULSADOR13, INPUT_PULLUP);
  pinMode(PULSADOR14, INPUT_PULLUP);
  pinMode(PULSADOR15, INPUT_PULLUP);
  pinMode(PULSADOR16, INPUT_PULLUP);
  pinMode(RELE1, OUTPUT);
  pinMode(RELE2, OUTPUT);
  pinMode(RELE3, OUTPUT);
  pinMode(RELE4, OUTPUT);
  pinMode(RELE5, OUTPUT);
  pinMode(RELE6, OUTPUT);
  pinMode(RELE7, OUTPUT);
  pinMode(RELE8, OUTPUT);
  pinMode(RELE9, OUTPUT);
  pinMode(RELE10, OUTPUT);
  pinMode(RELE11, OUTPUT);
  pinMode(RELE12, OUTPUT);
  pinMode(RELE13, OUTPUT);
  pinMode(RELE14, OUTPUT);
  pinMode(RELE15, OUTPUT);
  pinMode(RELE16, OUTPUT);
}

void loop() {
  Serial.println(digitalRead (BOTONRESET));
  int ESTADOBOTONRESET = digitalRead(BOTONRESET);
  int ESTADOPULSADOR1 = digitalRead(PULSADOR1);
  int ESTADOPULSADOR2 = digitalRead(PULSADOR2);
  int ESTADOPULSADOR3 = digitalRead(PULSADOR3);
  int ESTADOPULSADOR4 = digitalRead(PULSADOR4);
  int ESTADOPULSADOR5 = digitalRead(PULSADOR5);
  int ESTADOPULSADOR6 = digitalRead(PULSADOR6);
  int ESTADOPULSADOR7 = digitalRead(PULSADOR7);
  int ESTADOPULSADOR8 = digitalRead(PULSADOR8);
  int ESTADOPULSADOR9 = digitalRead(PULSADOR9);
  int ESTADOPULSADOR10 = digitalRead(PULSADOR10);
  int ESTADOPULSADOR11 = digitalRead(PULSADOR11);
  int ESTADOPULSADOR12 = digitalRead(PULSADOR12);
  int ESTADOPULSADOR13 = digitalRead(PULSADOR13);
  int ESTADOPULSADOR14 = digitalRead(PULSADOR14);
  int ESTADOPULSADOR15 = digitalRead(PULSADOR15);
  int ESTADOPULSADOR16 = digitalRead(PULSADOR16);
  int ESTADORELE1 = digitalRead(RELE1);
  int ESTADORELE2 = digitalRead(RELE2);
  int ESTADORELE3 = digitalRead(RELE3);
  int ESTADORELE4 = digitalRead(RELE4);
  int ESTADORELE5 = digitalRead(RELE5);
  int ESTADORELE6 = digitalRead(RELE6);
  int ESTADORELE7 = digitalRead(RELE7);
  int ESTADORELE8 = digitalRead(RELE8);
  int ESTADORELE9 = digitalRead(RELE9);
  int ESTADORELE10 = digitalRead(RELE10);
  int ESTADORELE11 = digitalRead(RELE11);
  int ESTADORELE12 = digitalRead(RELE12);
  int ESTADORELE13 = digitalRead(RELE13);
  int ESTADORELE14 = digitalRead(RELE14);
  int ESTADORELE15 = digitalRead(RELE15);
  int ESTADORELE16 = digitalRead(RELE16);

if (ESTADOPULSADOR9 == LOW) digitalWrite(RELE1, LOW); //HIGH sin pulsar, LOW pulsado.  //HIGH rele desactivado, LOW rele activado
else digitalWrite(RELE1,HIGH);
while(digitalRead(RELE1)==LOW);
if (ESTADOPULSADOR10 == LOW) digitalWrite(RELE2, LOW);
else digitalWrite(RELE2,HIGH);
if (ESTADOPULSADOR11 == LOW) digitalWrite(RELE3, LOW);
else digitalWrite(RELE3,HIGH);
if (ESTADOPULSADOR12 == LOW) digitalWrite(RELE4, LOW);
else digitalWrite(RELE4,HIGH);
if (ESTADOPULSADOR13 == LOW) digitalWrite(RELE5, LOW);
else digitalWrite(RELE5,HIGH);
if (ESTADOPULSADOR14 == LOW) digitalWrite(RELE6, LOW);
else digitalWrite(RELE6,HIGH);
if (ESTADOPULSADOR15 == LOW) digitalWrite(RELE7, LOW);
else digitalWrite(RELE7,HIGH);
if (ESTADOPULSADOR16 == LOW) digitalWrite(RELE8, LOW);
else digitalWrite(RELE8,HIGH);
if (ESTADOPULSADOR1 == LOW) digitalWrite(RELE9, LOW);
else digitalWrite(RELE9,HIGH);
if (ESTADOPULSADOR2 == LOW) digitalWrite(RELE10, LOW);
else digitalWrite(RELE10,HIGH);
if (ESTADOPULSADOR3 == LOW) digitalWrite(RELE11, LOW);
else digitalWrite(RELE11,HIGH);
if (ESTADOPULSADOR4 == LOW) digitalWrite(RELE12, LOW);
else digitalWrite(RELE12,HIGH);
if (ESTADOPULSADOR5 == LOW) digitalWrite(RELE13, LOW);
else digitalWrite(RELE13,HIGH);
if (ESTADOPULSADOR6 == LOW) digitalWrite(RELE14, LOW);
else digitalWrite(RELE14,HIGH);
if (ESTADOPULSADOR7 == LOW) digitalWrite(RELE15, LOW);
else digitalWrite(RELE15,HIGH);
if (ESTADOPULSADOR8 == LOW) digitalWrite(RELE16, LOW);
else digitalWrite(RELE16,HIGH);

What does reset the relay mean?

change the state

Simple solution would be to read tge button ib the while-loop and break from the while-loop if you detect that tge button is pressed.

What must happen with the other relays? Don't toy want to activate them while another one is active? If yes, then you can't use while-looos.

This does not work?

if (ESTADOPULSADOR14 == LOW) digitalWrite(RELE6, LOW);
else digitalWrite(RELE6,HIGH);

Does not work as in the relay is not enabled and disabled?

Please post the entire code.

this does not toggle the relay

// Pines de los Reles
const int RELE1 = 23; // Arduino Pin 23
const int RELE2 = 25; // Arduino Pin 25
const int RELE3 = 27; // Arduino Pin 27
const int RELE4 = 29; // Arduino Pin 29
const int RELE5 = 31; // Arduino Pin 31
const int RELE6 = 33; // Arduino Pin 33
const int RELE7 = 35; // Arduino Pin 35
const int RELE8 = 37; // Arduino Pin 37
const int RELE9 = 39; // Arduino Pin 39
const int RELE10 = 41; // Arduino Pin 41
const int RELE11 = 43; // Arduino Pin 43
const int RELE12 = 45; // Arduino Pin 45
const int RELE13 = 47; // Arduino Pin 47
const int RELE14 = 49; // Arduino Pin 49
const int RELE15 = 51; // Arduino Pin 51
const int RELE16 = 53; // Arduino Pin 53

// Pines de los pulsadores
const int PULSADOR1 = 50; // Pin 50
const int PULSADOR2 = 52; // Pin 52
const int PULSADOR3 = 46; // Pin 46
const int PULSADOR4 = 48; // Pin 48
const int PULSADOR5 = 42; // Pin 42
const int PULSADOR6 = 44; // Pin 44
const int PULSADOR7 = 38; // Pin 38
const int PULSADOR8 = 40; // Pin 40
const int PULSADOR9 = 34; // Pin 34
const int PULSADOR10 = 36; // Pin 36
const int PULSADOR11 = 30; // Pin 30
const int PULSADOR12 = 32; // Pin 32
const int PULSADOR13 = 26; // Pin 26
const int PULSADOR14 = 28; // Pin 28
const int PULSADOR15 = 22; // Pin 22
const int PULSADOR16 = 24; // Pin 24

// Pin reset
const int BOTONRESET = A0; // Pin A0

void setup() {
  Serial.begin(9600);
//  pinMode(BOTONRESET, INPUT_PULLUP);
//  pinMode(PULSADOR1, INPUT_PULLUP);
//  pinMode(PULSADOR2, INPUT_PULLUP);
//  pinMode(PULSADOR3, INPUT_PULLUP);
//  pinMode(PULSADOR4, INPUT_PULLUP);
//  pinMode(PULSADOR5, INPUT_PULLUP);
//  pinMode(PULSADOR6, INPUT_PULLUP);
//  pinMode(PULSADOR7, INPUT_PULLUP);
//  pinMode(PULSADOR8, INPUT_PULLUP);
//  pinMode(PULSADOR9, INPUT_PULLUP);
//  pinMode(PULSADOR10, INPUT_PULLUP);
//  pinMode(PULSADOR11, INPUT_PULLUP);
//  pinMode(PULSADOR12, INPUT_PULLUP);
//  pinMode(PULSADOR13, INPUT_PULLUP);
//  pinMode(PULSADOR14, INPUT_PULLUP);
//  pinMode(PULSADOR15, INPUT_PULLUP);
//  pinMode(PULSADOR16, INPUT_PULLUP);
  pinMode(RELE1, OUTPUT);
//  pinMode(RELE2, OUTPUT);
//  pinMode(RELE3, OUTPUT);
//  pinMode(RELE4, OUTPUT);
//  pinMode(RELE5, OUTPUT);
//  pinMode(RELE6, OUTPUT);
//  pinMode(RELE7, OUTPUT);
//  pinMode(RELE8, OUTPUT);
//  pinMode(RELE9, OUTPUT);
//  pinMode(RELE10, OUTPUT);
//  pinMode(RELE11, OUTPUT);
//  pinMode(RELE12, OUTPUT);
//  pinMode(RELE13, OUTPUT);
//  pinMode(RELE14, OUTPUT);
//  pinMode(RELE15, OUTPUT);
//  pinMode(RELE16, OUTPUT);
}

bool tick false;
void loop() 
{
  if (tick)
  {
  digitalWrite(RELE1, LOW);
  }else {
    digitalWrite(RELE1, HIGH);
  }
  delay (1000);
  //Serial.println(digitalRead (BOTONRESET));
//  int ESTADOBOTONRESET = digitalRead(BOTONRESET);
//  int ESTADOPULSADOR1 = digitalRead(PULSADOR1);
//  int ESTADOPULSADOR2 = digitalRead(PULSADOR2);
//  int ESTADOPULSADOR3 = digitalRead(PULSADOR3);
//  int ESTADOPULSADOR4 = digitalRead(PULSADOR4);
//  int ESTADOPULSADOR5 = digitalRead(PULSADOR5);
//  int ESTADOPULSADOR6 = digitalRead(PULSADOR6);
//  int ESTADOPULSADOR7 = digitalRead(PULSADOR7);
//  int ESTADOPULSADOR8 = digitalRead(PULSADOR8);
//  int ESTADOPULSADOR9 = digitalRead(PULSADOR9);
//  int ESTADOPULSADOR10 = digitalRead(PULSADOR10);
//  int ESTADOPULSADOR11 = digitalRead(PULSADOR11);
//  int ESTADOPULSADOR12 = digitalRead(PULSADOR12);
//  int ESTADOPULSADOR13 = digitalRead(PULSADOR13);
//  int ESTADOPULSADOR14 = digitalRead(PULSADOR14);
//  int ESTADOPULSADOR15 = digitalRead(PULSADOR15);
//  int ESTADOPULSADOR16 = digitalRead(PULSADOR16);
//  int ESTADORELE1 = digitalRead(RELE1);
//  int ESTADORELE2 = digitalRead(RELE2);
//  int ESTADORELE3 = digitalRead(RELE3);
//  int ESTADORELE4 = digitalRead(RELE4);
//  int ESTADORELE5 = digitalRead(RELE5);
//  int ESTADORELE6 = digitalRead(RELE6);
//  int ESTADORELE7 = digitalRead(RELE7);
//  int ESTADORELE8 = digitalRead(RELE8);
//  int ESTADORELE9 = digitalRead(RELE9);
//  int ESTADORELE10 = digitalRead(RELE10);
//  int ESTADORELE11 = digitalRead(RELE11);
//  int ESTADORELE12 = digitalRead(RELE12);
//  int ESTADORELE13 = digitalRead(RELE13);
//  int ESTADORELE14 = digitalRead(RELE14);
//  int ESTADORELE15 = digitalRead(RELE15);
//  int ESTADORELE16 = digitalRead(RELE16);
//
//  if (ESTADOPULSADOR9 == LOW) digitalWrite(RELE1, LOW); //HIGH sin pulsar, LOW pulsado.  //HIGH rele desactivado, LOW rele activado
//  else digitalWrite(RELE1, HIGH);
//  while (digitalRead(RELE1) == LOW);
//  if (ESTADOPULSADOR10 == LOW) digitalWrite(RELE2, LOW);
//  else digitalWrite(RELE2, HIGH);
//  if (ESTADOPULSADOR11 == LOW) digitalWrite(RELE3, LOW);
//  else digitalWrite(RELE3, HIGH);
//  if (ESTADOPULSADOR12 == LOW) digitalWrite(RELE4, LOW);
//  else digitalWrite(RELE4, HIGH);
//  if (ESTADOPULSADOR13 == LOW) digitalWrite(RELE5, LOW);
//  else digitalWrite(RELE5, HIGH);
//  if (ESTADOPULSADOR14 == LOW) digitalWrite(RELE6, LOW);
//  else digitalWrite(RELE6, HIGH);
//  if (ESTADOPULSADOR15 == LOW) digitalWrite(RELE7, LOW);
//  else digitalWrite(RELE7, HIGH);
//  if (ESTADOPULSADOR16 == LOW) digitalWrite(RELE8, LOW);
//  else digitalWrite(RELE8, HIGH);
//  if (ESTADOPULSADOR1 == LOW) digitalWrite(RELE9, LOW);
//  else digitalWrite(RELE9, HIGH);
//  if (ESTADOPULSADOR2 == LOW) digitalWrite(RELE10, LOW);
//  else digitalWrite(RELE10, HIGH);
//  if (ESTADOPULSADOR3 == LOW) digitalWrite(RELE11, LOW);
//  else digitalWrite(RELE11, HIGH);
//  if (ESTADOPULSADOR4 == LOW) digitalWrite(RELE12, LOW);
//  else digitalWrite(RELE12, HIGH);
//  if (ESTADOPULSADOR5 == LOW) digitalWrite(RELE13, LOW);
//  else digitalWrite(RELE13, HIGH);
//  if (ESTADOPULSADOR6 == LOW) digitalWrite(RELE14, LOW);
//  else digitalWrite(RELE14, HIGH);
//  if (ESTADOPULSADOR7 == LOW) digitalWrite(RELE15, LOW);
//  else digitalWrite(RELE15, HIGH);
//  if (ESTADOPULSADOR8 == LOW) digitalWrite(RELE16, LOW);
//  else digitalWrite(RELE16, HIGH);
}

?

I only put in the code the first relay with "while", to test, if I put it in the rest of 15 relays it works the same, the problem is that when I want to activate the relay, it stays active until I press a button (in this case A0), but being in a "while" loop I can't do it

Good luck.

1 Like

this is the entire code :sweat_smile:

I only want to activate one each time.

Use Arrays!

const int RELE[16] = {23, 25, 27, 29, 31, 33, 35, 37, 39, 41, 43, 45, 47, 49, 51, 53};
const int PULSADOR[16] = {50, 52, 46, 48, 42, 44, 38, 40, 34, 36, 30, 32, 26, 28, 22, 24};
const int BOTONRESET = A0;

bool banderaDelEstadoDelPestillo[16];

void setup() {
  Serial.begin(9600);
  pinMode(BOTONRESET, INPUT_PULLUP);
  for (auto &i : PULSADOR) pinMode(PULSADOR[i], INPUT_PULLUP);
  for (auto &j : RELE) pinMode(RELE[j], OUTPUT);
}

void loop() {
  for (auto &k : PULSADOR) {
    if (digitalRead(PULSADOR[k]) == LOW) banderaDelEstadoDelPestillo[k] ^= 1;
  }
  for (auto &l : RELE) digitalWrite(RELE[l], banderaDelEstadoDelPestillo[l]);
}
1 Like
const int RELE[16] = {23, 25, 27, 29, 31, 33, 35, 37, 39, 41, 43, 45, 47, 49, 51, 53};
const int PULSADOR[16] = {50, 52, 46, 48, 42, 44, 38, 40, 34, 36, 30, 32, 26, 28, 22, 24};
const int BOTONRESET = A0;

void setup() {
  Serial.begin(9600);
  pinMode(BOTONRESET, INPUT_PULLUP);
  for (auto &i : PULSADOR) pinMode(PULSADOR[i], INPUT_PULLUP);
  for (auto &j : RELE) pinMode(RELE[j], OUTPUT);
}

void loop() {
  unsigned int banderaDelEstadoDelPestillo = 0;
  for (auto &k : PULSADOR) {
    if (digitalRead(PULSADOR[k]) == LOW) banderaDelEstadoDelPestillo |=  (1 << k);
  }
  for (auto &l : RELE) {
    if (banderaDelEstadoDelPestillo == (1 << l)) digitalWrite(RELE[l], !digitalRead(RELE[l]));
  }
}
1 Like

thanks,
It seems like a much simpler code than mine of course, when I press a button no relay turns on

I made a couple edits. Make sure you are using the latest edit.

Your buttons pull the input to ground?

The idea of my code is to use arrays so you can iterate over the 16 as if it were 1....

Try the following code just for the first relay

const int RELE = 23;
const int PULSADOR = 50;
const int BOTONRESET = A0;

void setup() {
  Serial.begin(9600);
  pinMode(BOTONRESET, INPUT_PULLUP);
  pinMode(PULSADOR, INPUT_PULLUP);
  pinMode(RELE, OUTPUT);
}

void loop() {
  unsigned int banderaDelEstadoDelPestillo = 0;
  if (digitalRead(PULSADOR) == LOW) banderaDelEstadoDelPestillo =  1;
  if (banderaDelEstadoDelPestillo == 1) digitalWrite(RELE, !digitalRead(RELE));
}
1 Like

Thank you very much, yes, everything is grounded.
I did not know that way of using the code, as I mentioned, I am new to this, it helps me a lot.
Now the behavior is as follows, as I turn on the arduino, relay 1 is automatically activated without pressing the button

Does this behavior change if you comment out

if (banderaDelEstadoDelPestillo == 1) digitalWrite(RELE, !digitalRead(RELE));
1 Like

nop, it does the same

Then you probably want to go high in setup...

void setup() {
  Serial.begin(9600);
  pinMode(BOTONRESET, INPUT_PULLUP);
  pinMode(PULSADOR, INPUT_PULLUP);
  pinMode(RELE, OUTPUT);
  digitalWrite(RELE, HIGH);
}

I am guessing your relay isn't a relay but a relay module with jumpers that you can set logical 1.

If so, you can alternatively swap the jumpers.

1 Like

This is the module https://www.az-delivery.de/es/products/16-relais-modul

The logic is, relay OFF sends HIGH, relay ON sends LOW

Now even when I press the button, the relay does not activate

Step 1 is to get 1 relay latching on the pushbutton input, then you can use arrays and range-based loops to iterate over the arrays, to reuse the same code 16 times.

The same relay board is sold under many brands, including SainSmart and Gadgeteer. unfortunately, I cannot find if you can set the input to HIGH or LOW... so we will leave it HIGH... the inputs are pulled high by the internal pullup, and low by switch activation. Is this correct?

1 Like