Hi there!
I've been using the DHT11 sensor a lot and I spent time making the code its time. So I made a sketch example so anyone can use it.
I've been hoping that you can post my code to the examples, because its frustrating for new arduino users to use the DHT11 example code directly.
It also is very slow (2seconds delay in each loop).
So I made a sketch to match all your needs.
It reads the sensor every 2 seconds without delays.
The user can also use the temperature and humidity values directly as an integer and compare it to other values without making new arrays.
Please check it out and let me know if you find any errors.
/* DHT11 Example Sketch Code for reading the sensor without delay on your program!
Example Code by: Nick Athanasoulas
Date: 27/5/2012
FUNCTION: It reads the sensor every 2 seconds without delays.
The user can also use the temperature and humidity values directly as an integer
and compare it to other values without making new arrays.
*/
#define DHT11_PIN 4 // ADC0 Define the ANALOG Pin connected to DHT11 Sensor
int temp1[3]; //Temp1, temp2, hum1 & hum2 are the final integer values that you are going to use in your program.
int temp2[3]; // They update every 2 seconds.
int hum1[3];
int hum2[3];
byte read_dht11_dat()
{
byte i = 0;
byte result=0;
for(i=0; i< 8; i++){
while(!(PINC & _BV(DHT11_PIN))); // wait for 50us
delayMicroseconds(30);
if(PINC & _BV(DHT11_PIN))
result |=(1<<(7-i));
while((PINC & _BV(DHT11_PIN))); // wait '1' finish
}
return result;
}
long dht11delay_previousMillis = 0; // will store last time LED was updated
long dht11delay_interval = 1000; // dht11delay_interval at which to blink (milliseconds)
void setup()
{
DDRC |= _BV(DHT11_PIN);
PORTC |= _BV(DHT11_PIN);
Serial.begin(9600);
Serial.println("DHT11 without delay");
Serial.println("Example code by: Nick Athanasoulas");
Serial.println("Ready");
delay(1000);
}
void loop()
{
unsigned long dht11delay_currentMillis = millis();
if(dht11delay_currentMillis - dht11delay_previousMillis > dht11delay_interval) {
// save the last time you blinked the LED
dht11delay_previousMillis = dht11delay_currentMillis;
byte dht11_dat[5];
byte dht11_in;
byte i;
// start condition
// 1. pull-down i/o pin from 18ms
PORTC &= ~_BV(DHT11_PIN);
delay(18);
PORTC |= _BV(DHT11_PIN);
delayMicroseconds(40);
DDRC &= ~_BV(DHT11_PIN);
delayMicroseconds(40);
dht11_in = PINC & _BV(DHT11_PIN);
if(dht11_in){
Serial.println("dht11 start condition 1 not met");
return;
}
delayMicroseconds(80);
dht11_in = PINC & _BV(DHT11_PIN);
if(!dht11_in){
Serial.println("dht11 start condition 2 not met");
return;
}
delayMicroseconds(80);
// now ready for data reception
for (i=0; i<5; i++)
dht11_dat[i] = read_dht11_dat();
DDRC |= _BV(DHT11_PIN);
PORTC |= _BV(DHT11_PIN);
byte dht11_check_sum = dht11_dat[0]+dht11_dat[1]+dht11_dat[2]+dht11_dat[3];
// check check_sum
if(dht11_dat[4]!= dht11_check_sum)
{
Serial.println("DHT11 checksum error");
}
temp1[0]=dht11_dat[2];
temp2[0]=dht11_dat[3];
Serial.print("Temperature: ");
Serial.print(temp1[0]);
Serial.print(".");
Serial.print(temp2[0]);
Serial.print(" C");
Serial.print(" ");
hum1[0]=dht11_dat[0];
hum2[0]=dht11_dat[1];
Serial.print("Humidity: ");
Serial.print(hum1[0]);
Serial.print(".");
Serial.print(hum2[0]);
Serial.println("%");
}
}
// END OF CODE