# Rui Santos & Sara Santos - Random Nerd Tutorials # Complete project details at https://RandomNerdTutorials.com/micropython-bh1750-esp32-esp8266/ from machine import Pin, SoftI2C from bh1750 import BH1750 import time # Initialize I2C communication (ESP32) i2c = SoftI2C(scl=Pin(22), sda=Pin(21), freq=400000) # Initialize I2C communication (ESP8266) #i2c = SoftI2C(scl=Pin(5), sda=Pin(4), freq=400000) # Create BH1750 object light_sensor = BH1750(bus=i2c, addr=0x23) try: # Read lux every 2 seconds while True: lux = light_sensor.luminance(BH1750.CONT_HIRES_1) print("Luminance: {:.2f} lux".format(lux)) time.sleep(2) except Exception as e: # Handle any exceptions during sensor reading print("An error occurred:", e)