I2C LCD Thermometer
A temperature sensor shows its reading on a 16x2 LCD that needs just four wires, thanks to its I2C backpack.
- Intermediate
- Displays
- 3 parts
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
- Arduino Uno
- Temperature Sensor
- LCD 16x2 (I2C)
Wiring
- Arduino Uno 5V → Temperature Sensor VCC
- Temperature Sensor OUT → Arduino Uno A0
- Arduino Uno GND → Temperature Sensor GND
- Arduino Uno 5V → LCD 16x2 (I2C) VCC
- Arduino Uno GND → LCD 16x2 (I2C) GND
- Arduino Uno A4 → LCD 16x2 (I2C) SDA
- Arduino Uno A5 → LCD 16x2 (I2C) SCL
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
- See the address problem for yourself: select the LCD in the editor and change its address to 0x3F. The screen goes blank because the sketch still uses 0x27. Then change the sketch to LiquidCrystal_I2C lcd(0x3F, 16, 2) and it works again.
- Show Fahrenheit by changing lcd.print(tempC, 1) to lcd.print(tempC * 9.0 / 5.0 + 32.0, 1) and the unit to “ F”.