DHT11 Temperature and Humidity Sensor
The DHT11 is a digital temperature and humidity sensor that features a calibrated digital signal output. It is low-cost, long-term stable, and capable of measuring relative humidity and temperature. It can collect temperature and humidity data using just a single data line.
Module Source
Purchase Link:
https://item.taobao.com/item.htm?spm=a230r.1.14.23.735720126ougj3&id=522553143872&ns=1&abbucket=12
Baidu Netdisk Download Link: https://pan.baidu.com/s/1HQEL699-Yl5Jh3Hp87_FlQ
Password: 2sgq
Specifications
Operating Voltage: 3-5.5V
Operating Current: 1MA
Measurement Resolution: 8 bit
Communication Protocol: Single bus
Pin Count:3 Pin (2.54mm pitch header)
Hardware Connection
VCC to 5V of the development board
GND to GND of the development board
DATA to a digital pin (e.g., pin 2) on the development board.
The DHT11 typically has a separate data line, and an optional pull-up resistor connected to VCC. Some modules may already include this pull-up resistor.
Usage Method
Install Library
You need to install the library for reading DHT11 data. Adafruit provides a good library that can be installed using the library manager of the Arduino IDE.
- Open the Arduino IDE.
- Go to Tools > Manage Libraries.
- Search
DHT sensor library
and install it (by Adafruit).
Enter the code:
/******************************************************************************
* Test Hardware: LCSC ColorEasyDuino Development Board
* Version Number: V1.0
* Modified By: www.lckfb.com
* Modification Date: April 8, 2024
* Function Overview:
*****************************************************************************
* Open-source development board hardware and software information and related projects hardware and software information on official website
* Development board official website: www.lckfb.com
* Technical support resident forum, any technical problems are welcome at any time to exchange learning
* LCSC Forum: club.szlcsc.com
* Follow our Bilibili account: [立创开发板], stay toned to our latest news!
* We focus on cultivating Chinese engineers rather than profiting from board sales.
******************************************************************************/
#include "DHT.h"
#define DHTPIN 2 // 定义DHT11数据针脚连接到Arduino的2号引脚
#define DHTTYPE DHT11 // 定义DHT类型为DHT11
DHT dht(DHTPIN, DHTTYPE);
void setup() {
Serial.begin(9600);
Serial.println("DHT11 温湿度测试:");
// 初始化DHT11传感器
dht.begin();
}
void loop() {
// 等待几秒钟之间的读取(DHT11 的读取速度相对较慢)
delay(2000);
// 读取温度和湿度值
float h = dht.readHumidity();
float t = dht.readTemperature();
// 检查是否读取失败,并退出早期
if (isnan(h) || isnan(t)) {
Serial.println("读取DHT11失败!");
return;
}
// 输出读取到的温度和湿度
Serial.print("湿度: ");
Serial.print(h);
Serial.print(" %\t");
Serial.print("温度: ");
Serial.print(t);
Serial.println(" °C ");
}
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51