Skip to content

Instantly share code, notes, and snippets.

@fxprime
Created July 3, 2026 03:42
Show Gist options
  • Select an option

  • Save fxprime/2312033c01b0193d741ee6ca97adfad0 to your computer and use it in GitHub Desktop.

Select an option

Save fxprime/2312033c01b0193d741ee6ca97adfad0 to your computer and use it in GitHub Desktop.
#include <Arduino_GFX_Library.h> // By Moon On Our Nation
/* Use V1.3.1
* (https://github.com/moononournation/Arduino_GFX) Can be installed via the library manager of the Arduino IDE
*/
#include <XPT2046_Touchscreen.h> // By Paul Stoffregen
/* Use V1.4.0
* (https://github.com/PaulStoffregen/XPT2046_Touchscreen) Can be installed via the library manager of the Arduino IDE
*/
#include <SPI.h>
// Setup TFT screen
Arduino_DataBus *bus = new Arduino_ESP32SPI(2 /* DC */, 15 /* CS */, 14 /* SCK */, 13 /* MOSI */, 12 /* MISO */);
Arduino_GFX *gfx = new Arduino_ST7796(bus, -1 /* RST */, 3 /* rotation */, false /* IPS */);
#define GFX_BL DF_GFX_BL // default backlight pin, you may replace DF_GFX_BL to actual backlight pin
#define TFT_BL 27
// Setup touch
#define TOUCH_XPT2046
#define TOUCH_XPT2046_SCK 14
#define TOUCH_XPT2046_MISO 12
#define TOUCH_XPT2046_MOSI 13
#define TOUCH_XPT2046_CS 33
#define TOUCH_XPT2046_INT 36
#define TOUCH_XPT2046_ROTATION 3
#define TOUCH_MAP_X1 4000
#define TOUCH_MAP_X2 100
#define TOUCH_MAP_Y1 100
#define TOUCH_MAP_Y2 4000
XPT2046_Touchscreen ts(TOUCH_XPT2046_CS, TOUCH_XPT2046_INT);
void setup(void) {
Serial.begin(115200);
gfx->begin();
gfx->fillScreen(BLACK);
#ifdef TFT_BL
pinMode(TFT_BL, OUTPUT);
digitalWrite(TFT_BL, HIGH);
#endif
gfx->setCursor(10, 10);
gfx->setTextColor(RED);
gfx->println("Hello World!");
delay(2000); // 2 seconds
SPI.begin(TOUCH_XPT2046_SCK, TOUCH_XPT2046_MISO, TOUCH_XPT2046_MOSI, TOUCH_XPT2046_CS);
ts.begin();
ts.setRotation(TOUCH_XPT2046_ROTATION);
gfx->fillScreen(BLACK);
gfx->setCursor(0, 0);
gfx->print("ESP32-3248S035R touch test:");
gfx->setTextColor(GREEN);
Serial.println(digitalRead(-1));
}
void loop() {
if (ts.tirqTouched()) {
if (ts.touched()) {
// Get touched point
TS_Point p = ts.getPoint();
// Print touch in Serial Monitor
Serial.print("Touch: ");
Serial.print(" X: ");
Serial.print(p.x);
Serial.print(" Y: ");
Serial.print(p.y);
Serial.print(" Pressure: ");
Serial.println(p.z);
// Print touch on screen
gfx->setCursor(0, 20);
gfx->println("X: " + String(p.x) + " Y: " + String(p.y) + " Pressure: " + String(p.z));
delay(100);
gfx->fillRect(0, 20, 220, 40, BLACK);
gfx->fillRect(0, 300, 170, 20, BLACK);
gfx->setCursor(0, 310);
}
}
delay(50);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment