Built-in project · Arduino Uno R3

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 About 20 minutes to build and run. Runs in your browser. Free, and no account needed.

The parts retail for about $33. On Mokxi they're free.

Lights-out reminder · Arduino Uno R3live0.000 s 0.00x
Click to open it in the editor
The circuit itself, running here on the simulator. Press what can be pressed; click anything else to open it in the editor.

What you'll learn

  • Combining two sensors to make a decision
  • Timing without blocking, using millis
  • Thresholds on an analog light reading
  • Tracking state with boolean flags
  • Difficulty: Beginner
  • Time: About 20 minutes
  • Board: Arduino Uno R3
  • Parts: 7

How it works

Two sensors answer two questions. The light module on A0 says whether the room is lit. The PIR on pin 2 says whether anyone is moving. A reading below LIT_BELOW (400) counts as lights on.

Every time the PIR goes HIGH, the sketch notes the time. If the room has been lit and still for AWAY_MS (15 seconds), the reminder starts: two chime notes on the piezo, then the green LED blinks.

The reminder stops when someone moves or the lights go off. While it runs, the sketch counts the seconds wasted and prints a total every minute. In real use you would make the wait much longer.

Wiring table

Every connection, pin by pin, read from the circuit itself. Each row joins the pin on the left to the pin on the right, by a jumper wire or a breadboard strip.

Part and pinConnects to
Piezo speaker pin 1Arduino Uno R3 pin 8
Resistor, 220 Ω pin 1Arduino Uno R3 pin 5
Motion sensor pin OUTArduino Uno R3 pin 2
Motion sensor pin VCCArduino Uno R3 pin 5V
Photoresistor sensor module pin VCCArduino Uno R3 pin 5V
Motion sensor pin GNDArduino Uno R3 pin GND
Photoresistor sensor module pin GNDArduino Uno R3 pin GND
LED, green pin CArduino Uno R3 pin GND
Piezo speaker pin 2Arduino Uno R3 pin GND
Photoresistor sensor module pin AOArduino Uno R3 pin A0
LED, green pin AResistor, 220 Ω pin 2

The code

The firmware exactly as the editor opens it. Change a line there and press Run: it compiles in the browser.

sketch.ino · Arduino Uno R3
// 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);
}

Code walkthrough

The key functions of the sketch, and what each one does.

setup sketch.ino

Opens serial at 9600, sets the PIR pin as input and the lamp pin as output, and prints that it is watching the room.

void setup() {
  Serial.begin(9600);
  pinMode(PIR_PIN, INPUT);
  pinMode(LAMP_PIN, OUTPUT);
  Serial.println("Lights-out reminder watching the room");
}

loop sketch.ino

Works out lit from A0 and records lastMoved whenever the PIR is HIGH. The room is empty when it is lit and still for AWAY_MS. The first pass of empty plays two tones and prints a message. When the room is no longer empty, the lamp goes off. While reminding, the lamp blinks every half second.

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");
      }
    }
  // ... 3 more lines in the editor

Troubleshooting

The usual mistakes with this circuit, and the fix for each.

  • The reminder never starts This project
    The light reading must be under 400 for the room to count as lit. Drag the light sensor so the room is bright, then leave the PIR alone for 15 seconds.
  • The reminder starts too soon or too late This project
    Change AWAY_MS. It is in milliseconds, so 15000 is 15 seconds.
  • No chime This project
    Check that the piezo is between pin 8 and ground.
  • The sensor reads high for ages after power up. Motion sensor
    A real PIR needs 30 to 60 seconds to settle. Ignore its output during that time, and keep still in front of it until the delay passes.
  • Wrong output or it never triggers. Motion sensor
    Power it from 5 V, join GND, and read OUT as a digital input (3.3 V logic high). Check the trimmers for delay and sensitivity.
  • The digital output never changes, or is always on, no matter the light. Photoresistor sensor module
    Turn the trimmer on the module until DO flips at the light level you want. The digital output is set by that threshold, and which direction means bright depends on the board.
  • Analog readings go down when it gets brighter, and the code expects the opposite. Photoresistor sensor module
    The LDR sits in a divider, so the direction depends on how the module is built. Print the raw value in the light and dark, then flip it in code with 1023 minus the reading if needed.
  • The LED is dim, burns out, or the pin reads wrong because the resistor value is off. Resistor
    Read the color bands again. A 220 ohm to 330 ohm resistor suits a typical LED on 5 V. 10k is a common pull-up or pull-down value. Check with a multimeter if unsure.

Change it and keep it

Open it in the editor, change the circuit or the code, and keep your version in a free account.