import machine
import time
from machine import Pin, SoftI2C
from i2c_lcd import I2cLcd
# Calibration constants (replace with your own values)
ANALOG_MIN = 0 # Analog value in complete darkness
ANALOG_MAX = 4095 # Analog value in maximum light
LUX_MIN = 0 # Lux value in complete darkness
LUX_MAX = 1000 # Lux value in maximum light
# Initialize I2C
i2c = SoftI2C(scl=Pin(22), sda=Pin(21), freq=400000)
devices = i2c.scan()
# Check if any I2C devices are connected
if devices:
# Take the first I2C device found
device_address = devices[0]
print("I2C addr: " + hex(device_address))
lcd = I2cLcd(i2c, device_address, 2, 16)
else:
print("No I2C devices found!")
sys.exit()
# Initialize ADC
photo_pin = machine.ADC(13)
while True:
# Read analog value from the photoresistor
analog_value = photo_pin.read_u16()
# Convert analog value to lux using calibration curve
lux = (analog_value - ANALOG_MIN) * (LUX_MAX - LUX_MIN) / (ANALOG_MAX - ANALOG_MIN) + LUX_MIN
# Clear the LCD screen
lcd.clear()
# Move to the first line and display the lux value
lcd.move_to(0, 0)
lcd.putstr("Lux: {:.2f}".format(lux))
# Print the lux value to the console
print("Lux:", lux)
# Wait for a short duration before the next reading
time.sleep(0.2)
# Print the lux value to the console
print("Lux:", lux)
# Wait for a short duration before the next reading
time.sleep(0.2)