Skip to content

Python nightlight

Lesson 5 of 6

Python

9-12 yrs, 11-14 yrs

Make an automatic nightlight and learn about sensors and logic.

Key learning:

  • I can program a micro:bit using text-based code to make a light that switches on when it gets dark.  
  • I can explain that sensors are inputs that sense things in the real world such as movement and light. 
  • I can explain that logic is how computers make decisions in code based on whether things are true or false.

Computer systems:

Input/output

Sensors

Programming:

Selection

Text-based programming

Lesson plan preview

Slides preview

Student handouts

Completed lesson code

This provides an example of the code created during this lesson.

1from microbit import *
2
3while True:
4    if display.read_light_level() < 100:
5        display.show(Image(
6        "99999:"
7        "99999:"
8        "99999:"
9        "99999:"
10        "99999"))
11    else:
12        display.clear()
13    sleep(2000)

You may also be interested in

Introduces our Python Editor that helps learners to write code for the BBC micro:bit. We will highlight all the great features in our editor that support you and your learners to code the micro:bit successfully using Python.

A sequence of lessons that provide a pathway through six projects, ideal for getting started with the micro:bit. Students develop their use of some core computing concepts by coding and making practical projects including step counters, nightlights, and games.

Seven lessons introducing AI in general and unpacking what students already know about sorting data, before getting hands-on, creating, testing and improving their own machine learning models to be used as new inputs in code on the micro:bit.