Github:https://github.com/hicrater/raspberry-dht11-temperature.git
First, look at the wiring
Wiring diagram
Vcc |
Cathode |
Pick up the Raspberry Pi 3.3v power supply (choose one on the line) |
GND |
Negative |
Pick up the Raspberry Pi ground (just choose one). |
Out |
Data cable |
Pick Up Raspberry Pi GPIO7 |
Ii. Attaching the information of DHT11
Third, the Code
#include <wiringPi.h>#include<stdio.h>#include<stdlib.h>#include<stdint.h>#defineMax_time 85#defineGpioport 7//Store the Read dataintdht11_val[5] = {0,0,0,0,0 };//This function reads the temperature valueintDht11_read_val () {uint8_t lststate= high;//High in wiringPi.h defined as 1uint8_t counter =0;//counteruint8_t j =0, I;//Initializes an array of 0 for(i =0; I <5; i++) Dht11_val[i]=0;//Send start SignalPinmode (Gpioport, OUTPUT);//set Gpio to outputDigitalwrite (Gpioport, low);//keep low level at least 18msDelay -);d igitalwrite (Gpioport, high);//keep High Level 20-40usDelaymicroseconds ( +);//start receiving DHT response signalsPinmode (Gpioport, INPUT);//set Gpio as input//start waiting for temperature sensor response (will pull down the data cable when responding to the data sheet temperature sensor) for(i =0; i < max_time; i++) {counter=0; while(Digitalread (gpioport) = = lststate) {//If the current level is equal at this point, the level does not change, so continue waiting for the next cyclecounter++;d Elaymicroseconds (1);if(Counter = =255)//If the temperature sensor has not changed the data line after the cycle of the 255+1 time, exit this cycle Break;} Lststate= Digitalread (Gpioport);//read the latest state value of the data lineif(Counter = =255)//If the temperature sensor has not changed the data line after the cycle of the 255+1 time, exit this cycle Break;//The first three times being ignored may be in response to a temperature sensor (this thing is not as fast as the data sheet says) .... )if((I >=4) && (i%2==0)){//the 2 remainder is because the even level is used to transmit data (as can be seen from the data sheet)Dht11_val[j/8] <<=1;//Move left one (at this point the last one is 0)if(Counter > -)//the time here is very long, which means 1 here .Dht11_val[j/8] |=1;//writes data 1 toJ + +;}}/*Start processing data 1. Because a complete data output is 40 bits, J should be greater than 402.dht11_val[4] for checksum, here to determine if the checksum is correct 3. Print only dht11_val[0], dht11_val[2] because dht11_val[ 1], dht11_val[3] is a fractional part, according to the data sheet decimal is always zero*/if(J >= +) && (dht11_val[4] = = ((dht11_val[0] + dht11_val[1] + dht11_val[2] + dht11_val[3]) &0xFF)) {printf ("rh:%d,temp:%d\n", dht11_val[0], dht11_val[2]);return 1;}Elsereturn 0;}intMainvoid){//repeat 5 times to tryintAttempts =5;//Initialize Gpioif(Wiringpisetup () = =-1) Exit (1);//make 5 attempts while(attempts) {//Get temperature ValueintSuccess =dht11_read_val ();//exit after getting successfulif(Success) Break; attempts--;d Elay (2500);}return 0;}
Iv. Final Effect
Raspberry Pi DHT11 temperature and humidity sensor