This project turns an ESP32 with a touchscreen display into a real-time GPS speedometer. The Quescan G10A-F30 GPS module provides accurate speed data, which is displayed on a large, easy-to-read dial gauge with numerical speed readout. The guide includes a complete wiring diagram, parts list, and step-by-step assembly instructions to get the system up and running.
The firmware handles GPS communication, touchscreen input, automatic brightness adjustment, and persistent settings storage. Builders will learn how to interface a GPS module with an ESP32, render graphics on a TFT display, and create an interactive user interface. The speedometer is ready to use in vehicles or for any application requiring portable speed monitoring.
Wiring diagram

Gather all the parts
Assemble it in 4 steps
1. Place the CYD and GPS where they can see the sky
Keep the CYD screen where you can read it and place the GPS module with its ceramic patch antenna facing upward. Do not cover the antenna with metal, because it needs a clear view of the sky to calculate speed.
- The GPS may take several minutes to find satellites the first time it is powered outdoors.
- Do not mount the GPS antenna directly under metal or inside a sealed metal enclosure; it may never receive a reliable satellite signal.
2. Connect GPS power
Connect the GPS module VCC pin to the CYD 3V3 pin (power), then connect GPS GND to a CYD GND pin (ground).
- Use the pins printed 3V3 and GND on the CYD side header.
- Make sure VCC and GND are not swapped — swapped power can damage the GPS module.
3. Connect the two GPS data wires
Connect GPS TX to CYD GPIO22 (GPS data into the screen board), then connect GPS RX to CYD GPIO27 (optional data back to the GPS). Leave the GPS PPS pin unconnected.
- TX and RX cross over: the GPS transmitter connects to the CYD receiver pin.
- Use 3.3 V logic only. Do not connect a GPS module that drives its TX pin at 5 V directly to GPIO22, because 5 V can damage the CYD.
4. Use the screen controls safely
Tap the MPH area in the middle of the normal speed screen to open GPS Satellite Data. Tap one of the six GPS BAUD RATE buttons: 4800, 9600, 19200, 38400, 57600, or 115200. The selected green button is saved for the next power-up; it changes only what speed the CYD listens at and does not alter the GPS module. Tap the MPH area again to return. Hold the upper-left 20–30 area for 3 seconds to open Speed Calibration. Hold the bottom dial band for about 1.5 seconds to reset the odometer.
- Start with 9600 because it is the normal setting for the working GY-GPS6MV2.
- If the G10A shows GPS DATA ERROR or NO GPS DATA, try one speed at a time and wait about 10 seconds after each choice.
- Do not reset the odometer, change settings, or use the touchscreen while driving. Have a passenger operate the menu, or stop safely first.
Review all connections
1. Connections between "gps_1" and "ESP32"
Deploy the firmware
#include <Arduino.h>
#include <TFT_eSPI.h>
#include <SPI.h>
#include <TinyGPSPlus.h>
#include <HardwareSerial.h>
#include <Preferences.h>
#include <XPT2046_Touchscreen.h>
#define GPS_RX_PIN 22
#define GPS_TX_PIN 27
// A new installation listens at 9600 baud. A rate selected on the GPS page
// is saved separately and remains in use after restart.
#define GPS_BAUD 9600
#define MAX_MPH 90
#define GPS_BAUD_COUNT 6
#define SCREEN_ROTATION 1
#define TOUCH_CS_PIN 33
#define TOUCH_IRQ_PIN 36
#define TOUCH_CLK_PIN 25
#define TOUCH_MOSI_PIN 32
#define TOUCH_MISO_PIN 39
#define LONG_PRESS_MS 1500
#define BACKLIGHT_PIN 21
#define LDR_PIN 34
#define LDR_INVERTED true
#define PWM_FREQ 5000
#define PWM_RESOLUTION 8
#define MIN_BRIGHTNESS 25
#define MAX_BRIGHTNESS 255
#define DAY_BRIGHTNESS 255
#define NIGHT_BRIGHTNESS 30
// XPT2046 reports raw 12-bit coordinates. These limits map its usable
// touchscreen area to this board's 320 x 240 landscape display.
#define TOUCH_RAW_MIN 200
#define TOUCH_RAW_MAX 3800
enum BrightnessMode { BRIGHT_AUTO, BRIGHT_DAY, BRIGHT_NIGHT };
// Forward declarations
// Forward declarations
bool isLeapYear(int year);
int daysInMonth(int year, int month);
int dayOfWeek(int year, int month, int day);
float mphToAngle(float mph);
void polarToXY(int cx, int cy, int r, float angleDeg, int &x, int &y);
void drawArcZone(int fromMph, int toMph, uint32_t color);
void drawDialFace();
constexpr int SPEED_FONT = 6; // Solid numerals, same 48-pixel height as font 7.
void drawNeedleAndText(float mph);
void redrawGauge(float mph);
void drawClockFace();
void drawClock();
void updateOdometer();
void drawOdometerBar();
void showResetFlash();
void showBrightnessFlash();
void showCalibrationMenu();
void showSatelliteMenu();
void setGpsBaud(uint32_t newBaud);
const char *gpsReceptionText();
void handleTouch();
void updateBrightness();
bool isUkSummerTime(int year, int month, int day, int hour);
bool getUkLocalTime(int &hour, int &minute, int &second);
TFT_eSPI tft = TFT_eSPI();
TFT_eSprite gaugeSprite = TFT_eSprite(&tft);
TinyGPSPlus gps;
HardwareSerial GPSSerial(2);
Preferences prefs;
SPIClass touchSPI = SPIClass(VSPI);
XPT2046_Touchscreen ts(TOUCH_CS_PIN, TOUCH_IRQ_PIN);
// Full-screen 320 x 240 gauge. The dial is rectangular so it reaches the
// left and right edges as well as the top and bottom of the CYD screen.
const int GSIZE_W = 320;
const int GSIZE_H = 240;
const int CX = GSIZE_W / 2;
const int CY = GSIZE_H / 2 + 2;
const int RADIUS_X = 148;
const int RADIUS_Y = 104;
const int RADIUS = RADIUS_Y;
const float START_ANGLE = 135.0;
const float SWEEP_ANGLE = 270.0;
const int SCREEN_W = 320;
const int SCREEN_H = 240;
int gaugeX, gaugeY;
float currentSpeed = 0;
float displayedSpeed = 0;
double odometerMiles = 0;
double lastLat = 0, lastLon = 0;
bool haveLastFix = false;
double lastSavedMiles = 0;
unsigned long lastSaveMs = 0;
double lastDrawnOdo = -1;
bool touchActive = false;
unsigned long touchStartMs = 0;
bool resetTriggered = false;
bool calibrationMenu = false;
bool satelliteMenu = false;
bool satelliteTouchStart = false;
bool baudTouchStart = false;
uint32_t selectedBaud = GPS_BAUD;
bool calibrationTouchStart = false;
bool calibrationSaveTouchStart = false;
bool calibrationMinusTouchStart = false;
bool calibrationPlusTouchStart = false;
bool brightnessTouchStart = false;
bool odometerTouchStart = false;
float speedCorrection = 1.00f;
uint32_t gpsBaud = GPS_BAUD;
bool clockMode = false;
unsigned long stationarySinceMs = 0;
int lastClockSecond = -1;
// After each restart, run a short visual-only speed sweep so the dial can be
// checked while parked. It never adds demo distance to the odometer, and live
// GPS operation resumes automatically when this two-minute check ends.
bool demoSpeedActive = false;
bool demoModeEnabled = true;
bool demoTouchStart = false;
unsigned long demoStartMs = 0;
const unsigned long DEMO_SPEED_DURATION_MS = 120000UL;
const unsigned long DEMO_SPEED_CYCLE_MS = 40000UL;
// One full minute with essentially no GPS movement changes the dial into a
// GPS-time clock. Any detected movement returns straight to the speedometer.
// GPS can report a small false speed while the vehicle is parked. Treat
// anything below 5 mph as stationary, then show the clock after one minute.
const float CLOCK_STILL_MPH = 5.0f;
const unsigned long CLOCK_AFTER_STILL_MS = 60000;
// Hold only in this upper-left dial corner, between the printed 20 and 30,
// to enter the speed-calibration screen.
const int CAL_MENU_X_MAX = 90;
const int CAL_MENU_Y_MIN = 20;
const int CAL_MENU_Y_MAX = 125;
// Tap only in this upper-right dial corner, between the printed 60 and 70,
// to change brightness: Auto -> Day -> Night -> Auto.
const int BRIGHT_MENU_X_MIN = 245;
const int BRIGHT_MENU_Y_MIN = 15;
const int BRIGHT_MENU_Y_MAX = 110;
// Tap the MPH label in the middle of the dial to see the live GPS details.
// This center area does not overlap the calibration or brightness corners.
const int SATELLITE_MENU_X_MIN = 95;
const int SATELLITE_MENU_X_MAX = 225;
const int SATELLITE_MENU_Y_MIN = 40;
const int SATELLITE_MENU_Y_MAX = 100;
// The GPS data page has six common serial-speed buttons in two rows.
const uint32_t GPS_BAUD_OPTIONS[GPS_BAUD_COUNT] = {4800, 9600, 19200, 38400, 57600, 115200};
// The GPS page also includes a saved DEMO SPEED switch. Keep both controls
// clear of the live time and inside the bottom edge of the screen.
const int DEMO_BUTTON_TOP = 151;
const int DEMO_BUTTON_HEIGHT = 17;
const int BAUD_BUTTON_TOP = 169;
const int BAUD_BUTTON_HEIGHT = 24;
const int BAUD_BUTTON_WIDTH = 104;
// Hold only in the bottom dial band between the printed 0 and 90 marks to
// reset the odometer. A hold anywhere else on the dial does nothing.
const int ODOMETER_X_MIN = 0;
const int ODOMETER_X_MAX = 320;
const int ODOMETER_Y_MIN = 190;
const int ODOMETER_Y_MAX = 239;
// Auto brightness is always the default after power-up.
BrightnessMode brightnessMode = BRIGHT_AUTO;
float smoothedBrightness = 200;
// UK civil time is GMT in winter and British Summer Time (BST) from the
// last Sunday in March to the last Sunday in October. GPS itself provides UTC.
bool isLeapYear(int year) {
return (year % 4 == 0 && year % 100 != 0) || (year % 400 == 0);
}
int daysInMonth(int year, int month) {
static const int monthDays[] = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
if (month == 2) return monthDays[1] + (isLeapYear(year) ? 1 : 0);
return monthDays[month - 1];
}
// Returns 0 for Sunday, using a compact Gregorian-calendar calculation.
int dayOfWeek(int year, int month, int day) {
static const int offsets[] = {0, 3, 2, 5, 0, 3, 5, 1, 4, 6, 2, 4};
year -= month < 3;
return (year + year / 4 - year / 100 + year / 400 + offsets[month - 1] + day) % 7;
}
bool isUkSummerTime(int year, int month, int day, int hour) {
if (month < 3 || month > 10) return false;
if (month > 3 && month < 10) return true;
int lastSunday = daysInMonth(year, month) - dayOfWeek(year, month, daysInMonth(year, month));
if (month == 3) return day > lastSunday || (day == lastSunday && hour >= 1);
return day < lastSunday || (day == lastSunday && hour < 1);
}
bool getUkLocalTime(int &hour, int &minute, int &second) {
if (!gps.time.isValid()) return false;
hour = gps.time.hour();
minute = gps.time.minute();
second = gps.time.second();
// A valid date is needed to know whether the UK is on GMT or BST.
if (gps.date.isValid() && isUkSummerTime(gps.date.year(), gps.date.month(), gps.date.day(), hour)) {
hour = (hour + 1) % 24;
}
return true;
}
// This shows whether the CYD is receiving checksum-valid NMEA sentences,
// separately from whether the receiver has acquired satellites yet.
const char *gpsReceptionText() {
if (millis() < 5000) return "GPS STARTING";
if (gps.charsProcessed() == 0) return "NO GPS DATA";
if (gps.passedChecksum() == 0) return "GPS DATA ERROR";
if (!gps.satellites.isValid()) return "SEARCHING";
static char status[18];
snprintf(status, sizeof(status), "%d SATS", gps.satellites.value());
return status;
}
float mphToAngle(float mph) {
mph = constrain(mph, 0, MAX_MPH);
return START_ANGLE + (mph / (float)MAX_MPH) * SWEEP_ANGLE;
}
void polarToXY(int cx, int cy, int r, float angleDeg, int &x, int &y) {
float rad = angleDeg * DEG_TO_RAD;
float dx = cos(rad);
float dy = sin(rad);
float adx = fabs(dx);
float ady = fabs(dy);
if (adx < 0.0001) adx = 0.0001;
if (ady < 0.0001) ady = 0.0001;
float edgeScale = min((float)RADIUS_X / adx, (float)RADIUS_Y / ady);
float frac = (float)r / (float)RADIUS;
float scale = edgeScale * frac;
x = cx + (int)(dx * scale);
y = cy + (int)(dy * scale);
}
void drawArcZone(int fromMph, int toMph, uint32_t color) {
float a1 = mphToAngle(fromMph);
float a2 = mphToAngle(toMph);
for (float a = a1; a <= a2; a += 0.5) {
for (int r = RADIUS - 6; r <= RADIUS; r++) {
int x, y;
polarToXY(CX, CY, r, a, x, y);
gaugeSprite.drawPixel(x, y, color);
}
}
}
constexpr uint16_t digitalColours[] = {TFT_WHITE, TFT_CYAN, TFT_GREEN, TFT_YELLOW, 0x1178}; // Deep blue (RGB565).
constexpr unsigned colourCount = sizeof(digitalColours) / sizeof(digitalColours[0]);
uint8_t digitalColour = 0;
void drawDialFace() {
gaugeSprite.fillSprite(TFT_BLACK);
drawArcZone(0, 25, TFT_DARKGREEN);
drawArcZone(25, 35, TFT_YELLOW);
drawArcZone(35, 60, TFT_DARKGREEN);
drawArcZone(55, 75, TFT_YELLOW);
drawArcZone(75, 90, TFT_RED);
for (int a = 0; a <= (int)SWEEP_ANGLE; a++) {
int x, y;
polarToXY(CX, CY, RADIUS + 4, START_ANGLE + a, x, y);
gaugeSprite.drawPixel(x, y, TFT_WHITE);
}
for (int mph = 0; mph <= MAX_MPH; mph += 5) {
float ang = mphToAngle(mph);
int x1, y1, x2, y2, lx, ly;
polarToXY(CX, CY, RADIUS, ang, x1, y1);
polarToXY(CX, CY, RADIUS - 14, ang, x2, y2);
gaugeSprite.drawLine(x1, y1, x2, y2, TFT_WHITE);
// Keep five-mph tick marks, but print larger labels only every ten mph
// so adjacent numbers do not overlap.
if (mph % 10 == 0) {
polarToXY(CX, CY, RADIUS - 28, ang, lx, ly);
gaugeSprite.setTextDatum(MC_DATUM);
gaugeSprite.setTextColor(digitalColours[digitalColour], TFT_BLACK);
gaugeSprite.drawString(String(mph), lx, ly, 4);
}
}
gaugeSprite.setTextDatum(MC_DATUM);
gaugeSprite.setTextColor(digitalColours[digitalColour], TFT_BLACK);
gaugeSprite.drawString("MPH", CX, CY - 52, 4);
}
void drawNeedleAndText(float mph) {
float ang = mphToAngle(mph);
// Orange highlights the 30 and 60 mph reading bands. From 75 mph through
// the 90 mph end stop, the needle, centre cap, and large number turn red.
bool nearThirty = fabs(mph - 30.0f) <= 3.0f;
bool nearSixty = fabs(mph - 60.0f) <= 3.0f;
bool highSpeed = mph >= 75.0f;
uint32_t speedColor = highSpeed ? TFT_RED : (nearThirty || nearSixty) ? TFT_ORANGE : digitalColours[digitalColour];
int tipX, tipY, perpX1, perpY1, perpX2, perpY2;
polarToXY(CX, CY, RADIUS - 20, ang, tipX, tipY);
// Keep the existing length, but use a narrower base at the hub so the
// pointer does not look heavy where it meets the centre cap.
polarToXY(CX, CY, 3, ang + 90, perpX1, perpY1);
polarToXY(CX, CY, 3, ang - 90, perpX2, perpY2);
gaugeSprite.fillTriangle(tipX, tipY, perpX1, perpY1, perpX2, perpY2, speedColor);
gaugeSprite.fillCircle(CX, CY, 10, speedColor);
gaugeSprite.fillCircle(CX, CY, 5, speedColor);
gaugeSprite.setTextDatum(MC_DATUM);
gaugeSprite.setTextColor(digitalColours[digitalColour], TFT_BLACK);
gaugeSprite.drawString(demoSpeedActive ? "DEMO SPEED" : gpsReceptionText(), CX, CY - 27, 2);
gaugeSprite.setTextColor(speedColor, TFT_BLACK);
// Use the slightly larger digital font for easier reading, while keeping
// the number in the clear space between the needle hub and odometer.
gaugeSprite.drawString(String((int)round(mph)), CX, CY + 48, SPEED_FONT);
gaugeSprite.setTextColor(digitalColours[digitalColour], TFT_BLACK);
char buf[20];
snprintf(buf, sizeof(buf), "%.1f mi", odometerMiles);
gaugeSprite.drawString(buf, CX, CY + 88, 4);
}
void redrawGauge(float mph) {
drawDialFace();
drawNeedleAndText(mph);
gaugeSprite.pushSprite(gaugeX, gaugeY);
}
bool digitalPage = false;
bool digitalTap = false;
bool digitalColourTap = false;
bool digitalClockTouch = false;
void drawDigitalPage(bool force) {
static String previous;
String speed((int)round(displayedSpeed));
char distance[40];
snprintf(distance, sizeof(distance), "%.1f mi", odometerMiles);
const String sats = gps.satellites.isValid()
? String(gps.satellites.value()) + " SATS" : "-- SATS";
const float mph = displayedSpeed;
const uint16_t colour = mph >= 75.0f ? TFT_RED :
(fabs(mph - 30.0f) <= 3.0f || fabs(mph - 60.0f) <= 3.0f)
? TFT_ORANGE : digitalColours[digitalColour];
String key = speed + "|" + distance + "|" + sats + "|" + String(colour) + "|" + String(digitalColour);
if (!force && key == previous) return;
previous = key;
gaugeSprite.fillSprite(TFT_BLACK);
gaugeSprite.setTextDatum(MC_DATUM);
gaugeSprite.setTextColor(colour, TFT_BLACK);
gaugeSprite.setTextSize(1);
const int digitWidth = gaugeSprite.textWidth(speed, 7);
const int digitHeight = gaugeSprite.fontHeight(7);
const int scale = max(1, min((SCREEN_W - 8) / max(1, digitWidth),
(SCREEN_H - 36) / max(1, digitHeight)));
gaugeSprite.setTextSize(scale);
gaugeSprite.drawString(speed, CX, (SCREEN_H - 36) / 2, 7);
gaugeSprite.setTextSize(1);
gaugeSprite.setTextColor(digitalColours[digitalColour], TFT_BLACK);
gaugeSprite.drawString("MPH | " + String(distance), CX, SCREEN_H - 21, 4);
gaugeSprite.setTextDatum(BR_DATUM);
gaugeSprite.drawString(sats, SCREEN_W - 4, SCREEN_H - 2, 2);
gaugeSprite.pushSprite(gaugeX, gaugeY);
}
void drawClockFace() {
gaugeSprite.fillSprite(TFT_BLACK);
// A clean, open clock face: only the 12 white hour marks and hour numbers
// are shown. There is deliberately no oval around the clock.
for (int hour = 0; hour < 12; hour++) {
float angle = -90.0f + hour * 30.0f;
int innerX, innerY, outerX, outerY;
// The inner end of every mark meets its printed number; the mark then
// points outward toward the edge of the display.
polarToXY(CX, CY, RADIUS - 6, angle, innerX, innerY);
polarToXY(CX, CY, RADIUS + 4, angle, outerX, outerY);
gaugeSprite.drawLine(innerX, innerY, outerX, outerY, TFT_WHITE);
gaugeSprite.drawLine(innerX + 1, innerY, outerX + 1, outerY, TFT_WHITE);
}
gaugeSprite.setTextDatum(MC_DATUM);
gaugeSprite.setTextColor(digitalColours[digitalColour], TFT_BLACK);
for (int hour = 1; hour <= 12; hour++) {
float angle = -90.0f + hour * 30.0f;
int x, y;
// Put every numeral directly against its matching hour mark.
polarToXY(CX, CY, RADIUS - 17, angle, x, y);
gaugeSprite.drawString(String(hour), x, y, 4);
}
// Keep the UK-time label directly beneath the 12, rather than above it.
gaugeSprite.setTextColor(digitalColours[digitalColour], TFT_BLACK);
gaugeSprite.drawString("UK TIME", CX, 70, 2);
}
void drawClock() {
drawClockFace();
gaugeSprite.setTextDatum(MC_DATUM);
if (!gps.time.isValid()) {
gaugeSprite.setTextColor(digitalColours[digitalColour], TFT_BLACK);
gaugeSprite.drawString("WAITING FOR GPS TIME", CX, CY, 2);
gaugeSprite.drawString("CLOCK", CX, CY + 26, 4);
gaugeSprite.pushSprite(gaugeX, gaugeY);
return;
}
int hour, minute, second;
getUkLocalTime(hour, minute, second);
float minuteAngle = -90.0f + minute * 6.0f + second * 0.1f;
float hourAngle = -90.0f + (hour % 12) * 30.0f + minute * 0.5f;
float secondAngle = -90.0f + second * 6.0f;
int hx, hy, mx, my, sx, sy;
// Keep every hand inside the cyan oval. The second hand has a deliberately
// larger 20-pixel inset so its one-pixel red tip cannot cross the outline,
// including at diagonal angles where display pixels are stepped.
const int THICK_HAND_TIP_RADIUS = RADIUS - 20;
// Leave a clear margin at every angle. This is intentionally shorter than
// the coloured hour and minute hands so the red tip cannot appear beyond
// the cyan oval on the physical display.
const int SECOND_HAND_TIP_RADIUS = RADIUS - 36;
polarToXY(CX, CY, THICK_HAND_TIP_RADIUS, hourAngle, hx, hy);
polarToXY(CX, CY, THICK_HAND_TIP_RADIUS, minuteAngle, mx, my);
polarToXY(CX, CY, SECOND_HAND_TIP_RADIUS, secondAngle, sx, sy);
// Draw live satellite status before the time and hands, so every hand
// remains visible when it crosses this writing.
if (gps.satellites.isValid()) {
gaugeSprite.drawString(String(gps.satellites.value()) + " SATS", CX, 92, 2);
} else {
gaugeSprite.drawString(gpsReceptionText(), CX, 92, 2);
}
// Draw the digital time first, then the hands over it. This makes a hand
// crossing the digits visible instead of disappearing into matching ink.
gaugeSprite.setTextColor(digitalColours[digitalColour], TFT_BLACK);
char timeText[12];
snprintf(timeText, sizeof(timeText), "%02d:%02d", hour, minute);
// Keep the requested 2 mm lower placement while using the next larger
// legible clock font; this remains above the printed 6.
gaugeSprite.drawString(timeText, CX, CY + 32, 7);
// Use distinct hand colors: yellow hour, green minute, and red seconds.
// These colors remain visible when a hand crosses the digital time.
gaugeSprite.drawLine(CX - 3, CY, hx, hy, TFT_YELLOW);
gaugeSprite.drawLine(CX - 2, CY, hx, hy, TFT_YELLOW);
gaugeSprite.drawLine(CX - 1, CY, hx, hy, TFT_YELLOW);
gaugeSprite.drawLine(CX, CY, hx, hy, TFT_YELLOW);
gaugeSprite.drawLine(CX + 1, CY, hx, hy, TFT_YELLOW);
gaugeSprite.drawLine(CX + 2, CY, hx, hy, TFT_YELLOW);
gaugeSprite.drawLine(CX + 3, CY, hx, hy, TFT_YELLOW);
gaugeSprite.drawLine(CX - 2, CY, mx, my, TFT_GREEN);
gaugeSprite.drawLine(CX - 1, CY, mx, my, TFT_GREEN);
gaugeSprite.drawLine(CX, CY, mx, my, TFT_GREEN);
gaugeSprite.drawLine(CX + 1, CY, mx, my, TFT_GREEN);
gaugeSprite.drawLine(CX + 2, CY, mx, my, TFT_GREEN);
gaugeSprite.drawLine(CX, CY, sx, sy, TFT_RED);
gaugeSprite.fillCircle(CX, CY, 7, TFT_YELLOW);
gaugeSprite.fillCircle(CX, CY, 4, TFT_RED);
gaugeSprite.pushSprite(gaugeX, gaugeY);
}
void updateOdometer() {
// A displayed demo speed is not real travel, so it must never change miles.
if (demoSpeedActive) return;
if (!gps.location.isUpdated() || !gps.location.isValid()) return;
double newLat = gps.location.lat();
double newLon = gps.location.lng();
if (haveLastFix && currentSpeed > 2.0) {
double meters = TinyGPSPlus::distanceBetween(lastLat, lastLon, newLat, newLon);
odometerMiles += meters * 0.000621371;
}
lastLat = newLat;
lastLon = newLon;
haveLastFix = true;
if (odometerMiles - lastSavedMiles >= 0.1 || millis() - lastSaveMs > 60000) {
prefs.putDouble("odo", odometerMiles);
lastSavedMiles = odometerMiles;
lastSaveMs = millis();
}
}
void drawOdometerBar() {
if (fabs(odometerMiles - lastDrawnOdo) < 0.05) return;
lastDrawnOdo = odometerMiles;
// The odometer is drawn inside the full-screen gauge, so there is no
// unused strip below the dial to redraw.
}
void showResetFlash() {
tft.fillRect(0, 0, SCREEN_W, SCREEN_H, TFT_BLACK);
tft.setTextDatum(MC_DATUM);
tft.setTextColor(TFT_RED, TFT_BLACK);
tft.drawString("ODOMETER RESET", SCREEN_W / 2, SCREEN_H / 2, 4);
delay(2000);
lastDrawnOdo = -1;
redrawGauge(displayedSpeed);
drawOdometerBar();
}
void showBrightnessFlash() {
const char *label = brightnessMode == BRIGHT_AUTO ? "AUTO BRIGHTNESS" : brightnessMode == BRIGHT_DAY ? "DAY MODE" : "NIGHT MODE";
tft.fillRect(0, 0, SCREEN_W, SCREEN_H, TFT_BLACK);
tft.setTextDatum(MC_DATUM);
tft.setTextColor(digitalColours[digitalColour], TFT_BLACK);
tft.drawString(label, SCREEN_W / 2, SCREEN_H / 2, 4);
delay(2000);
lastDrawnOdo = -1;
redrawGauge(displayedSpeed);
drawOdometerBar();
}
void showCalibrationMenu() {
tft.fillScreen(TFT_BLACK);
tft.setTextDatum(MC_DATUM);
tft.setTextColor(digitalColours[digitalColour], TFT_BLACK);
tft.drawString("SPEED CALIBRATION", SCREEN_W / 2, 26, 4);
tft.setTextColor(digitalColours[digitalColour], TFT_BLACK);
tft.drawString("GPS SPEED x", SCREEN_W / 2, 70, 2);
char factorText[12];
snprintf(factorText, sizeof(factorText), "%.2f", speedCorrection);
tft.drawString(factorText, SCREEN_W / 2, 102, 6);
tft.fillRoundRect(20, 142, 120, 70, 8, TFT_DARKGREY);
tft.fillRoundRect(180, 142, 120, 70, 8, TFT_DARKGREEN);
tft.setTextColor(digitalColours[digitalColour], TFT_DARKGREY);
tft.drawString("- 1%", 80, 177, 4);
tft.setTextColor(digitalColours[digitalColour], TFT_DARKGREEN);
tft.drawString("+ 1%", 240, 177, 4);
tft.setTextColor(digitalColours[digitalColour], TFT_BLACK);
tft.drawString("Tap +1% / -1% Hold upper-left 3 sec: save", SCREEN_W / 2, 228, 2);
}
void setGpsBaud(uint32_t newBaud) {
if (newBaud == gpsBaud) return;
gpsBaud = newBaud;
prefs.putULong("gpsbaud", gpsBaud);
// This changes only the CYD's listening speed. It sends no setup command
// to the GPS receiver, so a receiver configured at another speed is safe.
GPSSerial.end();
GPSSerial.begin(gpsBaud, SERIAL_8N1, GPS_RX_PIN, GPS_TX_PIN);
gps = TinyGPSPlus();
}
void showSatelliteMenu() {
tft.fillScreen(TFT_BLACK);
tft.setTextDatum(MC_DATUM);
tft.setTextColor(digitalColours[digitalColour], TFT_BLACK);
tft.drawString("GPS SATELLITE DATA", SCREEN_W / 2, 18, 4);
tft.setTextColor(digitalColours[digitalColour], TFT_BLACK);
if (gps.satellites.isValid()) {
tft.drawString(String(gps.satellites.value()) + " SATELLITES", SCREEN_W / 2, 55, 4);
} else {
tft.drawString(gpsReceptionText(), SCREEN_W / 2, 55, 2);
}
char line[42];
if (gps.location.isValid()) {
snprintf(line, sizeof(line), "LAT %.5f", gps.location.lat());
tft.drawString(line, SCREEN_W / 2, 78, 2);
snprintf(line, sizeof(line), "LON %.5f", gps.location.lng());
tft.drawString(line, SCREEN_W / 2, 98, 2);
} else {
tft.drawString("POSITION: WAITING", SCREEN_W / 2, 88, 2);
}
if (gps.hdop.isValid()) {
snprintf(line, sizeof(line), "ACCURACY: %.1f m", gps.hdop.hdop() * 5.0f);
tft.drawString(line, SCREEN_W / 2, 120, 2);
} else {
tft.drawString("ACCURACY: WAITING", SCREEN_W / 2, 120, 2);
}
if (gps.time.isValid()) {
int hour, minute, second;
getUkLocalTime(hour, minute, second);
snprintf(line, sizeof(line), "UK TIME %02d:%02d:%02d", hour, minute, second);
tft.drawString(line, SCREEN_W / 2, 143, 2);
} else {
tft.drawString("UK TIME: WAITING", SCREEN_W / 2, 143, 2);
}
// This switch controls the parked two-minute demonstration and is saved
// so its choice remains after power is removed.
bool demoOn = demoModeEnabled;
tft.fillRoundRect(94, DEMO_BUTTON_TOP, 132, DEMO_BUTTON_HEIGHT, 4,
demoOn ? TFT_DARKGREEN : TFT_DARKGREY);
tft.setTextColor(digitalColours[digitalColour], demoOn ? TFT_DARKGREEN : TFT_DARKGREY);
tft.drawString(demoOn ? "DEMO SPEED: ON" : "DEMO SPEED: OFF", SCREEN_W / 2,
DEMO_BUTTON_TOP + DEMO_BUTTON_HEIGHT / 2, 2);
tft.setTextColor(digitalColours[digitalColour], TFT_BLACK);
tft.drawString("GPS BAUD RATE", SCREEN_W / 2, 175, 2);
for (int i = 0; i < GPS_BAUD_COUNT; i++) {
int column = i % 3;
int row = i / 3;
int x = column * BAUD_BUTTON_WIDTH + 3;
int y = BAUD_BUTTON_TOP + 12 + row * 25;
bool active = GPS_BAUD_OPTIONS[i] == gpsBaud;
tft.fillRoundRect(x, y, BAUD_BUTTON_WIDTH - 6, BAUD_BUTTON_HEIGHT - 2, 4,
active ? TFT_DARKGREEN : TFT_DARKGREY);
tft.setTextColor(digitalColours[digitalColour], active ? TFT_DARKGREEN : TFT_DARKGREY);
tft.drawString(String(GPS_BAUD_OPTIONS[i]), x + (BAUD_BUTTON_WIDTH - 6) / 2, y + 10, 2);
}
}
void handleTouch() {
// A tap anywhere on the parked clock returns to the normal speedometer.
// Restart the one-minute still timer so a stationary vehicle does not
// immediately switch back to the clock.
if (digitalClockTouch) {
if (!ts.touched()) digitalClockTouch = false;
return;
}
if (clockMode && ts.touched()) {
clockMode = false;
stationarySinceMs = 0;
lastClockSecond = -1;
touchActive = false;
if (digitalPage) {
digitalClockTouch = true;
drawDigitalPage(true);
} else {
redrawGauge(displayedSpeed);
}
return;
}
bool isTouched = ts.touched();
if (isTouched && !touchActive) {
TS_Point point = ts.getPoint();
// setRotation(1) makes point.x and point.y follow the landscape screen
// axes, but their values remain raw 0–4095 touch-controller readings.
int touchX = constrain(map(point.x, TOUCH_RAW_MIN, TOUCH_RAW_MAX, 0, SCREEN_W), 0, SCREEN_W);
int touchY = constrain(map(point.y, TOUCH_RAW_MIN, TOUCH_RAW_MAX, 0, SCREEN_H), 0, SCREEN_H);
touchActive = true;
touchStartMs = millis();
digitalColourTap = digitalPage && touchX < SCREEN_W / 3 && touchY >= 190;
digitalTap = !calibrationMenu && !satelliteMenu &&
abs(touchX - CX) <= 65 && touchY >= 190;
resetTriggered = false;
// In the calibration screen, record which visible button was pressed.
// This keeps + and - predictable and prevents a tap elsewhere changing
// the saved correction value.
calibrationMinusTouchStart = calibrationMenu &&
touchX >= 20 && touchX <= 140 &&
touchY >= 142 && touchY <= 212;
calibrationPlusTouchStart = calibrationMenu &&
touchX >= 180 && touchX <= 300 &&
touchY >= 142 && touchY <= 212;
satelliteTouchStart = !calibrationMenu &&
touchX >= SATELLITE_MENU_X_MIN && touchX <= SATELLITE_MENU_X_MAX &&
touchY >= SATELLITE_MENU_Y_MIN && touchY <= SATELLITE_MENU_Y_MAX;
baudTouchStart = false;
demoTouchStart = satelliteMenu && touchX >= 94 && touchX <= 226 &&
touchY >= DEMO_BUTTON_TOP && touchY <= DEMO_BUTTON_TOP + DEMO_BUTTON_HEIGHT;
if (satelliteMenu && touchY >= BAUD_BUTTON_TOP + 12 && touchY < BAUD_BUTTON_TOP + 62) {
int column = touchX / BAUD_BUTTON_WIDTH;
int row = (touchY - (BAUD_BUTTON_TOP + 12)) / 25;
int index = row * 3 + column;
if (column >= 0 && column < 3 && row >= 0 && row < 2 && index < GPS_BAUD_COUNT) {
baudTouchStart = true;
selectedBaud = GPS_BAUD_OPTIONS[index];
}
}
calibrationTouchStart = !calibrationMenu && !satelliteMenu &&
touchX <= CAL_MENU_X_MAX &&
touchY >= CAL_MENU_Y_MIN &&
touchY <= CAL_MENU_Y_MAX;
// Saving uses the same physical upper-left touch area as the 20–30 mph
// calibration entry; a three-second hold elsewhere does nothing.
calibrationSaveTouchStart = calibrationMenu &&
touchX <= CAL_MENU_X_MAX &&
touchY >= CAL_MENU_Y_MIN &&
touchY <= CAL_MENU_Y_MAX;
brightnessTouchStart = !calibrationMenu && !satelliteMenu &&
touchX >= BRIGHT_MENU_X_MIN &&
touchY >= BRIGHT_MENU_Y_MIN &&
touchY <= BRIGHT_MENU_Y_MAX;
odometerTouchStart = !calibrationMenu && !satelliteMenu &&
touchX >= ODOMETER_X_MIN && touchX <= ODOMETER_X_MAX &&
touchY >= ODOMETER_Y_MIN && touchY <= ODOMETER_Y_MAX;
return;
}
if (isTouched) return;
if (!touchActive) return;
unsigned long touchLength = millis() - touchStartMs;
if (digitalPage) {
if (digitalColourTap) {
digitalColour = (digitalColour + 1) % colourCount;
prefs.putUChar("digcolour", digitalColour);
drawDigitalPage(true);
touchActive = false;
return;
}
digitalPage = false;
prefs.putBool("digital", false);
stationarySinceMs = millis();
redrawGauge(displayedSpeed);
} else if (digitalTap && touchLength < 500) {
digitalPage = true;
prefs.putBool("digital", true);
drawDigitalPage(true);
} else if (satelliteMenu) {
if (demoTouchStart) {
demoModeEnabled = !demoModeEnabled;
prefs.putBool("demo", demoModeEnabled);
// Enabling the feature starts its full two-minute parked check now.
// Disabling it immediately leaves the display under live GPS control.
if (demoModeEnabled) demoStartMs = millis();
else demoSpeedActive = false;
showSatelliteMenu();
} else if (baudTouchStart) {
setGpsBaud(selectedBaud);
showSatelliteMenu();
} else if (satelliteTouchStart) {
satelliteMenu = false;
redrawGauge(displayedSpeed);
}
} else if (calibrationMenu) {
// The screen never times out: only a three-second hold in the same
// upper-left 20–30 mph touch area saves and returns to the speedometer.
if (calibrationSaveTouchStart && touchLength >= 3000) {
prefs.putFloat("spdcal", speedCorrection);
calibrationMenu = false;
redrawGauge(displayedSpeed);
} else if (calibrationMinusTouchStart) {
speedCorrection = max(0.50f, speedCorrection - 0.01f);
showCalibrationMenu();
} else if (calibrationPlusTouchStart) {
speedCorrection = min(1.50f, speedCorrection + 0.01f);
showCalibrationMenu();
}
} else if (satelliteTouchStart) {
satelliteMenu = true;
showSatelliteMenu();
} else if (calibrationTouchStart && touchLength >= 3000) {
calibrationMenu = true;
showCalibrationMenu();
} else if (odometerTouchStart && touchLength >= LONG_PRESS_MS) {
odometerMiles = 0;
lastSavedMiles = 0;
prefs.putDouble("odo", 0.0);
showResetFlash();
} else if (brightnessTouchStart) {
brightnessMode = (BrightnessMode)((brightnessMode + 1) % 3);
if (brightnessMode == BRIGHT_DAY) smoothedBrightness = DAY_BRIGHTNESS;
else if (brightnessMode == BRIGHT_NIGHT) smoothedBrightness = NIGHT_BRIGHTNESS;
ledcWrite(BACKLIGHT_PIN, (int)smoothedBrightness);
showBrightnessFlash();
}
touchActive = false;
resetTriggered = false;
satelliteTouchStart = false;
baudTouchStart = false;
demoTouchStart = false;
calibrationTouchStart = false;
calibrationSaveTouchStart = false;
calibrationMinusTouchStart = false;
calibrationPlusTouchStart = false;
brightnessTouchStart = false;
odometerTouchStart = false;
}
void updateBrightness() {
int target;
if (brightnessMode == BRIGHT_DAY) target = DAY_BRIGHTNESS;
else if (brightnessMode == BRIGHT_NIGHT) target = NIGHT_BRIGHTNESS;
else {
int rawLight = analogRead(LDR_PIN);
if (LDR_INVERTED) rawLight = 4095 - rawLight;
target = constrain(map(rawLight, 0, 4095, MIN_BRIGHTNESS, MAX_BRIGHTNESS), MIN_BRIGHTNESS, MAX_BRIGHTNESS);
}
smoothedBrightness += (target - smoothedBrightness) * 0.15;
ledcWrite(BACKLIGHT_PIN, (int)smoothedBrightness);
}
void setup() {
Serial.begin(115200);
// Keep 9600 as the default but remember a user-selected listening speed.
// No configuration bytes are ever sent to the GPS receiver.
prefs.begin("speedo", false);
digitalColour = prefs.getUChar("digcolour", 0) % colourCount;
digitalPage = prefs.getBool("digital", false);
gpsBaud = prefs.getULong("gpsbaud", GPS_BAUD);
bool knownBaud = false;
for (int i = 0; i < GPS_BAUD_COUNT; i++) {
if (gpsBaud == GPS_BAUD_OPTIONS[i]) knownBaud = true;
}
if (!knownBaud) gpsBaud = GPS_BAUD;
GPSSerial.begin(gpsBaud, SERIAL_8N1, GPS_RX_PIN, GPS_TX_PIN);
tft.init();
tft.setRotation(SCREEN_ROTATION);
tft.fillScreen(TFT_BLACK);
gaugeSprite.setColorDepth(8);
gaugeSprite.createSprite(GSIZE_W, GSIZE_H);
gaugeX = 0;
gaugeY = 0;
odometerMiles = prefs.getDouble("odo", 0.0);
lastSavedMiles = odometerMiles;
speedCorrection = constrain(prefs.getFloat("spdcal", 1.00f), 0.50f, 1.50f);
// Demo mode stays enabled by default, but the GPS page switch is remembered.
demoModeEnabled = prefs.getBool("demo", true);
// The installed XPT2046 library variant accepts begin() only and reads the
// Arduino default SPI object. Put that VSPI object on this CYD's dedicated
// touch wires; TFT_eSPI continues to use its own HSPI bus for the display.
SPI.begin(TOUCH_CLK_PIN, TOUCH_MISO_PIN, TOUCH_MOSI_PIN, TOUCH_CS_PIN);
ts.begin();
ts.setRotation(SCREEN_ROTATION);
ledcAttach(BACKLIGHT_PIN, PWM_FREQ, PWM_RESOLUTION);
ledcWrite(BACKLIGHT_PIN, MAX_BRIGHTNESS);
demoStartMs = millis();
if (digitalPage) drawDigitalPage(true);
else redrawGauge(0);
}
unsigned long lastUpdate = 0;
void loop() {
while (GPSSerial.available() > 0) {
char gpsChar = (char)GPSSerial.read();
gps.encode(gpsChar);
}
handleTouch();
updateBrightness();
ledcWrite(BACKLIGHT_PIN, (int)smoothedBrightness);
if (millis() - lastUpdate > 300) {
lastUpdate = millis();
// Run a two-minute parked demonstration after startup. The triangular
// sweep reaches both orange test bands (27–33 and 57–63 mph), then ends
// automatically and returns to the real GPS reading.
unsigned long demoElapsed = millis() - demoStartMs;
demoSpeedActive = demoModeEnabled && demoElapsed < DEMO_SPEED_DURATION_MS;
if (demoSpeedActive) {
unsigned long cyclePosition = demoElapsed % DEMO_SPEED_CYCLE_MS;
float halfCycle = DEMO_SPEED_CYCLE_MS / 2.0f;
currentSpeed = cyclePosition < halfCycle
? MAX_MPH * cyclePosition / halfCycle
: MAX_MPH * (DEMO_SPEED_CYCLE_MS - cyclePosition) / halfCycle;
} else if (gps.speed.isValid() && gps.location.isValid()) {
// Keep the most recent valid GPS speed between normal once-per-second
// NMEA updates. Only call it zero when that reading has gone stale.
currentSpeed = gps.speed.mph() * speedCorrection;
} else if (gps.speed.age() > 3000) {
currentSpeed = 0;
}
displayedSpeed += (currentSpeed - displayedSpeed) * 0.35;
if (displayedSpeed < 0.5) displayedSpeed = 0;
updateOdometer();
const bool wasClockMode = clockMode;
if (currentSpeed < CLOCK_STILL_MPH) {
if (stationarySinceMs == 0) stationarySinceMs = millis();
if (millis() - stationarySinceMs >= CLOCK_AFTER_STILL_MS) clockMode = true;
} else {
stationarySinceMs = 0;
clockMode = false;
lastClockSecond = -1;
}
// Keep the calibration screen visible until its own upper-left save hold
// closes it. The normal gauge redraw would otherwise paint over + and -.
if (digitalPage && !clockMode) {
drawDigitalPage(wasClockMode);
} else if (satelliteMenu) {
showSatelliteMenu();
} else if (!calibrationMenu) {
if (clockMode) {
int thisSecond = gps.time.isValid() ? gps.time.second() : (int)(millis() / 1000);
if (thisSecond != lastClockSecond) {
lastClockSecond = thisSecond;
drawClock();
}
} else {
redrawGauge(displayedSpeed);
}
lastDrawnOdo = -1;
}
}
}Remix this project
Make it yours in one click
Open a full copy of this project in your own Schematik workspace, diagram, code, parts, and assembly steps included. Swap the sensor, add features, or redesign the whole thing with AI. The author's original stays untouched.




