DHT11 Sensor Arduino Uno Code & Wiring

Posted on

Understanding the DHT11 Sensor

How to Use the DHT11 Sensor With Arduino Uno? – DHT11 is a temperature and humidity sensor. This sensor has two sensors in it, a capacitive humidity sensor and a thermistor.

Although it is a cheap sensor and slow in data transmission, this sensor is suitable for beginners in making temperature and humidity detectors.

This sensor has the following maximum specifications:

  • Temperature measurement range 0 – 50
  • Humidity measurement range 20 – 90% RH
  • Temperature reading accuracy ±2℃
  • Humidity reading accuracy ±5% RH
  • Working voltage 3.3 – 5.5V

Here are the detailed specifications of the Arduino DHT11 sensor which I quoted from the datasheet:

DHT11 Sensor Arduino Uno Code & Wiring

For more detail about this sensor, please refer to datasheet. You can download it using link below:

Download DHT11 Datasheet

DHT11 Working Principle

For sending and receiving data between the Arduino DHT11, this sensor uses a serial interface (single way two-way). This means that this sensor only uses one pin for sending and receiving data.

In the datasheet, this sensor takes 4ms at a time to communicate. Data packets issued or sent to Arduino are 40 bits.

The data format sent by DHT11 to Arduino is as follows: 8-bit integral RH data + 8-bit decimal RH data + 8-bit integral T data + 8-bit decimal T data + 8-bit condition data.

Arduino DHT11 Wiring

To be able to use the DHT11 sensor on Arduino, please follow the following circuit.

DHT11 Sensor Arduino Uno Code & Wiring

To make programming easier, there is a library for this sensor that we will use using Arduino.

There are two ways to get this library. First from library manager in arduino ide and include manually. If you are new in Arduino programming, you can read the steps on this page.

DHT Library Adafruit

The library that I taken from adafruit industries github page. Please download the following library and insert it into your Arduino IDE:

Please include the library in the Arduino IDE. If you don’t know how to include libraries into the Arduino IDE, you can read this article.

When finished including the library into your arduino IDE, copy the following program and paste it into your IDE.

#include "DHT.h"

#define DHTPIN 2
#define DHTTYPE DHT11   // DHT 11

DHT dht(DHTPIN, DHTTYPE);

void setup() {
  Serial.begin(9600);
  Serial.println(F("DHTxx test!"));
  dht.begin();
}


void loop() {
  delay(2000);
  float h = dht.readHumidity();

  // Read temperature as Celsius (the default)
  float t = dht.readTemperature();

  // Read temperature as Fahrenheit (isFahrenheit = true)
  float f = dht.readTemperature(true);

  // Check if any reads failed and exit early (to try again).
  if (isnan(h) || isnan(t) || isnan(f)) {
    Serial.println(F("Failed to read from DHT sensor!"));
    return;
  }


  // Compute heat index in Fahrenheit (the default)
  float hif = dht.computeHeatIndex(f, h);

  // Compute heat index in Celsius (isFahreheit = false)
  float hic = dht.computeHeatIndex(t, h, false);

  Serial.print(F("Humidity: "));
  Serial.print(h);
  Serial.print(F("%  Temperature: "));
  Serial.print(t);
  Serial.print(F("°C "));
  Serial.print(f);
  Serial.print(F("°F  Heat index: "));
  Serial.print(hic);
  Serial.print(F("°C "));
  Serial.print(hif);
  Serial.println(F("°F"));
}

Then click Upload.

If you encounter an error like this:

Fatal error: Adafruit_Sensor.h: No such file or directory

If you get an error libraries/DHT-sensor-library-master/DHT_U.h:36:10: fatal error: Adafruit_Sensor.h: No such file or directory
#include <Adafruit_Sensor.h>
^~~~~~~
compilation terminated.
exit status 1
Error compiling for board Arduino Uno.

Please read this page to fix it error.

If done, Now please re-upload above program to arduino board. If seccessfull, please open serial monitor to view some of data. The following is an example program displaying temperature data.

DHT11 Sensor Arduino Uno Code & Wiring

Hopefully this DHT11 Sensor Arduino Uno Code & Wiring article is usefull.

May be you like:
> Convert Data Type Arduino Serial Monitor
> Arduino Float to String Convert Tutorial
> DS3231 arduino RTC Tutorial Using Adafruit Library
> Show Degree Symbol Serial Monitor Arduino
> Letter Typing Effect LCD I2C 20×4 Arduino

4 comments

Leave a Reply

Your email address will not be published. Required fields are marked *