Lesson at a glance
maze.education/microbit/lessons/night-light
- Grades
- Grades 5 to 7
- Time
- 45 to 60 minutesThe 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 experienceFor 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 SciencesAligned: Grade 6, Term 1 · Where it fits
Learning intentions
Learners will learn to:
- Read a value from the light sensor and record it.
- Choose a threshold from measurements, and use it in an if, else decision.
- 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.
Introduction
5 to 10 minutesRetrieval practiceWhat 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 statementAt 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.
- 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).
- Read the problem statement together. Ask: how dark is dark? How would a light know when to switch on?
Lesson development
35 to 40 minutes- 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.
- 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.
- Predict. Ask: what light level does the classroom have now? Under a cupped hand? Inside a box? Pairs write three guesses beside the plan.
- 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.
- 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.
- 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
Input Processing Output The light level from the micro:bit's light sensor, 0 to 255 Is the light level below the threshold (50)? Ask again and again, forever Yes: the glow lights up. No: the display clears Flowchart
- Start
- Read the light level
- Is the light level below 50?Yes: show the glow on the lightsNo: clear the screen
- 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. Pass light level Below 50? The display shows 1 120 No Nothing (cleared) 2 35 Yes The glow 3 12 Yes The glow 4 50 No Nothing (cleared) 5 90 No Nothing (cleared) Consolidation
5 to 10 minutes- Ask: what is the input, the decision and the output in our night light? Point to each one on the flowchart.
- Pairs share their thresholds. Why are they different? What would go wrong if the threshold were 200?
- 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.
These are the MakeCode steps. The other routes are on the lesson's web page.
Build it in MakeCode blocks
- Start a New Project called Night light.
- From Logic, drag an if then else block into forever.
- From Logic, put a compare block (0 < 0) into the if.
- From Input, drag light level into the left side and type 50, or your own threshold, on the right.
- From Basic, put show leds in the then part and click squares to draw a glow.
- From Basic, put clear screen in the else part.
- Test in the simulator: change the light level with the light control that appears on the simulated board. Then Download.
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.
Purple Mash follows the UK school years, which run about one grade ahead of ours: Year 3 is roughly Grade 2 here, Year 4 roughly Grade 3 and Year 5 roughly Grade 4. 2Simple says its micro:bit lessons can be adapted for other ages, so older learners who are new to the micro:bit can use them too.
These steps are our own. 2Simple's lesson has its own plan, slides and videos, which teachers with a Purple Mash login can use alongside this one.
Our steps in Free Code micro:bit
Block names in Free Code micro:bit are not the same as MakeCode's, so these steps say what each block does. Check the exact names in Purple Mash.
- Log in to Purple Mash and open the lesson from the Computing Scheme of Work, or the micro:bit projects in the Computing area. They use Free Code micro:bit, 2Simple's block coding tool for the micro:bit (2Simple's help pages also call it 2Code micro:bit).
- Open Free Code micro:bit from the Night Light lesson or project.
- In a loop that repeats forever, check the light level with an if block.
- If it is below your threshold, show a picture. Otherwise, clear the display.
- Check in Purple Mash what range its light level uses, since your threshold depends on it.
- Test the program on the on-screen micro:bit (2Simple calls it the micro:bit emulator) before you send it to a board.
- Save your work, open the purple menu at the top and choose Download.Hex. Plug in the micro:bit with a USB cable that carries data and drag the .hex file onto the MICROBIT drive. On an iPad, open the file in the micro:bit app and tap Send to micro:bit.
2Simple says the micro:bit projects can also be coded in Python in Pieces, if your school has it.
Extension: text-based coding, beyond the CAPS
The same program in MicroPython, for the micro:bit Python Editor. It suits Grade 7 to 9 learners who are ready for text-based code. The CAPS itself follows a block-based approach (Coding and Robotics CAPS, Grades 7 to 9, page 24).
# Night light: the lights come on when the room gets dark.
from microbit import *
glow = Image("09990:99999:99999:99999:09990")
while True:
if display.read_light_level() < 50:
display.show(glow)
else:
display.clear()
sleep(500)Open the Python editor, replace the code that is there with this program (or open the downloaded file), then click Send to micro:bit. MakeCode also has a Python view, which is written a little differently.
Extension or homework
Challenges for learners who finish early, or to take further in a club or as homework.
- Challenge 1
Make the light fade: use plot x y brightness so the lights get brighter as the room gets darker.
- 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.
- 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.
- What range of numbers does the light sensor give?
- How did you choose your threshold?
- The threshold is 50 and the light level is exactly 50. Does the glow show? Why?
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 for | Beginning | Developing | Accomplished | Exemplary |
|---|---|---|---|---|
| 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
- The light flickers on and off.The room is right at the threshold. Choose a lower number, or cover the board completely.
- The light never comes on.Your threshold is lower than any reading in your room. Measure again and choose a number between the dark and light readings.
- The first reading is 0.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, 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, 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, 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.
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.

