Introduction
This guide will help you read light intensity data from your PiicoDev Ambient Light Sensor and a Micro:bit.
To follow along, it's best to have:
- A Micro:bit
- A PiicoDev Ambient Light Sensor VEML6030
- A PiicoDev adapter for Micro:bit
- A PiicoDev Cable
We'll program with Thonny in this tutorial. If you haven't used Thonny before, check out our guide for Programming a Micro:bit with Thonny. If you prefer, you can program your Micro:bit using python.microbit.org instead (see our guide).
If you prefer not to use the Micro:bit adapter, there are other connection options in our PiicoDev Connection Guide.
Connect the PiicoDev sensor to your Micro:bit
Plug your Micro:bit into the PiicoDev adapter (buttons + LED matrix facing up), connect your sensor to the adapter via the PiicoDev cable and connect your Micro:bit to your computer with a USB lead.
If you're unfamiliar with connecting PiicoDev modules, read the PiicoDev Connection Guide before proceeding.
Download the PiicoDev Modules and Example Code
Download the following files and save them to your working directory (Right Click > "Save Link As")
- PiicoDev_Unified.py - The PiicoDev Unified Libraries: Drives I2C communications for PiicoDev modules
- PiicoDev_VEML6030.py - The device driver.
- main.py - an example script for this PiicoDev Sensor
It will be best to keep this file wherever you like to keep your coding projects eg. Documents > PiicoDev
Example Code
Open Thonny, connect to your micro:bit and upload the three files from the previous step.
Press Ctrl+D to restart your micro:bit and run the main script - light data should begin streaming up the shell
Above: Plotted data starts at ambient levels, then I cover the sensor with my hand and the data dips. A flashlight is then shined on the sensor with creates a large peak.
Now we can easily read light data with our Pico.
Remix - Day/Night message
In the final part of the video, we modify the example code to keep track of whether it is day or night (bright/dark). When light data transitions between bright and dark, we can create a Good Morning or Goodnight sound with our micro:bit - You can use this structure for decision-making in your own projects - be sure to code along!
If you have any questions or uncertainty, start the discussion below. We're full-time makers and here to help!