当前位置:网站首页>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;
}
}
}
边栏推荐
- PLA不粘貼在床上:6個簡單的解决方案
- 【笔记】电商订单数据分析实战
- Continuous breakthrough and steady progress -- Review and Prospect of cross platform development technology of mobile terminal
- Some errors encountered in MySQL data migration
- srpingboot security demo
- Retention rate of SQL required questions
- OpenGL ES: (1) OpenGL ES的由来 (转)
- My experience from technology to product manager
- What if the data in the cloud disk is harmonious?
- SQL必会题之留存率
猜你喜欢
随机推荐
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
OpenGL ES: (4) EGL API详解 (转)
【文件系统】如何在ubi之上运行squashfs
Excel dynamic chart
码蹄集 - MT3149 · AND - 数据不是很强,暴力剪枝就能骗AC
论文学习记录随笔 多标签之GLOCAL
SystemVerilog学习-06-类的封装
SOE空间分析服务器 MySQL以及PostGres的地理空间库PostGIS防注入攻击
excel動態圖錶
芯片,建立在沙粒上的帝国!
利用百度地图查询全国地铁线路
FPGA - 7系列 FPGA内部结构之Clocking -02- 时钟布线资源
Code shoe set - mt3149 · and - the data is not very strong. Violent pruning can deceive AC
QT write custom control - self drawn battery
浏览器端保存数据到本地文件
Infinite horizontal marble game
3D打印机穿线:5种简单的解决方案
Advanced cross platform application development (II): uni app practice
从诺奖知“边缘计算”的未来!
扩展点系列之SmartInstantiationAwareBeanPostProcessor确定执行哪一个构造方法 - 第432篇








