当前位置:网站首页>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;
}
}
}
边栏推荐
- freeswitch拨打分机号
- SystemVerilog学习-10-验证量化和覆盖率
- The row and column numbers of each pixel of multi-source grid data in the same area are the same, that is, the number of rows and columns are the same, and the pixel size is the same
- 为了保护自己的数据,他奋斗了一天一夜
- 相同区域 多源栅格数据 各个像元行列号一致,即行数列数相同,像元大小相同
- Skywalking integrated Nacos dynamic configuration
- 机械臂速成小指南(六):步进电机驱动器
- Primary application case of Excel DuPont analyzer
- My experience from technology to product manager
- srpingboot security demo
猜你喜欢
![kotlin位运算的坑(bytes[i] and 0xff 报错)](/img/2c/de0608c29d8af558f6f8dab4eb7fd8.png)
kotlin位运算的坑(bytes[i] and 0xff 报错)

相同区域 多源栅格数据 各个像元行列号一致,即行数列数相同,像元大小相同

这才是大学生必备软件 | 知识管理

excel动态图表

数据库问题,如何优化Oracle SQL查询语句更快,效率更高

How to transmit and share 4GB large files remotely in real time?

【文件系统】如何在ubi之上运行squashfs

Crossing sect · paipan + Siyuan notes = private notebook

Advanced cross platform application development (III): online resource upgrade / hot update with uni app

表格中el-tooltip 实现换行展示
随机推荐
SQL必会题之留存率
健康照明中应用的LED照明灯
穿越派·派盘 + 思源笔记 = 私人笔记本
无限水平大理石游戏
Orcle创建用户+角色
excel初级应用案例——杜邦分析仪
excel動態圖錶
Bat operation FTP upload and download command
指数法和Random Forest实现山东省丰水期地表水体信息
MySQL里记录货币
数据库er图组成要素
芯片,建立在沙粒上的帝国!
Retention rate of SQL required questions
[note] e-commerce order data analysis practice
Ucosiii --- engineering transplantation
OpenGL es: (5) basic concepts of OpenGL, the process of OpenGL es generating pictures on the screen, and OpenGL pipeline
DEV XPO对比之XAF BO
Geoffrey Hinton: my 50 years of in-depth study and Research on mental skills
It's not that you have a bad mind, but that you haven't found the right tool
Build 2022 上开发者最应关注的七大方向主要技术更新