Read Temperature and Humidity with DHT11

Draft. This tutorial has not been checked against the physical product yet. Confirm wiring, pin assignments and voltage limits against the product documentation before building it.
DifficultyBeginner
CategorySensors
Supported boardsArduino Uno R3 and compatible ATmega328P boards
Time needed50 minutes
PrerequisitesInstalling and Managing Libraries
Tested boardNot yet verified
Tested onNot yet verified
Arduino IDE versionNot yet verified
LibraryNot yet verified

What it does

The DHT11 measures air temperature and relative humidity and reports both as digital values. It does its own measuring and converting, so you receive numbers rather than a voltage to interpret.

It is the usual first environmental sensor, and the first part most people meet that needs a library. The reason is worth understanding: the DHT11 does not use I2C or SPI. It has one data wire and a timing-critical protocol of its own, where bits are distinguished by how long the line stays high, measured in microseconds. Writing that by hand is possible and unpleasant. A library does it properly, and this is a good moment to learn how libraries fit in.

Before you begin

Required parts and tools

  • An Arduino Uno or compatible board, already set up.
  • One DHT11 sensor or module.
  • Three jumper wires, and a breadboard if your sensor is a bare component.
  • A USB data cable.

Know what accuracy you are buying

The DHT11 is cheap and adequate for a room monitor. It is not an instrument, and a tutorial that implies otherwise sets up a disappointment.

Typical figure
Temperature range about 0 to 50 degrees C
Temperature accuracy roughly plus or minus 2 degrees C
Humidity range about 20 to 90 percent relative humidity
Humidity accuracy roughly plus or minus 5 percent
Resolution whole numbers

Confirm these against the datasheet for the part you actually received, since the figures vary a little between suppliers.

If you need decimals or better accuracy, the DHT22 is the same wiring and almost the same code, and Siratek stocks it. For temperature alone, the DS18B20 is more accurate still and comes in a waterproof probe.

Note the lower humidity limit. Below about 20 percent the DHT11 simply cannot report, which matters during harmattan when indoor humidity can fall below its range.

The board Siratek sells

The DHT11 on the Siratek product page is a module: the blue sensor is soldered to a small circuit board with a three-pin header, and it comes with jumper leads.

That matters, because the module already carries the pull-up resistor the sensor needs. It is the small component marked 103 on the board, which is the code for 10 kilohms. You do not need to add one.

The three pins are marked +, OUT and -. Match those marks to your wires rather than counting positions, because the header can be fitted either way round.

A bare four-pin sensor, bought loose rather than on a board, is the other thing sold as a DHT11. That one does need a 10 kilohm resistor between its data pin and VCC, and its fourth pin is unused.

Once a second, no faster

The DHT11 takes its own time to measure. Asking more often than once per second returns a stale or failed reading. The sketches here wait two seconds, which is comfortable.

This is also why a library that returns the same number repeatedly is not necessarily broken. It may be handing back the last successful measurement.

Safety notes

Low voltage and tiny currents throughout. Wire it with the USB cable unplugged.

Pin reference

Pin or label Connect to Purpose Notes
+ Uno 5V Power Also works at 3.3 V on 3.3 V boards
OUT Uno D2 The one-wire data line Pull-up already fitted on the module
- Uno GND Common ground
Fourth pin, bare sensors only Nothing Unused Not present on the module

The header can be fitted either way round, so read the printing on your own board rather than counting positions from a picture, including the one below.

Wiring steps

DHT11 module wired to an Arduino Uno, plus to 5V, OUT to pin 2 and minus to ground
Three wires. The 10 kilohm pull-up, marked 103, is already on the board.
  1. Unplug the USB cable.
  2. Connect the pin marked - to a GND pin on the Uno.
  3. Connect the pin marked + to 5V.
  4. Connect the pin marked OUT to digital pin 2.
  5. Check the marks once more. Power and ground reversed will make the sensor hot, and this is the part where that mistake is easy.
  6. Plug the USB cable back in.

Software and libraries

This one needs a library. The widely used choice is the DHT sensor library from Adafruit, which also requires the Adafruit Unified Sensor library to be installed alongside it.

  1. Open Tools > Manage Libraries.
  2. Search for DHT sensor library and install it.
  3. If the IDE offers to install its dependencies, accept. If it does not, search for Adafruit Unified Sensor and install that too.
  4. Record the version you installed in this tutorial’s front matter when the sensor is tested on hardware.

That last step is not bookkeeping for its own sake. These libraries change, and a sketch that worked a year ago can fail against a newer version. Knowing which version was tested is what makes the tutorial reproducible.

Minimal working example

// Siratek: reading temperature and humidity from a DHT11.

#include <DHT.h>

const int dhtPin = 2;
DHT dht(dhtPin, DHT11);    // Second argument selects the sensor type.

void setup() {
  Serial.begin(9600);
  Serial.println("Siratek: DHT11 starting.");
  dht.begin();
}

void loop() {
  // Read both values first, then check them.
  float humidity = dht.readHumidity();
  float temperature = dht.readTemperature();   // Celsius by default.

  // A failed read returns NaN, which is not the same as zero.
  if (isnan(humidity) || isnan(temperature)) {
    Serial.println("read failed");
  } else {
    Serial.print("temperature = ");
    Serial.print(temperature, 1);
    Serial.print(" C   humidity = ");
    Serial.print(humidity, 1);
    Serial.println(" %");
  }

  delay(2000);   // Never faster than once per second.
}

The isnan() check is the line that separates a working project from a confusing one. When a read fails, the library returns “not a number”, and arithmetic on it produces more nonsense rather than an error. Checking every reading before using it is a habit worth forming here, because the same pattern applies to most sensor libraries.

Reacting to a reading

const float warmThreshold = 30.0;

if (!isnan(temperature)) {
  if (temperature > warmThreshold) {
    digitalWrite(fanPin, HIGH);
    Serial.println("above threshold: fan on");
  } else {
    digitalWrite(fanPin, LOW);
  }
}

A real fan or pump draws far more than a pin can supply, so fanPin here would drive a relay module or MOSFET module rather than the load itself. Voltage, Current and Resistance explains why, and the relay tutorial covers doing it safely.

Expected result

A reading every two seconds, with plausible values for the room:

Siratek: DHT11 starting.
temperature = 29.0 C   humidity = 68.0 %
temperature = 29.0 C   humidity = 68.0 %
temperature = 29.0 C   humidity = 67.0 %

Values will be whole numbers, because that is the sensor’s resolution. Breathing gently on the sensor should raise the humidity within a few seconds and bring it back down slowly, which is the quickest way to confirm it is really measuring rather than repeating a stored value.

The first read after power-up sometimes fails. That is normal; the second should succeed.

Troubleshooting

Symptom Likely cause Check or fix
read failed every time Data pin wrong, or sensor not powered Check all three wires and the pin in the sketch
read failed on a bare sensor Missing pull-up resistor Fit 10 k between data and VCC. The module already has one
First read fails, rest work Sensor still settling Normal, ignore the first
Same numbers forever Reading faster than once a second Increase the delay to 2000 ms
Sketch will not compile Library not installed Install DHT sensor library and Adafruit Unified Sensor
Compiles, no sensible values DHT22 passed instead of DHT11 Correct the second argument
Sensor is hot to touch Power and ground reversed Unplug at once and recheck the labels
Humidity stuck at the bottom Genuinely below the sensor’s range Expected below about 20 percent
Temperature out by a couple of degrees Within the sensor’s stated accuracy Use a DS18B20 if you need better
Values fine indoors, wrong in the sun Direct sunlight heats the housing Shade the sensor, keep air moving

Next steps

  • Easier first: Installing and Managing Libraries, if the library step was the difficult part.
  • Related product: the DHT22, for decimals and a wider range, or the DS18B20 for accurate temperature alone.
  • Next project: add the I2C LCD and build a mini weather station that shows both readings without a computer attached.

Sources and attribution

All text, wiring guidance and code in this tutorial are original Siratek work.

  • DHT11 datasheet, for ranges, accuracy and the minimum interval between readings.
  • Adafruit DHT sensor library documentation, for the function names and the unified sensor dependency. The library is linked rather than reproduced, and no library code appears here.

Pin ordering and labelling differ between suppliers of this module, so the tutorial directs the reader to the printing on their own board. No third-party tutorial text, diagrams, photographs or code has been reproduced here.

Product used in this tutorial: DHT11 Temperature and Relative Humidity Sensor2
Shop
Sidebar
0 Wishlist
0 Cart