Built-in project · Arduino Uno R3

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.

IntermediateRuns in your browser. Free, and no account needed.

Step by step, with the wiring and the common mistakes: Arduino I2C LCD 16x2: Wiring and Code

Thermometer on an I2C LCD · Arduino Uno R3live0.000 s 0.00x
Click to open it in the editor
The circuit itself, running here on the simulator. Press what can be pressed; click anything else to open it in the editor.

The code

The firmware exactly as the editor opens it. Change a line there and press Run: it compiles in the browser.

sketch.ino · Arduino Uno R3
// 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);
}

Parts list

5 parts, plus the jumper wires. Every one is in the editor's parts bin.

How it is wired

5 connections, pin by pin, read from the circuit itself. Each line is one set of pins joined together, by a jumper wire or a breadboard strip.

  • Arduino Uno R3 pin 5V; Thermistor, 10k NTC pin 1; Character LCD with I2C backpack pin VCC
  • Ground: Arduino Uno R3 pin GND; Resistor, 10k Ω pin 2; Character LCD with I2C backpack pin GND
  • Arduino Uno R3 pin A0; Thermistor, 10k NTC pin 2; Resistor, 10k Ω pin 1
  • Arduino Uno R3 pin A4; Character LCD with I2C backpack pin SDA
  • Arduino Uno R3 pin A5; Character LCD with I2C backpack pin SCL

Change it and keep it

Open it in the editor, change the circuit or the code, and keep your version in a free account.