icon

DFRobot - 4 Relay Shield for Arduino

Let's test this shield.

projectImage
HARDWARE LIST
1 DFRobot - 4 Channel Relay Shield for Arduin
1 Arduino UNO
1 Digilent 60W PCIe 12V 5A Power Supply

Software apps and online services

Arduino IDE

Story

 

With a relay circuits can be switched with a different voltage than is necessary for the operation of the Arduino. And how easy it can be with a matching shield for the Arduino, you'll learn here.

projectImage

When I put the Relay Shield on an Arduino Uno for the first time, I first had to smile for myself: there were actually two PINs on each side too many.

projectImage

This can certainly be explained with the compatibility with other Arduino boards, but a mega is just not available to me. The product page also lists no special boards and so I find the note "Compatible with Arduino UNO Rev3" a bit poor.

No matter - for a first test with my room fountain to switch a 4 colored LED via 12V I do not need these PINs. We focus on using one of the relays.

The default assignment for addressing the 4 relays can be easily changed via jumper. This is also not necessary here and so we start with D2, D7, D8 and D10.

The shield has 4 buttons (S1-S4), so that a connection test is also possible without uploaded sketch.

With the help of Power DC adapters, I have connected a Y-cable to both the Arduino and Relay1. In my setup I use the green terminal adapters COM1 and NO1, respectively the plus pole. This means that I use the relay like a light switch and then turn on my 4 LEDs when the circuit is closed.

 

COM1 and NO1 = Plus

 

projectImage
CODE
#define relay1 2
#define relay2 7
#define relay3 8
#define relay4 10
void setup() {
Serial.begin(9600);// setup Serial Monitor to display information
pinMode(relay1, OUTPUT);// connected to Relay 1
pinMode(relay2, OUTPUT);// connected to Relay 2
pinMode(relay3, OUTPUT);// connected to Relay 3
pinMode(relay4, OUTPUT);// connected to Relay 4
}
void loop() {
digitalWrite(relay1,HIGH);// turn relay 1 ON
Serial.println(" Relay 1 ON");
delay(3000);// keep in relay 1 On for 3 seconds
digitalWrite(relay1, LOW);// turn relay 1 OFF
Serial.println(" Relay 1 OFF");
delay(3000);// keep in relay 1 OFF for 3 seconds
}

With this "Hello World"-example I turn the Relay1 on Default D2 for 3 seconds on and off.

The example sketch offered for download on the product page is much more modern: Here, the Serial Monitor can be accessed by setting the digits 1-4 to the individual relays with a setting of 57, 600 baud.

projectImage
projectImage

Appropriately, you will find in the sketch the via "case" command possible reaction to it.

CODE
case '1':                         
        Serial.println("Relay1");
        val=digitalRead(relayPin[0]);
        val=!val;
        digitalWrite(relayPin[0],val);
        break;

With this we quickly approach the question, can the shield also be controlled via radio? The shield has room to hold an XBee device.

projectImage
projectImage

Unfortunately, in my case, Digi's configuration software - the XCTU - has not been able to detect my two devices - Pro S2B and Pro S1. If someone wants to help me - feel welcome!

CODE
/*
 # This Sample code is for testing the Relay shield V2.1 for Arduino.
  
 # Editor : Phoebe
 # Date   : 2013.2.28
 # Ver    : 0.1
 # Product: Relay shield for Arduino
 # SKU    : DRI0144
   
 # Hardwares:
 1. Arduino UNO
 2. Relay Shield For Arduino V2.1 
 3  Power Supply:7~ 12V
 */

byte relayPin[4] = {
  2,7,8,10};
//D2 -> RELAY1
//D7 -> RELAY2
//D8 -> RELAY3
//D10 -> RELAY
char input=0;
int val;

void setup() {
  for(int i = 0; i < 4; i++)  pinMode(relayPin[i],OUTPUT);
  Serial.begin(57600); 
  delay(100); 
  Serial.println("Press 1-4 to control the state of the relay");
  Serial.println("waiting for input:");
  for(int j = 0; j < 4; j++)  digitalWrite(relayPin[j],LOW);
}

void loop() {
  if (Serial.available()) 
  {  
    char input= Serial.read();  
    if(input != -1)
    {
      switch(input)
      {
   
      case '1':                         
        Serial.println("Relay1");
        val=digitalRead(relayPin[0]);
        val=!val;
        digitalWrite(relayPin[0],val);
        break;
      case '2':
        Serial.println("Relay2");
        val=digitalRead(relayPin[1]);
        val=!val;
        digitalWrite(relayPin[1],val);
        break;
      case '3':
        Serial.println("Relay3");
        val=digitalRead(relayPin[2]);
        val=!val;
        digitalWrite(relayPin[2],val);
        break;
      case '4':
        Serial.println("Relay4");
        val=digitalRead(relayPin[3]);
        val=!val;
        digitalWrite(relayPin[3],val);
        break;    
        
      default:
        if(input != '\r' && input != '\n')
         Serial.println("invalid entry");
        break;
      }
    }
//    else unablerelay();
  }
}

The article was first published in hackster, November 3, 2019

cr: https://www.hackster.io/ingo-lohs/dfrobot-4-relay-shield-for-arduino-9a56bd

author: Ingo Lohs

License
All Rights
Reserved
licensBg
1