Homeschooling the micro:bit: Sunlight Sensor

23/09/2022
micro:bit
Step 1: Make

working principle

In addition to being an output, the LEDs on the micro:bit can also act as light sensors for the input device, measuring the amount of light falling on them.

This means that the micro:bit program can make different things happen depending on the level of light and dark.

The program uses an 'if...other' statement to display the sun image only if the light level is greater than (>) by a certain degree. This is called selection - choosing when something different happens.

Flash this program onto your micro:bit, then light a light source such as a flashlight, sunlight, or a bright ceiling light on the micro:bit, and you should see the sun appear.

Cover the micro:bit with your hand and the sun icon should disappear.

If that doesn't work, try lowering the 100 number to suit your lighting location.

You can read more background on how the micro:bit light sensor works here.

Bill of Materials
micro:bit (or MakeCode emulator)

MakeCode or Python editor

battery pack (optional)

Light source and something covering the micro:bit - your hands can do it!

Step 2: Coding

Step 3: Improve

Show another picture, such as the moon or stars, when it's dark.

Display an animated sun when light hits your micro:bit.

When dimmed, the micro:bit transforms into a night light by lighting up the micro:bit's display.

PI-SUPPLY is a professional component supplier, providing everything you need for your component project. We also stock a huge selection of Raspberry piArduinomicro:bitrock pi and nvidia manufacturer accessories from your favorite brands.