// H.-J. Berndt 2015 auf www.hjberndt.de // DHT11-Sensor mit 1-Draht-Datenprotokoll // ohne Bibliotheken abfragen #define DHT_PIN 2 int feuchte, temperatur; char sfeuchte[10],stemperatur[10]; void setup(){ Serial.begin(9600); Serial.println("DHT11 Messung."); delay(1000); } void loop() {dht11(DHT_PIN); Serial.print("DHT11 Luftfeuchte[%]/Temperatur[C]: ");Serial.print(sfeuchte); Serial.print("/");Serial.println(stemperatur); delay(1000); } void dht11(int pin) {int bitcnt=7,ix=0; uint8_t Byte[5]; for(int i=0;i<5;i++)Byte[i]=0; pinMode(pin, OUTPUT); // REQUEST SAMPLE digitalWrite(pin, LOW); delay(18); digitalWrite(pin, HIGH);delayMicroseconds(30); pinMode(pin, INPUT); pulseIn(pin,HIGH); // ACKNOWLEDGE ~ 70 us for (int i=0; i<40; i++) // READ OUTPUT - 40 BITS = 5 BYTE {if (mypulseIn(pin,HIGH) > 40) Byte[ix] |= (1 << bitcnt); if (--bitcnt < 0)// next byte {bitcnt = 7; // restart ix++; } } feuchte = Byte[0]; //BYTE[1] bei DHT11 immer 0 temperatur = Byte[2]; //BYTE[3] bei DHT11 immer 0 if (Byte[4] != Byte[0]+Byte[2]){feuchte=88;temperatur=88;} itoa(feuchte,sfeuchte,9); itoa(temperatur,stemperatur,9); } unsigned long mypulseIn(int pin,int level) {unsigned long t0,t1,t2,timeout=200; t0=micros(); if(level==HIGH) {while(!digitalRead(pin) && (micros()-t0)