One-sentence summary
In this project we will build a small night light that turns itself on when the room gets dark and off again when it becomes bright, using a single light sensor.
Why it matters
So far we have learned to read sensors, drive motors and calibrate a sensor, each on its own. A real project asks us to combine these pieces into one flow: read, decide, act.
An automatic night light is a great first project for this. Its input is a single sensor, its output is a single LED, and its logic is a single condition. It is not complicated, yet it contains, at small scale, every problem you will meet in the real world: choosing a threshold, preventing flicker, testing, and finding and fixing a bug.
The same idea is all around us in daily life:
- Street lamps that switch on by themselves at dusk work with a light sensor.
- Some phones adjust their screen brightness automatically based on the surrounding light.
By the end of this project, you will have built the logic behind both of those examples yourself.
Getting to know the project
What will it do?
Our rule is one sentence: If the room is darker than a certain level, turn the LED on; if it is bright, turn it off.
To do this we need three things:
- A light sensor that measures how bright the room is.
- A threshold: the boundary number between "dark" and "bright."
- Decision logic that turns the LED on or off based on that threshold.
Materials list
- A micro:bit or Arduino board (a board with a built-in light sensor makes this easier)
- A light sensor: on the micro:bit the LED screen works as a sensor; on Arduino you use an LDR (a light-dependent resistor)
- 1 LED
- 1 resistor, 220–330 ohm (to protect the LED)
- A breadboard and a few jumper wires
- Power source: USB or a battery pack (low voltage only)
Circuit diagram (in text)
If we use the micro:bit's built-in light sensor, the sensor needs no extra wiring. The LED side is thought of like this:
[Board pin P0] ---> [220–330 ohm resistor] ---> [LED (+) long leg]
|
[LED (−) short leg]
|
[GND / ground]
The resistor limits the current through the LED; without it the LED can be damaged over time. The LED's long leg is the plus (+) side, the short leg is the minus (−) side.
Step-by-step logic
1. Read the sensor
The light sensor gives us a number. On the micro:bit this number is usually between 0 (very dark) and 255 (very bright). First we want to read this number and see it on the screen, because we can only choose a threshold once we can see the real values.
Start
repeat forever:
light = read the light sensor
print light to the screen/serial
2. Calibrate the threshold
In the previous lesson we learned calibration: measuring a sensor's real values in the actual environment and setting a boundary from them. That is exactly what we do here.
- Light up the room and note the reading (for example 200).
- Cover the sensor with your hand and note the dark value (for example 40).
- Choose the threshold near the middle of those two. For example threshold = 100.
The threshold is specific to your room. In another room the light is different, so you may need to measure the threshold again.
3. Decide and drive the LED
Now we can write the rule. As pseudocode:
threshold = 100
repeat forever:
light = read the light sensor
if light < threshold:
turn the LED on # room is dark
else:
turn the LED off # room is bright
In micro:bit-style code the same logic looks like this:
threshold = 100
while True:
light = display.read_light_level()
if light < threshold:
pin0.write_digital(1) # LED on
else:
pin0.write_digital(0) # LED off
sleep(100)
The form of the code may change, but the logic is always the same: read, compare with the threshold, decide.
Preventing flicker
Why does the problem happen?
When the light is right around the threshold, the sensor may read 99 one moment and 101 the next. Then the LED switches on and off very fast; this is called flicker. You can see it at dawn, or when you slowly cover the sensor with your hand.
Two simple solutions
1. Averaging (smoothing): Instead of a single reading, take the average of several readings. Sudden jumps are smoothed out this way.
total = 0
repeat 5 times:
total = total + read the light sensor
light = total / 5
2. Using two thresholds (hysteresis): Set one boundary for turning on and a slightly different boundary for turning off. Then, even if the sensor wobbles around a single number, the LED stays steady in its decision.
low = 90 # turn on when it drops below this
high = 110 # turn off when it rises above this
These two methods are real engineering solutions that real night lights use too.
Mini practice
Build your own night light and try these test scenarios in order. For each scenario, write down on paper what the LED does.
| Test | What you do | Expected result |
|---|---|---|
| 1 | Room is bright | LED off |
| 2 | Cover the sensor with your hand | LED turns on |
| 3 | Pull your hand away slowly | LED fades off softly, no flicker |
| 4 | Lower the threshold from 100 to 60 | LED turns on later |
Then change just one thing and test again: for example the threshold, or the number of readings you average. Changing one thing at a time lets you see which change affected what.
A bug and its fix
In our first attempt a very common bug appears: the LED works exactly backwards. That is, it turns on when the room is bright and off when it is dark.
Goal: LED should turn on in the dark
Problem: LED turns on in the light
Check: The condition is written "if light > threshold turn on"
The bug here is in the direction of the comparison: we wrote "turn on if greater," but if the light value is large, the room is bright. The fix is to flip the sign:
Fix: Change the condition to "if light < threshold turn on"
The lesson: if a sensor does the opposite of what you expect, the problem is often not in the wiring but in the direction of the comparison.
Common mistakes
Forgetting the resistor
Connecting the LED directly to the pin can damage it over time. Always place a 220–330 ohm resistor between the LED and the pin.
Choosing the threshold by guessing
Instead of saying "let's make it 100," first measure the sensor's bright and dark values, then choose the threshold in between. A threshold chosen without measuring will not work in another room.
Ignoring flicker
If the LED flickers quickly in borderline light, that is not a fault; averaging or two thresholds solves it easily.
Wiring the LED backwards
The LED's direction matters. The long leg goes to plus, the short leg to ground. If it is reversed, the LED will not light at all.
Safety note
- This project works with low voltage only: USB, micro:bit or a battery pack. It is never connected to a wall socket or mains electricity.
- An LED is a small component, but it can heat up if you skip the resistor. Always use the resistor.
- Cut the board's power while building the circuit or plugging and unplugging wires. Give power only after checking the connections.
- If a battery heats up, swells or smells, remove it right away and tell an adult.
- This project should be carried out with an adult present.
Lesson summary
- An automatic night light is the simplest example of the "read, decide, act" logic.
- The light sensor gives a number; you compare that number with a threshold and drive the LED.
- The threshold is calibrated by measuring; it is chosen from real values, not by guessing.
- Flicker is prevented with averaging or with two thresholds (hysteresis).
- Working safely requires low voltage, an LED resistor and adult supervision.
Check questions
- In this project, what is the sensor's job and what is the LED's job?
- Why do we set the threshold by measuring instead of guessing it directly?
- If the LED flickers quickly in borderline light, what is this called and what are two solutions?
- If the LED turns on when the room is bright and off when it is dark, where do you look for the bug?
- Why do we place a resistor between the LED and the pin?
Answers
- The sensor measures how bright the room is and gives a number; the LED then turns on and off based on that number, showing the project's output.
- Light is different in every room, so a guessed threshold can be wrong. Measuring the bright and dark values and choosing between them makes the threshold fit the real environment.
- It is called flicker. Solutions: take the average of several readings, or use two different thresholds for turning on and off (hysteresis).
- The problem is most likely in the direction of the comparison. It should read "light < threshold" instead of "light > threshold"; if the light value is large, the room is bright.
- The resistor limits the current through the LED. Without it, too much current can damage the LED.
Source and verification note
For “Project: Automatic Night Light”, verification focuses on whether the relationship between Getting to know the project and Materials list remains consistent across examples. Sensor readings can change with the model, supply voltage and environment. Thresholds in the lessons are therefore examples; a real project should use a measurement table and calibration.
Next lesson
Project: Distance Warning System