這篇文章主要為大家展示了“樹莓派如何實(shí)現(xiàn)溫濕度傳感器DHT11”,內(nèi)容簡而易懂,條理清晰,希望能夠幫助大家解決疑惑,下面讓小編帶領(lǐng)大家一起研究并學(xué)習(xí)一下“樹莓派如何實(shí)現(xiàn)溫濕度傳感器DHT11”這篇文章吧。
成都創(chuàng)新互聯(lián)專業(yè)IDC數(shù)據(jù)服務(wù)器托管提供商,專業(yè)提供成都服務(wù)器托管,服務(wù)器租用,四川雅安服務(wù)器托管,四川雅安服務(wù)器托管,成都多線服務(wù)器托管等服務(wù)器托管服務(wù)。
1、連線
我買的這個(gè)DHT11比較郁悶,三根管腳,沒有注明哪個(gè)是VCC,哪個(gè)是GND,網(wǎng)上搜了一堆,為毛別的傳感器都標(biāo)注清楚了,我的只標(biāo)注了個(gè)正負(fù)號(hào),后來跟某寶老板咨詢了下,確定是+是VCC,-是GND,中間的是Data(或許是因?yàn)槲沂请娦艑I(yè)的小白,不懂這些行話)
1–>3.3v 接左邊第1個(gè)GPIO針腳
2–>GPIO 數(shù)據(jù)接口,可隨意連接1個(gè)GPIO針腳(第7個(gè)針腳對(duì)應(yīng)的是GPIO Pin #4,和下面的代碼對(duì)應(yīng))
3–>GND 接地(第6個(gè)針腳或其他GND)
2、安裝gpio驅(qū)動(dòng)(姑且叫驅(qū)動(dòng)吧,原理還不清楚)
sudo apt-get install wiringpi
3、運(yùn)行例子(C代碼)
#include <wiringPi.h> #include <stdio.h> #include <stdlib.h> #include <stdint.h> #define MAX_TIME 100 #define DHT11PIN 7 //讀取數(shù)據(jù)引腳 #define ATTEMPTS 5 //retry 5 times when no response int dht11_val[5]={0,0,0,0,0}; int dht11_read_val(){ uint8_t lststate=HIGH; //last state uint8_t counter=0; uint8_t j=0,i; for(i=0;i<5;i++) dht11_val[i]=0; //host send start signal pinMode(DHT11PIN,OUTPUT); //set pin to output digitalWrite(DHT11PIN,LOW); //set to low at least 18ms delay(18); digitalWrite(DHT11PIN,HIGH); //set to high 20-40us delayMicroseconds(30); //start recieve dht response pinMode(DHT11PIN,INPUT); //set pin to input for(i=0;i<MAX_TIME;i++) { counter=0; while(digitalRead(DHT11PIN)==lststate){ //read pin state to see if dht responsed. if dht always high for 255 + 1 times, break this while circle counter++; delayMicroseconds(1); if(counter==255) break; } lststate=digitalRead(DHT11PIN); //read current state and store as last state. if(counter==255) //if dht always high for 255 + 1 times, break this for circle break; // top 3 transistions are ignored, maybe aim to wait for dht finish response signal if((i>=4)&&(i%2==0)){ dht11_val[j/8]<<=1; //write 1 bit to 0 by moving left (auto add 0) if(counter>16) //long mean 1 dht11_val[j/8]|=1; //write 1 bit to 1 j++; } } // verify checksum and print the verified data if((j>=40)&&(dht11_val[4]==((dht11_val[0]+dht11_val[1]+dht11_val[2]+dht11_val[3])& 0xFF))){ float f, h; h = dht11_val[0] + dht11_val[1]; f = dht11_val[2] + dht11_val[3]; printf("Temp = %.1f *C, Hum = %.1f \%\n", f, h); return 1; } else return 0; } int main(void){ int attempts=ATTEMPTS; if(wiringPiSetup()==-1) exit(1); while(attempts){ //you have 5 times to retry int success = dht11_read_val(); //get result including printing out if (success) { //if get result, quit program; if not, retry 5 times then quit break; } attempts--; delay(2500); } return 0; }
注意第48,49行,針對(duì)DHT11模塊的度出來的就是溫濕度值,不需要轉(zhuǎn)換,網(wǎng)上其他人給的代碼還轉(zhuǎn)換了下,不清楚原因。
編譯:
gcc testDHT11.c -o test -lwiringPi
運(yùn)行
sudo ./test
注意,要使用root權(quán)限才能正常運(yùn)行。
以上是“樹莓派如何實(shí)現(xiàn)溫濕度傳感器DHT11”這篇文章的所有內(nèi)容,感謝各位的閱讀!相信大家都有了一定的了解,希望分享的內(nèi)容對(duì)大家有所幫助,如果還想學(xué)習(xí)更多知識(shí),歡迎關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道!
網(wǎng)站欄目:樹莓派如何實(shí)現(xiàn)溫濕度傳感器DHT11
瀏覽路徑:http://chinadenli.net/article44/giceee.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供全網(wǎng)營銷推廣、服務(wù)器托管、企業(yè)建站、手機(jī)網(wǎng)站建設(shè)、網(wǎng)站導(dǎo)航、外貿(mào)建站
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來源: 創(chuàng)新互聯(lián)