Multiple relays.

Hi All,

First, apologies if this has been raised elsewhere, if so please point me in towards the right posts.

I'm very new to the Arduino, and have done the very basic sketches - blinking an LED,running the serial function and have got one of my UNOs reading the temperature and ambient light onto a two line LCD display. What I can't get to work is using two inputs to operate two relays. That is one input 'tied' to one relay, the other input to the other relay. I can easily get one input for one relay, but I cannot work out how to get the Uno to switch the relays independently. For example, one relay for outside lights at the front of the house, the other for the rear. I'm an electrician so the mains side of things is easy, but the programming is another matter.

The only sketches I've seen are either single relays or multiple relays being switched sequentially.

Do I need to involve interrupts? Or do I need if....then....else code?

Any pointers would be greatly appreciated.

Regards, Max.

Could you please post your code?

Just guessing that you're using delays and also need to control the relays at the instant something "changes" on an input and not "when" the input is HIGH or LOW.

Check these examples to see what I mean: https://www.arduino.cc/en/Tutorial/StateChangeDetection https://www.arduino.cc/en/Tutorial/Debounce https://www.arduino.cc/en/Tutorial/BlinkWithoutDelay

Hi Dlloyd,

Thanks for your prompt reply.

I'll find the code as requested, but it was a while ago - I gave up.

I've had a look at the examples you suggested and understand them, but the problem I have seems to me to be more fundamental- how to direct the program to 'if input X is on then go to the place in the code that turns output A on' and/or 'if input Y is on then goto the place in the code that turns output B on'. The important part here is the and/or, then go back up to the start of the loop and start again. I need to be able to switch the outputs in any order, not a fixed sequence.

Having thought about it, it might be easier from a practical point ( running the mains cables due to the layout outside) of view to use two UNOs as they are so cheap and just program the one input/output in each. It seems to be admitting defeat and overkill, but I can't see any way of doing a sub-routine type of coding, but it must be possible as there are plenty of robot sketches using more than one servo.

Thanks again.

Regards, Max.

The basic structure should be like this:

void loop ()
{
  if (condition1 holds) 
    do_relevant_thing1 () ;
  if (condition2 holds) 
    do_relevant_thing2 () ;
  if (condition3 holds) 
    do_relevant_thing3 () ;
  if (condition4 holds) 
    do_relevant_thing4 () ;
  ....
}

void do_relevant_thing1 ()
{
  ...
}
void do_relevant_thing2 ()
{
  ...
}
void do_relevant_thing3 ()
{
  ...
}
void do_relevant_thing4 ()
{
  ...
}

loop() is there to test for any change in input or timeout that requires an action, then you can have all the actions in their own functions, essentially written independently.

Don't hog the processor (ie don't call delay()) in any action, then all the separate strands of the program can get a look in regularly and all is good.

You will need the techniqeus in StateChangeDetection and BlinkWithoutDelay to achive this so study how they work.

MarkT +1

I’ve already prepared this (something to experiment with - untested), edited from "Project Guidance > Demonstration code for several things at the same time. I would also read through that thread.

ControlSeveralRelays.ino (4.09 KB)

Hi,

Many thanks to MarkT and Dlloyd.

Apologies for the delay in replying - the day job gets in the way sometimes.......

Also apologies for how I've copied the code, couldn't work out how to do it as per everybody else. If you could enlighten me for next time it would be appreciated.

I've got the two relays to work on a breadboard lighting two LED's. Is it clunky? Is it ok? Your thoughts would also be appreciated.

Regards, Max.

Code below:

/* Two buttons - red and green, turn on two LEDs red and green The circuit: * LED attached from pins 9 & 10 to ground * pushbutton attached to pins 2 & 3 from +5V * 10K resistor attached to pins 2 & 3 from ground */

// constants won't change. They're used here to // set pin numbers: const int rbuttonPin = 2; // the number of the pushbutton pin const int gbuttonPin = 3;

define RELAY_ON 0 // Relay are LOW on

define RELAY_OFF 1

define Relay_1 7 //Relay pins 7 and 8

define Relay_2 8

// variables will change: int rbuttonState = 0; int gbuttonState = 0; // variable for reading the pushbutton status

void setup() { digitalWrite(Relay_1, RELAY_OFF); digitalWrite(Relay_2, RELAY_OFF);

pinMode(Relay_1, OUTPUT); pinMode(Relay_2, OUTPUT);

pinMode(rbuttonPin, INPUT); pinMode(gbuttonPin, INPUT);

}

void loop(){ // read the state of the pushbutton value: rbuttonState = digitalRead(rbuttonPin);

// check if the pushbutton is pressed. // if it is, the buttonState is HIGH: if (rbuttonState == HIGH) { // turn RED LED on: digitalWrite(Relay_1, RELAY_ON); } else { // turn LED off: digitalWrite(Relay_1, RELAY_OFF); } gbuttonState = digitalRead(gbuttonPin);

if (gbuttonState == HIGH) { digitalWrite(Relay_2, RELAY_ON); } else { digitalWrite(Relay_2, RELAY_OFF); } }