DHT11 on pin 3, readings on an LCD, on an Arduino Uno
A DHT11 module on pin 3 measures temperature and humidity; the DHT library reads it every two seconds and a 16x2 LCD on pins 4, 6 and 10 to 13 shows both. The whole build, an Arduino Uno and 2 more parts, 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.
Intermediate About 25 minutes to build and run. Runs in your browser. Free, and no account needed.
The parts retail for about $39. On Mokxi they're free.
What you'll learn
- A one-wire sensor protocol read by a library
- Driving a 16x2 LCD in 4-bit mode
- Checking for failed readings with
isnan - Why the sketch waits two seconds between readings
- Difficulty: Intermediate
- Time: About 25 minutes
- Board: Arduino Uno R3
- Parts: 4
How it works
The sensor sends its reading down one wire, on pin 3, as a string of short and long pulses. The DHT library times those pulses and turns them into a temperature and a humidity.
The LCD is wired in 4-bit mode: RS on pin 4, E on pin 6, and data lines D4 to D7 on pins 10 to 13. Every two seconds the sketch rewrites both lines of the display and also prints the numbers to serial.
If the library cannot get a reading, the LCD says Sensor error.
Parts list
4 parts, plus the jumper wires. They retail for about $39; on Mokxi they're free.
- 1 × Arduino Uno R3In Starter Kit, Claw Machine Kit, Vending Machine Kit.
- 1 × Full-size breadboard
- 1 × Character LCD, 16x2
- 1 × Temperature and humidity, DHT22In Sensor Starter Kit.
Kits that can build it
These 5 starter kits have every part in the circuit. Each kit's page lists what else it can build.
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 pin | Connects to |
|---|---|
| Character LCD, 16x2 pin D7 | Arduino Uno R3 pin 13 |
| Character LCD, 16x2 pin D6 | Arduino Uno R3 pin 12 |
| Character LCD, 16x2 pin D5 | Arduino Uno R3 pin 11 |
| Character LCD, 16x2 pin D4 | Arduino Uno R3 pin 10 |
| Character LCD, 16x2 pin E | Arduino Uno R3 pin 6 |
| Character LCD, 16x2 pin RS | Arduino Uno R3 pin 4 |
| Temperature and humidity, DHT22 pin DATA | Arduino Uno R3 pin 3 |
| Character LCD, 16x2 pin VDD | Arduino Uno R3 pin 5V |
| Character LCD, 16x2 pin A | Arduino Uno R3 pin 5V |
| Temperature and humidity, DHT22 pin VCC | Arduino Uno R3 pin 5V |
| Character LCD, 16x2 pin VSS | Arduino Uno R3 pin GND |
| Character LCD, 16x2 pin V0 | Arduino Uno R3 pin GND |
| Character LCD, 16x2 pin RW | Arduino Uno R3 pin GND |
| Character LCD, 16x2 pin K | Arduino Uno R3 pin GND |
| Temperature and humidity, DHT22 pin GND | Arduino Uno R3 pin GND |
The code
The firmware exactly as the editor opens it. Change a line there and press Run: it compiles in the browser.
// Temperature and humidity: a DHT11 on pin 3, read out on the LCD.
//
// DHT11 DATA -> pin 3 VCC -> 5V GND -> GND
// LCD RS -> pin 4 LCD E -> pin 6
// LCD D4 -> pin 10 D5 -> pin 11 D6 -> pin 12 D7 -> pin 13
// LCD RW, VSS, K -> GND VDD, A -> 5V
// LCD V0 (contrast) -> GND here; on the bench, the middle leg of a pot
//
// The DHT11 sends its readings down one wire as a string of short and long
// pulses. The DHT library times them. The sensor needs about two seconds
// between readings, so the sketch waits that long.
//
// Click the sensor and change its temperature or humidity.
#include <LiquidCrystal.h>
#include <DHT.h>
DHT dht(3, DHT11);
LiquidCrystal lcd(4, 6, 10, 11, 12, 13);
void setup() {
dht.begin();
lcd.begin(16, 2);
Serial.begin(9600);
Serial.println("Mokxi Uno: DHT11 on pin 3, LCD on pins 4, 6 and 10 to 13");
}
void loop() {
delay(2000);
float humidity = dht.readHumidity();
float celsius = dht.readTemperature();
if (isnan(humidity) || isnan(celsius)) {
lcd.clear();
lcd.print("Sensor error");
Serial.println("no reading: check the DATA wire");
return;
}
lcd.setCursor(0, 0);
lcd.print("Temp: C ");
lcd.setCursor(6, 0);
lcd.print(celsius, 0);
lcd.setCursor(0, 1);
lcd.print("Humidity: % ");
lcd.setCursor(10, 1);
lcd.print(humidity, 0);
Serial.print(celsius, 0);
Serial.print(" C, ");
Serial.print(humidity, 0);
Serial.println(" %");
}
Code walkthrough
The key functions of the sketch, and what each one does.
setup sketch.ino
Starts the sensor with dht.begin() and the LCD with lcd.begin(16, 2), which means 16 columns and 2 rows. It also opens serial at 9600 baud and prints a short startup message.
void setup() {
dht.begin();
lcd.begin(16, 2);
Serial.begin(9600);
Serial.println("Mokxi Uno: DHT11 on pin 3, LCD on pins 4, 6 and 10 to 13");
}loop sketch.ino
Waits two seconds, then reads humidity and temperature. If either is not a number, it shows an error and tries again. Otherwise it writes the labels, places each value at a fixed column with setCursor, and prints the result to serial.
void loop() {
delay(2000);
float humidity = dht.readHumidity();
float celsius = dht.readTemperature();
if (isnan(humidity) || isnan(celsius)) {
lcd.clear();
lcd.print("Sensor error");
Serial.println("no reading: check the DATA wire");
return;
}
lcd.setCursor(0, 0);
lcd.print("Temp: C ");
lcd.setCursor(6, 0);
lcd.print(celsius, 0);
lcd.setCursor(0, 1);
lcd.print("Humidity: % ");
lcd.setCursor(10, 1);
lcd.print(humidity, 0);
Serial.print(celsius, 0);
Serial.print(" C, ");
Serial.print(humidity, 0);
Serial.println(" %");
}Troubleshooting
The usual mistakes with this circuit, and the fix for each.
- The LCD shows Sensor error This project
Check the DATA wire goes to pin 3 and that the sensor has 5 V and ground. - The LCD shows blocks or nothing This project
Check the LCD power, ground and the pins for RS, E and D4 to D7 against the sketch. V0 and RW go to ground. - Values update slowly This project
That is by design. The sensor needs about two seconds between readings. - The screen lights up but shows only a row of solid boxes, or nothing at all. Character LCD
Contrast pin V0 needs a voltage. Connect the middle pin of a 10 k pot to V0, with its ends on 5 V and GND. Also confirm that the code calls lcd.begin(16, 2). - Garbled characters or a blank display even with good contrast. Character LCD
In 4-bit mode only D4 to D7 are used, and D0 to D3 stay unconnected. Tie RW to GND. Match the pins in LiquidCrystal(rs, en, d4, d5, d6, d7) exactly to your wiring. - 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.
Change it and keep it
Open it in the editor, change the circuit or the code, and keep your version in a free account.