Home · Academy · Robotics & Coding · Sensors and Actuators · Project: Automatic Night Light

Project: Automatic Night Light

Build a night light that turns on in the dark using a light sensor, threshold calibration and filtering.

PROJECT COMPASS

What will you use this page for?

Core idea

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.

Evidence to produce

Complete the page task with your own input, test conditions and reasoning.

Control trap

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…

Next connection

Project: Distance Warning System

Module sources: Python Tutorial · Arduino Learn

LevelBeginner
Age10–16
Duration45–75 min
PrerequisiteCalibration
ContentProject guide · 1,604 words
Last updated

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:

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:

  1. A light sensor that measures how bright the room is.
  2. A threshold: the boundary number between "dark" and "bright."
  3. Decision logic that turns the LED on or off based on that threshold.

Materials list

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.

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.

Mini practice table
TestWhat you doExpected result
1Room is brightLED off
2Cover the sensor with your handLED turns on
3Pull your hand away slowlyLED fades off softly, no flicker
4Lower the threshold from 100 to 60LED 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

Lesson summary

Check questions

  1. In this project, what is the sensor's job and what is the LED's job?
  2. Why do we set the threshold by measuring instead of guessing it directly?
  3. If the LED flickers quickly in borderline light, what is this called and what are two solutions?
  4. If the LED turns on when the room is bright and off when it is dark, where do you look for the bug?
  5. Why do we place a resistor between the LED and the pin?

Answers

  1. 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.
  2. 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.
  3. It is called flicker. Solutions: take the average of several readings, or use two different thresholds for turning on and off (hysteresis).
  4. 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.
  5. 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

Start QuizBack to Sensors and Actuators
QUESTION POOL

Reinforce this lesson with 10 questions

This lesson has a pool of 20 questions. Each attempt selects 10 and reshuffles the choices.