当前位置:网站首页>DHT11 temperature and humidity sensor
DHT11 temperature and humidity sensor
2022-07-01 06:02:00 【Bitongo】
List of articles
Preface
Previous section , We learned about digital temperature sensors DS18B20 Use , In this section, we will introduce the digital temperature and humidity sensor
DHT11 Use , The sensor can not only measure temperature , It can also measure humidity . I'll show you how to use STM32
To read DHT11 Digital temperature and humidity sensor , So as to get the information of ambient temperature and humidity , And the temperature and humidity values are displayed in
TFTLCD On module .
One 、DHT11 brief introduction
DHT11 It's a digital sensor integrated with humidity and temperature . The sensor consists of a resistive humidity measuring element and a NTC
Temperature measuring element , And with a high performance 8 Bit single chip microcomputer connected . Through the simple circuit connection of MCU and other microprocessors, we can
Real time collection of local humidity and temperature .DHT11 Simple single bus can be used to communicate with MCU , Just one
individual I/O mouth . Sensor internal humidity and temperature data 40Bit The data is transmitted to the single chip microcomputer at one time , The data adopts checksum method
check , Effectively ensure the accuracy of data transmission .DHT11 Low power consumption ,5V At supply voltage , The average work is the largest
electric current 0.5mA.
DHT11 The technical parameters are as follows :
Operating voltage range :3.3V-5.5V
Working current : Average 0.5mA
Output : Single bus digital signal
measuring range : humidity 20~90%RH, temperature 0~50℃
precision : humidity ±5%, temperature ±2℃
The resolution of the : humidity 1%, temperature 1℃
DHT11 The pin arrangement of is shown in the figure below :
Electrical characteristics 
Pin description 
Package information 
Detailed parameters 
Two 、 The data transfer
although DHT11 And DS18B20 similar , It's all single bus access , however DHT11 The interview of , relative DS18B20 It's much simpler . Let's take a look DHT11 Data structure of .
DHT11 The digital wet temperature sensor adopts single bus data format . namely , A single data pin port completes the bidirectional transmission of input and output . Its data packet consists of 5Byte(40Bit) form . The data is divided into decimal part and integer part , A complete data transfer is 40bit, High first out .
DHT11 The data format of is :8bit Humidity integer data +8bit Humidity decimal data +8bit Temperature integer data +8bit Temperature decimal data +8bit The checksum . The checksum data is the sum of the first four bytes .
The sensor data output is uncoded binary data . data ( humidity 、 temperature 、 Integers 、 decimal ) Should be handled separately . for example , Once from DHT11 The data read is shown in the figure below :
From the above data, we can get the values of humidity and temperature , computing method :
humidity = byte4 . byte3=45.0 (%RH)
temperature = byte2 . byte1=28.0 ( ℃)
check = byte4+ byte3+ byte2+ byte1=73(= humidity + temperature )( Correct verification )
It can be seen that ,DHT11 The data format is very simple ,DHT11 and MCU The maximum one-time communication is 3ms about ,
It is recommended that the continuous reading interval of the host should not be less than 100ms. below , Let's introduce DHT11 The transmission timing of .
DHT11 The data sending process of is shown in the figure below :
First, the host sends a start signal , namely : Pull down the data line , keep t1( At least 18ms) Time , Then pull up the data cable t2(20~ 40us) Time , Then read DHT11 Response , Normal word ,DHT11 Will pull down the data cable , keep t3(40~50us) Time , As a response signal , then DHT11 Pull up the data line , keep t4(40 ~50us) After time , Start outputting data .
DHT11 Output number ‘0’ The timing of is shown in the figure below :
DHT11 Output number ‘1’ The timing of is shown in the figure below :
3、 ... and 、 Program realization
DHT11.c
// Reset DHT11
void DHT11_Rst(void)
{
DHT11_IO_OUT(); //SET OUTPUT
DHT11_DQ_OUT=0; // Pull it down DQ
delay_ms(20); // Pull down at least 18ms
DHT11_DQ_OUT=1; //DQ=1
delay_us(30); // The main engine is pulled up 20~40us
}
// wait for DHT11 The response of the
// return 1: Not detected DHT11 The existence of
// return 0: There is
u8 DHT11_Check(void)
{
u8 retry=0;
DHT11_IO_IN();//SET INPUT
while (DHT11_DQ_IN&&retry<100)//DHT11 Will pull down 40~80us
{
retry++;
delay_us(1);
};
if(retry>=100)return 1;
else retry=0;
while (!DHT11_DQ_IN&&retry<100)//DHT11 After pulling down, it will pull up again 40~80us
{
retry++;
delay_us(1);
};
if(retry>=100)return 1;
return 0;
}
// from DHT11 Read a bit
// Return value :1/0
u8 DHT11_Read_Bit(void)
{
u8 retry=0;
while(DHT11_DQ_IN&&retry<100)// Wait until it goes low
{
retry++;
delay_us(1);
}
retry=0;
while(!DHT11_DQ_IN&&retry<100)// Wait for high level
{
retry++;
delay_us(1);
}
delay_us(40);// wait for 40us
if(DHT11_DQ_IN)return 1;
else return 0;
}
// from DHT11 Read a byte
// Return value : Data read
u8 DHT11_Read_Byte(void)
{
u8 i,dat;
dat=0;
for (i=0;i<8;i++)
{
dat<<=1;
dat|=DHT11_Read_Bit();
}
return dat;
}
// from DHT11 Read data once
//temp: Temperature value ( Range :0~50°)
//humi: Humidity value ( Range :20%~90%)
// Return value :0, normal ;1, Read failed
u8 DHT11_Read_Data(u8 *temp,u8 *humi)
{
u8 buf[5];
u8 i;
DHT11_Rst();
if(DHT11_Check()==0)
{
for(i=0;i<5;i++)// Read 40 Bit data
{
buf[i]=DHT11_Read_Byte();
}
if((buf[0]+buf[1]+buf[2]+buf[3])==buf[4])
{
*humi=buf[0];
*temp=buf[2];
}
}else return 1;
return 0;
}
// initialization DHT11 Of IO mouth DQ Simultaneous detection DHT11 The existence of
// return 1: non-existent
// return 0: There is
u8 DHT11_Init(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOG, ENABLE); // Can make PG Port clock
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_11; //PG11 port configuration
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP; // Push pull output
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIOG, &GPIO_InitStructure); // initialization IO mouth
GPIO_SetBits(GPIOG,GPIO_Pin_11); //PG11 High output
DHT11_Rst(); // Reset DHT11
return DHT11_Check();// wait for DHT11 The response of the
}
main
int main(void)
{
u8 t=0;
u8 temperature;
u8 humidity;
delay_init(); // Delay function initialization
NVIC_PriorityGroupConfig(NVIC_PriorityGroup_2);// Set interrupt priority group to group 2:2 Bit preemption priority ,2 Bit response priority
uart_init(115200); // The serial port is initialized to 115200
LED_Init(); // Initialization and LED Connected hardware interface
LCD_Init(); // initialization LCD
POINT_COLOR=RED; // Set the font to red
while(DHT11_Init()) //DHT11 initialization
{
LCD_ShowString(30,130,200,16,16,"DHT11 Error");
delay_ms(200);
LCD_Fill(30,130,239,130+16,WHITE);
delay_ms(200);
}
LCD_ShowString(30,130,200,16,16,"DHT11 OK");
POINT_COLOR=BLUE;// Set the font to blue
LCD_ShowString(30,150,200,16,16,"Temp: C");
LCD_ShowString(30,170,200,16,16,"Humi: %");
while(1)
{
if(t%10==0) // Every time 100ms Read once
{
DHT11_Read_Data(&temperature,&humidity); // Read the temperature and humidity values
LCD_ShowNum(30+40,150,temperature,2,16); // Show the temperature
LCD_ShowNum(30+40,170,humidity,2,16); // Show humidity
}
delay_ms(10);
t++;
if(t==20)
{
t=0;
LED0=!LED0;
}
}
}
边栏推荐
- Skywalking integrated Nacos dynamic configuration
- 码蹄集 - MT3149 · AND - 数据不是很强,暴力剪枝就能骗AC
- Database problems, how to optimize Oracle SQL query statements faster and more efficient
- 从诺奖知“边缘计算”的未来!
- Geoffrey Hinton: my 50 years of in-depth study and Research on mental skills
- 云盘里资料被和谐了,怎么办?
- Cjc8988 Low Power Stereo codec with 2 stereo headphone drivers
- SOE spatial analysis server MySQL and PostGIS geospatial database of Postgres anti injection attack
- Why use huluer pie disk instead of U disk?
- DEV XPO对比之XAF BO
猜你喜欢

LeetCode 最大矩形,最大正方形系列 84. 85. 221. 1277. 1725. (单调栈,动态规划)

可动的机械挂钟

DHT11 温湿度传感器

El tooltip in the table realizes line breaking display

Build 2022 上开发者最应关注的七大方向主要技术更新

从诺奖知“边缘计算”的未来!

excel动态图表

Know the future of "edge computing" from the Nobel prize!

Database problems, how to optimize Oracle SQL query statements faster and more efficient

芯片,建立在沙粒上的帝国!
随机推荐
[note] e-commerce order data analysis practice
输入一个表达式(用字符串表示),求这个表达式的值。
restframework-simpleJWT重写认证机制
SOE spatial analysis server MySQL and PostGIS geospatial database of Postgres anti injection attack
解决麒麟V10上传文件乱码问题
excel动态图表
葫芦儿 APP 使用帮助
This is the necessary software for college students 𞓜 knowledge management
68 cesium code datasource loading czml
穿越派·派盘 + 思源笔记 = 私人笔记本
数据库er图组成要素
Small guide for rapid completion of mechanical arm (VI): stepping motor driver
LED lighting used in health lighting
Seven major technical updates that developers should pay most attention to on build 2022
芯片,建立在沙粒上的帝国!
Some errors encountered in MySQL data migration
OpenGL es: (3) EGL, basic steps of EGL drawing, eglsurface, anativewindow
TiDB单机模拟部署生产环境集群(闭坑实践,亲测有效)
srpingboot security demo
千万不要把笔记视频乱放!