Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to use Adafruit to read DHT11 temperature and humidity Sensor

2025-01-28 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)06/01 Report--

This article mainly shows you "how to use Adafruit to read DHT11 temperature and humidity sensor", the content is easy to understand, clear, hope to help you solve doubts, the following let Xiaobian lead you to study and learn "how to use Adafruit to read DHT11 temperature and humidity sensor" this article.

Although DHT11 temperature and humidity sensor is not the most efficient temperature and humidity sensor, it is widely used because of its low price. Next we introduce the method of reading DHT11 data based on Adafruit DHT library.

DHT11 specification

DHT11 has four pins, but one of them is not used. All modules will be simplified to 3 pins.

Humidity detection range: 20-80% (5% accuracy)

Temperature detection range: 0-50 °C (±2 °C accuracy)

The manufacturer does not recommend that the reading frequency be less than 2 seconds. If you do so, the data may be incorrect.

Hardware connection

It is necessary to connect a pull-up resistor (4.7K-10K) between the power supply and the data pin, which is usually included in the purchase of a DHT11 module. The pin positions of different module models are slightly different. The following is illustrated by the module on the figure:

DHT PinSignalPi Pin13.3V12Data/Out11 (GPIO17) 3not used-4Ground6 or 9

Data pins can be modified according to your needs.

Python library

The reading of DHT11 needs to follow a specific signal protocol, in order to facilitate us to use Adafruit DHT library.

Software installation

You need to update the package before you start:

one

two

Sudo apt-get update

Sudo apt-get install build-essential python-dev

Get the Adafruit library from GitHub:

one

two

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

Cd Adafruit_Python_DHT

Install the library for Python 2 and Python 3:

one

two

Sudo python setup.py install

Sudo python3 setup.py install

Sample program

Adafruit provides a sample program to run the following command test.

one

two

three

four

Cd ~

Cd Adafruit_Python_DHT

Cd examples

Python AdafruitDHT.py 11 17

These two parameters represent the DHT11 and the raspberry pie GPIO number connected to the data pin, respectively. If it is successful, it will output:

How Temp=22.0* Humidity=68.0% uses this library in other Python programs

Refer to the following method to introduce the Adafruit library, and then you can use the "read_retry" method to read DHT11 data:

one

two

three

four

five

six

seven

eight

nine

ten

eleven

twelve

thirteen

fourteen

fifteen

sixteen

seventeen

eighteen

Import Adafruit_DHT

# Set sensor type: Options are DHT11,DHT22 or AM2302

Sensor=Adafruit_DHT.DHT11

# Set GPIO sensor is connected to

Gpio=17

# Use read_retry method. This will retry up to 15 times to

# get a sensor reading (waiting 2 seconds between each retry).

Humidity, temperature = Adafruit_DHT.read_retry (sensor, gpio)

# Reading the DHT11 is very sensitive to timings and occasionally

# the Pi might fail to get a valid reading. So check if readings are valid.

If humidity is not None and temperature is not None:

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

Else:

Print ('Failed to get reading. Try others')

The above is all the contents of the article "how to use Adafruit to read DHT11 temperature and humidity sensors". Thank you for reading! I believe we all have a certain understanding, hope to share the content to help you, if you want to learn more knowledge, welcome to follow the industry information channel!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Internet Technology

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report