본문
DHT11 온습도 센서 실습
사물인터넷(IoT) 2017. 5. 17. 22:51
반응형
# DHT11 온습도 센서 실습
1. DHT11 회로구성
2. source
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 | #define dht11_pin 7 byte read_dht11_dat() { byte i = 0; byte result=0; for(i=0; i< 8; i++) { while (!digitalRead(dht11_pin)); delayMicroseconds(30); if (digitalRead(dht11_pin) != 0 ) bitSet(result, 7-i); while (digitalRead(dht11_pin)); } return result; } void setup() { pinMode(dht11_pin, OUTPUT); digitalWrite(dht11_pin, HIGH); Serial.begin(9600); Serial.println("Ready"); } void loop() { byte dht11_dat[5]; byte dht11_in; byte i;// start condition digitalWrite(dht11_pin, LOW); delay(18); digitalWrite(dht11_pin, HIGH); delayMicroseconds(1); pinMode(dht11_pin, INPUT); delayMicroseconds(40); if (digitalRead(dht11_pin)) { Serial.println("dht11 start condition 1 not met"); // wait for DHT response signal: LOW delay(1000); return; } delayMicroseconds(80); if (!digitalRead(dht11_pin)) { Serial.println("dht11 start condition 2 not met"); //wair for second response signal:HIGH return; } delayMicroseconds(80);// now ready for data reception for (i=0; i<5; i++) { dht11_dat[i] = read_dht11_dat();} //recieved 40 bits data. Details are described in datasheet pinMode(dht11_pin, OUTPUT); digitalWrite(dht11_pin, HIGH); byte dht11_check_sum = dht11_dat[0]+dht11_dat[2];// check check_sum if(dht11_dat[4]!= dht11_check_sum) { Serial.println("DHT11 checksum error"); } Serial.print("Current humdity = "); Serial.print(dht11_dat[0], DEC); Serial.print("% "); Serial.print("temperature = "); Serial.print(dht11_dat[2], DEC); Serial.println("C "); delay(2000); //fresh time } | cs |
P.S. 관련 자료
http://server-engineer.tistory.com/admin/entry/post/?id=390
반응형
댓글