Introduction
A motion-activated night light can be a useful addition to any home. It turns on automatically when it detects motion, providing illumination when you need it. Making one is a relatively simple electronics project using just a few components, including an Arduino microcontroller and a PIR (passive infrared) motion sensor.
In this guide, I will walk through the steps to build a basic motion-activated night light with an Arduino Uno and PIR sensor. I will cover:
- The components needed
- The circuit diagram
- Assembling the circuit on a breadboard
- Programming the Arduino
- Setting up the PIR sensor
- Enclosing the project in a case
With just a little bit of soldering and coding, you can create a handy gadget to light your way in the dark!
Components Needed
To build the motion-activated night light, you will need the following parts:
-
Arduino Uno - The brain of the device. This microcontroller board will run the code and control the circuit.
-
PIR motion sensor - Detects movement and triggers the Arduino to turn on the light. HC-SR501 and HC-SR504 models are common choices.
-
LED - The light source. Use a bright LED for visibility at night.
-
220 ohm resistor - Limits current for safety. Needed when connecting an LED to the Arduino's 5V digital pins.
-
Breadboard - Used to easily prototype circuits without soldering.
-
Jumper wires - For making connections on the breadboard.
-
9V battery - Portable power source. Can also use a 9V wall adapter.
-
9V battery clip - Connects the 9V battery to the breadboard rails.
-
Enclosure - A box to contain the project. A shoebox or plastic food container works well.
That covers the essential electronics pieces. You may also want hot glue, tape, solder, and basic hand tools to assemble the project.
Circuit Diagram
Here is a diagram showing how to connect the components:
The PIR sensor has three pins - power, ground, and output. The power pin connects to 5V, ground to GND, and output to Arduino digital pin 2.
The LED connects through a 220 ohm resistor to Arduino pin 3. The resistor protects the LED from too much current.
Power comes from the 9V battery clip connected to the breadboard rails.
Assembling the Circuit
To begin, connect the PIR motion sensor to the breadboard according to the circuit diagram. The pins on PIR sensors are typically labeled, so match the GND, 5V, and OUT pins to the corresponding rows on the breadboard.
Next, insert the LED and resistor. Make sure the positive LED leg lines up with the resistor and Arduino pin 3.
Connect four jumper wires - one from the PIR output to Arduino pin 2, one from the PIR 5V to Arduino 5V, and one each from PIR and Arduino GND to a ground rail.
Attach the 9V battery clip by snapping the red and black clips onto the breadboard's power rails.
Plug the Arduino into your computer using a USB cable so you can program it shortly.
Once everything is neatly lined up on the breadboard based on the circuit diagram, double check the connections against the diagram. Also confirm the resistor value and that the LED polarity is correct.
Programming the Arduino
With the hardware assembled, it's time to program. The Arduino code for this project simply reads the PIR sensor pin and turns the LED on or off based on whether motion is detected.
Here are the steps:
-
Install the Arduino IDE on your computer if you don't already have it.
-
Open the IDE, then open a new sketch (File > New).
-
Copy the code below and paste it into the sketch:
```c++
int pirPin = 2; //PIR Out pin connected to pin 2
int ledPin = 3; //LED connected to pin 3
void setup() {
pinMode(ledPin, OUTPUT); // Set LED as output
pinMode(pirPin, INPUT); // Set PIR Out as input
}
void loop(){
if(digitalRead(pirPin) == HIGH){ // If motion detected
digitalWrite(ledPin, HIGH); // Turn on LED
}
else{
digitalWrite(ledPin, LOW); // Else turn off LED
}
}
```
- Verify and upload the sketch to your Arduino board.
This simple sketch continually checks the PIR sensor pin in the loop. When motion is detected, the PIR output goes HIGH and the code turns the LED on. When no motion, the LED turns off.
Setting Up the PIR Sensor
The PIR sensor requires a few adjustments to work properly for this project:
-
Set the time delay - This sets how long the output pin stays HIGH after motion is detected. Turn the knob on the PIR module to anywhere from 3 seconds to 5 minutes. Start low for testing.
-
Adjust the sensitivity - There is also a sensitivity knob that filters noise. Set it so the PIR triggers when you walk by several feet away.
-
Position the sensor - It should point toward the area you want to detect motion. The detection range is typically 3-10 feet.
-
Let it warm up - PIR sensors need 10-60 seconds to stabilize when power is first applied.
Make tweaks to these settings as needed so the sensor reliably detects you walking by but doesn't trigger unnecessarily.
Enclosing the Project in a Case
For a polished finish, mount the project in an enclosure. Here are options:
-
Use a plastic food container, craft box, or even an old shoebox. Cut holes for the PIR sensor window and LED wires.
-
Print a custom enclosure using a 3D printer if you have access to one. Design it to neatly fit all the electronics.
-
For a DIY option, build a wooden enclosure and add a frosted plastic panel in front of the LED to diffuse the light.
Use hot glue, tape, screws or mounting brackets to secure the components inside the enclosure. Make it light-tight so the LED illuminates only when triggered.
Attach the 9V battery with velcro or glue, or solder the battery clip wires directly to a power switch. Add a removable lid to access the electronics.
Conclusion
Building a motion-activated night light with Arduino is an easy, inexpensive project using common components like a PIR sensor, LED, and resistor.
With the help of the circuit diagram, sample code, and setup instructions in this guide, you now have the knowledge to create your own motion-sensing light for navigating dark rooms and corridors.
Customize the design by adding more LEDs in different colors, incorporating a photoresistor to detect ambient light, or using a brighter lamp in place of the LED. The basic functionality will work the same.
So get building and soon you'll have a handy gadget to light up the night! Let me know if you have any other questions.