Skip to content
GEEKnodes

Electronics · Updated on

Arduino I2C LCD with DHT11 Temperature Sensor

Connect a 16x2 LCD with an I2C module and a DHT11 to an Arduino UNO: wiring diagram, LiquidCrystal_I2C library, I2C address scanner, full code and fixes.

In this project you’ll display temperature and humidity on a 16×2 LCD connected to an Arduino UNO through an I2C module, using a DHT11 sensor. The I2C module (a small board soldered to the back of the display, based on the PCF8574 chip) is what makes the build easy: without it you’d need about 16 wires and 6 Arduino pins; with it, 4 wires are enough, two of them for power.

It’s a good project to get started with I2C and sensors, and the basis for any homemade thermometer or weather station.

Parts

  • Arduino UNO (or compatible).
  • 16×2 LCD with I2C module (it usually comes pre-soldered; in the diagram it appears as LCM1602 IIC).
  • DHT11 sensor (bare or on a 3-pin module).
  • 10 kΩ resistor if you use the bare DHT11 (modules already include it).
  • Breadboard and jumper wires.

Wiring diagram

Fritzing diagram: Arduino UNO connected to a 16x2 LCD with an I2C module and to a DHT11 sensor

I2C LCD

I2C moduleArduino UNO
GNDGND
VCC5V
SDAA4
SCLA5

On the Arduino UNO, A4 and A5 are the I2C pins (they’re also duplicated as SDA and SCL next to AREF). They differ on other boards: on an Arduino Mega they’re pins 20 and 21.

DHT11 sensor

DHT11Arduino UNO
VCC5V
DATADigital pin 2
GNDGND

If your DHT11 is the bare 4-pin sensor, pin 3 is left unconnected and you need a 10 kΩ resistor between DATA and VCC.

1. Install the libraries

In the Arduino IDE, open the Library Manager (Tools › Manage Libraries or the libraries icon in the sidebar in IDE 2) and install:

  • LiquidCrystal I2C, by Frank de Brabander. It drives the display through the I2C module.
  • DHT sensor library, by Adafruit. When you install it, the IDE will offer to install its dependency, Adafruit Unified Sensor, as well: accept.

The Wire library, for the I2C bus, already comes with the IDE.

2. Find the display’s I2C address

Every I2C device has an address. For these modules it’s usually 0x27 or 0x3F, depending on the chip they use, but the quickest way is to check with this scanner. Upload it to the Arduino and open the Serial Monitor at 9600 baud:

Scanner querying the display over the SDA and SCL bus and showing the possible I2C addresses 0x27 or 0x3F.
The scanner finds the address you need to use in the code.
#include <Wire.h>

void setup() {
  Wire.begin();
  Serial.begin(9600);
  Serial.println("Scanning for I2C devices...");

  byte encontrados = 0;
  for (byte direccion = 1; direccion < 127; direccion++) {
    Wire.beginTransmission(direccion);
    if (Wire.endTransmission() == 0) {
      Serial.print("Device at 0x");
      if (direccion < 16) Serial.print("0");
      Serial.println(direccion, HEX);
      encontrados++;
    }
  }
  if (encontrados == 0) Serial.println("No devices found. Check SDA, SCL and power.");
}

void loop() {}

Write down the address that shows up: you’ll use it in the code.

3. Full code

#include <Wire.h>
#include <LiquidCrystal_I2C.h>
#include <DHT.h>

#define DHTPIN 2        // DHT11 data pin
#define DHTTYPE DHT11   // Change to DHT22 if you use that sensor

// I2C address (from the scanner), display columns and rows
LiquidCrystal_I2C lcd(0x27, 16, 2);
DHT dht(DHTPIN, DHTTYPE);

void setup() {
  lcd.init();        // Initialize the display
  lcd.backlight();   // Turn on the backlight
  dht.begin();       // Initialize the sensor

  lcd.setCursor(0, 0);
  lcd.print("Starting...");
  delay(2000);       // The DHT11 needs a moment after power-up
  lcd.clear();
}

void loop() {
  float humedad = dht.readHumidity();
  float temperatura = dht.readTemperature();  // In degrees Celsius

  // If the reading fails, the library returns NaN
  if (isnan(humedad) || isnan(temperatura)) {
    lcd.setCursor(0, 0);
    lcd.print("Sensor error    ");
    delay(2000);
    return;
  }

  // First row: temperature
  lcd.setCursor(0, 0);
  lcd.print("Temp: ");
  lcd.print(temperatura, 1);
  lcd.print((char)223);  // Degree symbol on the display
  lcd.print("C   ");

  // Second row: humidity
  lcd.setCursor(0, 1);
  lcd.print("Hum:  ");
  lcd.print(humedad, 0);
  lcd.print(" %   ");

  delay(2000);  // The DHT11 can't take more than one reading per second
}

A few details about the code:

  • The spaces at the end of each string clear leftovers from the previous reading without having to call lcd.clear(), which makes the display flicker.
  • (char)223 is the degree symbol in these displays’ character set.
  • If your scanner gave you a different address, change it in LiquidCrystal_I2C lcd(0x27, 16, 2);. For a 20×4 display, use 20, 4.

Check that it works

When you power the Arduino, the display should show “Starting…” and, two seconds later, the temperature and humidity, which update every two seconds. Hold your hand near the sensor: humidity and temperature should rise slowly.

Two-row LCD showing temperature and humidity, next to a DHT11 sensor and a two-second update interval.
The display shows temperature and humidity and updates every two seconds.

Common problems

The display lights up but shows no text, or only rectangles. It’s the contrast. Turn the blue potentiometer on the I2C module with a screwdriver until the characters appear. This is by far the most common issue.

The display doesn’t light up or show anything. Check VCC and GND. If the scanner doesn’t find any device, SDA and SCL are swapped or badly connected.

The scanner finds the display, but nothing gets written. The address in the code doesn’t match the scanner’s, or the backlight jumper on the module isn’t fitted.

“Sensor error” appears. Check that the DHT11’s DATA goes to pin 2, that DHTTYPE matches your sensor and that it has the 10 kΩ resistor if it’s the bare sensor.

The values don’t change or look odd. The DHT11 is slow and not very accurate (about ±2 °C). If you need more accuracy, the DHT22 works with the same code by changing DHTTYPE.

Frequently asked questions

Can I connect other I2C devices at the same time? Yes: they share SDA and SCL, as long as each one has a different address. The scanner will tell you.

Does it work with a 3.3 V board, like an ESP32? Most of these displays need 5 V to look right. You can power it from 5 V, but it’s a good idea to use a level shifter on SDA and SCL.

Parts used

ELEGOO UNO R3 AZDelivery LCD 16x2 I2C

Accessibility preferences

Quick profiles

Text and reading

Text size
Default
Line height
Default
Letter spacing
Default
Word spacing
Default

Colour

Motion and orientation

Cursor