I am integrating a 2.8" Waveshare Touch LCD with a NODEMCU ESP32 Dev board (38pin). The display has SD card and touch (XPT2046) integrated. I can get each component to work in isolation, but as soon as I try to use the LCD and the SD, or the LCD and the touch, only the LCD works.
My connections are as follows:
MOSI - GPIO23
MISO - GPIO19
SCLK - GPIO18
RESET - GPIO4
LCD_DC - GPIO2
LCD_CS - GPIO15
LCD_BL - GPIO32
SD_CS - GPIO5
TP_IRQ - GPIO22
TP_CS - GPIO21
The sketch below draws four rectangles and monitors if they are touched. I am printing the touch coordinates to the serial monitor. As the code is below, the touch function returns 65512 or the touch coordinates regardless of where I touch on the screen. If I comment out the lcd functions and upload the sketch, the touch coordinates are registered correctly (x= 0..240, y=0..320). I have tested the display on an Arduino mega with the sample programs downloaded from the Waveshare website, and these work perfectly, so I know the hardware is not damaged. (Although the example below is specific to the lcd/touch combination, I get the same response when using the lcd/sd combination. Any recommendations or assistance would be greatly appreciated.
#include <TFT_eSPI.h> // Include the TFT_eSPI library
#include <XPT2046.h> // Include the XPT2046 touch screen library
#define TFT_CS 15 // TFT CS pin
#define TFT_DC 2 // TFT DC pin
#define TS_CS 21 // Touchscreen CS pin
#define TP_IRQ_PIN 22 // Touchscreen IRQ pin
TFT_eSPI tft = TFT_eSPI(); // Initialize TFT library object
setup_t tft_setup_info;
// Button coordinates and size
#define BUTTON_W 80
#define BUTTON_H 60
#define BUTTON_COLOR TFT_BLUE
#define BUTTON_PRESSED_COLOR TFT_GREEN
// Button positions
#define BUTTON1_X 20
#define BUTTON1_Y 20
#define BUTTON2_X (320 - BUTTON_W - 20)
#define BUTTON2_Y 20
#define BUTTON3_X 20
#define BUTTON3_Y (240 - BUTTON_H - 20)
#define BUTTON4_X (320 - BUTTON_W - 20)
#define BUTTON4_Y (240 - BUTTON_H - 20)
void setup() {
Serial.begin(115200);
tft.begin(); // Initialize TFT
tft.setRotation(1); // Set screen orientation if needed
Serial.begin(115200);
tft.fillScreen(TFT_BLACK); // Clear screen
pinMode(TP_IRQ_PIN, INPUT_PULLUP); // Set the IRQ pin as input with pullup resistor
pinMode(TS_CS, OUTPUT); // Set touchscreen CS pin as output
digitalWrite(TS_CS, HIGH); // Deselect the touchscreen
Xpt.xpt2046_init(); // Initialize the touch screen
// Draw initial buttons
drawButton(BUTTON1_X, BUTTON1_Y, BUTTON_W, BUTTON_H, BUTTON_COLOR);
drawButton(BUTTON2_X, BUTTON2_Y, BUTTON_W, BUTTON_H, BUTTON_COLOR);
drawButton(BUTTON3_X, BUTTON3_Y, BUTTON_W, BUTTON_H, BUTTON_COLOR);
drawButton(BUTTON4_X, BUTTON4_Y, BUTTON_W, BUTTON_H, BUTTON_COLOR);
}
void loop() {
if (digitalRead(TP_IRQ_PIN) == LOW) { // Check if the screen is being touched
uint16_t x, y;
digitalWrite(TS_CS, LOW); // Select the touchscreen
if (Xpt.xpt2046_twice_read_xy(&x, &y)) {
digitalWrite(TS_CS, HIGH); // Deselect the touchscreen
// Map the raw touch coordinates to the screen coordinates
uint16_t screenX = map(x, 150, 3800, 0, 320);
uint16_t screenY = map(y, 120, 3700, 0, 240);
Serial.print("Touch at: ");
Serial.print("X = ");
Serial.print(screenX);
Serial.print(", Y = ");
Serial.println(screenY);
// Check if touch point is within button areas
if (isButtonPressed(screenX, screenY, BUTTON1_X, BUTTON1_Y, BUTTON_W, BUTTON_H)) {
handleButtonPress(BUTTON1_X, BUTTON1_Y, BUTTON_W, BUTTON_H);
} else if (isButtonPressed(screenX, screenY, BUTTON2_X, BUTTON2_Y, BUTTON_W, BUTTON_H)) {
handleButtonPress(BUTTON2_X, BUTTON2_Y, BUTTON_W, BUTTON_H);
} else if (isButtonPressed(screenX, screenY, BUTTON3_X, BUTTON3_Y, BUTTON_W, BUTTON_H)) {
handleButtonPress(BUTTON3_X, BUTTON3_Y, BUTTON_W, BUTTON_H);
} else if (isButtonPressed(screenX, screenY, BUTTON4_X, BUTTON4_Y, BUTTON_W, BUTTON_H)) {
handleButtonPress(BUTTON4_X, BUTTON4_Y, BUTTON_W, BUTTON_H);
}
}
}
delay(100);
}
void drawButton(int x, int y, int w, int h, uint32_t color) {
tft.fillRect(x, y, w, h, color); // Draw button rectangle
tft.drawRect(x, y, w, h, TFT_WHITE); // Draw button outline
}
bool isButtonPressed(uint16_t touchX, uint16_t touchY, int x, int y, int w, int h) {
return (touchX > x && touchX < x + w && touchY > y && touchY < y + h);
}
void handleButtonPress(int x, int y, int w, int h) {
drawButton(x, y, w, h, BUTTON_PRESSED_COLOR);
delay(500);
drawButton(x, y, w, h, BUTTON_COLOR);
}