CYTHEIA AIOpen the editor

I2C LCD Thermometer

A temperature sensor shows its reading on a 16x2 LCD that needs just four wires, thanks to its I2C backpack.

Open this circuit and run it

Opens in your browser with the parts, wiring and code already in place. Press Run to watch it work, then change anything you like.

How it works

The temperature sensor works as in the Temperature Monitor. The display is a 16×2 character LCD with an I2C “backpack”: a small board that turns the display’s many pins into just two wires, SDA (A4) and SCL (A5), plus power and ground. Like every I2C device it answers at an address, usually 0x27 (some modules use 0x3F). If the address in the sketch does not match the module, the screen stays blank, which is the first thing to check on real hardware.

setup() starts the display with lcd.init(), turns the backlight on and writes “Temperature:” on the first row. loop() reads the sensor, converts it to degrees (10 millivolts per degree), moves the cursor to the start of the second row, and prints the temperature to one decimal place followed by “ C” and a few spaces. The spaces wipe out leftover characters when a reading gets shorter, for example from 100.5 to 99.5. It updates twice a second.

Parts

Wiring

Arduino code

// I2C LCD Thermometer
// An I2C backpack lets a 16x2 LCD run from just SDA and SCL (plus power).
// The module answers at one address, usually 0x27 (some are 0x3F): if the
// screen stays blank on a real board, a wrong address is the first thing to
// check. Click the LCD and change its address to 0x3F to see for yourself.
#include <Wire.h>
#include <LiquidCrystal_I2C.h>

LiquidCrystal_I2C lcd(0x27, 16, 2);

void setup() {
  lcd.init();
  lcd.backlight();
  lcd.setCursor(0, 0);
  lcd.print("Temperature:");
}

void loop() {
  int reading = analogRead(A0);
  float tempC = reading * 5.0 / 1024.0 * 100.0;   // 10 mV per degree

  lcd.setCursor(0, 1);
  lcd.print(tempC, 1);
  lcd.print(" C   ");   // the spaces wipe out digits left from a longer reading
  delay(500);
}

Try changing it

Open this circuit and run it

Next challenge

More projects in Displays