DHT11 digital temperature and humidity sensor is a composite Sensor contains a calibrated digital signal output of the temperature and humidity. The sensor includes a resistive sense of wet components and an NTC temperature measurement device and connected with a high-performance 8-bit microcontroller.
RT: Typical application circuit recommended in the short cable length of 20m on the 5.1kΩ pull-up register, the resistance of greater than 20m under the pull-up resistor on the lower actual situation. To get the temperature and humidity data we need 3 steps. Let’s look at all steps one by one
Here the pulse width of 26µs ~ 28µs indicates a bit 0 and 70µs represent bit 1. We measure the pulse using External Interrupt and timer. We enable external interrupt at any edge. Let’s look at the program below for simplicity-
void init_dht11(void)
{
DDRD|=(1<<DDD2); //DD2 for read data
PORTD|=(1<<PORTD2); //Output high
PORTD&=~(1<<PORTD2); //Ouput Low for 18ms
_delay_ms(18);
PORTD|=(1<<PORTD2); //Output high for relise bus
DDRD&=~(1<<DDD2); //Input sate declear for interrupt triger
while(PIND&(1<<PIND2));//Wait for pull the data line low
while(!(PIND&(1<<PIND2)));//wait 80us low
while(PIND&(1<<PIND2)); //wait 80us high
while(!(PIND&(1<<PIND2)));//wait 50us low
sei();
//SREG|=(1<<I); //Global interrupt enable
GICR |=(1<<INT0); //External Interrupt Request 0 Enable
MCUCR|=(1<<ISC00); //any edge of INT0 generates an interrupt request
}
Let’s use timer0 with prescaler 8. So the new frequency & time of timer0 is
ƒtimer0 = 8MHz/8 = 1MHz & Ttimer0 = 1µs
TCNT0 value will determine the pulse width value.
Look at the interrupt service routine function for more details –
uint8_t start_flag=1,millisecond;
/**** Interrupt service routine ********************/
ISR(INT0_vect)
{
if(start_flag){
pulse=TCNT0;
TCCR0=0; // Timer stop
if(pulse<30) sample[count]=0;
else if(pulse>50 && pulse<75) sample[count]=1;
count++;
start_flag=0;
}else{ // start timer with flag clear
TCCR0|=(1<<CS01); //prescaler 8
TCNT0=0; //intialize timer value
start_flag=1;
}
if(count>40)
{
count=0;
start_flag=1;
}
}
We have found the bit, now it is time for calculation-
Constant variable: const is used with a data type declaration or definition to specify an unchanging value.
- Example- const int five=5;
Const objects may not be changed.
Volatile variable: Volatile specifies a variable whose value may be changed by process outside the current program. One example of a volatile object might be a buffer used to exchange data with an eternal device.
In our program we declare volatile uint16_t data_packet[40]; and volatile float humidity,temperature; global variable which can be used in any .c file.
Let’s back to our main program. Sensor transmit a total of 40bits containing the temperature and humidity.
So that the humidity is byte1+byte2*0.1 and in temperature calculation the 7th bit of fractional data byte represent the positive and negative sign. If 7th bit is 1 temperature is positive and if it is 0 then temperature is positive.
/************************************************************************************************
*************************************************************************************************
This program will read the temperature and humidity data and display in LCD
The value sould be update in every 5s
*************************************************************************************************
Subeer Kumar Sarkar
Electrical & Electronic Engineer
************************************************************************************************
************************************************************************************************
***********************************************************************************************/
#include<avr/io.h>
#include<util/delay.h>
#include<avr/interrupt.h>
#include"dht11.h"
#include"lcd.h"
char str[40],deg=223,hum=37;
int main(void)
{
LCD_INIT(); //LCD intilize
LCD_write_string(1,1,"Temp and Humidity");
timer2_250ms();
init_dht11();
_delay_ms(1000);
while(1)
{
for(uint8_t i=0;i<8;i++)
{
humidity|=(sample[i]<<(7-i));
temperature|=(sample[16+i]<<(7-i));
}
sprintf(str,"HR:%02d%c T:%02d%cC ",humidity,hum,temperature,deg);
LCD_write_string(1,2,str);
temperature=0;humidity=0;
_delay_ms(1000); //delay for LCD update
}
return 0;
}
Here timer2 is initialized at 250ms to call sensor at every 5s. So the data will be update in every 5s. One interesting thing that we use %02d to display only 2digit of temperature and humidity data.