Sei sulla pagina 1di 5

Uso del sensor de humedad y

temperatura DHT11
Uso del
DHT11 is a 4 pin sensor which can measure temperatures ranging from 0-50C & 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 HIGHfor 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 2628s, and 70s for transmitting '1'.A delay of 50s(LOW) is introduced before any new data
bit is transmitted.After the transmission of last data-bit the data line is held LOW for 50s
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 withsudo 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...
view plainprint?

1.

#include <wiringPi.h>

2.

#include <stdio.h>

3.

#include <stdlib.h>

4.

#include <stdint.h>

5.

#define MAX_TIME 85

6.

#define DHT11PIN 7

7.

int dht11_val[5]={0,0,0,0,0};

8.
9.

void dht11_read_val()

10. {
11.

uint8_t lststate=HIGH;

12.

uint8_t counter=0;

13.

uint8_t j=0,i;

14.

float farenheit;

15.

for(i=0;i<5;i++)

16.

dht11_val[i]=0;

17.

pinMode(DHT11PIN,OUTPUT);

18.

digitalWrite(DHT11PIN,LOW);

19.

delay(18);

20.

digitalWrite(DHT11PIN,HIGH);

21.

delayMicroseconds(40);

22.

pinMode(DHT11PIN,INPUT);

23.

for(i=0;i<MAX_TIME;i++)

24.

25.

counter=0;

26.

while(digitalRead(DHT11PIN)==lststate){

27.

counter++;

28.

delayMicroseconds(1);

29.

if(counter==255)

30.

break;

31.

32.

lststate=digitalRead(DHT11PIN);

33.

if(counter==255)

34.

break;

35.

// top 3 transistions are ignored

36.

if((i>=4)&&(i%2==0)){

37.

dht11_val[j/8]<<=1;

38.

if(counter>16)

39.

dht11_val[j/8]|=1;

40.
41.

j++;
}

42.

43.

// verify cheksum and print the verified data

44.

45.
46.
47.

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);

48.

49.

else

50.

printf("Invalid Data!!\n");

51. }
52.
53. int main(void)
54. {
55.

printf("Interfacing Temperature and Humidity Sensor (DHT11) With Raspberry Pi\n");

56.

if(wiringPiSetup()==-1)

57.

exit(1);

58.

while(1)

59.

60.

dht11_read_val();

61.

delay(3000);

62.

63.

return 0;

64. }

Compile the code as...


gcc -o sensor temp_rh_sensor.c -L/usr/local/lib lwiringPi
gcc -o temperatura temperatura.c -L/usr/local/lib -lwiringPi

gcc -o humedad humedad.c -L/usr/local/lib -lwiringPi

Now execute it...


sudo ./sensor
Lo pongo en el sbin para que este accesible desde el path
Llamarlo con:
Sudo sensor
[ Humidity = 87.0 % Temperature = 32.2 *C (90.0 *F) ]
Como conectarlo a la Rapberry PI?
El OUT al GPID4

Funciona a 3,3V!!! no a 5V

Potrebbero piacerti anche