MS-007 · Electronics
Smart Motion-Activated Nightlight
Overview
A nightlight that turns itself on when someone comes near, at a brightness the user picks, and that can be switched out of the way entirely with one button. Built with Dana Sapir as the final project for Programming for Electrical Engineers, on an Arduino Uno and a breadboard with parts out of the starter kit.
An HC-SR04 ultrasonic sensor measures the distance in front of it on a loop. Come within 30 cm and the blue channel of an RGB LED lights up. A 10 kΩ potentiometer sets how bright it gets, mapped straight onto PWM, so the light can be tuned from a faint glow to full output while it is running. A momentary pushbutton acts as a master switch, latching the whole system on and off so the sensor is ignored completely when the light is not wanted.
Dana and I walking through the finished build
How it works
The sketch reads the potentiometer every pass and scales it from the 10-bit analog range onto the 8-bit PWM range, so the brightness setting is always current no matter what the rest of the loop is doing. The button is read the same way every pass, but what the code looks for is the transition from released to pressed rather than the level itself. That is what turns a momentary button into a latch.
With the system off, the LED is written to zero and the loop returns early, so the sensor is never even polled. With it on, the sensor fires a 10 microsecond trigger pulse and times the echo, and the distance follows from the speed of sound. Anything inside 30 cm lights the LED at the current brightness, anything outside it turns the LED back off.
What went wrong first
The button started life as a plain momentary switch. The light only stayed on while it was physically held down, which is useless for a nightlight. Because INPUT_PULLUP inverts the logic, released reads HIGH and pressed reads LOW, and reading the pin on its own can never produce an on and off latch. Storing the previous state and acting only on the change, with a short debounce, is what made a single press toggle the system.
The sensor was the next problem. The HC-SR04 has blind spots and it returned nonsense at both ends of its range, which fired the LED when nothing was there. Discarding zero and negative readings, capping the activation range at 30 cm, and putting a short delay in the loop made the detection consistent.
Right at the threshold the LED would flicker, because tiny changes in distance flipped the comparison back and forth. Smoothing the readings and simplifying the logic around the threshold took the jitter out.
The last one was purely mechanical. Four components on one breadboard made it easy to misplace a wire, especially between the sensor pins and the PWM output. We rewired the whole thing to a color code, red for 5 V, black for ground, green for trigger, yellow for echo, orange for the LED output and blue for the analog read. The circuit worked immediately afterwards, which says more about documenting wiring early than about the wiring itself.
What I took away
Every real problem on this build was a state problem rather than a wiring problem. The button, the noisy sensor and the flicker at the threshold were all the same lesson in different clothes, which is that hardware gives you a stream of messy values and the code has to decide what counts as an event.