Displaying temperature sensor data on a Liquid Crystal Display with Arduino

This blog post expands on displaying Hello World on a Liquid Crystal Display to show temperature sensor values on this display.

Combining the LCD display and DS18B20 Temperature Sensor

This project combines two of my favourite pieces of electrical components the 16×2 LCD display and the DS18B20 temperature sensor. Both have high-quality Arduino libraries and a simple connection interface. This makes this project ideal for a beginner in electronics.

If you haven’t already I recommend looking at both the 16×2 Liquid crystal display and the DS18B20 temperature sensor posts. These demonstrate using each of these devices separately.

In this post I will be reading the current temperature and displaying it on the LCD display.

Wiring it up to the Arduino

As noted above this project combines both the 16×2 Liquid crystal display and the DS18B20 temperature sensor. For convenience, the combined sketch is represented below.

The pins used for this sketch are the same as the previous ones linked above to make it easy to expand from these projects.

Programming the LCD to display the Temperature

The code below combines using both the LCD display library and the DallasTemperature library to obtain the temperature.

#include <LiquidCrystal.h>
#include <OneWire.h>
#include <DallasTemperature.h>

#define ONE_WIRE_PIN 8
OneWire oneWire(ONE_WIRE_PIN);
DallasTemperature sensors(&oneWire);

LiquidCrystal lcd(12, 11, 5, 4, 3, 2);

void setup() { 
    lcd.begin(16, 2);
    lcd.print("Current Temp:");
}

void loop() {
    lcd.setCursor(0, 1);
    sensors.requestTemperatures();
    lcd.print(sensors.getTempCByIndex(0));
    lcd.print("C");
    delay(1000);
}

First both LCD and temperature libraries are included and the lcd and sensor variables initialized.

Once this is done the LCD is configured in the setup command and I write the first line “Current Temp:” to the LCD. This is performed in the setup as the top line will not change. By default writing to the LCD screen begins from the first row and first column (denoted 0, 0).

In the loop the position of the cursor is set to the first character (numbered 0) of the second line (numbered 1). This is denoted as (0, 1). Then the temperature is requested and written. The second print call then adds C to the end of the temperature reading to finish off the code.

Once done, we delay by 1000ms and repeat the process. This then slowly updates the LCD so it is always displaying the current temperature.

Summary

Here I combine the 16×2 LCD display with a temperature sensor to have an updatable display of the current temperature. I have used a DS18B20 temperature sensor but any sensor could be used to display any kind of value.

In the next project I will look at display several different values by scrolling the LCD screen.

Add a Comment

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.