from machine import Pin, ADC, I2C
from time import sleep
import ssd1306
import dht
# --- I2C Initialization for OLED ---
# For Pico, typical I2C pins:
# SDA -> GP0, SCL -> GP1 (Using I2C0)
i2c = I2C(0, sda=Pin(0), scl=Pin(1), freq=400000)
# --- Initialize OLED (128x64 pixels) ---
oled = ssd1306.SSD1306_I2C(128, 64, i2c)
print("I2C device addresses found:", i2c.scan())
# --- Initialize LDR (on ADC pin, e.g., GP27/ADC1) ---
ldr_pin = ADC(27) # LDR connected to ADC1 (GP27)
# --- Initialize DHT22 (on digital GPIO pin, e.g., GP15) ---
sensor_dht = dht.DHT22(Pin(15))
while True:
# Read LDR Value
ldr_value = ldr_pin.read_u16() # For Pico, use read_u16()
# Read DHT22 values
sensor_dht.measure()
temperatura = sensor_dht.temperature()
humid = sensor_dht.humidity()
# Clear OLED screen
oled.fill(0)
# Draw border lines on OLED
for i in range(128):
oled.pixel(i, 0, 1)
oled.pixel(i, 20, 1)
oled.pixel(i, 40, 1)
oled.pixel(i, 63, 1)
for i in range(64):
oled.pixel(0, i, 1)
oled.pixel(127, i, 1)
# Display sensor data
oled.text("Humid :", 3, 6)
oled.text(f"{humid:.1f}%", 60, 6)
oled.text("Temp :", 3, 27)
oled.text(f"{temperatura:.1f}C", 60, 27)
oled.text("Light :", 3, 47)
oled.text(f"{ldr_value}", 60, 47)
# Print to Serial for debugging
print("LDR Value:", ldr_value)
print("Temp:", temperatura)
print("Humidity:", humid)
# Update OLED display
oled.show()
# Wait 1 second before next reading
sleep(1)