Relay Control: 4 Relays, 4 Pushbuttons

Hi all,

I’m fairly new to Arduino and I would like some advice for a project with the following constraints.

*4 Relays.
*4 Push buttons.
*Using the normally open contacts on all relays.
*Activating a relay closes contacts.
*Activate a relay with a push button (active low) (INPUT_PULLUP) type.
*Once any of the relays are activated by pushing a button all other relays must disconnect.

This is for switching a common signal (200-800mVpp AC sinusoidal waveform) to ONE of FOUR outputs. Only one at a time. So each relay’s common point will be connected to the incoming signal and when a relay is activated the signal with go out that corresponding relay and all others will stay disconnected. Additionally if another push button is pressed I would like all the relays to deactivate and then activated the relay corresponding to the button that was pressed.

Arudino being used: Arudino UNO R3 (see attached)
Relay module being used: 4 Relay Module (see attached)

I’m not sure how this code should be structured. How should the deactivation of the other relays take place when another button is pressed?

Here is the basic stuff I want.

int relay1 = 13; //Amp CH1 relay
int relay2 = 12; //Amp CH2 relay
int relay3 = 11; //Amp CH3 relay
int relay4 = 10; //Amp CH4 relay
int button1 = 2; //CH1 Button
int button2 = 3; //CH2 Button
int button3 = 4; //CH3 Button
int button4 = 5; //CH4 Button
void setup()
{
// Initialize digital output pins for relays:
pinMode(relay1, OUTPUT);
pinMode(relay2, OUTPUT);
pinMode(relay3, OUTPUT);
pinMode(relay4, OUTPUT);
// Initialize digital input pins for push buttons:
pinMode(button1, INPUT_PULLUP);
pinMode(button2, INPUT_PULLUP);
pinMode(button3, INPUT_PULLUP);
pinMode(button4, INPUT_PULLUP);
}

I tried to get this to work something like this but nothing happened correctly.

void loop()
{
  digitalRead(button1);
  if(button1 == LOW)
  {
    digitalWrite(relay1, LOW);
  }else{
    digitalWrite(relay2, HIGH);
    digitalWrite(relay3, HIGH);
    digitalWrite(relay4, HIGH);
  }
}

Any advice? Example code I can modify?

Thanks in advance.

EJ

lc-201-3.png

Hi, Welcome to the forum.

Please read the first post in any forum entitled how to use this forum. http://forum.arduino.cc/index.php/topic,148850.0.html

What did happen?

Can you please post a copy of your circuit, in CAD or a picture of a hand drawn circuit in jpg, png?

Tom... :)

Hi, This

  digitalRead(button1);

needs to be;

  button1State = digitalRead(button1);

and you need to declare

bool button1State ;

Tom.. :)

Hi,

Thanks for the quick reply. Attached is a schematic of what’s going on. Refer to the relay module picture previously attached for the “Relay module driver circuitry” (Driver transistor, flyback diode, optoisolator etc…)

EJ

Thanks Tom. I think I got it figured out. I tried this code and it seems to work.

int relay1 = 13; //Amp CH1 relay
int relay2 = 12; //Amp CH2 relay
int relay3 = 11; //Amp CH3 relay
int relay4 = 10; //Amp CH4 relay
int button1 = 2; //CH1 Button
int button2 = 3; //CH2 Button
int button3 = 4; //CH3 Button
int button4 = 5; //CH4 Button
void setup()
{
  // Initialize digital output pins for relays:
pinMode(relay1, OUTPUT);
pinMode(relay2, OUTPUT);
pinMode(relay3, OUTPUT);
pinMode(relay4, OUTPUT);
pinMode(button1, INPUT_PULLUP);
pinMode(button2, INPUT_PULLUP);
pinMode(button3, INPUT_PULLUP);
pinMode(button4, INPUT_PULLUP);
}

void loop()
{
  bool button1State ;
  button1State = digitalRead(button1);
  if(button1State == LOW)
  {
    digitalWrite(relay1, LOW);
    digitalWrite(relay2, HIGH);
    digitalWrite(relay3, HIGH);
    digitalWrite(relay4, HIGH);
  }
  bool button2State ;
  button2State = digitalRead(button2);
  if(button2State == LOW)
  {
    digitalWrite(relay2, LOW);
    digitalWrite(relay1, HIGH);
    digitalWrite(relay3, HIGH);
    digitalWrite(relay4, HIGH);
  }
  bool button3State ;
  button3State = digitalRead(button3);
  if(button3State == LOW)
  {
    digitalWrite(relay3, LOW);
    digitalWrite(relay1, HIGH);
    digitalWrite(relay2, HIGH);
    digitalWrite(relay4, HIGH);
  }
  bool button4State ;
  button4State = digitalRead(button4);
  if(button4State == LOW)
  {
    digitalWrite(relay4, LOW);
    digitalWrite(relay1, HIGH);
    digitalWrite(relay2, HIGH);
    digitalWrite(relay3, HIGH);
  }
}

You'll probably want to debounce your pushbuttons.

.

Is this so that in the event two push buttons are pressed at the same time they won't trigger two relays?

Debouncing in this case is probably not such a big issue - as the OP only wants to set one output state at a time (at finger frequency). Bounce (contact 'chatter') will simply appear to 'press' the same button a number of times within the ~50mS following the initial finger push(!). (If you can press the button reliably faster than 100mS apart - good for you! :) )

Only one at a time.

Just check that you don't need to 'break-before-make' on your outputs. The mechanical latency of the relays could allow two relays to be 'closed' simultaneously for a few mS

I updated the code to have an initial startup so that CH1 is activated and the others are deactivated. Preventing all relays from being active on power up. I changed the digital output pins to 12, 11, 10 & 9 so that when the unit boots relay 1 isn't clicked on and off a few times. On boot up it seems that the LED attached to pin 13 blinks a few times which subsequently clicks the relay. I also added more comments.

I'll try the de bouncing stuff later. For now proof of concept is adequate.

Thanks for all the help. It's most appreciated. 8)

// Guitar amp relay module driven my microcontroller input
// 2018 - Eric Carlson - ECS Audio
int relay1 = 12; //Amp CH1 relay
int relay2 = 11; //Amp CH2 relay
int relay3 = 10; //Amp CH3 relay
int relay4 = 9; //Amp CH4 relay
int button1 = 2; //CH1 Button
int button2 = 3; //CH2 Button
int button3 = 4; //CH3 Button
int button4 = 5; //CH4 Button
void setup()
{
// Initialize digital output pins for relays:
pinMode(relay1, OUTPUT);
pinMode(relay2, OUTPUT);
pinMode(relay3, OUTPUT);
pinMode(relay4, OUTPUT);
// Initialize digital inputs with internal pullup resistors:
pinMode(button1, INPUT_PULLUP);
pinMode(button2, INPUT_PULLUP);
pinMode(button3, INPUT_PULLUP);
pinMode(button4, INPUT_PULLUP);
//Set the initial startup to CH1:
digitalWrite(relay1, LOW);
digitalWrite(relay2, HIGH);
digitalWrite(relay3, HIGH);
digitalWrite(relay4, HIGH);
}

void loop()
{
//CH1 EN
  bool button1State ;
  button1State = digitalRead(button1);
  if(button1State == LOW)
  {
    digitalWrite(relay1, LOW);
    digitalWrite(relay2, HIGH);
    digitalWrite(relay3, HIGH);
    digitalWrite(relay4, HIGH);
  }
//CH2 EN
  bool button2State ;
  button2State = digitalRead(button2);
  if(button2State == LOW)
  {
    digitalWrite(relay2, LOW);
    digitalWrite(relay1, HIGH);
    digitalWrite(relay3, HIGH);
    digitalWrite(relay4, HIGH);
  }
//CH3 EN
  bool button3State ;
  button3State = digitalRead(button3);
  if(button3State == LOW)
  {
    digitalWrite(relay3, LOW);
    digitalWrite(relay1, HIGH);
    digitalWrite(relay2, HIGH);
    digitalWrite(relay4, HIGH);
  }
//CH4 EN
  bool button4State ;
  button4State = digitalRead(button4);
  if(button4State == LOW)
  {
    digitalWrite(relay4, LOW);
    digitalWrite(relay1, HIGH);
    digitalWrite(relay2, HIGH);
    digitalWrite(relay3, HIGH);
  }
}

Hi,

To prevent a response to two or more buttons being pressed you can assign each button a binary value

int Button1Val =1;
int Button2Val =2;
int Button3Val =4;
int Button4Val =8:

int ButtonSel = (Button1Val * button1state) + (Button2Val * button2state) + (Button3Val * button3state) + (Button4Val * button4state) ;

So ButtonSel will be unique for any button combination.

Then use switch .. case to interperet your ButtonSel

switch (ButtonSel) 
{ case 1: // statements 
your button 1 relay config
  break; 
  case 2: // statements 
your button 2 relay config
  break; 
  case 4: // statements 
  your button 3 relay config
  break; 
  case 8; // statements 
  your button 4 relay config
  break; 
  default: // statements 
  your output config if any other illegal combination is pressed
}

I hope that makes sense.

Tom.. :)