from machine import Pin, ADC, time_pulse_us
import time
import dht
# DHT22 (DATA → GP15)
dht_sensor = dht.DHT22(Pin(15))
# LDR (AO → GP26)
ldr = ADC(26)
# Ultrasonic (TRIG=GP9, ECHO=GP8)
trig = Pin(9, Pin.OUT)
echo = Pin(8, Pin.IN)
def get_distance():
trig.low()
time.sleep_us(2)
trig.high()
time.sleep_us(10)
trig.low()
duration = time_pulse_us(echo, 1)
distance = duration * 0.0343 / 2
return distance
while True:
# DHT22
dht_sensor.measure()
temp = dht_sensor.temperature()
hum = dht_sensor.humidity()
# LDR
light_value = ldr.read_u16()
# Ultrasonic
distance = get_distance()
print(f"Temp: {temp}°C | Humidity: {hum}% | Light: {light_value} | Distance: {distance:.2f}cm")
time.sleep(1)