Having a night light that automatically turns on when it detects motion can be incredibly handy for navigating your home at night. With just a few simple components, you can build your own motion-activated night light using an Arduino microcontroller and a passive infrared (PIR) motion sensor.
What You Will Need
To build this motion-activated night light, you will need the following components:
- An Arduino Uno or any other Arduino microcontroller board
- A PIR motion sensor module
- A 5mm LED (any color you like)
- A 220 ohm resistor for the LED
- A breadboard
- Jumper wires
- A 9V battery and battery clip (or other power supply)
How a PIR Motion Sensor Works
A PIR motion sensor detects movement and changes in infrared radiation. It is able to detect the difference between the infrared radiation emitted by the surrounding environment and that emitted by a human body.
The PIR sensor has two slots inside that are made of a special material that is sensitive to infrared. When a warm body like a human or animal passes by, it first intercepts one half of the PIR sensor, which causes a positive differential change between the two halves. This results in a high output signal. As the warm body moves away, the reverse happens, causing a negative differential change and a low output signal. These change pulses are what is detected.
The Circuit
The circuit for this project is very simple and only requires connecting a few components.
Here is how to connect everything:
-
Connect the output pin of the PIR motion sensor to a digital pin on the Arduino. This will be used to detect motion.
-
Connect the positive (longer) leg of the LED to a digital pin on the Arduino through a 220 ohm resistor. The resistor is needed to limit the current through the LED.
-
Connect the negative (shorter) leg of the LED directly to ground.
-
Connect the positive voltage from the 9V battery to the voltage input on the Arduino.
-
Connect the ground from the battery to the ground pin on the Arduino.
The PIR motion sensor and the LED will both be controlled from the Arduino code.
Here is a circuit diagram showing how everything needs to be connected:
The Arduino Code
The Arduino code for this project reads the output pin of the PIR motion sensor, and turns the LED on or off based on whether motion is detected.
It starts by initializing variables for the PIR pin and the LED pin. In the setup() function, it initializes the LED pin as an output, and initializes serial communication for debugging.
In the loop(), it continuously reads the state of the PIR pin. If motion is detected (the PIR pin goes HIGH), it turns the LED on. If no motion is detected (the PIR pin is LOW), it turns the LED off.
There is a slight delay after turning the LED on to avoid flickering. The state of the PIR and LED pins is also printed to serial so you can observe what is happening.
```cpp
// Pins
const int pirPin = 2;
const int ledPin = 13;
// Variables
int pirValue; // Holds PIR status
void setup() {
// Set LED pin as output
pinMode(ledPin, OUTPUT);
// Initialize serial communication
Serial.begin(9600);
}
void loop() {
// Read PIR motion value
pirValue = digitalRead(pirPin);
// Print out results
Serial.print("Motion detected: ");
Serial.print(pirValue);
// Turn on LED if motion detected
if (pirValue == HIGH) {
digitalWrite(ledPin, HIGH);
Serial.println(" - LED ON");
delay(100); // Avoid flickering
}
// Turn off LED if no motion
else {
digitalWrite(ledPin, LOW);
Serial.println(" - LED OFF");
}
}
```
This simple code is all you need to detect motion and turn on the LED when triggered by the PIR sensor.
Upload it to your Arduino board and open the serial monitor at 9600 baud to see the sensor readings.
Putting it Together
Once you have the circuit assembled and code uploaded, you just need to enclose it in a housing of some kind to block external light and finish it off.
Some options for the enclosure:
- A cardboard box
- A section of PVC pipe
- A plastic food container
- A painted wooden box
Cut a hole in the enclosure for the PIR sensor to peek through. Make sure no external light gets in.
Power it up with the 9V battery, and your motion-sensing night light is ready! Walk in front of the PIR sensor to test and the LED should illuminate.
Usage Tips
-
Place the night light in a high-traffic area of your home where the light is useful, like a hallway, entryway, or staircase.
-
Adjust the sensitivity of the PIR sensor with the potentiometer. Turn it up to make it trigger from farther away.
-
Make sure the PIR sensor has line-of-sight to the area you want to detect motion.
-
Use a diffuse or wide-angle LED to provide soft illumination.
-
Swap in a brighter LED if you want more light output.
With this easy DIY project, you can build a handy motion-activated night light to install anywhere in your home!