from machine import Pin, UART, ADC
import dht, time
# DHT22 on GPIO14
dht_sensor = dht.DHT22(Pin(14))
# LDR on GPIO34
ldr = ADC(Pin(34))
ldr.atten(ADC.ATTN_11DB) # 0 - 3.3V range
# UART
uart = UART(1, baudrate=9600, tx=Pin(17), rx=Pin(16))
while True:
try:
# DHT22
dht_sensor.measure()
t = dht_sensor.temperature() # °C
h = dht_sensor.humidity() # %
# LDR
ldr_value = ldr.read() # 0 - 4095
# CSV message
msg = "{:.1f},{:.1f},{}\n".format(t, h, ldr_value)
uart.write(msg)
print("Sent:", msg.strip())
except Exception as e:
print("DHT read error:", e)
time.sleep(2)