- Introduction
- YouTube Video
- What You'll Need
- Connecting the Pico 2 to the Light Sensor
- Creating the Program
Introduction
Hi there. In this tutorial, I'll go over how you can use an ambient light sensor board with a Pico 2 and MicroPython. This project will get the current light level and then turn on the LED onboard the Pico 2 when the light is low.
This project will also work on the original Pico.
YouTube Video
If you would prefer to see a video of this article, there is a YouTube video available below:
What You'll Need
Let's start by going over what's needed for this project. In terms of hardware, you'll need:
- A Pico or a Pico 2, with or without Wi-Fi & Bluetooth
- A USB-A or USB-C to Micro-B cable
- A breadboard
- A VEML7700 based ambient light sensor. The one I have is an Adafruit VEML7700 Lux Sensor
- And lastly, suitable wires to connect the Pico to the light sensor board. I used 4 dupont male to male cables to go from the Pico 2 to the breadboard and a STEMMA QT / Qwiic JST-SH 4-pin to 4 Male dupont wires to go from the sensor to the breadboard.
As for software, you'll need:
- MicroPython installed on the Pico
- Python installed on your local computer
- Visual Studio Code with the Python and Raspberry Pi Pico extensions installed. You can use Thonny instead if you prefer.
Connecting the Pico 2 to the Light Sensor
After inserting the Pico 2 into the breadboard, the cabling of the light sensor to the Pico 2 is done by using the GPIO pins for I2C bus 0 and 3.3v power & ground.
Coming from the Pico 2, the 3.3v power (the red wire) is connected from pin 36 and ground (the black wire) is coming from pin 33.
The i2C connections are from pin 1 for SDA (the blue wire) and pin 2 for SCL (the yellow wire). Again, I2C bus 0 was what I used for this build but you can use another if you would like.
As for the VEML7700 sensor, I plugged the Stemma QT cable into the right-side connector and then simply matched each wire with the same coloured wire from the Pico 2.
Creating the Program
Before you get started, make sure to download the source code from my GitHub repository as there are custom modules in there.
Oh, a quick shout out to palouf34 on GitHub for the veml7700.py module that I found which worked on the Pico. The one from Adafruit wouldn't work as MicroPython didn't include some of the libraries that were needed.
To begin with, connect the Pico or Pico 2 to your computer.
Next, open VS Code. Click on "Create Pico Project".
Select MicroPython from the drop down menu.
Give the project a name and specify a location to save it to. Leave everything else as is and then click on create.
Once the new window is open and the files appear in explorer, delete blink.py. Leave the .vscode folder and .micropico file as alone as they are required for the Pico extension to work.
Create a new folder called modules. In that folder, create a file called init.py and close it if it opens.
Next, create a new folder in the modules folder called light_sensor. In that folder, create another new file called init.py. Again, close it if it opens.
Next, put the veml.py and veml7700.py files in the light_sensor folder open veml.py. The folder structure at this point should look like this:
Now, open veml.py. The code should look like this:
from machine import I2C, Pin
from .veml7700 import VEML7700
def veml_setup(i2c_bus:int = 0, sda_pin:int = 0,
scl_pin:int = 1, address = 0x10,
freq:int = 400000) -> VEML7700:
"""_summary_
This function setups the connection to the light sensor.
args:
i2c_bus (int, optional): The I2C bus number to use. Defaults to 0.
sda_pin (int, optional): The SDA pin to use. Defaults to 0.
scl_pin (int, optional): The SCL pin to use. Defaults to 1.
address (int, optional): The light sensors i2c address. Defaults to 0x10.
freq (int, optional): The frequency to use. Defaults to 400000.
returns:
VEML7700: An object for interacting with the light sensor.
"""
# --- 1. Create the i2c interface:
sda = Pin(sda_pin)
scl = Pin(scl_pin)
i2c = I2C(i2c_bus, sda = sda, scl = scl, freq = freq)
# --- 2. Instantiate the sensor:
try:
veml = VEML7700(address = address, i2c = i2c,
it = 100, gain = 1/8)
except:
raise Exception("Sensor not found. Check the sensor is connected and the i2c settings are correct.")
return veml
Let's go over what this module does:
- First, there's the library and modules that need to be imported. I2C and Pin are used for interacting with the i2c bus. VEML7700 is used for interacting with the light sensor.
- Next, there is a function called veml_setup that has parameters that are required for setting up the i2c bus. This includes the bus number, GPIO pins and frequency to use. All of these have default values which match the wiring diagram and the i2c address on the back of the sensor board.
- Once the i2c bus is setup, the function will then attempt to connect to the light sensor. If it is successful, the function will return a VEML7700 object back to the caller. If not, it will raise an exception and terminate the program.
Close the file down.
In the root of the project, copy the main.py file. The final folder structure should look like this:
Open main.py if it doesn't automatically open.
The code should look like this:
# --- Import required libraries / modules:
from machine import Pin
from utime import sleep
from modules.light_sensor.veml import veml_setup
def main() -> None:
"""_summary_
This is the main function of the program.
returns:
Nothing is returned.
"""
# --- 1. Attempt to instantiate a VEML7700 light sensor:
light_sensor = veml_setup()
# --- 2. Attempt to setup the onboard LED:
try:
led_gpio = Pin("LED", Pin.OUT)
except:
raise Exception("LED not present. Please check your Pico board.")
# --- 3. Display the ambient light brightness value:
while True:
# --- 4. Attempt to get a reading from the VEML7700 light sensor:
try:
light_level = light_sensor.read_lux()
except:
raise Exception("Unable to get reading. Check the sensor is connected.")
print(light_level)
# --- 38 or above for light level:
try:
if light_level < 39:
led_gpio.on()
else:
led_gpio.off()
except:
raise Exception("Cannot change LED state. Please check your Pico board.")
sleep(1)
# --- Start the program:
if __name__ == "__main__":
main()
To go through what this will do:
- First, it imports the libraries and the custom veml module.
- Then there's a main function. Inside it:
- There is a variable to instantiate the light sensor. If all is well, that will be assigned a VEML7700 object that can be used to interact with the light sensor. If not, an exception will be raised in the veml module and terminate the program.
- After that, an attempt to setup the onboard LED on the Pico is made. Again, if it fails, an exception will be raised.
- Next, a while loop is used to continuously read the sensor every so many seconds and print out the result to the terminal.
- Following on from that, an attempt to read the light level from the sensor is made. If it can't be read for whatever reason, perhaps due to a hardware failure, an exception will be raised.
- If the sensor reading is under 39, the program will try to turn the LED on. If it is over 39, it will turn off. If there is an issue interacting with the LED, an exception will be raised.
- The sleep of one second is just for demonstration purposes. A much higher number, such as 60 or more would be more reasonable.
- The last part of the file is to call the main function when the program starts.
Save the file, if needed, but don't close it.
Now, right-click on a blank space in explorer and select "upload project to pico".
Once that is done, right click on an empty space in the main.py file and select "run current file on pico".
You can now see the output from the sensor.
If the sensor is covered over, the number should go below 39 and the LED turns on. Uncover it and the LED goes off.
Now, whilst it's running, disconnect the sensor from the Pico. It now gives the expected exception error, indicating it can't read the sensor.
Running the program again with the sensor disconnected will show the exception error that implies it cannot setup the sensor on the i2c bus. Those errors are expected for the two scenarios.
Once the sensor is plugged back in and the program is run again, it will work as expected.
Now, what this project covered is pretty simple in terms of it just turning on the LED on the Pico, but it can be useful in other ways. For example, making a call to an API or a web hook to turn on or off a smart light or a smart power socket to turn a lamp on that doesn't have smart capabilities. It could also work with Home Assistant to do some other home automation function.
The choice is up to you, but that concludes this project.
If you want some other Pico project ideas, there is a playlist at the bottom of this article with some other Pico starter projects I've done.
But for now, thanks for reading and have a nice day.












Top comments (0)