Project Overview
This project creates a smart closet light that automatically turns on when you open the closet door. A PIR sensor detects motion inside the closet, and an LED strip illuminates the space. The light turns off after a set period of no motion.
Difficulty: Beginner
Estimated time: 1-2 hours
Estimated cost: $15-25
How It Works
A PIR sensor mounted inside the closet detects motion when you open the door. When motion is detected, an LED strip turns on. After a set period of no motion (e.g., 30 seconds), the lights turn off automatically. An optional door contact sensor can also trigger the light when the door opens.
Materials Needed
- Arduino Nano or ESP32 (1)
- AM312 PIR sensor (compact, 3.3V compatible) (1)
- LED strip (5V, warm white, cut to closet length)
- MOSFET or relay module (for switching LED strip)
- Magnetic reed switch (optional, for door detection)
- Jumper wires
- Power supply (5V 2A)
Circuit Diagram
Connection Table
| Component |
Pin |
Arduino Pin |
AM312 PIR |
VCC
5V
AM312 PIR |
GND
GND
AM312 PIR |
OUT
Digital Pin 2
LED Strip (5V) |
VCC
5V
LED Strip |
GND
MOSFET Drain
MOSFET |
Gate
Digital Pin 3
MOSFET |
Source
GND
Reed Switch (optional) |
One side
Digital Pin 4
Reed Switch |
Other side
GND
表
Arduino Code
// Smart Closet Light with PIR Sensor
const int pirPin = 2;
const int ledPin = 3;
const int doorSensorPin = 4; // optional
unsigned long lastMotionTime = 0;
const unsigned long lightTimeout = 30000;
bool lightsOn = false;
void setup() {
pinMode(pirPin, INPUT);
pinMode(ledPin, OUTPUT);
pinMode(doorSensorPin, INPUT_PULLUP);
digitalWrite(ledPin, LOW);
delay(30000);
}
void turnLightsOn() {
digitalWrite(ledPin, HIGH);
lightsOn = true;
lastMotionTime = millis();
}
void turnLightsOff() {
digitalWrite(ledPin, LOW);
lightsOn = false;
}
void loop() {
bool motion = digitalRead(pirPin) == HIGH;
bool doorOpen = digitalRead(doorSensorPin) == LOW;
if (motion || doorOpen) {
if (!lightsOn) {
turnLightsOn();
} else {
lastMotionTime = millis();
}
}
if (lightsOn && (millis() - lastMotionTime > lightTimeout)) {
turnLightsOff();
}
delay(100);
}
Installation Steps
- Assemble the circuit and test on a breadboard.
- Mount the LED strip inside the closet along the door frame.
- Position the PIR sensor to face the interior of the closet.
- If using a reed switch, mount it on the door and frame.
- Enclose the Arduino and power supply in a small box inside the closet.
- Close the door and open it to test the light activation.
Project Extensions
- Use an RGB LED strip for color-changing effects
- Add a potentiometer to adjust light timeout
- Add a DHT22 to monitor closet humidity (prevent mold)
- Add Wi-Fi to report when the closet is opened
Conclusion
This smart closet light eliminates the need to fumble for a switch in the dark.