Arduino - Relay Shield

The relay shield has 4 relays that can be individually controlled by Arduino code. It is very easy to use. We just need to stack it on Arduino and program for it. We can use this shield to control high-power devices such as an actuator, pump, LED strip, light bulb, fan...

An alternative to the relay shield is the 4-channel relay module. The differences between a relay shield and a 4-channel relay module are below:

Relay shield 4-channel relay module
Does not need to do the wiring (just stack on Arduino) Needs to do the wiring
Compatible with Arduino Uno, Uno Wifi, Mega only Compatible with all Arduino board
Can be controlled by pre-fixed Arduino's digital pins (inflexible) Can be controlled by any Arduino's digital pins

In this tutorial, we are going to learn how to use the relay shield with Arduino Uno, Uno Wifi, Or Mega

Hardware Required

1×Arduino UNO or Genuino UNO
1×USB 2.0 cable type A/B
1×Relay Shield
1×Jumper Wires
1×(Optional) 9V Power Adapter for Arduino
1×(Recommended) Screw Terminal Block Shield for Arduino Uno
1×(Optional) Transparent Acrylic Enclosure For Arduino Uno

Or you can buy the following sensor kits:

1×DIYables Sensor Kit (30 sensors/displays)
1×DIYables Sensor Kit (18 sensors/displays)
Please note: These are Amazon affiliate links. If you buy the components through these links, We will get a commission at no extra cost to you. We appreciate it.

About Relay Shield

Pinout

Relay Shield Pinout

A relay shield has 4 relays named: Relay 1, Relay 2, Relay 3, Relay 4.

A relay shield has the following pins:

  • Controlled pins (also called input pin, or signal pin): When stacking on Arduino, the controlled pins are internally connected to the Arduino's pins, so we do not need to do the wiring. Read the detail of each shield to know which pin controls which relay. For example, For Official Arduino Shield:
    • Arduino pin 4 is internally connected to Relay 1
    • Arduino pin 7 is internally connected to Relay 2
    • Arduino pin 8 is internally connected to Relay 3
    • Arduino pin 12 is internally connected to Relay 4
  • Output pins: each relay has three pins: NC, NO, COM. These pins connect to a high-voltage device that is controlled by the relay

For detail of what is NO, NC, COM pins, how to connect relay to high-voltage, what are differences between normally closed and normally open, see Arduino - Relay tutorial

※ NOTE THAT:

  • Each manufacturer designs different maximum output voltage that the relays can control. Read the specification carefully. For example, Official Arduino relay shield has a maximum voltage of 48VDC
  • Each manufacturer connects relay to different Arduino pins. Read the specification or schematic to know the exact pins
  • The official relay shield from Arduino connects pin 4, pin 7, pin 8 and pin 12 to relay 1, relay 2, relay 3 and relay 4, respectively.
  • When using the official relay shield with Arduino Uno, if you use SPI communication to another device, You should NOT use relay 4. That is because relay 4 is connected to pin 12, which is used for SPI communication

The official relay shield also has 4 LED indicators for each relay. The LED indicators reflect the state of relays

Wiring Diagram

  • Stack the relay shield on Arduino
  • Connect to high-voltage devices
Arduino relay shield wiring diagram

This image is created using Fritzing. Click to enlarge image

※ NOTE THAT:

  • The above wiring diagram uses normally open mode. Therefore, only COM and NO are used. NC is not used.
  • If 4 devices that are controlled by a relay shield use the same voltage, we can use a single high-voltage power adapter for all. However, if they use different voltages, we can use different high-voltage power adapters independently.

How To Program For 4-Channel Relay Module

  • Based on the specification of the shield, define the pin map.
#define RELAY_1 4 // the Arduino pin, which connects to the relay 1 #define RELAY_2 7 // the Arduino pin, which connects to the relay 2 #define RELAY_3 8 // the Arduino pin, which connects to the relay 3 #define RELAY_4 12 // the Arduino pin, which connects to the relay 4
  • Initializes the Arduino pin to the digital output mode by using pinMode() function.
pinMode(RELAY_1, OUTPUT); pinMode(RELAY_2, OUTPUT); pinMode(RELAY_3, OUTPUT); pinMode(RELAY_4, OUTPUT);
  • Activates relay by using digitalWrite() function to set output state to HIGH.
digitalWrite(RELAY_1, HIGH); digitalWrite(RELAY_2, HIGH); digitalWrite(RELAY_3, HIGH); digitalWrite(RELAY_4, HIGH);
  • Deactivate relay by using digitalWrite() function to set output state to LOW.
digitalWrite(RELAY_1, HIGH); digitalWrite(RELAY_2, HIGH); digitalWrite(RELAY_3, HIGH); digitalWrite(RELAY_4, HIGH);

Arduino Code

/* * Created by ArduinoGetStarted.com * * This example code is in the public domain * * Tutorial page: https://arduinogetstarted.com/tutorials/arduino-relay-shield */ #define RELAY_1 4 // the Arduino pin, which connects to the relay 1 #define RELAY_2 7 // the Arduino pin, which connects to the relay 2 #define RELAY_3 8 // the Arduino pin, which connects to the relay 3 #define RELAY_4 12 // the Arduino pin, which connects to the relay 4 void setup() { Serial.begin(9600); // initialize Arduino pins as digital output pins pinMode(RELAY_1, OUTPUT); pinMode(RELAY_2, OUTPUT); pinMode(RELAY_3, OUTPUT); pinMode(RELAY_4, OUTPUT); } // the loop function runs over and over again forever void loop() { Serial.println("Activate all relays"); digitalWrite(RELAY_1, HIGH); digitalWrite(RELAY_2, HIGH); digitalWrite(RELAY_3, HIGH); digitalWrite(RELAY_4, HIGH); delay(1000); Serial.println("Deactivate all relays"); digitalWrite(RELAY_1, LOW); digitalWrite(RELAY_2, LOW); digitalWrite(RELAY_3, LOW); digitalWrite(RELAY_4, LOW); delay(1000); }

Quick Steps

  • Copy the above code and open with Arduino IDE
  • Click Upload button on Arduino IDE to upload code to Arduino
  • Listen the click sound on relays.
  • See the result on Serial Monitor.
COM6
Send
Activate all relays Deactivate all relays Activate all relays Deactivate all relays Activate all relays Deactivate all relays Activate all relays Deactivate all relays
Autoscroll Show timestamp
Clear output
9600 baud  
Newline  

Video Tutorial

We are considering to make the video tutorials. If you think the video tutorials are essential, please subscribe to our YouTube channel to give us motivation for making the videos.

Function References

The Best Arduino Starter Kit

※ OUR MESSAGES