Ingo Lohs
Published © LGPL

DFRobot - 4 Relay Shield for Arduino

Let's test this shield.

BeginnerProtip30 minutes48
DFRobot - 4 Relay Shield for Arduino

Things used in this project

Hardware components

DFRobot - 4 Channel Relay Shield for Arduino
×1
Arduino UNO & Genuino UNO
Arduino UNO & Genuino UNO
×1
60W PCIe 12V 5A Power Supply
Digilent 60W PCIe 12V 5A Power Supply
a Power Supply with 12V - 5A is not necessary.
×1

Software apps and online services

Arduino IDE
Arduino IDE

Story

Read more

Code

Relay Shield v2.1 Sample Code - Source: DFRobot

C/C++
/*
 # 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();
  }
}

Credits

Ingo Lohs

Ingo Lohs

94 projects • 125 followers
I am well over 40 years and come from the middle of Germany.

Comments

Add projectSign up / Login