Built-in project · Arduino Uno R3

Motion night light (PIR on 12, RGB on 5, 6 and 9), on an Arduino Uno

An HC-SR501 on pin 12 turns on an RGB LED (red 5, green 6, blue 9) when someone moves, keeps it on for 20 seconds, and checks again before it goes dark. 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 $34. On Mokxi they're free.

Motion night light (PIR on 12, RGB on 5, 6 and 9) · 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

  • Reading a PIR motion sensor
  • Mixing color with an RGB LED and PWM
  • Timing without delay() using millis()
  • Remembering state with a boolean
  • Keeping a light on while motion continues
  • Difficulty: Beginner
  • Time: About 20 minutes
  • Board: Arduino Uno R3
  • Parts: 7

How it works

The HC-SR501 senses moving warm bodies and sets its output on pin 12 HIGH. The sketch then turns the RGB LED a warm white by writing PWM values to pins 5, 6 and 9: red 255, green 180, blue 90.

It then keeps the light on for 20 seconds. After that it checks the sensor again: if something is still moving, it stays on for another 20 seconds. If not, the light turns off.

Parts list

7 parts, plus the jumper wires. They retail for about $34; on Mokxi they're free.

Kits that can build it

These 3 starter kits have every part in the circuit. Each kit's page lists what else it can build.

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
Motion sensor pin OUTArduino Uno R3 pin 12
Resistor, 220 Ω (1) pin 1Arduino Uno R3 pin 9
Resistor, 220 Ω (2) pin 1Arduino Uno R3 pin 6
Resistor, 220 Ω (3) pin 1Arduino Uno R3 pin 5
Motion sensor pin VCCArduino Uno R3 pin 5V
RGB LED pin CArduino Uno R3 pin GND
Motion sensor pin GNDArduino Uno R3 pin GND
Resistor, 220 Ω (3) pin 2RGB LED pin R
Resistor, 220 Ω (2) pin 2RGB LED pin G
Resistor, 220 Ω (1) pin 2RGB LED pin B

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
// A night light that comes on when someone walks past.
//
//   HC-SR501 OUT -> pin 12   VCC -> 5V   GND -> GND
//   RGB LED red -> pin 5, green -> pin 6, blue -> pin 9, each through 220 ohm
//   the RGB LED's common cathode -> GND
//
// The HC-SR501 sees the warmth of a moving body. Its output goes HIGH when it
// sees movement and stays high for a few seconds after. The sketch lights the
// LED a warm white, waits 20 seconds, and only turns it off if nothing has
// moved in that time.
//
// Click the sensor's dome to walk past it.

const int PIR = 12;
const int RED_PIN = 5;
const int GREEN_PIN = 6;
const int BLUE_PIN = 9;
const unsigned long ON_MS = 20000;

bool lit = false;
unsigned long litAt = 0;

void color(int r, int g, int b) {
  analogWrite(RED_PIN, r);
  analogWrite(GREEN_PIN, g);
  analogWrite(BLUE_PIN, b);
}

void setup() {
  pinMode(PIR, INPUT);
  pinMode(RED_PIN, OUTPUT);
  pinMode(GREEN_PIN, OUTPUT);
  pinMode(BLUE_PIN, OUTPUT);
  color(0, 0, 0);
  Serial.begin(9600);
  Serial.println("Mokxi Uno: motion night light");
}

void loop() {
  bool moving = digitalRead(PIR) == HIGH;
  if (moving && !lit) {
    lit = true;
    litAt = millis();
    color(255, 180, 90);
    Serial.println("someone moved: light on");
  } else if (lit && millis() - litAt >= ON_MS) {
    if (moving) {
      litAt = millis();
      Serial.println("still moving: staying on");
    } else {
      lit = false;
      color(0, 0, 0);
      Serial.println("nobody there: light off");
    }
  }
  delay(50);
}

Code walkthrough

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

color sketch.ino

Sets the RGB LED color by writing three PWM values to the red, green and blue pins. Every color change in the sketch goes through this one function.

void color(int r, int g, int b) {
  analogWrite(RED_PIN, r);
  analogWrite(GREEN_PIN, g);
  analogWrite(BLUE_PIN, b);
}

setup sketch.ino

Sets the PIR as an input and the three color pins as outputs. It starts with color(0, 0, 0), so the light is off, then opens the serial port.

void setup() {
  pinMode(PIR, INPUT);
  pinMode(RED_PIN, OUTPUT);
  pinMode(GREEN_PIN, OUTPUT);
  pinMode(BLUE_PIN, OUTPUT);
  color(0, 0, 0);
  Serial.begin(9600);
  Serial.println("Mokxi Uno: motion night light");
}

loop sketch.ino

If there is motion and the light is off, it turns the light on, saves the time in litAt and prints a message. Once ON_MS (20 seconds) has passed, it either restarts the timer if motion continues or turns the light off.

void loop() {
  bool moving = digitalRead(PIR) == HIGH;
  if (moving && !lit) {
    lit = true;
    litAt = millis();
    color(255, 180, 90);
    Serial.println("someone moved: light on");
  } else if (lit && millis() - litAt >= ON_MS) {
    if (moving) {
      litAt = millis();
      Serial.println("still moving: staying on");
    } else {
      lit = false;
      color(0, 0, 0);
      Serial.println("nobody there: light off");
    }
  }
  delay(50);
}

Troubleshooting

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

  • The light never comes on This project
    Check the PIR's OUT goes to pin 12 and it has 5V and ground. In the simulator, click the sensor's dome.
  • The color is wrong This project
    Match each LED leg to its pin: red to 5, green to 6, blue to 9, each through a 220 ohm resistor.
  • The colors are wrong or one color never lights. RGB LED
    Check whether your LED is common cathode (shared leg to ground) or common anode (shared leg to 5 V). Common anode works backwards: LOW turns a color on.
  • One color is much brighter than the others, or the LED runs too hot. RGB LED
    Use one resistor per color leg, never one for the shared leg. Typical values are 220 ohm for red and 100 to 150 ohm for green and blue on 5 V.
  • 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.
  • A pull-up or pull-down resistor is missing, so the input floats and reads random values. Resistor
    Tie the input to 5 V or ground through about 10k. Or use INPUT_PULLUP and wire the switch to 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.

Change it and keep it

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