Random Nerd Tutorials

Guide for Relay Module with Arduino


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.


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:


  • 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:


  • 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 $3. Visit Maker Advisor to find the best price.

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


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.

Parts required

Here’s the needed parts for this example:

You can use the preceding links or go directly to MakerAdvisor.com/tools to find all the parts for your projects at the best price!


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. 


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.



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


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.

MicroPython Programming with ESP32/ESP8266 eBook

Learn how to program the ESP boards with MicroPython, including projects, tips, and tricks! The registrations are open for a limited time, so SIGN UP NOW »

Recommended Resources

Home Automation using ESP8266 »
Build IoT projects and home automation gadgets with the ESP8266 Wi-Fi module.

Build a Home Automation System »
Learn how to build a automation system using open-source hardware and software from scratch.

Arduino Step-by-Step Projects »
Build 25 cool Arduino projects with our course even with no prior experience!

Leave a Comment:

Add Your Reply

⏰ Registrations are open for "MicroPython Programming with ESP32 and ESP8266" eBook