How to display a temperature value on a 20x4 LCD display?

Jun 01, 2026Leave a message

Hey there! As a supplier of 20x4 LCD displays, I often get asked about how to display a temperature value on these nifty little screens. So, I thought I'd put together this blog post to walk you through the whole process.

16 * 4 Lcd Character Display20x2 Lcd Display Lcd Module

First off, let's talk a bit about the 20x4 LCD display. It's a great choice for a lot of projects, especially when you need to show a decent amount of information. The "20x4" means it has 20 characters per line and 4 lines in total. That gives you plenty of space to display not just the temperature but also some additional details like units (Celsius or Fahrenheit), and maybe a timestamp.

Now, before we dive into the technical stuff, let's quickly mention some other LCD displays that you might also be interested in. If you're looking for something a bit smaller, we've got the 16x2 lcd display. It's a classic, with 16 characters per line and 2 lines. Then there's the 20x2 LCD Display, which gives you a bit more width but still just 2 lines. And if you want a bit more height, the 16 x 4 lcd display is a solid option.

Alright, back to the 20x4 LCD and displaying temperature. The first thing you'll need is a temperature sensor. There are tons of them out there, but a popular one is the DHT11 or DHT22. These sensors are relatively easy to use and can give you accurate temperature readings.

Once you've got your sensor, you'll need to connect it to your microcontroller. I'm going to assume you're using an Arduino for this example, but the general concepts apply to other microcontrollers too.

Connecting the Temperature Sensor to the Arduino

  • Power: Connect the VCC pin of the DHT sensor to the 5V pin on the Arduino. This provides the power the sensor needs to work.
  • Ground: Connect the GND pin of the sensor to the GND pin on the Arduino. This completes the electrical circuit.
  • Data: Connect the data pin of the sensor to one of the digital pins on the Arduino. Let's say we connect it to pin 2.

Connecting the 20x4 LCD to the Arduino

  • Power: Connect the VSS pin of the LCD to GND and the VDD pin to 5V.
  • Contrast: Connect the VO pin to a potentiometer. This allows you to adjust the contrast of the display so you can see the characters clearly.
  • Control Pins: Connect the RS pin to digital pin 12, the EN pin to digital pin 11, and the D4 - D7 pins to digital pins 5 - 8 respectively. These pins are used to send commands and data to the LCD.

Writing the Code

Now that everything is connected, it's time to write the code. Here's a basic example:

#include <LiquidCrystal.h>
#include <DHT.h>

// Initialize the LCD library with the numbers of the interface pins
LiquidCrystal lcd(12, 11, 5, 6, 7, 8);

// Initialize DHT sensor
#define DHTPIN 2
#define DHTTYPE DHT11
DHT dht(DHTPIN, DHTTYPE);

void setup() {
  // Set up the LCD's number of columns and rows:
  lcd.begin(20, 4);
  // Start the DHT sensor
  dht.begin();
}

void loop() {
  // Reading temperature or humidity takes about 250 milliseconds!
  // Sensor readings may also be up to 2 seconds 'old' (its a very slow sensor)
  float h = dht.readHumidity();
  float t = dht.readTemperature();

  // Check if any reads failed and exit early (to try again).
  if (isnan(h) || isnan(t)) {
    lcd.setCursor(0, 0);
    lcd.print("Failed to read from DHT sensor!");
    return;
  }

  // Display temperature on the LCD
  lcd.setCursor(0, 0);
  lcd.print("Temperature: ");
  lcd.print(t);
  lcd.print(" C");

  delay(2000);
}

Let's break down what's going on in this code:

  • Libraries: We're including the LiquidCrystal library to control the LCD and the DHT library to read the temperature sensor.
  • Initialization: In the setup() function, we initialize the LCD with the correct number of columns and rows (20x4). We also start the DHT sensor.
  • Reading the Sensor: In the loop() function, we read the humidity and temperature from the DHT sensor. If the readings are invalid, we display an error message on the LCD.
  • Displaying the Temperature: If the readings are valid, we set the cursor to the beginning of the first line on the LCD, print the text "Temperature: ", followed by the temperature value and the unit " C".
  • Delay: We add a delay of 2 seconds before reading the sensor again. This gives the sensor enough time to take accurate readings.

Additional Considerations

  • Unit Conversion: If you want to display the temperature in Fahrenheit instead of Celsius, you can use the following formula: t = dht.readTemperature(true); The true parameter tells the readTemperature() function to return the temperature in Fahrenheit.
  • Error Handling: In the code above, we just display an error message if the sensor readings are invalid. You could add more sophisticated error handling, like trying to read the sensor again a few times before giving up.
  • Display Formatting: You can play around with the display formatting to make it look nicer. For example, you could add padding to the temperature value so it always takes up the same number of characters.

Conclusion

And that's it! You now know how to display a temperature value on a 20x4 LCD display using an Arduino and a DHT sensor. It's a fun and useful project that can be expanded in many ways.

If you're interested in purchasing 20x4 LCD displays or any of our other LCD products, we'd love to hear from you. Just reach out to us to start a conversation about your specific needs. We're here to help you find the perfect display solution for your project.

References

  • Arduino Documentation
  • DHT Sensor Datasheet
  • LiquidCrystal Library Documentation