#include <Adafruit_GFX.h>
#include <Adafruit_ILI9341.h>
#include <Adafruit_FT6206.h>
// Pin definitions for the Arduino Mega
#define TFT_CS 10
#define TFT_DC 53
#define TFT_RST -1 // You can also connect this to the Arduino reset pin
// Create the display and touchscreen objects
Adafruit_ILI9341 tft = Adafruit_ILI9341(TFT_CS, TFT_DC, TFT_RST);
Adafruit_FT6206 ts = Adafruit_FT6206();
void setup() {
Serial.begin(9600);
tft.begin();
ts.begin();
tft.fillScreen(ILI9341_BLACK);
tft.setTextSize(2);
tft.setTextColor(ILI9341_WHITE);
tft.setCursor(10, 10);
tft.print("Hello, Wokwi!");
// Draw some shapes
tft.drawRect(10, 40, 100, 50, ILI9341_GREEN);
tft.fillCircle(160, 65, 30, ILI9341_BLUE);
tft.drawLine(10, 100, 220, 100, ILI9341_RED);
}
void loop() {
if (ts.touched()) {
TS_Point p = ts.getPoint();
// Due to the screen orientation, we need to invert and map the coordinates
p.x = map(p.x, 0, 240, 240, 0);
p.y = map(p.y, 0, 320, 320, 0);
Serial.print("X: "); Serial.print(p.x);
Serial.print(" Y: "); Serial.println(p.y);
// Draw a circle where the screen is touched
tft.fillCircle(p.x, p.y, 5, ILI9341_YELLOW);
}
}
Loading
ili9341-cap-touch
ili9341-cap-touch