Lights-out reminder on an Arduino Uno
A PIR and a light sensor notice when a room is lit and nobody has moved for a while, then a piezo chimes and a green LED blinks a reminder to switch the lights off. The whole build, an Arduino Uno and 5 more parts, runs here in your browser on the firmware below; open it in the editor to change the wiring or the code and run it again.
Teaching? Assign it to your class in one click.
Beginner Runs in your browser. Free, and no account needed.
The code
The firmware exactly as the editor opens it. Change a line there and press Run: it compiles in the browser.
// Lights-out reminder: a small helper for Earth Day and every day after. If
// the lights are on in a room and nobody has moved for a while, it chimes
// softly and blinks a green reminder to switch them off.
//
// PIR OUT -> pin 2, VCC -> 5 V, GND -> GND
// light sensor module AO -> A0, VCC -> 5 V, GND -> GND
// lamp pin 5 -> 220 ohms -> green LED -> GND
// piezo pin 8 -> piezo -> GND
//
// Two sensors answer two questions. The light module says whether the room is
// lit: its reading falls as the light grows, so a reading below LIT_BELOW is
// a room with the lights on. The PIR says whether anyone is there: its output
// goes high for a few seconds each time something warm moves.
//
// Every movement starts the timer again. When the room has been lit and
// still for AWAY_MS, the reminder starts: one soft chime, then the green lamp
// blinks until someone moves or the lights go off. It also keeps a running
// count of the minutes the lights were left on in an empty room, printed on
// the Serial Monitor, which is a good number for a class to try to bring
// down.
//
// AWAY_MS is 15 seconds here so you can see it work. In a real room, 10 or 15
// minutes is kinder: people sit still to read.
//
// Things to try. Leave the PIR alone and wait. Click the PIR to move. Drag
// the light sensor's slider down to turn the room's lights off.
const int PIR_PIN = 2;
const int LAMP_PIN = 5;
const int PIEZO_PIN = 8;
const int LIT_BELOW = 400; // A0 reading: lights on below this
const unsigned long AWAY_MS = 15000UL; // still this long before a reminder
unsigned long lastMoved = 0;
bool reminding = false;
unsigned long wastedSeconds = 0;
unsigned long lastTick = 0;
void setup() {
Serial.begin(9600);
pinMode(PIR_PIN, INPUT);
pinMode(LAMP_PIN, OUTPUT);
Serial.println("Lights-out reminder watching the room");
}
void loop() {
unsigned long now = millis();
bool lit = analogRead(A0) < LIT_BELOW;
if (digitalRead(PIR_PIN) == HIGH) lastMoved = now;
bool empty = lit && now - lastMoved >= AWAY_MS;
if (empty && !reminding) {
reminding = true;
Serial.println("Lights on in an empty room: please switch them off");
tone(PIEZO_PIN, 660, 150);
delay(180);
tone(PIEZO_PIN, 880, 250);
} else if (!empty && reminding) {
reminding = false;
digitalWrite(LAMP_PIN, LOW);
Serial.println(lit ? "Someone is here, thanks" : "Lights off, thank you!");
}
if (reminding) {
digitalWrite(LAMP_PIN, (now / 500) % 2);
if (now - lastTick >= 1000) {
lastTick = now;
wastedSeconds++;
if (wastedSeconds % 60 == 0) {
Serial.print(wastedSeconds / 60);
Serial.println(" minutes of light in an empty room so far");
}
}
}
delay(20);
}
Parts list
7 parts, plus the jumper wires. Every one is in the editor's parts bin.
- 1 × Arduino Uno R3
- 1 × Half breadboard
- 1 × Motion sensor
- 1 × Photoresistor sensor module
- 1 × Resistor, 220 Ω
- 1 × LED, green
- 1 × Piezo speaker
How it is wired
7 connections, pin by pin, read from the circuit itself. Each line is one set of pins joined together, by a jumper wire or a breadboard strip.
- Arduino Uno R3 pin 8; Piezo speaker pin 1
- Arduino Uno R3 pin 5; Resistor, 220 Ω pin 1
- Arduino Uno R3 pin 2; Motion sensor pin OUT
- Arduino Uno R3 pin 5V; Motion sensor pin VCC; Photoresistor sensor module pin VCC
- Ground: Arduino Uno R3 pin GND; Motion sensor pin GND; Photoresistor sensor module pin GND; LED, green pin C; Piezo speaker pin 2
- Arduino Uno R3 pin A0; Photoresistor sensor module pin AO
- Resistor, 220 Ω pin 2; LED, green pin A
Change it and keep it
Open it in the editor, change the circuit or the code, and keep your version in a free account.