Introduction:

DHT 11 is a sensor which gives the temperature and humidity of the air molecules. It is a 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 which means to activate the sensor we have to give the HIGH pulse of 20ms of time then the sensor gets activated and the sensor produces 40 pulses for each pulse we can denote it as 1 bit so, total we get 40 bits of data.

Here, starting 16 bits of data represent humidity (i.e., MSB of 40 bits), the next 16 bits are temperature and the last 8 bits represent 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 to the parity value then the received data is correct or else incorrect then we have to repeat the procedure and reread the data.

The 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 initialise the data pin as output in the setup() function.

In loop() function made the data pin HIGH with 20ms of time duration then the sensor will transmit the data of 40 pulses with starting pulse. After starting the pulse we get the actual pulses so, here pulses are of 2 combinations one pulse width represents the bit ‘1’ and the other is ‘0’. For each pulse, we store 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.

Leave a Reply

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