当前位置:网站首页>Arduino temperature and humidity sensor DHT11 (including code)

Arduino temperature and humidity sensor DHT11 (including code)

2022-06-23 05:10:00 Loading_ create

 Insert picture description here

Product introduction

In our daily life , Temperature and humidity have a great influence on our life , Especially for factories The production of , If we can't master and take relevant measures , Then it will bring great losses , But now it's all right , There is a sensor which can measure not only temperature but also humidity , That can really solve our troubles . Okay , Let's learn how to use it , Let it bring convenience to your life .

Technical parameters

Supply voltage : 3.3~5.5V DC
transport Out : Single bus digital signal
measuring range : humidity 20-90%RH, temperature 0~50℃ Measurement accuracy : humidity ±5%RH, temperature ±2℃
The resolution of the : humidity 1%RH, temperature 1℃ Long term stability : <±1%RH/ year

connection

  • Modular “+” Termination +5V Output
  • “-” Termination GND
  • “S” Terminate the digital port 7 Pin No ( When
    However, you can also define the digital pin by yourself ); The connection is simple , Now there is the testing phase ......
     Insert picture description here
int DHpin = 8; 
byte dat[5]; 
byte read_data(){
    
	byte data;
	for(int i=0; i<8; i++){
    
		if(digitalRead(DHpin) == LOW){
    
			while(digitalRead(DHpin) == LOW);	// wait for  50us;
			delayMicroseconds(30);	// Judge the duration of high level , To determine whether the data is ‘0’ still ‘1’; if(digitalRead(DHpin) == HIGH)
			data |= (1<<(7-i));	// High in the former , Low in the back ;
			while(digitalRead(DHpin) == HIGH);	// data ‘1’, Wait for the next bit to receive ;
		}
	}
	return data;
}

void start_test()
{
    
	digitalWrite(DHpin,LOW);	// Pull down the bus , Send a start signal ; delay(30); // The delay should be greater than  18ms, In order to  DHT11  Can detect the start signal ; digitalWrite(DHpin,HIGH);
	delayMicroseconds(40);	// wait for DHT11  Respond to ; pinMode(DHpin,INPUT); while(digitalRead(DHpin) == HIGH);
	delayMicroseconds(80);	//DHT11  Respond , Pull down the bus  80us; if(digitalRead(DHpin) == LOW);
	delayMicroseconds(80);	//DHT11  Pull up the bus  80us  Start sending data after ;
	for(int i=0;i<4;i++)	// Receive temperature and humidity data , The check bit does not consider ; dat[i] = read_data();
	pinMode(DHpin,OUTPUT);
	digitalWrite(DHpin,HIGH);	// Release the bus after sending data once , Wait for the next start signal from the host ;
}

void setup()
{
    
	Serial.begin(9600); pinMode(DHpin,OUTPUT);
}

void loop()

{
    
	start_test();
	
	Serial.print("Current humdity = "); Serial.print(dat[0], DEC);	// Displays the integer digits of humidity ; Serial.print('.');
	Serial.print(dat[1],DEC);	// Decimal places for humidity ; Serial.println('%');
	Serial.print("Current temperature = "); Serial.print(dat[2], DEC);	// Displays the integer digits of the temperature ; Serial.print('.');
	Serial.print(dat[3],DEC);	// Display decimal places of temperature ; Serial.println('C');
	delay(700);
}

Okay , Let's compile the test code , After the compilation, we can see the results , I really want to see What is the temperature and humidity in the environment , They are invisible and untouchable , We burn the program into Arduino The board , Then I can't wait to open Serial Monitor window , see , And here it is , wow , Is it a little excited !
 Insert picture description here

原网站

版权声明
本文为[Loading_ create]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/174/202206230129230715.html