Motion Activated Light With Arduino and HC-SR04 Sensor




Introduction: Motion Activated Light With Arduino and HC-SR04 Sensor

I did this project long time ago to light up the kitchen when I open the back door.

The idea was when I open the back door, this light turns on for 15 seconds, which will give me enough time to reach the light switch on the opposite wall. Before this project, I had to walk in the dark to turn on the light.

This instructable assumes that you have working knowledge with Arduino and electronics in general and that you have already installed the Arduino IDE on your computer. Other wise I made sure to detail it as much as possible and took enough pictures to make as clear as possible. If you have any questions after reading through do not hesitate to contact me.

Happy building.

Step 1: What You Need for This Project

Electronic equipment:

Arduino UNO

HC-SR04 Ultrasonic Sensor

Power Relay (I used a Songle srd-05vdc-sl-c)


A regular extension cord (Optional, because I didn't want to cut the cable attached to the light Bulb)

7 Watt light Bulb with a power cable (I took it from a Christmas decoration :) )

9 Jumper cables (3 male/female for the relay and 6 male/male for the Arduino and the HC-SR04)


Solderless breadboard

USB cable to connect the Arduino to the computer

Arduino IDE

Ruler (30 cm / 12 inches)

A small screwdriver

Step 2: Cut the Extension Cord

Separate the extension cord in half, about 10cm/4inches and cut one of the sides to expose the copper (see picture).

It doesn't matter which side you cut because the cord will be plugged into an AC power outlet (wall jack)

I tinted the exposed copper with a bit of solder but this is completely optional, you can just twist them together.

Step 3: Connect the Arduino to the Breadboard

First we will connect the Arduino UNO and the Breadboard:

I use red Jumper wire to connect 5V PIN on the Arduino to the red + rail on the breadboard

and I used a Black Jumper wire to connect the GND (ground) PIN on the Arduino to the blue - rail on the breadboard.

See picture for details.

Step 4: Connect the HC-SR04 to the Breadboard and the Arduino

Place the HC-SR04 Ultrasonic sensor on the edge of the breadboard with the sensors pointing away from the board, see picture.

The HC-SR04 Ultrasonic sensor has 4 PINs

  1. The first PIN is the VCC and it will be connected with a red jumper wire to the red + rail on the bread board.
  2. The second PIN is the Trig or trigger and it will be connected to PIN 6 on the Arduino. The trigger PIN is used by the ultrasonic sensor to send out a sound wave.
  3. The third PIN is the Echo and it will be connected to PIN 5 on the Arduino. The echo PIN is used to pick up returning sound waves sent by the trigger.
  4. The last PIN in the GND or ground and it will be connected with a black jumper wire to the blue - rail on the breadboard.

Step 5: Connect the Power Relay to the Arduino, the Breadboard and the Extention Cord

The songle srd-05vdc-sl-c power relay has three pins on one side and three screw terminals on the opposite side.

The three PINs are labeled as follows: s, +, -

The S PIN is the signal and it is connected to PIN 9 on the Arduino

The + PIN is the 5V or VCC and it is connected with a red jumper wire to the red + rail on the breadboard.

The - PIN is the ground and it is connected with a black jumper wire to the blue - rail on the breadboard.

The screw terminal side of the power relay is labeled as follows: left NC, middle nothing, right NO

Here you will take the cut section of the extension cord and screw in one end to the middle (no label) and the other end to the NO using a small screw driver.

NO = Naturally open, which means that the circuit is open and no electricity is flowing through.

NC = Naturally closed, which means that the circuit is closed and electricity is freely flowing.

We will start with an open circuit = the light bulb is off, and when the relay receives a signal from the Arduino on the Signal PIN to close the circuit, the light bulb will turn on and we hear the distinctive "Click" sound of the power relay activating.

Before plugging in the extension cord in the AC power outlet, lets work on the sketch and upload it to the Arduino first.

Step 6: The Arduino Sketch

First I will try to explain the sketch with words:

Initially I define three variables which are the physical pins from the HC-SR04 "trigger and echo" and the power relay.

In the void setup, I start the serial monitor, define the Trigger and the Relay pins as outputs because they will output a result while the echo pin is defined as input because it will be waiting for information from the trigger pin.

The action happens in the void loop:

First I define two integers duration and distance to store the information gathered by the trigger. Then I tell the trigger to send a sound wave using the digitalWrite(trigPin, HIGH); command, then wait half a millisecond then I turn it off using the digitalWrite(trigPin, LOW); command. When the wave returns I store the information in the "duration" integer and activate the echo pin using the command pulseIn(echoPin, HIGH); Now we do some math to determine the distance, which is duration/2 then we divide this answer by 29.1 to get the result in centimeters.

The next step is to verify the result that we got: If the result is less than 13cm, execute the light subroutine, if not then do nothing. And display the result on the serial monitor.

The light subroutine:

When invoked, it activates the power relay pin, waits 15 seconds then it turns it off.

As simple as that :)

And now the Sketch:

#define trigPin 6 //Define the HC-SE04 triger on pin 6 on the arduino
#define echoPin 5 //Define the HC-SE04 echo on pin 5 on the arduino

#define bulb 9 //Define the relay signal on pin 9 on the arduino

void setup()


Serial.begin (9600); //Start the serial monitor

pinMode(trigPin, OUTPUT); //set the trigpin to output

pinMode(echoPin, INPUT); //set the echopin to input

pinMode (bulb, OUTPUT); //set the bulb on pin 9 to output


void loop()


int duration, distance; //Define two intregers duration and distance to be used to save data

digitalWrite(trigPin, HIGH); //write a digital high to the trigpin to send out the pulse

delayMicroseconds(500); //wait half a millisecond

digitalWrite(trigPin, LOW); //turn off the trigpin

duration = pulseIn(echoPin, HIGH); //measure the time using pulsein when the echo receives a signal set it to high

distance = (duration/2) / 29.1; //distance is the duration devided by 2 becasue the signal traveled from the trigpin then back to the echo pin, then divide by 29.1 to convert to centimeters

if (distance < 13) //if the distance is less than 13 CM


Light(); //execute the Light subroutine below


Serial.print(distance); //Dispaly the distance on the serial monitor

Serial.println(" CM"); //in centimeters

delay(500); //delay half a second


void Light() //Start the Light subroutine

{ digitalWrite(bulb, HIGH); //turn on the light

delay (15000); //wait 15 seconds

digitalWrite(bulb, LOW); //turn off the light


Step 7: End Result

Once you verify and upload the sketch to the Arduino, plug the extension cord in AC wall outlet, the light will turn on if you place an object less than 13 cm in front of the HC-SR04.

I will make another instructable once I find a 3D print master to make this project look pretty.

I hope you like it.

3 People Made This Project!


  • Cheese Challenge

    Cheese Challenge
  • Plywood Contest

    Plywood Contest
  • Metal Contest

    Metal Contest



1 year ago on Step 6

Hi !
Is it possible to make the same project for 3 hcsr 04 sensors to command 3 separate relays?


Question 1 year ago on Introduction

Is it necessary to turn only one relay on, can it be possible to turn on 6 relays? Because I want to make similar project but for my classroom, Is it possible?


Question 1 year ago on Step 6

Can you please post the circuit diagram.


1 year ago

Do you have any ideea to do the same but with a WS2812b addressable LED ?

Thank you


Question 1 year ago

this is a great application.
I need this for 4 lamps.
The switc on period must be variable
Is this possible.
Could you do the program for me and what would be the cost.

Thank you for a response




3 years ago

It actually worked (finally). Usually none have a viable method and usually seem like all scams to get notoriety ($$$). But well done.
However, there's a problem with the code, it tends to go off without being directly in front of the ultrasonic, and won't stay on that long. I want to have this activate an industrial vacuum for other purposes and won't stay on long enough and I fear it's cause blockage in the tube.


3 years ago


Great project I wanted to make this for my garage door, got it working, but still not good. I did use the sketch here, but the light is not on for 15 seconds here, its much longer or much shorter, even if I'm not close to the ultrasonic sensor. I can turn it on/off if I use my hand in front, but it does not turn off after 15 seconds - it goes off if I take my hand in front of it


3 years ago on Introduction

Thank you for uploading this easy to follow and very well put together tutorial, I've been wanting to make a sensor light like this for ages but couldn't find a good enough video.


Question 4 years ago

Hello! I'm trying to alter this to use this for a dc motor, simply to turn it on and off. Would it be possible? because I've followed your steps closely but it seems like it does not work :( hope i could get some help, thank you in advance!


5 years ago

Hello, nice tuto!

I would like to do this using a HC-SR05. Is it possible and what do I have to modify? Thanks in advance. Vincent


6 years ago

I want to turn on the light when i enter the room and turn it off when i leave.
what modification should i do to this code?


6 years ago

Awesome, is it also possible to add a stronger light bulb to it, like the ones we use around in our houses? and is it also possible to let the lightbulb stay on as long there is something blocking its sight?



7 years ago on Introduction

Is it necessary to say the arduino to close the lamp after 15 seconds??? because I want to make a similar project to this...And I want the arduino close the light's only if I pass my hand again from this 13cm distance :P

for exaple

I enter to my room and light's are off.....I pass my hand from hcsr-04 and lights auto turn on
And When I leave the room I will pass my hand from the hcsr-04 and the light's auto turn off.

can it be possible ???


Reply 7 years ago on Introduction

Hi Katsibelos20, it is absolutely possible. With Arduino you are only limited by your imagination.

Here you go, a simple modification to the sketch

replace the void Light () with this:

void Light()
int reading;
reading = digitalRead(bulb);
if (reading == HIGH) {digitalWrite (bulb, LOW);}
else {digitalWrite (bulb, HIGH);}


This will make the Sensor wait for a signal at less than 13 cm to either turn the light on or off.



7 years ago