树莓派使用 DHT11 / DHT22 温湿度传感器

发布于 2018-11-17  266 次阅读


接线说明

从左到右
3.3V-6V 电源
GPIO接口
NO Connected
接地

依赖库

为了简化工作,我们基于Adafruit_Python_DHT的Python库进行实验

Clone下这个依赖库

git clone https://github.com/adafruit/Adafruit_Python_DHT.git

安装依赖库

cd Adafruit_Python_DHT
sudo python setup.py install
cd ..

调用代码

#!/usr/bin/python

import Adafruit_DHT

#两个选一个填写
sensor = Adafruit_DHT.DHT11 #调用DHT11温湿度传感器
sensor = Adafruit_DHT.DHT22 #调用DHT22温湿度传感器


pin = 26  #使用GPIO-26口

humidity, temperature = Adafruit_DHT.read_retry(sensor, pin)

if humidity is not None and temperature is not None:

    print('Temp={0:0.1f}*C  Humidity={1:0.1f}%'.format(temperature, humidity))

else:

    print('Failed to get reading. Try again!')

全文完 🙂


Hope to be a free geek forever.