Week 3
DHT22
標題「DHT22」的錨點DHT22 是一款數位式溫濕度感測器,能夠測量環境的溫度與濕度,並將數據以數位訊號的形式輸出。
讀取 DHT22 感測器資料
標題「讀取 DHT22 感測器資料」的錨點| DHT22 | Pico 2W |
|---|---|
| + | 36 |
| data | 34 |
| - | 38 |
from machine import Pinimport utimeimport dht
dSensor = dht.DHT22(Pin(28))
led_onboard = machine.Pin('LED', machine.Pin.OUT)
def readDHT(): try: dSensor.measure() temp = dSensor.temperature() temp_f = (temp * (9/5)) + 32.0 hum = dSensor.humidity()
current_seconds = utime.time() local_time = utime.localtime(current_seconds)
print(f'{local_time[0:3]} {local_time[3:6]} Temperature= {temp:.2f} C, {temp_f:.2f} F, Humidity= {hum:.2f}')
led_onboard.value(1) utime.sleep(0.3) led_onboard.value(0) utime.sleep(0.3) except OSError as e: print('Failed to read data from DHT sensor')
while True: readDHT() utime.sleep(1)