Skip to content

Light Sensor (built-in)

Level Beginner
Board V1 & V2
Parts micro:bit

What you'll build

The LED matrix can also sense light. Read the ambient light level and scroll Day or Night.

Code

6 light-level.py
1
2
3
4
5
6
7
8
from microbit import *

while True:
    if display.read_light_level() < 150:
        display.scroll('Night')
    else:
        display.scroll('Day')
    sleep(2000)

How it works

display.read_light_level() returns a value from 0 (dark) to 255 (bright). Below 150 the program decides it is night.

Try this next

  • Scroll the raw light level to find a better threshold for your room.
  • Turn the display into a night-light that glows when it gets dark.
  • Log light levels over a day with Data Logging.