Skip to content

Motion Night Light

You'll write the rules for a lamp that comes on only when it's dark and someone moves, holds for a minute, and then install it where it earns its keep.

About

Two sensors and a few lines of logic make something people actually want. A PIR motion module costs a dollar, a light sensor costs less, and the code is three conditions and a timer. The hard part is the timer. You want the light to stay on for a minute after the last movement and not flicker when the PIR drops out, and delay() can't do that, because the board is deaf while it waits. You'll use millis() instead, which is the habit that makes every later badge possible.

At a glance

Per requirement
15 XP
Completion bonus
75 XP
Requirements
5
Takes
One evening, then a week in place
Skill level
Beginner (done it a few times)
Cost
Cheap (under $10)
Supplies
You need some
Where
At home
Other people
On your own

Requirements

  1. 1
    Read a PIR module and print when it sees movement. The HC-SR501 has two small pots, one for sensitivity and one for how long the output stays high; turn the second all the way down so your code owns the timing. It takes about a minute to settle after power-up, so don't panic. Take a screenshot of the serial monitor with the motion events. Photo
  2. 2
    Add a light sensor and set a dark threshold by measuring, not guessing. Print the LDR divider reading in daylight, at dusk and at night, and pick a number between dusk and night. Take a photo of your three readings written down with the threshold circled. Photo
  3. 3
    Write the rule with millis() and no delay(). On movement in the dark, note the time and turn the light on; turn it off when the time since the last movement passes 60 seconds. Movement during the countdown restarts it. Film the light coming on, staying on while you move, and going off after a minute (speed the video up if you like). Video
  4. 4
    Fade the light on and off instead of snapping it. Use PWM on the LED pin and ramp over about half a second. A snap at 3 a.m. is unpleasant and a fade isn't. Film the fade. Video
  5. 5
    Install it for a week and report the one annoying thing. A hallway, the stairs, under the bed or the bathroom at night all count. Use a phone charger or a 4xAA pack. After a week, say what annoyed you (too sensitive, too dim, the cat sets it off) and what you changed. Take a photo of it in place, on, at night. Photo