Built-in project · Arduino Uno R3

I2C LCD clock and thermometer on an Arduino Uno

An I2C LCD on A4 and A5 shows the time, kept by a timer interrupt every second, and the temperature from a thermistor on A0. Type hour, minute and second into the serial monitor to set it. 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 Runs in your browser. Free, and no account needed.

The parts retail for about $34. On Mokxi they're free.

I2C LCD clock and thermometer · 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
// A clock and thermometer on an I2C LCD.
//
//   LCD GND -> GND   VCC -> 5V   SDA -> A4   SCL -> A5
//   10k from 5V -> A0; the thermistor from A0 -> GND
//
// MsTimer2 calls tick() every 1000 ms from a timer interrupt, so the seconds
// keep counting however long the rest of the loop takes. Type three numbers,
// like 12,30,0, into the serial monitor to set the time.

#include <Wire.h>
#include <LiquidCrystal_I2C.h>
#include <MsTimer2.h>

LiquidCrystal_I2C lcd(0x27, 16, 2);
volatile int seconds = 0;
int hour = 0, minute = 0;

// The thermistor is the bottom of a divider under a 10k, so its resistance
// is 10k * v / (5 - v). The beta equation turns that into kelvin:
// 1/T = 1/T0 + ln(R/R0)/B, with T0 = 25 C, R0 = 10k and B = 3950.
float celsius() {
  float v = analogRead(A0) * 5.0 / 1024.0;
  float rt = 10.0 * v / (5.0 - v);
  float kelvin = 1.0 / (log(rt / 10.0) / 3950.0 + 1.0 / (273.15 + 25.0));
  return kelvin - 273.15;
}

void tick() {
  seconds++;
}

void twoDigits(int n) {
  lcd.print(n / 10);
  lcd.print(n % 10);
}

void setup() {
  lcd.init();
  lcd.backlight();
  lcd.print("starting...");
  delay(500);
  lcd.clear();
  MsTimer2::set(1000, tick);
  MsTimer2::start();
  Serial.begin(9600);
  Serial.println("Type hour,minute,second to set the time.");
}

void loop() {
  if (Serial.available() > 0) {
    int h = Serial.parseInt();
    int m = Serial.parseInt();
    int s = Serial.parseInt();
    hour = constrain(h, 0, 23);
    minute = constrain(m, 0, 59);
    noInterrupts();
    seconds = constrain(s, 0, 59);
    interrupts();
    Serial.println("time set");
  }
  noInterrupts();
  if (seconds >= 60) {
    seconds -= 60;
    if (++minute >= 60) {
      minute = 0;
      if (++hour >= 24) hour = 0;
    }
  }
  int sec = seconds;
  interrupts();
  lcd.setCursor(0, 0);
  lcd.print("TEMP: ");
  lcd.print(celsius());
  lcd.print("C ");
  lcd.setCursor(0, 1);
  lcd.print("TIME: ");
  twoDigits(hour);
  lcd.print(':');
  twoDigits(minute);
  lcd.print(':');
  twoDigits(sec);
  delay(200);
}

Parts list

5 parts, plus the jumper wires. They retail for about $34; on Mokxi they're free.

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; Resistor, 10k Ω pin 2; Character LCD with I2C backpack pin VCC
  • Ground: Arduino Uno R3 pin GND; Thermistor, 10k NTC pin 1; 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.