Built-in project · Arduino Uno R3

Touch climate display on an Arduino Uno

An AHT20 and a 16x2 I2C LCD on the same two wires, and a TTP223 touch pad that flips the reading between Celsius and Fahrenheit. 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.

Touch climate display · Arduino Uno R3live0.000 s 0.00x
Hold the button
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
// Touch climate display: an AHT20 temperature and humidity sensor and a 16x2
// LCD on the same two I2C wires, and a TTP223 touch pad that switches the
// temperature between Celsius and Fahrenheit.
//
//   A4, A5  SDA and SCL, to the AHT20 (0x38); the LCD backpack (0x27) is on
//           the header's other SDA and SCL, which are the same two wires
//   pin 2   the TTP223's SIG: high while a finger is on the pad
//   power   the AHT20 on 5V, the LCD on IOREF (also 5 V), the TTP223 on 3V3:
//           its 3.3 V high is still a HIGH to a 5 V Uno
//
// Two I2C devices on one pair of wires is what the bus is for: each answers
// only its own address. The AHT20 module and the backpack both carry
// pull-ups, which is normal and harmless.
//
// # The sensor
//
// Adafruit_AHTX0.h asks the AHT20 for a reading and waits the 80 ms the chip
// takes to make one. The sketch then takes the numbers in hundredths
// (getTemperatureCentis and getHumidityCentis), because Fahrenheit is a sum
// and there is no floating point on this board:
//
//   F x 100 = C x 100 x 9 / 5 + 3200
//
// # The touch pad
//
// The TTP223 needs no pull-up and does not bounce: SIG is a real output,
// high while touched. It takes up to 60 ms to decide, and it ignores the pad
// for the first half second after power comes up. The sketch flips the units
// on the rising edge, so holding a finger there flips them once.
//
// Everything is on the display and nothing goes to the serial monitor: the
// sensor library, two I2C drivers and the LCD font fill most of the 8 KB an
// example is allowed.

#include <Adafruit_AHTX0.h>
#include "mokxi_lcd1602_i2c.h"

const int TOUCH = 2;

Adafruit_AHTX0 aht;
Lcd1602I2c lcd(A5, A4);

bool fahrenheit = false;
bool wasTouched = false;

/** Hundredths to tenths, rounded half away from zero. */
int tenthsOf(long centis) {
  return (int)((centis >= 0 ? centis + 5 : centis - 5) / 10);
}


void show() {
  sensors_event_t humidity, temp;
  if (!aht.getEvent(&humidity, &temp)) {
    lcd.setCursor(0, 0);
    lcd.print("sensor?");
    lcd.padLine();
    return;
  }
  long c = aht.getTemperatureCentis();
  int shown = tenthsOf(fahrenheit ? c * 9 / 5 + 3200 : c);
  int rh = (int)((aht.getHumidityCentis() + 50) / 100);

  lcd.setCursor(0, 0);
  lcd.print("Temp  ");
  lcd.printFixed(shown, 1);
  lcd.print(fahrenheit ? " F" : " C");
  lcd.padLine();
  lcd.setCursor(0, 1);
  lcd.print("Humid ");
  lcd.print(rh);
  lcd.print(" %");
  lcd.padLine();
}

void setup() {
  pinMode(TOUCH, INPUT);
  lcd.begin();
  lcd.print("Mokxi climate");
  if (!aht.begin()) {
    lcd.setCursor(0, 1);
    lcd.print("no AHT20");
    while (true) {
      delay(100);
    }
  }
  delay(400);
  lcd.clear();
}

void loop() {
  bool touched = digitalRead(TOUCH) == HIGH;
  bool flipped = touched && !wasTouched;
  wasTouched = touched;
  if (flipped) {
    fahrenheit = !fahrenheit;
  }

  static unsigned long last = 0;
  if (flipped || millis() - last >= 500) {
    last = millis();
    show();
  }
  delay(20);
}

Parts list

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

How it is wired

11 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 SCL; Character LCD with I2C backpack pin SCL
  • Arduino Uno R3 pin SDA; Character LCD with I2C backpack pin SDA
  • Ground: Arduino Uno R3 pin GND; Character LCD with I2C backpack pin GND
  • Arduino Uno R3 pin 2; Touch sensor, TTP223 pin SIG
  • Arduino Uno R3 pin IOREF; Character LCD with I2C backpack pin VCC
  • Arduino Uno R3 pin 3V3; Touch sensor, TTP223 pin VCC
  • Arduino Uno R3 pin 5V; Humidity sensor, AHT20 pin VCC
  • Ground: Arduino Uno R3 pin GND; Humidity sensor, AHT20 pin GND
  • Ground: Arduino Uno R3 pin GND; Touch sensor, TTP223 pin GND
  • Arduino Uno R3 pin A4; Humidity sensor, AHT20 pin SDA
  • Arduino Uno R3 pin A5; Humidity sensor, AHT20 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.