How to connect and use a Relay module with an Arduino

//How to connect and use a Relay module with an Arduino

How to connect and use a Relay Module with an Arduino


This might seem like a very simple tutorial, and in a way it is!

But knowing how to connect and use these small Relay Modules the right way is important since we are playing with High Voltage (120v) and we don’t want to damage our Arduino, or ourselves…

In this tutorial we will use an IR Motion Sensor to detect movement and turn on a 120V light using the relay module.


Since we are using LINE VOLTAGE of 120V, you should take the necessary precautions when dealing with high voltage.  If you are worried or not sure, you should not proceed any further….  

You have been warned!  🙂


We connect the IR Motion sensor “OUT” to Pin 4 and the Relay “IN1″ to Pin 8.

The light bulb has one of the 120V wire spliced to connect the end connected to the power plug to the COM(Common) Pin of the Relay module and the one going to the lamp is connected to the NC(Normally Closed) Pin.

We use a breadboard to connect the VCC and Ground to both modules from our UNO, but we connect a 1N4007 rectifier Diode to the VCC line of the Relay Module to allow the current to go only one way.

One important thing that is sometime forgotten is to use a Rectifier Diode when switching High Voltage.

Some Relays have this protection integrated, but these Diode are so cheap, why not add another layer of protection to our circuit.

Putting the Diode between the Relay and Arduino, will protect us from power surges when the Relay switches since the Diode only allows current to flow one way.

For more information about the Relay module connections and IR Motion sensor jumper settings, please watch our Tutorial video..


No libraries or complicated stuff here…

All we are doing is looking at the Pin the IR Motion sensor is connected to.

If the Pin 4 is LOW, it means no motion is detected, if HIGH then motion is detected.

We are using a WHILE loop to check if Pin 4 is HIGH, if yes then we make Pin 8 HIGH turning on the Relay module.

If Pin 4 is LOW, then we make Pin 8 LOW turning off the relay.

As always please watch our Tutorial video for more information.

int irmotionPin = 4;  // Pin of IR Motion Sensor
int relayPin = 8;  // Pin of Relay Module

void setup(){
  pinMode(relayPin, OUTPUT);  // Set Pin connected to Relay as an OUTPUT
  digitalWrite(relayPin, LOW);  // Set Pin to LOW to turn Relay OFF

void loop(){
  while (digitalRead(irmotionPin) == HIGH) {  // If Motion detected 
       digitalWrite(relayPin, HIGH);  // Turn Relay ON
       Serial.println("Relay is ON");

       digitalWrite(relayPin, LOW);  // Turn Relay OFF
       Serial.println("Relay is OFF");

Copy and paste the above code in the Arduino IDE to program your Arduino.

By | 2018-02-23T17:22:22-05:00 February 27th, 2015|Tutorials|


  1. CHuck April 20, 2020 at 8:33 pm - Reply

    Made a few change to this code to operate a similar latching relay – thanks for the initial code

  2. Zeeshan January 6, 2020 at 7:28 am - Reply

    Hi sir, I am making a automation lighting system for my class in which I am using a nodemcu with 2 pir sensors and 4 relay channel module, I want to connect 2 relays for one pir sensor in which it detect the motion for every 5 minutes and will turn on the lights until there are motion and when the class is empty then will turn off the lights automatically. Can you help me to make a coding for this… Plzz

  3. Luis November 14, 2018 at 10:11 pm - Reply

    Should it really be connected to NC? Or NO?

    • Ken H November 25, 2018 at 2:13 am - Reply

      NC = Normally Closed (switched on), NO = Normally Open (switched off). I usually determine what state the load (whatever is being driven by the relay) will spend most of its life doing. If (in this case) the light is in a bathroom and will be off most of the time and only come on occasionally, I would hook it up to NO. That way the coil in the relay is only on for a short duration and will prolong its life. This is an even more important consideration if your arduino is running on battery power as current is also only drawn to power the coil for the shorter duration as well, extending your battery life.

  4. Michael kruis August 25, 2018 at 10:48 am - Reply

    can you help with a project i am trying to switch 4 relays on sequentially with sw1,e ach press wil activate the next relay and switch of the previous one
    sw2 will then return to the beginning, relay one no until sw1 is pressed

  5. shreya June 20, 2018 at 3:55 am - Reply

    What are type of wires used for the connections ?
    also can you provide a detailed list of the components required

  6. Ryan March 4, 2018 at 2:08 pm - Reply

    Can you give a list of the required materials to complete this project? I want to be sure to purchase the right components needed.

    Thank you.

Leave A Comment