Raspberry Pi Pico: BH1750 Ambient Light Sensor (MicroPython)
Introduction
In this tutorial, we will learn how to interface the BH1750 Ambient Light Sensor with the Raspberry Pi Pico using MicroPython. The BH1750 is a digital light sensor that provides precise ambient light intensity measurements in lux (lx). It communicates over the I2C protocol, making it easy to interface with microcontrollers like the Raspberry Pi Pico.
Components Required
- Raspberry Pi Pico
- BH1750 Ambient Light Sensor module
- Jumper wires
- Breadboard (optional)
Understanding the BH1750 Sensor
The BH1750 is a light intensity sensor that converts light into a digital signal output. It operates within a wide range (1 to 65535 lux) and is commonly used in automatic brightness control applications.
BH1750 Pinout:
BH1750 Pin | Raspberry Pi Pico Pin |
---|---|
VCC | 3.3V |
GND | GND |
SDA | GP20 (I2C0 SDA) |
SCL | GP21 (I2C0 SCL) |
Circuit Diagram
Connect the BH1750 module to the Raspberry Pi Pico as per the table above. Ensure that the SDA and SCL lines are properly connected.
Installing MicroPython and Thonny IDE
If you haven’t already installed MicroPython on your Raspberry Pi Pico, follow these steps:
- Download the MicroPython UF2 file from the official Raspberry Pi website.
- Press and hold the BOOTSEL button on the Pico and connect it to your PC.
- Copy the UF2 file to the Pico’s storage.
- Install Thonny IDE and select “MicroPython (Raspberry Pi Pico)” as the interpreter.
MicroPython Code for BH1750 Sensor
Initializing the BH1750 and Reading Light Intensity
from machine import Pin, I2C
import utime
BH1750_I2C_ADDR = 0x23
# Define BH1750 commands
POWER_ON = 0x01
RESET = 0x07
CONTINUOUS_HIGH_RES_MODE = 0x10
i2c = I2C(0, scl=Pin(21), sda=Pin(20), freq=100000)
def bh1750_init():
i2c.writeto(BH1750_I2C_ADDR, bytearray([POWER_ON]))
i2c.writeto(BH1750_I2C_ADDR, bytearray([RESET]))
def read_light():
i2c.writeto(BH1750_I2C_ADDR, bytearray([CONTINUOUS_HIGH_RES_MODE]))
utime.sleep(0.2)
data = i2c.readfrom(BH1750_I2C_ADDR, 2)
light_level = (data[0] << 8 | data[1]) / 1.2 # Convert to lux
return light_level
bh1750_init()
while True:
lux = read_light()
print("Light Intensity: {:.2f} lux".format(lux))
utime.sleep(1)
Explanation of the Code
- Initializing I2C: We set up I2C communication using GP20 (SDA) and GP21 (SCL) on the Raspberry Pi Pico.
- Powering the BH1750: The sensor is powered on using the
POWER_ON
command. - Resetting the Sensor: We send a
RESET
command to ensure a clean startup. - Reading Light Data: The BH1750 measures light intensity in lux, and the data is read in a 2-byte format.
- Displaying the Data: The lux value is printed every second in a continuous loop.
Conclusion
In this tutorial, we successfully interfaced the BH1750 ambient light sensor with the Raspberry Pi Pico using MicroPython. We learned how to initialize the sensor, read light intensity, and display the results. This sensor can be useful in various applications, including automatic lighting control and environmental monitoring.