AdSense

Sunday 11 September 2016

Read DHT11 sensor with Arduino

(Deutsche Version) Some time ago, I bought a temperature and humidity sensor which is called DHT11. The sensor is easy to use and can be used with 3.3V or 5V. The sensor has four pins, for the normal operation, only three of them are needed.
The left pin (as you can see in the image) is connected to 5V, the pin on the right with GND. The left pin of the middle pins is the signal pin which can be connected with a random digital pin on the arduino (in my example, this is pin 2). Additionally, a 10k resistor is needed between the data pin and 5V.

Next, you need the corresponding library which you can download here: Download. In the arduino software the sensor can be read as it is shown in the following example. The source code should be self-explainatory. If a sensor with a higher precision is needed, you can use a DHT22, therefore you just have to adjust the DHTTYPE definition.

#include "DHT.h"
#define DHTPIN 2

#define DHTTYPE DHT11

DHT dht(DHTPIN, DHTTYPE);

void setup() {
  Serial.begin(9600);

  dht.begin();
}

void loop() {
  float h = dht.readHumidity();
  float t = dht.readTemperature();
  
  Serial.print("Humidity: ");
  Serial.print(h);
  Serial.print(" %\t");
  Serial.print("Temperature: ");
  Serial.print(t);
  Serial.println(" *C ");

  delay(2000);
}