Introduction
Having a night light that automatically turns on when it senses motion can be incredibly handy for navigating your home at night. The best part is that with an Arduino microcontroller, some basic electronic components, and a 3D printed housing, you can build one yourself for less than $15!
In this comprehensive, step-by-step guide, I'll walk you through everything you need to know to build your own motion-sensing night light with an Arduino.
Prerequisites
To complete this project, you'll need:
- An Arduino Uno or compatible microcontroller board
- A USB cable to connect the Arduino to your computer
- A breadboard for connecting circuits
- A PIR (passive infrared) motion sensor to detect movement
- A photoresistor to detect ambient light levels
- A 10K ohm resistor
- A 220 ohm resistor
- A LED for the light
- Jumper wires for making connections
- A 3D printed housing (or you can build your own enclosure)
Additionally, you'll need:
- The Arduino IDE software installed on your computer
- Basic understanding of Arduino code and electronics
- Soldering skills to assemble the final device
As long as you have all of these things, you can build this night light even if you're completely new to Arduinos!
How It Works
The main components of the motion-sensing night light are the PIR sensor and the photoresistor.
-
The PIR sensor detects when a person moves within its range, up to approximately 6-10 feet away. When it senses motion, it outputs a high signal voltage.
-
The photoresistor changes its resistance based on the amount of light in the room. More light means lower resistance, while less light means higher resistance.
The Arduino reads both of these sensor values. When it detects motion (high signal from PIR) AND low light levels (high resistance from photoresistor), it turns the LED light on.
This circuit gives the night light smart, automated functionality to turn on only when needed!
Circuit Diagram
Here is a circuit diagram showing how all the components are connected:
Let's go through what each component does:
- The PIR motion sensor detects movement and outputs a high signal when triggered.
- The photoresistor and 10K ohm resistor form a voltage divider. This allows the Arduino to measure ambient light levels.
- The 220 ohm resistor is used with the LED to limit current.
- The Arduino reads the sensors and controls the LED.
Code
The Arduino code brings everything together to give the motion sensing functionality.
Libraries
You need to include the Servo library to use the attach()
and write()
functions for controlling the servo:
```cpp
include
```
Variables
Next, you need to initialize variables to store values:
```cpp
// Assign pins
const int pirPin = 3;
const int ledPin = 13;
const int photoPin = A0;
// Threshold light level
const int threshold = 100;
// Servo variables
Servo myServo;
int pos = 0;
```
pirPin
,ledPin
,photoPin
store the Arduino pins everything is connected tothreshold
is the light level that determines when the LED turns onmyServo
controls the servopos
stores the servo position
Setup
In the setup, initialize the servo and LED pin outputs:
```cpp
void setup() {
myServo.attach(9);
myServo.write(0);
pinMode(ledPin, OUTPUT);
}
```
This attaches the servo to pin 9, sets initial position to 0, and sets the LED pin as an output.
Main Loop
The main loop continuously checks the PIR and light sensor values to control the night light:
```cpp
void loop() {
// Check if motion detected
if (digitalRead(pirPin) == HIGH) {
// Check light level
int lightLevel = analogRead(photoPin);
// Turn LED on if light level below threshold
if (lightLevel < threshold) {
digitalWrite(ledPin, HIGH);
}
else {
digitalWrite(ledPin, LOW);
}
}
else {
digitalWrite(ledPin, LOW);
}
}
```
- It first checks if the PIR detects motion.
- Then it reads the photoresistor voltage using
analogRead()
and compares to the threshold. - If motion is detected AND light is below the threshold, it turns the LED on.
- Otherwise, the LED remains off.
This gives the desired night light behavior!
Building the Circuit
With the diagram and code ready, it's time to build the physical circuit:
Steps
- Insert the Arduino into a breadboard
- Connect the PIR VCC pin to 5V and GND to ground
- Connect photo resistor to 5V through 10K ohm resistor (voltage divider)
- Connect LED through 220 ohm resistor to ground
- Connect all other pins according to circuit diagram
- Upload the Arduino code
- Power on and test! The LED should turn on when you move in low light.
Tips
- Refer to the pin labels on each component rather than just following the colors
- Double check all connections match circuit before powering on
- Start by testing the PIR and photoresistor separately before combining
Taking the time to carefully build the circuit is key to getting the night light working!
Building the Housing
To complete the night light, you need some sort of housing for the components and LED. Here are a few options:
3D Printed Case
- Design a case in a 3D modeling software like Fusion 360
- Leave space for all components and wiring
- Print with translucent filament for light diffusion
Recycled Container
- Find a recycled plastic container or tub aware
- Cut holes for the PIR sensor, switch, and LED
- Use hot glue to hold components in place
DIY Cardboard Enclosure
- Build an enclosure out of cardboard, wood, etc.
- Decorate with paints, paper, stickers, etc to customize
The housing really comes down to personal preference and what you have available. Anything that safely contains the electronics and directs the LED light works great!
Conclusion
Building your own motion-sensing night light with an Arduino is a fun electronics project that anyone can complete in an evening for under $15. With the detailed guide above on the circuit, code, and housing, you should have everything you need to bring your own version to life!
Some possible ways to expand on this project include:
- Adding a real-time clock to turn on at set times
- Including a light sensor so it only turns on in the dark
- Adding a WiFi module for smartphone control
- Creating multiple night lights that sync together
The opportunities are endless when you dive into DIY Arduino projects. Get creative and most importantly - have fun tinkering!