当前位置:网站首页>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;
}
}
}
边栏推荐
猜你喜欢

机械臂速成小指南(六):步进电机驱动器

Some errors encountered in MySQL data migration

ArcServer密码重置(账号不可以重置)

表格中el-tooltip 实现换行展示

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

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

论文学习记录随笔 多标签之LSML

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

Arcserver password reset (account cannot be reset)

Advanced cross platform application development (II): uni app practice
随机推荐
LED lighting used in health lighting
[note] e-commerce order data analysis practice
Oracle sequence + trigger
Chip, an empire built on sand!
OpenGL es: (3) EGL, basic steps of EGL drawing, eglsurface, anativewindow
3D printer threading: five simple solutions
jdbc-连接池
Preliminary level of C language -- selected good questions on niuke.com
69 cesium code datasource loading geojson
Debug details under pycharm
4GB大文件,如何实时远程传输和共享?
健康照明中应用的LED照明灯
穿越派·派盘 + Mountain Duck = 数据本地管理
Talking from mlperf: how to lead the next wave of AI accelerator
68 Cesium代码datasource加载czml
Pla ne colle pas sur le lit: 6 solutions simples
excel可视化
Ucosiii --- engineering transplantation
SOE spatial analysis server MySQL and PostGIS geospatial database of Postgres anti injection attack
2022 年面向初学者的 10 大免费 3D 建模软件