Measuring temperature and humidity with Raspberry Pi
To measure temperature with Raspberry Pi, you'll need DHT-11 sensor. This sensor is also capable of measuring humidity of the air. There is also improved version DHT-22 which offers more accuracy. This tutorial covers DHT-11 only.
Setup
Physical PIN 1 (3.3 V) goes to + on the sensor; PIN 6 is ground
Data goes to PIN 7 (BCM 4 or GPIO7) - you need to define DHTPIN 7 in the c code!
In some setups there is 10k ohm resistor between + and Data. I have no idea why.
+-----------+ PIN 1 (3.3 V)-----------------|+ | PIN 7 (BCM4 or GPIO7)---------|D DHT11 | PIN 6 (Ground)----------------|- | +-----------+
The python script that came with Raspberry always finished with ERR_RANGE error. Also the received sensor data was kinda corrupt (I could not found the CRC). Googling around revealed that Python on Raspberry is not fast enough to receive all data sent from sensor. So I tried to use the C code.
$ nano dht11.c
/*
* File name : dht11.c
* Description : Acquire temperature and humidity.
* Website : www.adeept.com
* E-mail : support@adeept.com
* Author : Jason
* Date : 2015/06/21
*/
#include <wiringPi.h>
#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>
#define MAXTIMINGS 85
#define DHTPIN 7
int dht11_dat[5] = {0,0,0,0,0};
void read_dht11_dat()
{
uint8_t laststate = HIGH;
uint8_t counter = 0;
uint8_t j = 0, i;
float f;
dht11_dat[0] = dht11_dat[1] = dht11_dat[2] = dht11_dat[3] = dht11_dat[4] = 0;
// pull pin down for 18 milliseconds
pinMode(DHTPIN, OUTPUT);
digitalWrite(DHTPIN, LOW);
delay(18);
// then pull it up for 40 microseconds
digitalWrite(DHTPIN, HIGH);
delayMicroseconds(40);
// prepare to read the pin
pinMode(DHTPIN, INPUT);
// detect change and read data
for ( i=0; i< MAXTIMINGS; i++) {
counter = 0;
while (digitalRead(DHTPIN) == laststate) {
counter++;
delayMicroseconds(1);
if (counter == 255) {
break;
}
}
laststate = digitalRead(DHTPIN);
if (counter == 255) break;
// ignore first 3 transitions
if ((i >= 4) && (i%2 == 0)) {
// shove each bit into the storage bytes
dht11_dat[j/8] <<= 1;
if (counter > 16)
dht11_dat[j/8] |= 1;
j++;
}
}
// check we read 40 bits (8bit x 5 ) + verify checksum in the last byte
// print it out if data is good
if ((j >= 40) &&
(dht11_dat[4] == ((dht11_dat[0] + dht11_dat[1] + dht11_dat[2] + dht11_dat[3]) & 0xFF)) ) {
f = dht11_dat[2] * 9. / 5. + 32;
printf("Humidity = %d.%d %% Temperature = %d.%d *C (%.1f *F)\n",
dht11_dat[0], dht11_dat[1], dht11_dat[2], dht11_dat[3], f);
}
else
{
printf("Data not good, skip\n");
}
}
int main (void)
{
printf ("Raspberry Pi wiringPi DHT11 Temperature test program\n") ;
if (wiringPiSetup () == -1)
exit (1) ;
while (1)
{
read_dht11_dat();
delay(1000); // wait 1sec to refresh
}
return 0 ;
}
Compile the c code:
$ sudo gcc dht11.c -lwiringPi
Run the generated executable:
$ sudo ./a.out
The result:
Raspberry Pi wiringPi DHT11 Temperature test program
Data not good, skip
Humidity = 19.0 % Temperature = 24.0 *C (75.2 *F)
Humidity = 19.0 % Temperature = 24.0 *C (75.2 *F)
Data not good, skip
Humidity = 19.0 % Temperature = 24.0 *C (75.2 *F)
Humidity = 19.0 % Temperature = 24.0 *C (75.2 *F)
...
The C code occasionally still fails to read the data. With Python there was 0% success.