16×2 LCD guides · On this page

Show Temperature and Humidity on a 16×2 LCD

Show the temperature and humidity on a 16×2 LCD, with an Arduino Uno and a DHT11. The top line shows the temperature with a degree sign, the second line the humidity, and both update every two seconds.

It builds on two guides: the 16×2 I²C LCD guide covers the screen on its own, and the DHT11 guide the sensor. A DHT11 is typically within 2 °C, and within 5 percentage points of humidity between 8 and 85 % at 25 °C and 5 V without condensation, so treat the readings as a guide to the room, not a measurement.

Try it first, no parts needed

This is the whole project, running in your browser. Press Run simulation, then move the temperature or the humidity, or hold the button to breathe on the sensor, and watch the LCD follow within two seconds.

Ready
Open in Cirkit Designer

The air around the DHT11

The sketch reads it every 2 seconds.

25 °C
60 %

What the LCD shows

The display's 16 × 2 characters, enlarged.

The screen lights up when the simulation runs.

Arduino Uno serial monitor115200 baud

Output appears here when the simulation runs.

What the code does

The sketch reads the DHT11 every two seconds, clears the LCD and writes the two readings, one to a line.

dht11_lcd.ino
// A DHT11's readings on a 16x2 I2C LCD: the temperature on
// line 1 and the humidity on line 2, every two seconds.
#include <Wire.h>
#include <LiquidCrystal_I2C.h>
#include <DHT.h>

LiquidCrystal_I2C lcd(0x27, 16, 2);  // address, columns, rows
DHT dht(2, DHT11);                    // DATA on pin 2

void setup() {
  Serial.begin(115200);
  dht.begin();
  lcd.init();
  lcd.backlight();
  lcd.print("Reading...");
}

void loop() {
  delay(2000);  // the DHT11 reads at most every 2 s
  float humidity = dht.readHumidity();
  float temperature = dht.readTemperature();
  lcd.clear();
  if (isnan(humidity) || isnan(temperature)) {
    lcd.print("Read failed");
    Serial.println("Read failed");
    return;
  }
  lcd.print("Temp     ");
  lcd.print(temperature, 1);
  lcd.print((char)223);  // the LCD's degree sign
  lcd.print("C");
  lcd.setCursor(0, 1);   // column 0, line 2
  lcd.print("Humidity ");
  lcd.print(humidity, 1);
  lcd.print(" %");
  Serial.print(temperature, 1);
  Serial.print(" C  ");
  Serial.print(humidity, 1);
  Serial.println(" %");
}
  1. DHT dht(2, DHT11);                    // DATA on pin 2

    The DHT11 on pin 2. The LCD line above it is the same as in the LCD guide: address 0x27, 16 columns, 2 lines.

  2. lcd.print("Reading...");

    Shows Reading... until the first reading, two seconds later. Starting the LCD takes about a second, so that is about three seconds after the Uno starts.

  3. delay(2000);  // the DHT11 reads at most every 2 s

    Waits two seconds between readings, as the DHT11's manual asks.

  4. if (isnan(humidity) || isnan(temperature)) {
      lcd.print("Read failed");
      Serial.println("Read failed");
      return;
    }

    A failed read gives NaN, not a number, so the sketch shows Read failed and tries again in two seconds. See troubleshooting.

  5. lcd.print((char)223);  // the LCD's degree sign

    Prints the LCD's own degree sign, character 223, then C. See how the lines fit.

  6. lcd.setCursor(0, 1);   // column 0, line 2

    Moves to the start of the second line for the humidity.

How the two lines fit

Each line of the LCD holds 16 characters, so the sketch lays each reading out to fit: a word, the number to one decimal place, and its unit. At 25.0 °C and 60.0 %, each line comes to 15 characters, and the spaces afterTemp line the two numbers up.

The project's two lines on the 16 by 2 screen, at 25.0 °C and 60.0 %. Top line: Temp, five spaces, 25.0, the degree sign, C. Second line: Humidity, a space, 60.0, a space, the percent sign. Each line is 15 characters. The degree sign, in column 13, is character 223 of the LCD's own character set.

The LCD draws characters from its own set: on the common one, Hitachi's ROM A00, ° is character 223, a small raised circle. A "°" typed into the sketch is stored as two other codes, which the LCD draws as two other characters. So the sketch prints (char)223.

The sketch clears the screen before each reading, so a shorter number, such as 9.0 after 10.0, never leaves an old digit behind.

The readings come from the DHT11 as in its own guide: it answers at most every two seconds, and a failed read gives NaN, which the sketch shows as Read failed. How the DHT11 sends a reading is in the DHT11 guide.

Build it

Now with real parts. Wire it, get the code, then upload it and watch the screen.

You'll need

  • An Arduino Uno and its USB cable
  • A DHT11 and a 4.7 kΩ resistor, or a three-pin DHT11 module, which has the resistor built in
  • A 16×2 LCD with an I²C backpack (GND, VCC, SDA, SCL)
  • A breadboard, so the two parts can share the Uno's 5V and GND, and jumper wires: female-to-male for the LCD's four pins
  • Software to upload the sketch: Cirkit Designer, free in your browser, or the Arduino IDE
  1. Wire it

    The DHT11 is wired as in the DHT11 guide, and the LCD as in the LCD guide.

    Tap a connection to see it on the circuit.

    PinConnect to the UnoShow
    DHT11 VDD5VShow
    DHT11 DATAD2Show
    Pull-up4.7 kΩ resistor from DATA to 5VShow
    DHT11 NCLeave unconnectedShow
    DHT11 GNDGNDShow
    LCD GNDGNDShow
    LCD VCC5VShow
    LCD SDAA4Show
    LCD SCLA5Show

    Both parts share 5V and GND. In the picture their wires meet at the Uno's pins; with real parts, run 5V and GND to a breadboard's rails and take both parts' power from there. A three-pin DHT11 module needs no resistor: wire its labelled pins to 5V, D2 and GND.

  2. Get the code

    I'm using:

    Steps 2 and 3 follow the tool you pick.

    Open in Cirkit Designer

    The project opens with the sketch and its libraries already in it, so there's nothing to install.

    The sketch needs two libraries. Open the Library Manager (1), search for LiquidCrystal I2C (2), and install the one by Frank de Brabander (3). Then search for DHT sensor library and install the one by Adafruit; when it asks about dependencies, choose Install all, which adds Adafruit Unified Sensor.

    Arduino UnoLIBRARY MANAGERLiquidCrystal I2CType: AllTopic: AllLiquidCrystal I2Cby Frank de BrabanderA library for I2C LCD displays.More infoINSTALL123

    Then copy the sketch into a new one:

    How it works line by line is in what the code does, above.

  3. Upload and check it

    Plug in your Uno. In Cirkit, open the Build tab, then Upload firmware: click Select Port and pick your Uno, then Upload to Device. The first time, Cirkit asks you to install Cirkit Create Agent, a small desktop helper that lets it reach the board over USB. Then open Upload output and serial monitor and click Start Serial Monitor.

    Show me where to click in Cirkit
    DHT11 on a 16x2 I2C LCDFileEditExportHelp
    DesignCodeSimulateBuild
    1Parts & purchasing2Assemble3Upload firmware
    Arduino UnoEdit Code

    Board

    Arduino Uno Change

    2

    Port

    Select PortCOM3 Connected

    3

    Upload

    Upload to DeviceUploading...Upload successful

    4

    Check that it works

    Your project should now run as designed.

    Upload output and serial monitor
    Compile / Upload Output
    Sketch uses 8032 bytes (24%) of program storage space. Maximum is 32256 bytes.
    Global variables use 543 bytes (26%) of dynamic memory, leaving 1505 bytes for local variables. Maximum is 2048 bytes.
    Device SerialStop

    Read serial data from your device.

    Start Serial Monitor
    Listening on COM3 at 115200 baud
    25.0 C 60.0 %
    25.0 C 61.0 %
    25.0 C 66.0 %
    25.0 C 74.0 %
    25.0 C 80.0 %
    Select port

    Select a device port

    Select the device port for uploading code and serial communications

    COM3 (Arduino Uno)
    1. Open Build
    2. Choose Upload firmware
    3. Click Select Port
    4. Pick COM3 (Arduino Uno)
    5. Click Upload to Device
    6. Open Upload output and serial monitor
    7. Click Start Serial Monitor

    In the Arduino IDE, choose Arduino Uno and its port from the board menu, press Upload, then open the Serial Monitor and set it to 115200 baud.

    Show me where to click in the Arduino IDE
    Select BoardArduino Uno

    Select other board and port

    Arduino UnoCOM3
    dht11_lcd.ino
    // A DHT11's readings on a 16x2 I2C LCD: the temperature on
    // line 1 and the humidity on line 2, every two seconds.
    #include <Wire.h>
    #include <LiquidCrystal_I2C.h>
    #include <DHT.h>
    
    OutputSerial Monitor115200 baud

    Compiling sketch...

    Sketch uses 8032 bytes (24%) of program storage space. Maximum is 32256 bytes.

    Global variables use 543 bytes (26%) of dynamic memory, leaving 1505 bytes for local variables. Maximum is 2048 bytes.

    Uploading...

    25.0 C 60.0 %

    25.0 C 61.0 %

    25.0 C 66.0 %

    25.0 C 74.0 %

    25.0 C 80.0 %

    Done uploading.

    1. Click the board menu
    2. Pick Arduino Uno on COM3
    3. Click Upload
    4. Wait for Done uploading
    5. Click Serial Monitor
    6. Check it is set to 115200 baud

    It works when:

    • The LCD shows Reading..., then the temperature and humidity two seconds later.
    • You breathe on the sensor and, within a few readings, the humidity on the second line rises.
    • The Serial Monitor prints the same reading, like 25.0 C 60.0 %.
    Two pictures of the LCD. First: Temp 25.0°C, Humidity 60.0 %. Second, after breathing on the sensor: Temp 25.0°C, Humidity 80.0 %.
    A reading of 25 °C and 60 %, then the humidity at 80 % after breathing on the sensor.

    Not working? See troubleshooting.

    Next guideWant a bar and a comfort label? Try an OLEDThe same DHT11 on a 128 × 64 screen, with a humidity bar and a label for dry or humid air.

Troubleshooting

Find your symptom; the checks under it run from the most common cause down.

The LCD stays blank, but the Serial Monitor shows readings

The DHT11 and the Uno work; the trouble is the LCD. Turn its contrast trimmer, check its address and check SDA on A4 and SCL on A5, as in LCD troubleshooting.

The LCD shows "Read failed"

The LCD works, but the DHT11 isn't answering: check its type, pull-up and DATA pin as in DHT11 troubleshooting.

The LCD stays on "Reading..."

  • Wait three seconds. The first reading comes about three seconds after the Uno starts: a second to start the LCD, then two.
  • Check the Serial Monitor. If it prints readings, the LCD isn't updating: check its wires as in LCD troubleshooting.

The degree sign shows as another character

Print (char)223, as the sketch does, not "°"; see how the lines fit. If 223 shows as ß, your LCD has the European character set, ROM A02, where ° is 176: print (char)176 instead.

"LiquidCrystal_I2C.h" or "DHT.h": No such file or directory

Arduino IDE only
  • Both libraries are installed. Install LiquidCrystal I2C by Frank de Brabander, then DHT sensor library by Adafruit with Install all. Cirkit Designer includes them.

Make it yours

  • Show Fahrenheit: read the temperature in °F and change the unit the LCD prints.
    Change two lines
    float temperature = dht.readTemperature(true);  // °F
    // and where the unit is printed:
    lcd.print("F");
  • Stop the flicker: clearing the screen blanks it for a moment every two seconds. Instead, print the words once in setup(), drop lcd.clear(), and write each number over the last one at the same place, with a space after it to cover a digit left over. The humidity goes the same way at column 9 of line 2. Print the words again after a Read failed, which writes over them.
    The temperature, written in place
    lcd.setCursor(9, 0);   // where the temperature starts
    lcd.print(temperature, 1);
    lcd.print((char)223);
    lcd.print("C ");        // a space covers a digit left over
  • More accurate readings over a wider range: swap in a DHT22 and change DHT11 to DHT22. See DHT11 vs DHT22.

FAQ

Why do my readings end in .0?

Because your DHT11 reports whole numbers, as older DHT11s and Cirkit's DHT11 do. Aosong's current DHT11 reports tenths, and the sketch's one decimal place shows them, as it does a DHT22's.

My LCD's address isn't 0x27. What do I change?

The 0x27 in the lcd(…) line. Try 0x3F next: a backpack with a PCF8574A chip answers there when its A0 to A2 are all high. To find yours for certain, see finding the address.

Can I use a 20×4 LCD and show more?

Yes. Change the lcd(…) line to 20 columns and 4 rows, and use setCursor(0, 2) and setCursor(0, 3) for the extra lines, for a heading or the time since the last reading.

Simulating the DHT11 and LCD in Cirkit Designer

Cirkit's LCD answers the LiquidCrystal I2C library over the Uno's real I²C pins, as described in the LCD guide: its degree sign is character 223, and there is no contrast to adjust. The DHT11 is simulated as in the DHT11 guide: whole numbers, with no measurement error or response lag.

References