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:

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:

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:

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

With this easy DIY project, you can build a handy motion-activated night light to install anywhere in your home!