How to Build a Simple Motion-Sensing Night Light With An Arduino For Under

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:

Additionally, you'll need:

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 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:

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;
```

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);

}

}
```

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

  1. Insert the Arduino into a breadboard
  2. Connect the PIR VCC pin to 5V and GND to ground
  3. Connect photo resistor to 5V through 10K ohm resistor (voltage divider)
  4. Connect LED through 220 ohm resistor to ground
  5. Connect all other pins according to circuit diagram
  6. Upload the Arduino code
  7. Power on and test! The LED should turn on when you move in low light.

Tips

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

Recycled Container

DIY Cardboard Enclosure

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:

The opportunities are endless when you dive into DIY Arduino projects. Get creative and most importantly - have fun tinkering!