友情提示点击顶部放大镜 可以使用站内搜索 记住我们的地址 www.hainabaike.com
DHT11 数字温湿度传感器是一款含有已校准数字信号输出的温湿度复合传感器,应用领域:暖通空调;汽车;消费品;气象站;湿度调节器;除湿器;家电;医疗;自动控制等。
DHT11 的特性
湿度的测量范围是:20-90%RH,量程根据温度不同有所变化;
温度的测量量程是:0 ~ 50 摄氏度;
测湿精密度是:±5%RH;
测温精密度是:±2 摄氏度;
接线
DHT11 温湿度传感器模块有三个引脚,VCC 接 Pico 的 3V3 引脚,GND 接 Pico 的 GND 引脚,DATA 接 Pico 的 GP22 引脚。
编程
这里直接使用前人造好的轮子,将下面的代码保存在 Pico 上,命名为 PicoDHT22.py。
import utime import rp2 from rp2 import PIO, asm_pio from machine import Pin @asm_pio(set_init=(PIO.OUT_HIGH),autopush=True, push_thresh=8) def DHT22_PIO(): # clock set at 500Khz Cycle is 2us # drive output low for at least 20ms set(y,1) # 0 pull() # 1 mov(x,osr) # 2 set(pindirs,1) # 3 set pin to output set(pins,0) # 4 set pin low label ('waitx') jmp(x_dec,'waitx') # 5 decrement x reg every 32 cycles set(pindirs,0) # 6 set pin to input # STATE A. Wait for high at least 80us. max should be very short set(x,31) # 7 label('loopA') jmp(pin,'got_B') # 8 jmp(x_dec,'loopA') # 9 label('Error') in_(y,1) # 10 jmp('Error') # 11 Infinity loop error # STATE B. Get HIGH pulse. max should be 40us label('got_B') set(x,31) # 12 label('loop_B') jmp(x_dec,'check_B') # 13 jmp('Error') # 14 label('check_B') jmp(pin,'loop_B') # 15 # STATE C. Get LOW pulse. max should be 80us set(x,31) # 16 label('loop_C') jmp(pin,'got_D') # 17 jmp(x_dec,'loop_C') # 18 jmp('Error') # 19 # STATE D. Get HIGH pulse. max should be 80us label('got_D') set(x,31) # 20 label('loop_D') jmp(x_dec,'check_D') # 21 jmp('Error') # 22 label('check_D') jmp(pin,'loop_D') # 23 # STATE E. Get Low pulse delay. should be around 50us set(x,31) # 24 label('loop_E') jmp(pin,'got_F') # 25 jmp(x_dec,'loop_E') # 26 jmp('Error') # 27 # STATE F. # wait 40 us label('got_F') nop() [20] # 28 in_(pins,1) # 29 # now wait for low pulse set(x,31) # 30 jmp('loop_D') # 31 class PicoDHT22: def __init__(self,dataPin, powerPin=None,dht11=False,smID=1): self.dataPin = dataPin self.powerPin = powerPin self.dht11 = dht11 self.smID = smID self.dataPin.init(Pin.IN, Pin.PULL_UP) if self.powerPin is not None: self.powerPin.init(Pin.OUT) self.powerPin.value(0) self.sm= rp2.StateMachine(self.smID) def read_array(self): if self.powerPin is not None: self.powerPin.value(1) utime.sleep_ms(800) utime.sleep_ms(200) #start state machine self.sm.init(DHT22_PIO,freq=500000, set_base=self.dataPin, in_base=self.dataPin, jmp_pin=self.dataPin) if self.dht11: self.sm.put(10000) else: self.sm.put(1000) self.sm.active(1) value = [] for i in range(5): value.append(self.sm.get()) self.sm.active(0) if self.powerPin is not None: self.powerPin.value(0) return value def read(self): value = self.read_array() sumV = 0 for i in range(4): sumV += value[i] if (sumV & 0xff) == value[4]: if self.dht11: humidity=value[0] & 0x7f temperature=value[2] else: humidity=((value[0]<<8) + value[1])/10.0 temperature=(((value[2] &0x7f) << 8) + value[3]) /10.0 if (value[2] & 0x80) == 0x80: temperature = -temperature return temperature, humidity else: return None, None
再创建 main.py 并输入一下主程序:
from machine import Pin from PicoDHT22 import PicoDHT22 import utime dht_sensor=PicoDHT22(Pin(22,Pin.IN,Pin.PULL_UP),dht11=True) while True: T,H = dht_sensor.read() if T is None: print(" sensor error") else: print("{}'C {}%".format(T,H)) #DHT22 not responsive if delay to short utime.sleep_ms(500)
注意,这个库也支持 DHT22 模块。如果使用的是 DHT22 传感器,请将上面 dht_sensor 的那行修改为下面:
dht_sensor=PicoDHT22(Pin(22,Pin.IN,Pin.PULL_UP),dht11=False)
你还可以:
查看系列教程中的其他文章
购买本教程所用到的 Pico 套件
标签: 树莓派 Pico传感器编程物联网DHT22Raspberry PiDHT11
评论列表