ArduinoESP32-S3 DevKitC-1Lab virtual

Round gauge (GC9A01)

A round 240x240 TFT on the ESP32-S3: a dial whose needle follows the knob.

Round gauge (GC9A01) in Argynix Lab

The code

sketch.inoC++
// A round GC9A01 TFT on the ESP32-S3's SPI (SCK 12, MOSI 11): a dial, and a needle
// that follows the potentiometer on GPIO1. Turn the knob while it runs.
#include <SPI.h>
#include <Adafruit_GFX.h>
#include <Adafruit_GC9A01A.h>

#define TFT_CS 10
#define TFT_DC 9
#define TFT_RST 8

Adafruit_GC9A01A tft(TFT_CS, TFT_DC, TFT_RST);
const int CX = 120, CY = 120;
float last = -1;

void needle(float value, uint16_t colour) {
  float a = (value * 270.0 - 225.0) * DEG_TO_RAD;
  tft.drawLine(CX, CY, CX + cos(a) * 88, CY + sin(a) * 88, colour);
  tft.drawLine(CX + 1, CY, CX + 1 + cos(a) * 88, CY + sin(a) * 88, colour);
}

void setup() {
  tft.begin();
  tft.fillScreen(GC9A01A_BLACK);
  for (int i = 0; i <= 10; i++) {
    float a = (i * 27.0 - 225.0) * DEG_TO_RAD;
    tft.drawLine(CX + cos(a) * 100, CY + sin(a) * 100, CX + cos(a) * 115, CY + sin(a) * 115, i > 7 ? GC9A01A_RED : GC9A01A_CYAN);
  }
  tft.setTextColor(GC9A01A_WHITE);
  tft.setTextSize(2);
  tft.setCursor(96, 170);
  tft.print("LEVEL");
}

void loop() {
  float value = analogRead(1) / 4095.0;
  if (fabs(value - last) > 0.01) {
    if (last >= 0) needle(last, GC9A01A_BLACK);
    needle(value, GC9A01A_ORANGE);
    tft.fillCircle(CX, CY, 8, GC9A01A_WHITE);
    tft.fillRect(90, 196, 70, 20, GC9A01A_BLACK);
    tft.setCursor(98, 198);
    tft.print(int(value * 100));
    tft.print("%");
    last = value;
  }
  delay(30);
}