Light Sensor

The mPython Board has an onboard light sensor that can be used to sense changes in the ambient light.

Example: light-controlled lamp:

from mpython import *
from lv_gui import *
import time

gui = GUI()
while True:
    gui.fill(type=0)                                # Clear the screen
    gui.draw_label(text=str('Brightness:'+str(light.read())), row=1, color=0xffffff, wrap=False)  # Display the onboard light sensor reading
    gui.update()                                    # Refresh
    time.sleep_ms(100)                                   # Delay 100 ms
    if light.read() < 500 :                    # When the light level is below 500, turn the LED on
        rgb.fill((50,50,50))
        rgb.write()
    else:                                      # Otherwise, turn the LED off
        rgb.fill((0,0,0))
        rgb.write()

Use the light object to get light sensor data:

light.read()

Note

The light sensor uses the read() function to read data. The returned value is in lux.

Now that you have learned how to collect ambient light data, you can combine it with other features to create more interesting scenarios.