Communication with the device is tricky, but there is an Arduino Library "dht11.h" which make the whole thing very simple. See Here. You have to copy the ".h" and ".cpp" files one by one to a text editor and save them. Then put them in your Arduino library folder in a sub folder called "dht11"
The connections are (S = Signal, Centre Pin = +5V, - = GND):
The pull up resistor is essential, otherwise you get wrong readings...
Here's my test bed:
And the display on the Serial Monoitor of the Arduino IDE:
The code
#include "dht11.h" dht11 DHT11; #define DHT11PIN 2 void setup() { Serial.begin(9600); Serial.println("DHT11 TEST PROGRAM "); delay(2000); } void loop() { Serial.println("\n"); int chk = DHT11.read(DHT11PIN); Serial.print("Read sensor: "); switch (chk) { case DHTLIB_OK: Serial.println("OK"); break; case DHTLIB_ERROR_CHECKSUM: Serial.println("Checksum error"); break; case DHTLIB_ERROR_TIMEOUT: Serial.println("Time out error"); break; default: Serial.println("Unknown error"); break; } Serial.print("Humidity (%): "); Serial.println((float)DHT11.humidity, 2); Serial.print("Temperature (C): "); Serial.println((float)DHT11.temperature, 2); delay(2000); }
No comments:
Post a Comment