from machine import Pin, ADC, I2C
from time import sleep
from lcd_api import LcdApi
from pico_i2c_lcd import I2cLcd
# Initialize the I2C bus and LCD display
i2c = I2C(0, scl=Pin(9), sda=Pin(8), freq=400000)
lcd = I2cLcd(i2c, 0x27, 2, 16)
# Initialize the LDR sensor
ldr = ADC(27)
pir_pin = Pin(14, Pin.IN)
MAX_ADC_READING = 65535;
ADC_REF_VOLTAGE = 3.3;
REF_RESISTANCE = 50;
LUX_CALC_SCALAR = 12518931;
LUX_CALC_EXPONENT = -1.405;
while True:
rawData = ldr.read_u16()
print(f"Raw data: {rawData}")
resistorVoltage = rawData / MAX_ADC_READING * ADC_REF_VOLTAGE;
print(f"Resistor voltage: {resistorVoltage}")
ldrVoltage = ADC_REF_VOLTAGE - resistorVoltage;
print(f"LDR voltage: {ldrVoltage}")
ldrResistance = ldrVoltage/resistorVoltage * REF_RESISTANCE;
print(f"LDR resistance: {ldrResistance}")
ldrLux = LUX_CALC_SCALAR * pow(ldrResistance, LUX_CALC_EXPONENT);
print(f"Lux: {ldrLux}")
pir_value = pir_pin.value()
print(f"PIR value: {pir_value}")
print("==========")
# Clear the LCD display
lcd.clear()
# Print the LDR and PIR sensor readings on the first line of the LCD display
lcd.putstr(f"LDR: {int(ldrLux)} PIR: {pir_value}")
lcd.putstr(f"Temp: {:.1f} C".format(temperature), 0, 0)
lcd.putstr(f"Hum: {:.1f} %".format(humidity), 0, 10)
# Print the raw LDR data on the second line of the LCD display
lcd.move_to(0, 1)
lcd.putstr(f"Raw data: {rawData}")
sleep(2)