/* Abundance DHT Based on: https://www.electronicwings.com/avr-atmega/dht11-sensor-interfacing-with-atmega16-32 modified 126 June 2021 by Jean-Luc Pierite */ //KY015 DHT11 Temperature and humidity sensor int DHpin = 21; uint8_t I_RH,D_RH,I_Temp,D_Temp,CheckSum; 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); // determine the duration of the high level to determine the data is '0 'or '1' if (digitalRead (DHpin) == HIGH) _data = (_data<<1)|(0x01); /* then its logic HIGH */ else /* otherwise its logic LOW */ _data = (_data<<1); while (digitalRead (DHpin) == HIGH); // data '1 ', wait for the next one receiver } } return _data; } void start_test () { // Request pinMode (DHpin, OUTPUT); digitalWrite (DHpin, LOW); // bus down, send start signal delay (20); // delay greater than 18ms, so DHT11 start signal can be detected digitalWrite (DHpin, HIGH); // Response pinMode (DHpin, INPUT); while (digitalRead (DHpin) == HIGH); while (digitalRead (DHpin) == LOW); while (digitalRead (DHpin) == HIGH); I_RH = read_data(); /* store first eight bit in I_RH */ D_RH = read_data(); /* store next eight bit in D_RH */ I_Temp = read_data(); /* store next eight bit in I_Temp */ D_Temp = read_data(); /* store next eight bit in D_Temp */ CheckSum = read_data(); } void setup() { Serial.begin (9600); delay(2000); // unstable period } void loop() { start_test (); if ((I_RH + D_RH + I_Temp + D_Temp) != CheckSum) { Serial.println ("parity failed"); } else { Serial.print ("Current humdity = "); Serial.print (I_RH, DEC); // display the humidity-bit integer; Serial.print ('.'); Serial.print (D_RH, DEC); // display the humidity decimal places; Serial.println ('%'); Serial.print ("Current temperature = "); Serial.print (I_Temp, DEC); // display the temperature of integer bits; Serial.print ('.'); Serial.print (D_Temp, DEC); // display the temperature of decimal places; Serial.println ('C'); } delay (5000); }