DHT11 is a 4 pin sensor which can measure temperatures ranging from 0-50°C & relative humidity ranging from 20-95%.The sensor uses its own proprietary 1-wire protocol to communicate with Raspberry Pi and runs from 3.3V-5V. The timings must be precise and according to the datasheet of the sensor.
Raspberry Pi initiates the data transmission process by pulling the data bus low for about 18 ms and keeps it HIGH for about 20-40 μs before releasing it.Subsequently, the sensor responds to the Pi's data transfer request by pulling the data bus LOW for 80 μs followed by 80 μs of HIGH.At this point Pi is ready to receive data from the sensor.Data is sent in packet of 40 bits (5 bytes) via the data line with the most significant bit at the beginning.
Data is transmitted in the following order:- Integer Part of Relative Humidity--->Decimal Part of Relative Humidity--->Integer Part of Temperature--->Decimal Part of Temperature---> Checksum. Checksum consists the last 8 bits of each part. Transmission of '0' & '1' is done by varying the width of the pulse.For transmitting '0' the data bus is held HIGH for 26-28μs, and 70μs for transmitting '1'.A delay of 50μs(LOW) is introduced before any new data bit is transmitted.After the transmission of last data-bit the data line is held LOW for 50μs and released.
Circuit:-
Holding the DHT11 towards you (the one with grid), the left pin is connected to VCC (pin 1).The data pin is next after VCC and is connected to pin 7.Next pin is NC(no connection).Finally the last pin is connected to GND(pin 25).To prevent random data connect a 10K resistor between data and VCC pin of DHT11.
Software:-
WiringPi which uses C like Arduino language is used to read the sensor value. WiringPi is maintained under GIT for ease of change tracking.If you do not have GIT installed, then under any of the Debian releases, you can install it with-
sudo apt-get install git-core
To obtain WiringPi using GIT:
git clone git://git.drogon.net/wiringPi
If you have already used the clone operation for the first time, then
cd wiringPi
git pull origin
Will fetch an updated version then you can re-run the build script below.
To build/install there is a new simplified script:
cd wiringPi
./build
Save the below code as temp_rh_sensor.c...
Raspberry Pi initiates the data transmission process by pulling the data bus low for about 18 ms and keeps it HIGH for about 20-40 μs before releasing it.Subsequently, the sensor responds to the Pi's data transfer request by pulling the data bus LOW for 80 μs followed by 80 μs of HIGH.At this point Pi is ready to receive data from the sensor.Data is sent in packet of 40 bits (5 bytes) via the data line with the most significant bit at the beginning.
Data is transmitted in the following order:- Integer Part of Relative Humidity--->Decimal Part of Relative Humidity--->Integer Part of Temperature--->Decimal Part of Temperature---> Checksum. Checksum consists the last 8 bits of each part. Transmission of '0' & '1' is done by varying the width of the pulse.For transmitting '0' the data bus is held HIGH for 26-28μs, and 70μs for transmitting '1'.A delay of 50μs(LOW) is introduced before any new data bit is transmitted.After the transmission of last data-bit the data line is held LOW for 50μs and released.
Software:-
WiringPi which uses C like Arduino language is used to read the sensor value. WiringPi is maintained under GIT for ease of change tracking.If you do not have GIT installed, then under any of the Debian releases, you can install it with-
sudo apt-get install git-core
To obtain WiringPi using GIT:
git clone git://git.drogon.net/wiringPi
If you have already used the clone operation for the first time, then
cd wiringPi
git pull origin
Will fetch an updated version then you can re-run the build script below.
To build/install there is a new simplified script:
cd wiringPi
./build
Save the below code as temp_rh_sensor.c...
#include <wiringPi.h>
#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>
#define MAX_TIME 85
#define DHT11PIN 7
int dht11_val[5]={0,0,0,0,0};
void dht11_read_val()
{
uint8_t lststate=HIGH;
uint8_t counter=0;
uint8_t j=0,i;
float farenheit;
for(i=0;i<5;i++)
dht11_val[i]=0;
pinMode(DHT11PIN,OUTPUT);
digitalWrite(DHT11PIN,LOW);
delay(18);
digitalWrite(DHT11PIN,HIGH);
delayMicroseconds(40);
pinMode(DHT11PIN,INPUT);
for(i=0;i<MAX_TIME;i++)
{
counter=0;
while(digitalRead(DHT11PIN)==lststate){
counter++;
delayMicroseconds(1);
if(counter==255)
break;
}
lststate=digitalRead(DHT11PIN);
if(counter==255)
break;
// top 3 transistions are ignored
if((i>=4)&&(i%2==0)){
dht11_val[j/8]<<=1;
if(counter>16)
dht11_val[j/8]|=1;
j++;
}
}
// verify cheksum and print the verified data
if((j>=40)&&(dht11_val[4]==((dht11_val[0]+dht11_val[1]+dht11_val[2]+dht11_val[3])& 0xFF)))
{
farenheit=dht11_val[2]*9./5.+32;
printf("Humidity = %d.%d %% Temperature = %d.%d *C (%.1f *F)\n",dht11_val[0],dht11_val[1],dht11_val[2],dht11_val[3],farenheit);
}
else
printf("Invalid Data!!\n");
}
int main(void)
{
printf("Interfacing Temperature and Humidity Sensor (DHT11) With Raspberry Pi\n");
if(wiringPiSetup()==-1)
exit(1);
while(1)
{
dht11_read_val();
delay(3000);
}
return 0;
}
Compile the code as...
gcc -o sensor temp_rh_sensor.c -L/usr/local/lib -lwiringPi
Now execute it...
sudo ./sensor
[ Humidity = 87.0 % Temperature = 32.2 *C (90.0 *F) ]








