用熱敏電阻來量測並與 DHT11做交叉比對,成本會省很多。
[量測結果]
// Temp and Humidity Module "DHT11" int DHpin = A0; byte dat[5]; byte read_data() { byte data; for(int i=0; i<8; i++) { if(digitalRead(DHpin) == LOW) { while(digitalRead(DHpin) == LOW); //等待50us; delayMicroseconds(30); //判斷高電平的持續時間,以判定數據是'0'還是'1'; if(digitalRead(DHpin) == HIGH) data | = ( 1 << ( 7 - i ) ); //高位在前,低位在後; while(digitalRead(DHpin) == HIGH); //數據‘1’,等待下一位的接收; } } return data; } void start_test() { digitalWrite(DHpin,LOW); //拉低總線,發開始信號; delay(30); //延時要大於18ms,以便DHT11能檢測到開始信號; digitalWrite(DHpin,HIGH); delayMicroseconds(40); //等待DHT11響應; pinMode(DHpin,INPUT); while(digitalRead(DHpin) == HIGH); delayMicroseconds(80); //DHT11發出響應,拉低總線80us; if(digitalRead(DHpin) == LOW); delayMicroseconds(80); //DHT11拉高總線80us後開始發送數據; for(int i=0;i<4;i++) //接收溫濕度數據,校驗位不考慮; dat[i] = read_data(); pinMode(DHpin,OUTPUT); digitalWrite(DHpin,HIGH); //發送完一次數據後釋放總線,等待主機的下一次開始信號; } void setup() { Serial.begin(9600); pinMode(DHpin,OUTPUT); } void loop() { start_test(); Serial.print("Current humdity = "); Serial.print(dat[0], DEC); //顯示濕度的整數位; Serial.print('.'); Serial.print(dat[1],DEC); //顯示濕度的小數位; Serial.println('%'); Serial.print("Current temperature = "); Serial.print(dat[2], DEC); //顯示溫度的整數位; Serial.print('.'); Serial.print(dat[3],DEC); //顯示溫度的小數位; Serial.println('C'); delay(700); }
沒有留言:
張貼留言