Built-in project · Arduino Uno R3

DHT11 temperature and humidity on an Arduino Uno

A DHT11 module on pin 2: the DHT library reads the temperature and humidity every two seconds and prints both. 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 Runs in your browser. Free, and no account needed.

DHT11 temperature and humidity · Arduino Uno R3live0.000 s 0.00x
Click to open it in the editor
The circuit itself, running here on the simulator. Press what can be pressed; click anything else to open it in the editor.

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
// DHT11: temperature and humidity on one wire, printed every two seconds.
//
//   DHT11 VCC  -> 5V
//   DHT11 DATA -> pin 2
//   DHT11 GND  -> GND
//
// The DHT11 has its own little chip inside. Pull its data line low and it
// answers with forty bits: humidity, temperature and a checksum. The DHT
// library does that handshake; the sketch only asks for the two numbers.
//
// The sensor takes about a second to make a new reading, so asking more often
// than every two seconds just returns the last one again. The DHT11 reads
// whole degrees and whole percent.

#include <DHT.h>

DHT dht(2, DHT11);

void setup() {
  Serial.begin(9600);
  Serial.println("Mokxi Uno: DHT11 temperature and humidity");
  dht.begin();
}

void loop() {
  delay(2000);
  float humidity = dht.readHumidity();
  float celsius = dht.readTemperature();
  if (isnan(humidity) || isnan(celsius)) {
    Serial.println("no reading: check the DATA wire");
    return;
  }
  Serial.print("temperature ");
  Serial.print(celsius, 0);
  Serial.print(" C, humidity ");
  Serial.print(humidity, 0);
  Serial.println(" %");
}

Parts list

3 parts, plus the jumper wires. Every one is in the editor's parts bin.

How it is wired

3 connections, pin by pin, read from the circuit itself. Each line is one set of pins joined together, by a jumper wire or a breadboard strip.

  • Ground: Arduino Uno R3 pin GND; Temperature and humidity, DHT22 pin GND
  • Arduino Uno R3 pin 2; Temperature and humidity, DHT22 pin DATA
  • Arduino Uno R3 pin 5V; Temperature and humidity, DHT22 pin VCC

Change it and keep it

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