* 点击
示例效果
使用python不断读取DHT11温湿度传感器当下测量所得的温度和湿度。
![5497cef94424d7198acc369963ba9ef0.png](https://img5.php1.cn/3cdc5/935d/339/8020f292adc8a39c.png)
元件
![fad44cfbd02c3a4f89a29ec3cc17ee67.png](https://img5.php1.cn/3cdc5/935d/339/10689a2f8ba94abc.png)
BOM表
名称 | 数量 |
---|
树莓派(raspberry pi 4) | x1 |
DHT11 温湿度传感器 | x1 |
跳线(杜邦线) | x1 |
接线方式
DHT11温湿度传感器 | | pi(树莓派) |
---|
DATA | | GPIO/BCM pin 27 |
GND | | GND |
VCC | | 5V |
![f2c1a1378a7c988d31febc8d95212e63.png](https://img5.php1.cn/3cdc5/935d/339/c39e8e7f71b98ea7.png)
推荐一个树莓派引脚的网站:https://pinout.xyz/
程序代码
在写本示例之前一直使用的是 Adafruit_Python_DHT ,但在验证代码的时候,发现已经使用不了了。
然后看到 Adafruit_Python_DHT的Github上已经作出了声明:
![3e413a9e7cb122c5f529aa8188f1c38a.png](https://img5.php1.cn/3cdc5/935d/339/9915bb0ebb0bd515.png)
翻译过来就是:
废弃库 Adafruit Python DHT传感器库
他的库已经被废止了! 为了历史和研究的目的,我们将保留这个库,但会对库进行归档。
我们现在只支持在Python中使用我们的CircuitPython库。
查看这个指南,了解如何在CircuitPython库中使用DHT传感器的信息:https://learn.adafruit.com/dht-humidity-sensing-on-raspberry-pi-with-gdocs-logging/python-setup。
详细资料请查看 https://github.com/adafruit/Adafruit_Python_DHT
1,安装库和依赖
在树莓派上使用python3编写应用DHT系列传感器的代码,先安装一些库和依赖
打开terminal(终端/命令行),输入以下命令:
install adafruit-circuitpython-dht
2,代码编写
安装完成后,打开jupyter lab,新建一个python3 的 notebook,或者新建一个.py的文件
# 加载需要用到的库
以下是完整的 jupyter notebook 的python代码示例截图
![e6d18090ec90c70c0830b1d6f597362a.png](https://img5.php1.cn/3cdc5/935d/339/f44da8b88d3718c7.png)
本文参考资料:
https://learn.adafruit.com/dht-humidity-sensing-on-raspberry-pi-with-gdocs-logging/python-setup
https://github.com/adafruit/Adafruit_Python_DHT