How to Interface DHT 11 with Arduino

Introduction:

DHT 11 is a sensor which gives temperature and humidity of the air molecules. It is an 3 pin Module.

VCC – 5v
D0 – digital out (sensor values transmit from this pin)
GND – ground

Requirements:

Arduino UNO – 1
DHT11 – 1
Jumper wires

Working;

D0 pin is treated as input and output it means to activate the sensor we have to give the HIGH pulse of 20ms of time then sensor gets activated and the sensor produces 40 pulses for each pulse we can denote as 1 bit so, totally we get 40 bits of data.

Here, starting 16 bits of data represents humidity (i.e., MSB of 40 bits) , next 16 bits are temperature and last 8 bits represents parity of starting 32 bits. So, we should do error checking with the parity value  and sum of 4 (values)* 8 bits. If the result is equal with the parity value then received data is correct or else incorrect then we have repeat the procedure and read the data again.

Below image shows the sensor data representation and error checking with the parity value.

Sensor Data

Example program to read the data from DHT11:

  1. #define data 8
  2. int z;
  3. unsigned long duration=0;
  4. unsigned char i[5];
  5. unsigned char value=0;
  6. unsigned answer=0;
  7. void setup()
  8. {
  9.    Serial.begin (9600);
  10.    pinMode(data,OUTPUT);
  11. }
  12. void loop()
  13. {
  14.   digitalWrite(data,LOW);
  15.   delay(20);
  16.   digitalWrite(data,HIGH);
  17.   pinMode(data,INPUT_PULLUP);
  18.   duration=pulseIn(data, LOW);
  19.   if(duration = 72)
  20.   {
  21.       while(1)
  22.       {
  23.         duration=pulseIn(data, HIGH);
  24.         if(duration = 18)
  25.         value=0;
  26.         else if(duration = 48)
  27.         value=1;
  28.         else if(z==40)
  29.         break;
  30.         i[z/8]|=value<<(7- (z%8));
  31.         z++;
  32.       }
  33.     }
  34. answer=i[0]+i[1]+i[2]+i[3];
  35. if(answer==i[4] && answer!=0)
  36. {
  37.    Serial.print(“Temp=”);
  38.    Serial.println (i[2]);
  39.    Serial.print(“Humdity=”);
  40.    Serial.println (i[0]);
  41. }
  42. z=0;
  43. i[0]=i[1]=i[2]=i[3]=i[4]=0;
  44.   delay(1000);
  45.   pinMode(data,OUTPUT);
  46. }

Firstly,  we have to configure the serial transmission and initializing the data pin as output in the setup() function.

In loop() function made data pin HIGH with of 20ms of time duration then sensor will transmits the data of 40 pulses with starting pulse. After starting pulse we get the actual pulses so, here pulses are of 2 combination one pulse width represents the bit ‘1’ and other is ‘0’. For each pulse we are storing a respective bit in value in the character array (i[5]).

i[0] = high humidity
i[1] = low humidity
i[2] = high temperature
i[3] = low temperature
i[4] = parity.

You may also like...

Leave a Reply

Your email address will not be published. Required fields are marked *

%d bloggers like this: