Random Nerd Tutorials
Shares

Guide for Relay Module with Arduino

Shares

This post shows how to use a relay module with your Arduino. It starts by demonstrating how to connect the relay to mains voltage and to the arduino. Then, you’ll have an example on how to control a lamp with a relay module and a PIR motion sensor.

Introducing the Relay Module

A relay is an electrically operated switch of mains voltage. It means that it can be turned on or off, letting the current go through or not.

Controlling a relay with the Arduino is as simple as controlling an output such as an LED.

The relay module is the one in the figure below.

relay-module

This module has two channels (those blue cubes). There are other varieties with one, four and eight channels.

Mains voltage connections

In relation to mains voltage, relays have 3 possible connections:

relay-labeled

  • COM: common pin
  • NO (Normally Open): there is no contact between the common pin and the normally open pin. So, when you trigger the relay, it connects to the COM pin and supply is provided to a load
  • NC (Normally Closed): there is contact between the common pin and the normally closed pin. There is always connection between the COM and NC pins, even when the relay is turned off. When you trigger the relay, the circuit is opened and there is no supply provided to a load.

If you want to control a lamp for example, it is better to use a normally-open circuit, because we just want to light up the lamp occasionally.

Pin wiring

The connections between the relay module and the Arduino are really simple:

img_6361

  • GND: goes to ground
  • IN1: controls the first relay (it will be connected to an Arduino digital pin)
  • IN2: controls the second relay (it should be connected to an Arduino digital pin if you are using this second relay. Otherwise, you don’t need to connect it)
  • VCC: goes to 5V

Where to buy?

You can buy a relay module for approximately $2 on eBay (view on eBay).

Example: Controlling a Lamp with a Relay Module and PIR Motion Sensor

dsc09897

In this example you will create a motion sensitive lamp. A lamp will light up for 10 seconds every time motion is detected.

Motion will be detected using a PIR motion sensor.

If you are not familiar with the PIR motion sensor, you can read the following post:

Safety warning

Before proceeding with this project, I want to let you know that you’re dealing with mains voltage. Please read the safety warning below carefully.

warning-m

Parts required

Here’s the needed parts for this example:

Code

Copy the following code to your Arduino IDE and upload it to your Arduino board.

Warning: you shouldn’t upload new code while your Arduino is connected to the relay. 

»» View and download code on GitHub!

Schematics

Assemble all the parts as in the schematics below.

Warning: do not touch any wires that are connected to mains voltage. Make sure you have tighten all the screws.

schematics

Demonstration

Now, when motion is detected, your lamp lights up.

relay-module-gif

Wrapping up

Controlling a relay module with the Arduino is as simple as controlling an output.

With the relay module you can control almost any AC electronics appliance (not just lamps).

I hope you’ve found this guide useful.

If you’d like to know more about arduino sensor modules, make sure you take a look at the following posts:

Thanks for reading,

Rui and Sara

Leave a Comment:

Add Your Reply