Raspberry Pi Pico: BH1750 Ambient Light Sensor (MicroPython)
Raspberry PiTutorials/DIY

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 PinRaspberry Pi Pico Pin
VCC3.3V
GNDGND
SDAGP20 (I2C0 SDA)
SCLGP21 (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:

  1. Download the MicroPython UF2 file from the official Raspberry Pi website.
  2. Press and hold the BOOTSEL button on the Pico and connect it to your PC.
  3. Copy the UF2 file to the Pico’s storage.
  4. Install Thonny IDE and select “MicroPython (Raspberry Pi Pico)” as the interpreter.

MicroPython Code for BH1750 Sensor

Initializing the BH1750 and Reading Light Intensity

Python
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

  1. Initializing I2C: We set up I2C communication using GP20 (SDA) and GP21 (SCL) on the Raspberry Pi Pico.
  2. Powering the BH1750: The sensor is powered on using the POWER_ON command.
  3. Resetting the Sensor: We send a RESET command to ensure a clean startup.
  4. Reading Light Data: The BH1750 measures light intensity in lux, and the data is read in a 2-byte format.
  5. 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.

Harshvardhan Mishra

Hi, I'm Harshvardhan Mishra. Tech enthusiast and IT professional with a B.Tech in IT, PG Diploma in IoT from CDAC, and 6 years of industry experience. Founder of HVM Smart Solutions, blending technology for real-world solutions. As a passionate technical author, I simplify complex concepts for diverse audiences. Let's connect and explore the tech world together! If you want to help support me on my journey, consider sharing my articles, or Buy me a Coffee! Thank you for reading my blog! Happy learning! Linkedin

Leave a Reply

Your email address will not be published. Required fields are marked *