Built-in project · Arduino Uno R3

DHT11 module on pin 4, on an Arduino Uno

A DHT11 module on pin 4: the dht11 library reads the humidity and the temperature, as whole numbers, once a second. The whole build, an Arduino Uno and 1 more part, runs here in your browser on the firmware below; open it in the editor to change the wiring or the code and run it again.

Teaching? Assign it to your class in one click.

Beginner About 10 minutes to build and run. Runs in your browser. Free, and no account needed.

The parts retail for about $34. On Mokxi they're free.

The circuit runs here once the page has loaded.

What you'll learn

  • Sensors that talk over one data wire
  • Using a library to read a sensor
  • Checking a reading for errors
  • Printing values to the serial monitor
  • Difficulty: Beginner
  • Time: About 10 minutes
  • Board: Arduino Uno R3
  • Parts: 3

How it works

The sensor has three wires: power from 5V, ground, and one data wire to pin 4. It measures temperature and humidity, then sends the numbers back along that single data wire as a timed string of 40 bits.

Once a second the sketch asks for a reading. If the checksum matches, it prints the humidity and temperature to the serial monitor. If not, it prints Read error. Click the sensor in the simulator to change the weather.

Parts list

3 parts, plus the jumper wires. They retail for about $34; on Mokxi they're free.

Wiring table

Every connection, pin by pin, read from the circuit itself. Each row joins the pin on the left to the pin on the right, by a jumper wire or a breadboard strip.

Part and pinConnects to
Temperature and humidity, DHT22 pin GNDArduino Uno R3 pin GND
Temperature and humidity, DHT22 pin DATAArduino Uno R3 pin 4
Temperature and humidity, DHT22 pin VCCArduino Uno R3 pin 5V

The code

The firmware exactly as the editor opens it. Change a line there and press Run: it compiles in the browser.

sketch.ino · Arduino Uno R3
// A DHT11 on pin 4: temperature and humidity.
//
//   module S -> pin 4   + -> 5V   - -> GND
//
// read() starts the sensor talking and times its 40 bits; it returns
// DHTLIB_OK when the checksum matches.
//
// Click the sensor to change the weather.

#include <dht11.h>

dht11 DHT;

void setup() {
  Serial.begin(9600);
  Serial.println("Mokxi Uno: DHT11 on pin 4");
}

void loop() {
  if (DHT.read(4) == DHTLIB_OK) {
    Serial.print("Humidity: ");
    Serial.print(DHT.humidity);
    Serial.print(" %  Temperature: ");
    Serial.print(DHT.temperature);
    Serial.println(" C");
  } else {
    Serial.println("Read error");
  }
  delay(1000);
}

Code walkthrough

The key functions of the sketch, and what each one does.

setup sketch.ino

Starts the serial port at 9600 baud and prints a hello line. There is no pin setup, because the library handles pin 4 itself when it reads.

void setup() {
  Serial.begin(9600);
  Serial.println("Mokxi Uno: DHT11 on pin 4");
}

loop sketch.ino

Calls DHT.read(4) and compares the result with DHTLIB_OK. If it matches, it prints DHT.humidity and DHT.temperature. Otherwise it prints Read error. Then delay(1000) waits a second, so the sensor is not asked too often.

void loop() {
  if (DHT.read(4) == DHTLIB_OK) {
    Serial.print("Humidity: ");
    Serial.print(DHT.humidity);
    Serial.print(" %  Temperature: ");
    Serial.print(DHT.temperature);
    Serial.println(" C");
  } else {
    Serial.println("Read error");
  }
  delay(1000);
}

Troubleshooting

The usual mistakes with this circuit, and the fix for each.

  • The monitor keeps printing Read error. This project
    Check that the data wire goes to pin 4 and matches the 4 in DHT.read. Also check 5V and GND.
  • The numbers never change. This project
    Click the sensor in the simulator to change the temperature and humidity it reports.
  • Readings are NaN or fail, or never appear. Temperature and humidity
    Put a 10 k ohm pull-up between DATA and VCC (bare sensors need it). A 4-pin DHT22 has pin 3 unused. Check the data pin number in your code.
  • Values freeze or errors show up when you read often. Temperature and humidity
    The DHT22 can only be read about once every 2 seconds. Wait between reads, and check for NaN before using the numbers.
  • Upload fails or nothing runs after wiring pins 0 and 1 to a module. Arduino Uno R3
    Pins 0 and 1 are the USB serial port. Move the module to other pins, or unplug it while uploading. Also check the right board is selected.
  • Pins misbehave or the board resets when you power motors or servos from it. Arduino Uno R3
    Each I/O pin gives about 20 mA, 40 mA max. Power motors and servos from their own supply and join its ground to the Uno's GND.

Change it and keep it

Open it in the editor, change the circuit or the code, and keep your version in a free account.