Thermometer on an I2C LCD, on an Arduino Uno
A 10 k NTC bead in a divider on A0, the temperature on a 16x2 character LCD over its PCF8574 backpack. Four wires to the display. The whole build, an Arduino Uno and 3 more parts, runs here in your browser on the firmware below; open it in the editor to change the wiring or the code and run it again.
Teaching? Assign it to your class in one click.
Intermediate About 20 minutes to build and run. Runs in your browser. Free, and no account needed.
The parts retail for about $34. On Mokxi they're free.
Step by step, with the wiring and the common mistakes: Arduino LCD I2C 16x2: Wiring, Code, Scrolling Text
What you'll learn
- A voltage divider with a thermistor
- Looking up values in a table and interpolating between them
- Averaging samples to calm a noisy reading
- Talking to an LCD over I2C with four wires
- Tracking a minimum and a maximum
- Difficulty: Intermediate
- Time: About 20 minutes
- Board: Arduino Uno R3
- Parts: 5
How it works
A 10k thermistor and a fixed 10k resistor form a voltage divider. The junction goes to A0. As the thermistor warms, its resistance drops and the voltage on A0 rises. The sketch reads it as a number from 0 to 1023.
The Uno has no easy way to calculate a logarithm, so the sketch uses a table. It lists what the reading should be at every ten degrees from -20 to 100 C. It finds the two entries the reading falls between and draws a straight line across.
The LCD only needs four wires: power, ground, SDA and SCL. The top line shows the temperature. The bottom line shows the lowest and highest seen so far.
Parts list
5 parts, plus the jumper wires. They retail for about $34; on Mokxi they're free.
- 1 × Arduino Uno R3In Starter Kit, Claw Machine Kit, Vending Machine Kit.
- 1 × Half breadboard
- 1 × Thermistor, 10k NTC
- 1 × Resistor, 10k ΩIn Starter Kit, Claw Machine Kit, Halloween Prop Kit.
- 1 × Character LCD with I2C backpackIn Claw Machine Kit, Vending Machine Kit, Rhythm Game Kit.
Kits that can build it
These 3 starter kits have every part in the circuit. Each kit's page lists what else it can build.
Wiring table
Every connection, pin by pin, read from the circuit itself. Each row joins the pin on the left to the pin on the right, by a jumper wire or a breadboard strip.
| Part and pin | Connects to |
|---|---|
| Thermistor, 10k NTC pin 1 | Arduino Uno R3 pin 5V |
| Character LCD with I2C backpack pin VCC | Arduino Uno R3 pin 5V |
| Resistor, 10k Ω pin 2 | Arduino Uno R3 pin GND |
| Character LCD with I2C backpack pin GND | Arduino Uno R3 pin GND |
| Thermistor, 10k NTC pin 2 | Arduino Uno R3 pin A0 |
| Resistor, 10k Ω pin 1 | Arduino Uno R3 pin A0 |
| Character LCD with I2C backpack pin SDA | Arduino Uno R3 pin A4 |
| Character LCD with I2C backpack pin SCL | Arduino Uno R3 pin A5 |
The code
The firmware exactly as the editor opens it. Change a line there and press Run: it compiles in the browser.
// I2C thermometer: a 10 k NTC bead on A0, the reading on a 16x2 LCD that
// only needs four wires.
//
// A0 the junction of the bead and a fixed 10 k
// bead from 5 V down to A0, 10 k from A0 down to GND
// LCD SDA -> A4 SCL -> A5 VCC -> 5 V GND -> GND
//
// Two things are worth the trip here, and they are both about wires.
//
// # Four wires instead of sixteen
//
// The display is the same HD44780 panel as the six-wire one (see the
// `thermometer` example), with a PCF8574 port expander soldered to the back of
// it. Every byte written to the expander lands on its eight pins, and those
// pins are the LCD's RS, RW, E, backlight and D4..D7. So the driver
// (firmware/lib/mokxi_lcd1602_i2c.h) sends a character as six port writes and
// the controller sees exactly what it would have seen from six board pins.
//
// The backlight is bit 3 of that port and nothing else. Forget it and the
// display works perfectly and shows you nothing, which is the one fault this
// module has that the six-wire one does not. begin() turns it on.
//
// # A logarithm without a logarithm
//
// The bead is 10 k at 25 C and falls steeply as it warms; it follows
//
// R(T) = 10000 x exp(3950 x (1/T - 1/298.15))
//
// with T in kelvin. Turning a reading back into degrees means a logarithm, and
// this board has no floating point at all, let alone a log. So the sketch does
// what a real one does: a table of what the ADC reads at each ten degrees,
// worked out once on a desk, and a straight line between the two entries the
// reading falls between. Ten-degree steps are close enough that the error from
// the interpolation is under half a degree over the whole range, which is
// smaller than the bead's own tolerance.
//
// Drag the slider on the bead to change the temperature. The second line keeps
// the lowest and highest it has seen.
#include "mokxi_lcd1602_i2c.h"
const int SENSOR = A0;
const int SDA_PIN = A4;
const int SCL_PIN = A5;
Lcd1602I2c lcd(SCL_PIN, SDA_PIN);
// What the ADC reads at -20, -10, 0 ... 100 C, for a 10 k bead above a fixed
// 10 k against the 5 V reference. Rising, because a warm bead is a small
// resistance and pulls A0 up.
const int TABLE_LOW_C = -20;
const int TABLE_STEP_C = 10;
const int TABLE_N = 13;
const int READINGS[TABLE_N] = {89, 150, 235, 339, 454, 568, 669, 754, 820, 871, 909, 937, 957};
int lowest = 32767;
int highest = -32768;
// Tenths of a degree for an ADC reading, by walking the table and drawing a
// straight line between the two entries it falls between.
int tenthsFor(int reading) {
if (reading <= READINGS[0]) {
return TABLE_LOW_C * 10;
}
for (int i = 1; i < TABLE_N; i++) {
if (reading <= READINGS[i]) {
long span = READINGS[i] - READINGS[i - 1];
long into = reading - READINGS[i - 1];
long base = (long)(TABLE_LOW_C + (i - 1) * TABLE_STEP_C) * 10;
return (int)(base + (into * TABLE_STEP_C * 10) / span);
}
}
return (TABLE_LOW_C + (TABLE_N - 1) * TABLE_STEP_C) * 10;
}
// Sixteen samples added up and shifted right by four: a divide-free mean, and
// the ADC's last bit is noise so an unfiltered display never stops flickering.
int readAveraged() {
long total = 0;
for (int i = 0; i < 16; i++) {
total += analogRead(SENSOR);
}
return (int)(total >> 4);
}
void setup() {
lcd.begin();
Serial.begin(115200);
Serial.println("Mokxi Uno: NTC thermometer on an I2C LCD at 0x27");
lcd.print("NTC on I2C");
delay(600);
lcd.clear();
}
void loop() {
int tenths = tenthsFor(readAveraged());
if (tenths < lowest) {
lowest = tenths;
}
if (tenths > highest) {
highest = tenths;
}
lcd.setCursor(0, 0);
lcd.print("Temp ");
lcd.printFixed(tenths, 1);
lcd.write('C');
lcd.padLine();
lcd.setCursor(0, 1);
lcd.print("L");
lcd.printFixed(lowest, 1);
lcd.print(" H");
lcd.printFixed(highest, 1);
lcd.padLine();
Serial.print("temp ");
Serial.print(tenths / 10);
Serial.println(" C");
delay(400);
}
Code walkthrough
The key functions of the sketch, and what each one does.
tenthsFor sketch.ino
Turns an ADC reading into tenths of a degree. It walks the READINGS table until it finds the first entry at or above the reading, then works out how far between the two neighbors the reading sits. Readings outside the table clamp to its ends.
// Tenths of a degree for an ADC reading, by walking the table and drawing a
// straight line between the two entries it falls between.
int tenthsFor(int reading) {
if (reading <= READINGS[0]) {
return TABLE_LOW_C * 10;
}
for (int i = 1; i < TABLE_N; i++) {
if (reading <= READINGS[i]) {
long span = READINGS[i] - READINGS[i - 1];
long into = reading - READINGS[i - 1];
long base = (long)(TABLE_LOW_C + (i - 1) * TABLE_STEP_C) * 10;
return (int)(base + (into * TABLE_STEP_C * 10) / span);
}
}
return (TABLE_LOW_C + (TABLE_N - 1) * TABLE_STEP_C) * 10;
}readAveraged sketch.ino
Takes 16 readings of A0, adds them and shifts right by four, which divides by 16 without a slow division. Averaging stops the last digit flickering from noise.
// Sixteen samples added up and shifted right by four: a divide-free mean, and
// the ADC's last bit is noise so an unfiltered display never stops flickering.
int readAveraged() {
long total = 0;
for (int i = 0; i < 16; i++) {
total += analogRead(SENSOR);
}
return (int)(total >> 4);
}loop sketch.ino
Gets the averaged reading, converts it, and updates lowest and highest. It then writes the current temperature on line one and the extremes on line two. It repeats every 400 ms.
void loop() {
int tenths = tenthsFor(readAveraged());
if (tenths < lowest) {
lowest = tenths;
}
if (tenths > highest) {
highest = tenths;
}
lcd.setCursor(0, 0);
lcd.print("Temp ");
lcd.printFixed(tenths, 1);
lcd.write('C');
lcd.padLine();
lcd.setCursor(0, 1);
lcd.print("L");
lcd.printFixed(lowest, 1);
lcd.print(" H");
lcd.printFixed(highest, 1);
lcd.padLine();
Serial.print("temp ");
Serial.print(tenths / 10);
Serial.println(" C");
delay(400);
}Troubleshooting
The usual mistakes with this circuit, and the fix for each.
- The screen is on but shows nothing This project
Check SDA on A4 and SCL on A5. Also look at the contrast knob on the backpack if you build it for real. - The temperature looks wrong This project
The table assumes a 10k thermistor above a fixed 10k resistor. A different resistor value gives wrong readings. - Temperature goes down when it should go up This project
The thermistor must be on the 5V side and the resistor on the ground side, as in the diagram. - The temperature reads far off or jumps around. Thermistor
Use a fixed resistor equal to the thermistor's 10k at 25 C in the divider. Convert with the Steinhart-Hart or Beta equation (Beta is about 3950 for common parts), not a straight line. - The reading goes the wrong way, or self-heating shifts it. Thermistor
An NTC lowers its resistance as it warms, so the divider voltage moves with the wiring side. Check which side the thermistor is on. Keep current small, with 3.3 V or 5 V through 10k, so it does not self-heat. - The LED is dim, burns out, or the pin reads wrong because the resistor value is off. Resistor
Read the color bands again. A 220 ohm to 330 ohm resistor suits a typical LED on 5 V. 10k is a common pull-up or pull-down value. Check with a multimeter if unsure. - A pull-up or pull-down resistor is missing, so the input floats and reads random values. Resistor
Tie the input to 5 V or ground through about 10k. Or use INPUT_PULLUP and wire the switch to ground. - The screen stays blank or shows only a row of blocks. Character LCD with I2C backpack
The I2C address is usually 0x27 or 0x3F. Run an I2C scanner to find yours. Then turn the contrast trimmer on the backpack slowly until text appears.
Change it and keep it
Open it in the editor, change the circuit or the code, and keep your version in a free account.