Sunday 7 July 2019

Elegoo 8 Channel Relay Module Tutorial

Carrying on from the previous post about an interactive confetti room...

https://langster1980.blogspot.com/2019/07/interactive-confetti-room-project.html

Lets get out the 8 channel relay PCB and see what makes it tick.

Here are the specifications of the device in case it is needed:

● Output Channels: 8
● Operating Voltage: 5 Vdc
● Operating Current: 480 ma
● Switching Voltages: 250 Vac at 10 Amps, 30 Vdc at 10 A

● Mechanical Dimensions (Length*Width*Height): 137 mm x 56 mm x 17 mm (5.4" x 2.2" x 0.7")
● Mounting Hole Size: 3 mm (0.12")
● Mounting Hole Centres (L*W): 132 x 50 mm (5.2" x 2")

Here is the schematic diagram again:


I connected up the relay board to the Arduino R3 as shown in the diagram below:
This connection arrangement will actuate the channel 1 relay switching the positive feed (+5 Vdc) on it's common and normally closed connections.  Anything connected between the common pin and the normally closed pin will be switched, It could be anything as long as it is within the specifications of the relay contacts.  The normally open pin will also be switched as the common pin is in the middle of the switching contacts.

We want to write some simple test code to drive the relay coil on channel one on and off for a brief period of time:

1.  Initialise control variables
2.  Start the Serial Monitor
3.  Set the relay drive pins to be outputs
4.  Open channel 1 relay contacts
5.  Wait half a second
6.  Close channel 1 relay contacts
7.  Loop back to step 4 and repeat continuously

Here is the code:

/* Langster's Test Code for 
 *  Elegoo 8 Channel Relay PCB
 *  07/07/2019
 *  Test circuit output is a Green 5 mm LED 
 *  and a 220 Ohm resistor connected
 *  between common and Normally Closed screw
 *  terminals on the channel 1 relay outputs
 *  
 *  The Elegoo relay module is connected to
 *  an Arduino Uno R3 on the following pins:
 *  
 *  GND connects to GND of the Arduino
 *  IN1 connects to Pin 11 of the Arduino
 *  IN1 connects to Pin 10 of the Arduino
 *  IN2 connects to Pin 9 of the Arduino
 *  IN3 connects to Pin 8 of the Arduino
 *  IN4 connects to Pin 7 of the Arduino
 *  IN5 connects to Pin 6 of the Arduino
 *  IN6 connects to Pin 5 of the Arduino
 *  IN7 connects to Pin 4 of the Arduino
 *  VCC connects to Vin pin of the Arduino
 */

int eightChanRelayIN1 = 11;   //variable for relay channel 1 drive pin
int eightChanRelayIN2 = 10;   //variable for relay channel 2 drive pin
int eightChanRelayIN3 = 9;   //variable for relay channel 3 drive pin
int eightChanRelayIN4 = 8;   //variable for relay channel 4 drive pin
int eightChanRelayIN5 = 7;   //variable for relay channel 5 drive pin
int eightChanRelayIN6 = 6;   //variable for relay channel 6 drive pin
int eightChanRelayIN7 = 5;   //variable for relay channel 7 drive pin
int eightChanRelayIN8 = 4;   //variable for relay channel 8 drive pin

int relayDwellTimeMS = 500; //variable for setting the dwell time a relay is actuated for

void setup() {

Serial.begin(9600);      // open the serial port at 9600 bps:
pinMode(eightChanRelayIN1, OUTPUT); //set the relay drive pin for channel 1 to be an output
pinMode(eightChanRelayIN2, OUTPUT); //set the relay drive pin for channel 2 to be an output
pinMode(eightChanRelayIN3, OUTPUT); //set the relay drive pin for channel 3 to be an output
pinMode(eightChanRelayIN4, OUTPUT); //set the relay drive pin for channel 4 to be an output
pinMode(eightChanRelayIN5, OUTPUT); //set the relay drive pin for channel 5 to be an output
pinMode(eightChanRelayIN6, OUTPUT); //set the relay drive pin for channel 6 to be an output
pinMode(eightChanRelayIN7, OUTPUT); //set the relay drive pin for channel 7 to be an output
pinMode(eightChanRelayIN8, OUTPUT); //set the relay drive pin for channel 8 to be an output

}

void loop() {

digitalWrite(eightChanRelayIN1, LOW);   //switch relay channel 1
delay(relayDwellTimeMS);                //wait for the dwell time (500 ms)
digitalWrite(eightChanRelayIN1, HIGH);  //switch relay channel 1
delay(relayDwellTimeMS);                //wait for the dwell time (500 ms)
 
}

The code is fairly easy to understand.  Each relay channel has been assigned a variable to easily reference it.  A variable to select how long the relay will be open or closed is also added, commonly known as the dwell time.

Next in the setup function the serial terminal is initialised although it isn't actually used for anything (I had plans to add serial messages and then didn't bother).

After that all the relay channel drive pins are set to outputs. 

Finally in the loop function the relay on channel one is driven low which opens it's contacts.  The contacts stay open for half a second and then the contacts are closed - the pin is driven high for half a second and then the function loops back to the start and repeats.

Here is a short video of things in action:


Things to note - The relay drive pins are active low. That means in order to make a relay switch the contact open the drive pin must be driven low.

It would not be difficult to modify the code to drive all of the relays, just modify the code in the loop section:

void loop() {

  digitalWrite(eightChanRelayIN1, LOW);    //switch relay channel 1
  digitalWrite(eightChanRelayIN2, LOW);    //switch relay channel 2
  digitalWrite(eightChanRelayIN3, LOW);    //switch relay channel 3
  digitalWrite(eightChanRelayIN4, LOW);    //switch relay channel 4
  digitalWrite(eightChanRelayIN5, LOW);    //switch relay channel 5
  digitalWrite(eightChanRelayIN6, LOW);    //switch relay channel 6
  digitalWrite(eightChanRelayIN7, LOW);    //switch relay channel 7
  digitalWrite(eightChanRelayIN8, LOW);    //switch relay channel 8
  
  delay(relayDwellTimeMS);                 //wait for the dwell time (500 ms)
  
  digitalWrite(eightChanRelayIN1, HIGH);   //switch relay channel 1
  digitalWrite(eightChanRelayIN2, HIGH);   //switch relay channel 2
  digitalWrite(eightChanRelayIN3, HIGH);   //switch relay channel 3
  digitalWrite(eightChanRelayIN4, HIGH);   //switch relay channel 4
  digitalWrite(eightChanRelayIN5, HIGH);   //switch relay channel 5
  digitalWrite(eightChanRelayIN6, HIGH);   //switch relay channel 6
  digitalWrite(eightChanRelayIN7, HIGH);   //switch relay channel 7
  digitalWrite(eightChanRelayIN8, HIGH);   //switch relay channel 8
  
  delay(relayDwellTimeMS);                //wait for the dwell time (500 ms)
 
}

I haven't got eight LEDS to hand to give a demonstration of this but I have tested the code and the circuit it works perfectly...the clicking of the relays sounds a lot like an old fashioned watch tick or a metronome.

If one were to change value in the dwell time variable the relays will switch more quickly or slowly...the maximum speed they should be switched is 100 ms...any faster will work but might damage the relays and the noise is exceptionally irritating! 

The code can also be modified to independently switch each relay with independent dwell times as required. 

That's about it for now, take care - Langster!

3 comments :

  1. This comment has been removed by a blog administrator.

    ReplyDelete
  2. This comment has been removed by a blog administrator.

    ReplyDelete
  3. This comment has been removed by a blog administrator.

    ReplyDelete