Temperature & Humidity (DHT11)¶
At a glance
What you'll learn: how to read the temperature and humidity of the air around you, print the values to the Serial Monitor, and use them to trigger other things.
Prerequisites: How Blocks Work, Pin Map, Your First Program.
Time: about 20 minutes.
What is it?¶
The DHT11 is a small blue-and-white sensor that measures two things at once:
- the temperature of the air around it (in °C)
- the relative humidity — how much moisture is in the air, as a percentage
You'll find this kind of sensor inside smart thermostats, greenhouses, and environmental monitoring stations in real smart cities.
How it works¶
Inside the DHT11 there are two separate sensing elements — one for temperature, one for humidity. A tiny chip on the back reads both, packs them into a single digital message, and sends that message down a single data wire to your ESP32 whenever it's asked.
This is why the DHT11 only needs three connections (Power, Ground, Data) even though it measures two things.
Why are the readings rounded to whole numbers?
The DHT11 only reports to the nearest 1 °C and 1 % humidity. That's fine for most smart-city projects, but if you need more precision you'd use a DHT22 or BME280 sensor instead.
Specifications¶
| Property | Value |
|---|---|
| Temperature range | 0 – 50 °C (±2 °C accuracy) |
| Humidity range | 20 – 80 % RH (±5 % accuracy) |
| Operating voltage | 3.3 V – 5 V |
| Sampling rate | Once every ~2 seconds |
| Protocol | Single-wire digital |
Pin layout¶
The DHT11 module in your kit has three pins labelled on the breakout board:
| DHT11 pin | What it is | Connects to |
|---|---|---|
| S | Signal (data) | ESP32 Plus IO 15 |
| V | Power | ESP32 Plus V on the same header as IO 15 |
| G | Ground | ESP32 Plus G on the same header as IO 15 |
Because your ESP32 Plus shield has G / V / S headers on every pin, all three connections happen through a single 3-pin cable — no breadboard, no separate wires.
Wiring¶
Step by step:
- Unplug the USB cable from your ESP32 Plus.
- Take one of the 3-pin Dupont cables from your kit.
- Plug one end into the DHT11 module, matching
S → S,V → V,G → G. - Plug the other end into the IO 15 header on your ESP32 Plus, again matching S/V/G.
- Plug the USB cable back in.
The cable only fits one way
The Dupont cable has a keyed connector — if it doesn't slide on easily, you're trying it backwards. Don't force it.
Code¶
Build the program¶
Drag these blocks together in the workspace:
Block-by-block:
| # | Block | What it does |
|---|---|---|
| 1 | when Arduino begin |
Program starts here |
| 2 | serial 0 begin baudrate 115200 |
Turns on the Serial Monitor so we can see messages |
| 3 | init dht 1 pin IO15 model dht11 |
Tells Flowlence there's a DHT11 on pin 15, calling it dht_1 |
| 4 | forever |
Repeats the blocks inside, non-stop |
| 5 | serial 0 print join "Temp: " (read dht_1 temperature) |
Prints the temperature reading |
| 6 | serial 0 print join "Humidity: " (read dht_1 humidity) |
Prints the humidity reading |
| 7 | wait 2 seconds |
Waits 2 seconds before reading again |
Why the 2-second wait?
The DHT11 can only give a fresh reading about every 2 seconds. If you ask it faster than that, you'll get the same number repeated or an error. Always include wait 2 seconds inside the loop.
The generated Arduino code¶
Look at the right-hand Code Panel. Flowlence Code translates your blocks into this Arduino C++:
// generated by Flowlence
#include <Arduino.h>
#include <DHT.h>
DHT dht_1(15, 11); // pin 15, model DHT11
void setup() {
dht_1.begin();
Serial.begin(115200);
}
void loop() {
Serial.print("Temp: ");
Serial.println(dht_1.readTemperature());
Serial.print("Humidity: ");
Serial.println(dht_1.readHumidity());
delay(2000);
}
You don't need to understand every line yet. The point is: every block you drag corresponds to a line of real code. As you get more confident, you'll start recognising the patterns.
Upload and watch¶
- Click Upload (green button, top right).
- Wait for Compiling… and Uploading… to finish (about 30 seconds the first time).
- Open the Serial Monitor (bottom right panel).
- Set the baud rate to 115200.
Expected result¶
Every two seconds you should see two lines scroll in the Serial Monitor:
Temp: 23.00
Humidity: 45.00
Temp: 23.00
Humidity: 45.00
Temp: 24.00
Humidity: 62.00
Breathe on the sensor
Your breath is warm and humid. Hold the DHT11 close to your mouth and breathe out — you should see the humidity number jump up within a couple of readings. This is a great way to confirm the sensor is really working and not just showing a stuck value.
Try it!¶
Challenge 1 · Temperature alert
Add an LED to your circuit (see the LED lesson). Make the LED turn on when temperature is above 28 °C, and off otherwise.
Hints:
- You'll need an
if (temperature > 28) then … else …block from the Control category. - The
read dht_1 temperatureblock goes where you'd normally put a number.
Challenge 2 · Comfort meter
Use the LCD display (see the LCD lesson) instead of the Serial Monitor. Show two lines:
Temp: 23 C
Hum: 45 %
Update the display every 2 seconds.
Challenge 3 · The first step toward a Weather Station
Combine the two challenges above. You've just built the core of the Weather Station project — read it to see what one more piece of logic gets you.
What's next?¶
- Need to go deeper into outputs? → LCD Display
- Ready to combine this with the LCD and Fan? → Build the Smart Temperature System