Night light

The micro:bit measures the light in the room and switches its own lights on when it gets dark.

Go to the code ↓

Lesson at a glance

45 to 60 minutes · Grades 5 to 7

maze.education/microbit/lessons/night-light

Grades
Grades 5 to 7
Time
45 to 60 minutes
The CAPS gives Coding and Robotics 2 hours a week (Coding and Robotics CAPS, Grades 4 to 6, page 22; Grades 7 to 9, page 22).
Level
Some experience
For classes that have used the micro:bit and MakeCode once or twice.
Board
Any micro:bit
Code with
MakeCode and Purple Mash blocks, with Python as an extension
CAPS
Coding and Robotics, Natural Sciences and Technology, Natural Sciences
Aligned: Grade 6, Term 1 · Where it fits

Learning intentions

Learners will learn to:

  1. Read a value from the light sensor and record it.
  2. Choose a threshold from measurements, and use it in an if, else decision.
  3. Explain how a sensor, a decision and an output make an automatic system.

Success criteria

Each learner can say:

  • I can read the light level and record it in normal light, under a cupped hand and inside a box.
  • I can choose a threshold between my dark and light readings and use it in an if, else decision.
  • I can explain how the sensor, the decision and the output make our night light work by itself.
  • Grade 7: I can trace the light level and the display through five passes of the loop.

Competencies

From the Coding and Robotics CAPS places this lesson cites.

  • R.5Design a simple artefact based on a set of design instructions
  • D.3Demonstrate an understanding of the concept of a computing device

Key words

sensor
A part that measures something, such as light or temperature.
light level
The micro:bit's measure of light, from 0 (dark) to 255 (very bright).
threshold
The value where a decision changes, such as dark below 50.
condition
A question with a yes or no answer, such as: is the light level below 50?
automatic
Working by itself, without anyone pressing a switch.
flowchart
A diagram of the steps and decisions in a process, read from the top.
trace table
A table that follows the value of each variable, step by step, as a program runs.

What you need

  • One micro:bit per pair, with a USB cable and a battery holder
  • A computer with Chrome or Edge for each pair
  • Something to make it dark: a cardboard box, a book or a cupped hand

From our shop: BBC micro:bit V2 Go, BBC micro:bit V2.21 Next Gen Classroom Pack

Safety

  • Use the battery holder with two AAA batteries. Never mix old and new batteries, and take the batteries out before the kits are packed away.
  • Unplug the battery holder by holding its plug, never by pulling the wires.
  • Keep the micro:bit dry, and hold it by its edges rather than the parts on the back.

The lesson plan

One lesson of 45 to 60 minutes in three parts: introduction, lesson development and consolidation. The CAPS gives Coding and Robotics 2 hours a week, so it fits one of the week's lessons. Times are a guide.

  1. Introduction

    5 to 10 minutes
    Retrieval practice

    What is a sensor, and what does it give the computer? (A part that measures something, such as light or temperature, and gives the computer a reading as a number.)

    Problem statement

    At some schools the security lights burn all day because nobody remembers to switch them off, and the school pays for every unit of electricity. Program a light that measures the room and switches itself on only when it gets dark.

    1. Ask: what switches on by itself when it gets dark? Street lights, garden solar lights, a phone screen getting dimmer. Explain that the micro:bit can sense light with its own LEDs, and gives a number from 0 (dark) to 255 (very bright).
    2. Read the problem statement together. Ask: how dark is dark? How would a light know when to switch on?
  2. Lesson development

    35 to 40 minutes
    1. Unplugged first. Play light or dark: one learner is the sensor and holds up a number from 0 to 255 for how bright the room is. The class is the program: if the number is below 50, everyone says lights on, otherwise lights off. Change the number and see the decision change.
    2. Plan on paper first. Pairs fill in an IPO table for the night light and draw it as a flowchart, leaving a gap in the decision for their own threshold. Grade 7 learners add a trace table for five passes of the loop.
    3. Predict. Ask: what light level does the classroom have now? Under a cupped hand? Inside a box? Pairs write three guesses beside the plan.
    4. Build in blocks. Pairs share one computer. The driver first builds a measuring program from the Blocks tab: forever with show number and light level (from Input). The pair downloads it, records the light level in normal light, under a cupped hand and inside a box, and writes a threshold between the dark and the light readings into the flowchart. Swap roles halfway, once the threshold is chosen: the new driver builds the night light from the steps, with the pair's own threshold instead of 50, and the navigator checks each block against the flowchart.
    5. Test and debug. Test it in the simulator with the light control on the board, then download it and test it in the box. How close were your guesses to the readings? If the light flickers or never comes on, compare your readings with the threshold and adjust it.
    6. Discuss: where would an automatic light like this save energy at school or at home?

    Plan on paper: a model answer

    In pairs, fill in an IPO table and draw the night light as a flowchart, with your own threshold in the decision. Grade 7 learners also trace five passes of the loop.

    IPO table
    InputProcessingOutput
    The light level from the micro:bit's light sensor, 0 to 255Is the light level below the threshold (50)? Ask again and again, foreverYes: the glow lights up. No: the display clears
    Flowchart
    1. Start
    2. Read the light level
    3. Is the light level below 50?Yes: show the glow on the lightsNo: clear the screen
    4. Go back and read the light level again, forever
    Trace table
    Grade 7: five passes of the forever loop as a hand covers the board and moves away again. The threshold is 50.
    Passlight levelBelow 50?The display shows
    1120NoNothing (cleared)
    235YesThe glow
    312YesThe glow
    450NoNothing (cleared)
    590NoNothing (cleared)
  3. Consolidation

    5 to 10 minutes
    1. Ask: what is the input, the decision and the output in our night light? Point to each one on the flowchart.
    2. Pairs share their thresholds. Why are they different? What would go wrong if the threshold were 200?
    3. Back to the problem: would your light stay off on a bright afternoon and come on when it gets dark? Where at school would it save the most electricity? Learners check themselves against the success criteria.

Build it, step by step

Pairs build the program in blocks, following the numbered steps, with the same program as text beside them to check against or paste. Test in the simulator before sending it to a board.

A light level scale from 0 to 255 with the threshold at 50: below 50 the board shows a rounded block of lights, and at 50 or above it clears the screen.
The sensor, a decision (is the light level below 50?) and an output make an automatic night light. Choose your own threshold from measurements.

These are the MakeCode steps. The other routes are on the lesson's web page.

Build it in MakeCode blocks

  1. Start a New Project called Night light.
  2. From Logic, drag an if then else block into forever.
  3. From Logic, put a compare block (0 < 0) into the if.
  4. From Input, drag light level into the left side and type 50, or your own threshold, on the right.
  5. From Basic, put show leds in the then part and click squares to draw a glow.
  6. From Basic, put clear screen in the else part.
  7. Test in the simulator: change the light level with the light control that appears on the simulated board. Then Download.
The same blocks as text (to paste into MakeCode)
basic.forever(function () {
    if (input.lightLevel() < 50) {
        basic.showLeds(`
            . # # # .
            # # # # #
            # # # # #
            # # # # #
            . # # # .
            `)
    } else {
        basic.clearScreen()
    }
})

To use the text instead: open MakeCode, start a new project, click JavaScript at the top, replace what is there with this code, then click Blocks to see it as blocks.

To send it to the board, click Download. In Chrome or Edge, MakeCode can connect to the micro:bit by USB so the program goes straight onto it. Otherwise a .hex file downloads: drag it onto the MICROBIT drive.

Extension or homework

Challenges for learners who finish early, or to take further in a club or as homework.

  1. Challenge 1

    Make the light fade: use plot x y brightness so the lights get brighter as the room gets darker.

  2. Challenge 2

    Find the light level in different places at school, such as a window seat, the corridor and a storeroom, and record them in a table.

  3. Challenge 3

    Build a sunrise alarm: when the light level rises above a level you choose, play a tune on a micro:bit V2.

Support and extension

Support

For learners who need more help

  • Give learners the threshold of 50 to start with, and let them adjust it later.
  • Do the measuring as a whole class on the board, with one micro:bit connected.

Extension

For learners who are ready for more

  • Add a middle setting: dim lights when it is getting dark, bright when it is very dark, using else if.
  • Use plot bar graph of from Led to show the light level as a bar instead.

Informal assessment

Ask these as learners work. Informal assessment happens every day and is not recorded.

  1. What range of numbers does the light sensor give?
  2. How did you choose your threshold?
  3. The threshold is 50 and the light level is exactly 50. Does the glow show? Why?
Exit question

Name the input, the decision and the output of a street light.

Observation checklist

Watch pairs as they plan, build and test. Write each pair's initials under the level you see.

Watch forBeginningDevelopingAccomplishedExemplary
Records the light level in three conditions before choosing a threshold.
Draws the flowchart with a decision that has a yes and a no.
Predicts the light levels, then compares them with the readings.
Builds the if, else decision with the pair's own threshold.
Explains how the sensor, the decision and the output work together.
Beginning:
not yet, even with help
Developing:
with help
Accomplished:
on their own
Exemplary:
on their own, and explains it or takes it further

The four levels are the CAPS rubric levels (Coding and Robotics CAPS, Grades 4 to 6, page 176; Grades 7 to 9, page 163). The short descriptions are ours.

This lesson is not a formal task, but it is good practice for the term's practical task in Grades 4 to 6 (Coding and Robotics CAPS, Grades 4 to 6, page 170), and for the mini-PATs in Grades 7 to 9 (Coding and Robotics CAPS, Grades 7 to 9, page 157).

Common mistakes and fixes

  • Problem: The light flickers on and off.Fix: The room is right at the threshold. Choose a lower number, or cover the board completely.
  • Problem: The light never comes on.Fix: Your threshold is lower than any reading in your room. Measure again and choose a number between the dark and light readings.
  • Problem: The first reading is 0.Fix: The sensor needs a moment to start. Ignore the first reading or wait a second.

Where it sits in CAPS

  • Coding and RoboticsAlignedGrade 6, Term 1: use the micro:bit's light sensor to control a light.Source: Coding and Robotics CAPS, Grades 4 to 6 (opens in a new tab), Grade 6, Term 1, Robotics, R.5 Design a simple artefact based on a set of design instructions, page 133: Use light sensor on microcontroller to control and LED
  • Coding and RoboticsAlignedGrade 6, Term 1: the light comes on when the room is dark and goes off when it is light, with IF, THEN and ELSE.Source: Coding and Robotics CAPS, Grades 4 to 6 (opens in a new tab), Grade 6, Term 1, Robotics, R.5 Design a simple artefact based on a set of design instructions, page 133: Show how the LED turns on when the room is dark and off when the room is light.
  • Coding and RoboticsAlignedGrade 6, Term 1: sensors are inputs that the micro:bit processes into an output.Source: Coding and Robotics CAPS, Grades 4 to 6 (opens in a new tab), Grade 6, Term 1, Digital Concepts, D.3 Demonstrate an understanding of the concept of a computing device, page 135: The microcontroller has built-in sensors and buttons that serve as sources of input
  • Natural Sciences and TechnologyFits Natural Sciences and Technology, Energy and Change: using energy only when it is needed.

Coding and Robotics quotes come from the Department of Basic Education's final CAPS, with the page number of the 2024 printed edition (the Department's October 2025 library copies number their pages differently). Schools may teach a topic in another term: the CAPS leaves the order to each school's annual teaching plan.

Ready to teach it?

Get a micro:bit for every learner or pair, and let us train your staff to run lessons like this one.

More lessons

All lessons →
  • Grades 5 to 7

    Grades 5 to 745 to 60 minLevel: Getting started

    Thermometer

    Learners turn the micro:bit into a thermometer, compare it with the weather report and work out the difference.

    Subjects: Mathematics · Natural Sciences and Technology · Natural Sciences · Coding and Robotics

    Code with: MakeCodePurple MashPython

  • Grades 4 to 7

    Grades 4 to 745 to 60 minLevel: Getting started

    Step counter

    Every step adds one: learners use a variable to count steps, show the total and reset it, then test how accurate it is.

    Subjects: Coding and Robotics · Mathematics · Life Skills · Life Orientation

    Code with: MakeCodePurple MashPython

  • Grades 4 to 7

    Grades 4 to 745 to 60 minLevel: Some experience

    Music maker

    Buttons play notes, A and B together play a scale and a shake starts a tune: a pocket instrument learners compose for.

    Subjects: Creative Arts · Coding and Robotics

    Code with: MakeCodePurple MashPython

micro:bit is a trade mark of the Micro:bit Educational Foundation, and BBC is a trade mark of the British Broadcasting Corporation. Maze Education is an independent reseller of genuine BBC micro:bit products. Our teacher training and our other products and services are our own, and are not endorsed by the Foundation or the BBC.

WhatsApp