Do it yourself Motion Lamp

For this tutorial our goal is to make a motion detect lamp. The lamp will turn on for a set amount of time if it sees any activity near it’s motion detector.

This tutorial is an expansion on our tutorial for controlling a light with an arduino using a relay. You should start with it before coming to this tutorial. It will make sure the relay and light fixture are working correctly.

Everything needed to follow along with this tutorial can be found in the elegoo uno starter kit. You can get your kit off amazon.


In addition to the parts used in the arduino tutorial you will need:

  1. HC-SR501 motion sensor
  2. 12 V wall wart power supply
  3. jumper wires.

Our first step is to connect the HC-SR501 motion sensor to our Arduino. The convenient thing about the motion sensor is that it just has 3 pins : VCC, GND and OUT(Output signal)

photo-oct-05-7-26-13-pm

Connect the VCC to 5V of the Arduino, GND to ground of the Arduino and OUT to pin 7 of the Arduino.

We first want to write a quick sketch that uses the output of the motion sensor to open or close the relay.

Simply:

  1. If the motion sensor outputs a detection – > Close the relay,
  2. If the motion sensor does not output a detection – > Open the relay.

7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
const int SensorPin = 7; // The pin number for the MotionSensor input signal
const int RelayPin = 3; // The pin number for the relay that turns on the light output signal
boolean motionstate = false; // State if someone is present or not
 
void setup() {
// initialize digital pin 3 as an output.
pinMode(RelayPin, OUTPUT);
}
</code></span><span style="color: #0000ff;"><code>
//The main loop will continue to run until the arduino loses power
void loop() {
motionstate = digitalRead(SensorPin);
if(motionstate = true)
{
digitalWrite(RelayPin, HIGH); // turn the relay on to turn on the light
}
else
{
digitalWrite(RelayPin, LOW); // turn the relay off to turn off the light
}
 
}

Run this for a while, it is actually pretty fun. You can see that there is a slight delay for how long the relay is closed. This is because the Motion sensor has a delay built into it so that whenever it detects something, it latches that value for a short period of time. This is a form of software filtering that’s inherent to this motion sensor. It actually helps us, because without it this program would be constantly chattering the relay depending on if it was detecting motion or not. You can get the simple relay lamp code here.

Although this is fun to play with, let’s make this a little more applicable as a motion sensor light. To do this, we will use the arduinos timer using the millis() command to turn the light on for 5 minutes if motion is detected. With that, the timer will restart if motion is ever detected again.

The first thing we will do is setup our constants


7
8
9
10
11
12
13
const int SensorPin = 7; // The pin number for the MotionSensor input signal</span>
<span style="color: #0000ff;">const int RelayPin = 3; // The pin number for the relay that turns on the light output signal</span>
<span style="color: #0000ff;">boolean motionstate = false; // State if someone is present or not</span>
<span style="color: #0000ff;">long lighttimer = <span style="color: #ff0000;">300000</span>; // Timer for how long to keep light on (in milliseconds)</span>
<span style="color: #0000ff;">int motionstarttime = millis(); // Variable to store time on the arduino when the motion is sensed</span>
<span style="color: #0000ff;">int currenttime = millis(); // Variable to store current time of the loop</span></code><code>
<span style="color: #0000ff;">int elapsedtime = 0; // Variable to store ellapsed time from the detected motion event.

This includes our sensor pin info and motion state info from the simple script we wrote above, our light timer, and timing information variables. The “lighttimer” is how long in milliseconds the light will be on after detecting a motion. The timing information is one variable to store the time at which something is detected. Another to store the current time. Then the final timer variable will subtract the two in order to determine the elapsed amount of time.

To begin writing this code, we will expand on the simple script we wrote again. If motion is detected, we will store the time and open the relay


25
26
27
28
 if(motionstate == HIGH)</span>
<span style="color: #0000ff;"> {</span>
<span style="color: #0000ff;"> motionstarttime = millis(); // store the time that our motion is detected</span>
<span style="color: #0000ff;"> digitalWrite(3, HIGH); // turn the relay on to turn on the light

After this we need to calculate the elapsed time to know whether the relay should be turned off or not. We do this

by storing the present time then subtracting the motion state time from that


30
31
currenttime = millis(); // get the current time
elapsedtime = currenttime - motionstarttime;

Then if the elapsed time has exceed our lighttimer, it is now time to turn off the light.


32
33
34
if((elapsedtime - lighttimer) >= 0)
<span style="color: #0000ff;"> {
<span style="color: #0000ff;"> digitalWrite(3, LOW); // turn the relay off to turn off the light

Motion Detect Light Bulb Lamp

So now you have it. A very simple motion detect light. You can put this in a living room , work bench area, or a place to spook your pets.

You can get the entire code set by clicking here.

If you liked this tutorial make sure you subscribe! We will continually put out more tutorials, reviews and projects to help you become more familiar and comfortable with using Arduinos and other hobbyist electronics tools.

Leave a Reply

Your email address will not be published. Required fields are marked *