Microbit Sunshine Detector

A variation on the nightlight example is a simple, adjustable sunshine detector.

Set the threshold level for brightness using the a and b buttons then the screen/light detector will flash a sun icon if enough light is falling on the screen. In dark environments, you can go down to a low threshold and in very light areas you can adjust upwards so that you can tell the difference between light falling on an angle on the screen compared to shining fully on the face.


from microbit import *

sun_icon = Image("90909:09990:99999:09990:90909")

max_light_level = 255
min_light_level = 0
light_trigger = 100
button_increment = 20

while True:
    # increase light threshold
    if button_a.was_pressed():
        light_trigger = min(light_trigger + button_increment, max_light_level)
        display.scroll(str(light_trigger))
    # decrease light threshold
    if button_b.was_pressed():
        light_trigger = max(light_trigger - button_increment, min_light_level)
        display.scroll(str(light_trigger))

    # is it bright enough?
    if display.read_light_level() > light_trigger:
        display.show(sun_icon)
        sleep(500)

    display.clear()
    sleep(100)


I am going to include a snippet like this in the auto driving car so that it will turn its headlights on when it starts getting dark. Microbit safety first :)