In your upcoming project, you might want to use your Arduino to control a high-voltage device, like a lamp, fan, or other household appliance. However, because the Arduino runs on 5 volts, it cannot directly control these high-voltage appliances.
This is where relay modules come into play. These well-contained modules are inexpensive, simple to connect, and ideal for home-brew projects that require switching modest amounts of AC or DC power. The only downside is that, because these are electro-mechanical devices, they are more prone to wear and tear over time.
This tutorial will walk you through setting up the relay module to turn on a lamp or other device, but first, a quick primer on relays.
How Do Relays Work?
At the core of a relay is an electromagnet (a wire coil that becomes a temporary magnet when electricity is passed through it). A relay can be thought of as an electric lever; you turn it on with a relatively small current, and it turns on another device with a much larger current.
Relay Basics
Here’s a small animation showing how a relay links two circuits together.
To illustrate, think about two simple circuits: one with an electromagnet and a switch or sensor, and the other with a magnetic switch and a light bulb.
Initially, both circuits are open, with no current flowing through them.
When a small current flows through the first circuit, the electromagnet is energized, creating a magnetic field around it. The energized electromagnet attracts the second circuit’s contact, closing the switch and allowing a large current to flow.
When the current in the first circuit stops flowing, the contact returns to its original position, reopening the second circuit.
Relay Operation
A relay typically has five pins, three of which are high voltage terminals (NC, COM, and NO) that connect to the device being controlled.
The device is connected between the COM (common) terminal and either the NC (normally closed) or NO (normally open) terminal, depending on whether the device should remain normally on or off.
Between the remaining two pins (coil1 and coil2) is a coil that acts as an electromagnet.
Normally (initial position), the COM terminal is connected to the NC terminal and the NO terminal is open.
When current flows through the coil, the electromagnet becomes energized, causing the switch’s internal contact to move. The COM then connects to the NO terminal, disconnecting from the NC terminal.
When the current stops flowing through the coil, the internal contact is returned to its initial position, re-connecting the NC terminal to the COM and re-opening the NO terminal.
To put it another way, the relay functions as a single-pole-double-throw switch (SPDT).
One-Channel Relay Module Hardware Overview
The one-channel relay module is designed to allow your Arduino to control a single high-powered device. It has a relay with a maximum current rating of 10A at 250VAC or 30VDC.
Modules with two, four, and eight channels are also available. You can choose the one that best meets your needs.
LEDs
The relay module contains two LEDs.
The power LED will light up when the module is powered on. The status LED will light up when the relay is activated.
Output Terminal Block
The high voltage terminals (NC, COM, and NO) of the relay are broken out to a screw terminal. The device you want to control can be connected across them.
Control Pin
On the other side of the module, there is an input pin IN for controlling the relay. This pin is 5V logic compatible, so if you have a microcontroller like an Arduino, you can drive a relay with any digital output pin.
The input pin is active low, which means that a logic LOW activates the relay and a logic HIGH deactivates it.
Module Power
The module operates on 5 volts and draws approximately 70 mA when the relay is activated.
The module also includes a flyback diode that is connected in parallel with the relay coil to safely shunt current when the relay coil is de-energized.
One-Channel Relay Module Pinout
Let’s take a look at the pinout.
Power Pins:
GND is the common ground pin.
VCC pin provides power to the module.
Control Pin:
IN pin is used to control the relay. This is an active low pin, which means that pulling it LOW activates the relay and pulling it HIGH deactivates it.
Output Terminals:
COM terminal connects to the device you intend to control.
NC terminal is normally connected to the COM terminal, unless you activate the relay, which breaks the connection.
NO terminal is normally open, unless you activate the relay that connects it to the COM terminal.
Wiring a One-Channel Relay Module to an Arduino
Now that we know everything about the relay module, it’s time to put it to use! Let’s wire up our relay module to operate a lamp.
Warning: |
Begin by connecting the module’s VCC pin to the Arduino’s 5V pin and the GND pin to ground. Connect digital pin #6 to the IN input pin.
You’ll also need to connect the relay module to the AC-powered device you want to control, in this case, a lamp. You’ll need to cut your live AC line and connect one end of the cut wire (coming from the wall) to COM and the other to NC or NO, depending on what you want your device’s initial state to be.
If you want to keep your device off most of the time and turn it on occasionally, connect the other end of the wire to NO. Otherwise, connect it to NC.
For this project, we want our lamp to be off at first and then turn on when we activate the relay, so we will connect one end of the wire to COM and the other to NO.
The following illustration shows the wiring.
Arduino Example Code
Controlling a relay module with the Arduino is as easy as controlling an LED. Here’s a simple code that will activate the relay for 3 seconds and then deactivate it for 3 seconds.
int RelayPin = 6;
void setup() {
// Set RelayPin as an output pin
pinMode(RelayPin, OUTPUT);
}
void loop() {
// Let's turn on the relay...
digitalWrite(RelayPin, LOW);
delay(3000);
// Let's turn off the relay...
digitalWrite(RelayPin, HIGH);
delay(3000);
}
Code Explanation:
The sketch begins by declaring the pin to which the relay module’s input pin is connected.
int RelayPin = 6;
In the setup function, we configure the input pin to behave as an output.
pinMode(RelayPin, OUTPUT);
In the loop function, we turn the device ON/OFF by pulling the relay pin LOW/HIGH.
digitalWrite(RelayPin, LOW)
pulls the pin LOW, whereas digitalWrite(RelayPin, HIGH)
pulls the pin HIGH.
digitalWrite(RelayPin, LOW);
delay(3000);
digitalWrite(RelayPin, HIGH);
delay(3000);