当前位置:网站首页>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;
}
}
}
边栏推荐
- 论文学习记录随笔 多标签之GLOCAL
- Advanced drawing skills of Excel lecture 100 (1) - use Gantt chart to show the progress of the project
- 云盘里资料被和谐了,怎么办?
- 码蹄集 - MT3149 · AND - 数据不是很强,暴力剪枝就能骗AC
- 从诺奖知“边缘计算”的未来!
- 相同区域 多源栅格数据 各个像元行列号一致,即行数列数相同,像元大小相同
- bat操作ftp上传下载命令
- Don't put your notes and videos everywhere!
- 68 cesium code datasource loading czml
- 码蹄集 - MT3114 · 有趣的平衡 - 用样例通俗地讲解
猜你喜欢

喊我们大学生个人云服务特供商

SystemVerilog学习-08-随机约束和线程控制

three.js小结

Preliminary level of C language -- selected good questions on niuke.com

Linux closes the redis process SYSTEMd+

PLA不粘贴在床上:6个简单的解决方案

基于LabVIEW的计时器

芯片,建立在沙粒上的帝国!

TiDB单机模拟部署生产环境集群(闭坑实践,亲测有效)

It's not that you have a bad mind, but that you haven't found the right tool
随机推荐
Call us special providers of personal cloud services for College Students
Thoughts on a "01 knapsack problem" expansion problem
云盘里资料被和谐了,怎么办?
π disk, turning your computer into a personal private cloud
OpenGL ES: (1) OpenGL ES的由来 (转)
El tooltip in the table realizes line breaking display
ArcServer密码重置(账号不可以重置)
Orcle创建用户+角色
Why use huluer pie disk instead of U disk?
Bat operation FTP upload and download command
Crossing pie · pie pan + Mountain duck = local data management
Continue to learn MySQL
数据库er图组成要素
Oracle 序列+触发器
基于LabVIEW的计时器
LED lighting used in health lighting
Some errors encountered in MySQL data migration
What if the data in the cloud disk is harmonious?
TiDB单机模拟部署生产环境集群(闭坑实践,亲测有效)
Scope data export mat