ESP32 - Touchscreen - TFT_eSPI - ILI9341

I've got my TFT screen working beautifully with my ESP32 with the TFT_eSPI library. All rendered beautifully with LittlevGL.

However i'm making no headway getting the TOUCH element to work.

Has anyone already got touch working with the ESP32? Any pinouts or any advice as to how i can get up and going? Pins for Touch are:

  • IRQ
  • DO
  • DIN
  • CS
  • CLK
2 Likes

Wire as follows:

IRQ - no connection
DO to ESP32 MISO
DIN to ESP32 MOSI
CS to a pin of your choice, make sure you define this pin in the library setup file
CLK to ESP32 SCK

Test initially using this example.

Thanks for that, my hangup was not realizing that it is possible to have multiple connections going in to MISO MOSI and SCK on the ESP32.

For anyone searching in the future. Here is a pinout that will get the ESP32 working with TFT & Touch & SD card.....

TOUCH SPI CONNECTIONS (HSPI) - set up in User_setup.h (Note #define USE_HSPI_PORT uncommented in User_setup.h ) :

  • T_IRQ No connection required
  • T_DO 12
  • T_DIN 13
  • T_CS 33 //set in User_setup.h
  • T_CLK 14

TFT ILI9341 SPI CONNECTIONS (HSPI) - set up in User_setup.h :

  • SDO(MISO) 12
  • LED 3.3V
  • SCK 14
  • SDI(MOSI) 13
  • DC 2
  • RESET 4
  • CS 15
  • GND gnd
  • VCC 3.3V

**TFT SD CARD CONNECTIONS (VSPI): **

  • SD_SCK 18
  • SD_MISO 19
  • SD_MOSI 23
  • SD_CS 5 set in .h header using SDFat library
1 Like

Slider working fine with touch. Rpi display.

// ###### EDIT THE PIN NUMBERS IN THE LINES FOLLOWING TO SUIT YOUR ESP32 SETUP ######

// For ESP32 Dev board (only tested with ILI9341 display)
// The hardware SPI can be mapped to any pins

#define TFT_MISO 19
#define TFT_MOSI 23
#define TFT_SCLK 18
#define TFT_CS 15 // Chip select control pin
#define TFT_DC 2 // Data Command control pin
//#define TFT_RST 4 // Reset pin (could connect to RST pin)
#define TFT_RST -1 // Set TFT_RST to -1 if display RESET is connected to ESP32 board RST

//#define TFT_BL 32 // LED back-light (only for ST7789 with backlight control pin)

#define TOUCH_CS 5 // Chip select pin (T_CS) of touch screen

//#define TFT_WR 22 // Write strobe for modified Raspberry Pi TFT only

You can use this example for inspiration. The library uses the button class from Adafruit_GFX so you can use the Adafruit tutorials for that class.

These are very basic style.I'm looking for a modern GUI Like littlevgl:D

Ah, I suppose that is the danger of trying to answer such a brief question. :smiley:

No point in looking for an alternative to LittlevGL, there is none better. I assume you have the slider example in LittlevGL running and the touch screen working…

All you need to do now is take time to read the excellent online LittlevGL tutorial (example button code is on page 1!).

Use the LittlevGL reference documents to understand what each line in the slider example β€œsetup” tab of the existing slider sketch does. All graphical elements follow the same coding approach.

With a bit of effort you will be able to very quickly generate screens like this one that I made to control some Neopixel LEDs:

The example author redraws the screen pixel by pixel in a loop, fortunately the TFT_eSPI library has efficient functions so the redraw speed is very good. This code change will speed up redraw but a RPi display may not cope as the interface design is sensitive to SPI clock rate and the pixel rate…:

void my_disp_flush(lv_disp_drv_t *disp, const lv_area_t *area, lv_color_t *color_p)
{
  uint32_t w = (area->x2 - area->x1 + 1);
  uint32_t h = (area->y2 - area->y1 + 1);

  tft.startWrite();
  tft.setAddrWindow(area->x1, area->y1, w, h);
  tft.pushColors(&color_p->full, w*h, true);
  tft.endWrite();

  lv_disp_flush_ready(disp); /* tell lvgl that flushing is done */
}

This is probably not the answer you were looking for :smiley:

Now i’m looking for a button example which toggles a led on/off pressing on same area.
PLEASE help me to complete this code.

boolean wastouched = true;
void loop() {
boolean istouched = ts.touched();
if (istouched) {
TS_Point p = ts.getPoint();
if (!wastouched) {
Serial.println(β€œTouched”);
}
p.x = map(p.x, TS_MAXX, TS_MINX, 0, 320);
p.y = map(p.y, TS_MAXY, TS_MINY, 0, 480);

if(p.x>160 && p.x<320 && p.y>240 && p.y<360){
Serial.println(β€œLED ON”);

digitalWrite(LED,LOW);

} else {
if (wastouched) {
}
wastouched = istouched;
}
}
}

I am not sure why you are so insistent that it is one button. It is much easier to have two buttons. one for "off" and one for "on" as screens with touch capability have plenty of room for two buttons. The urgency of request also suggests you have an exam assignment...

If you use two buttons then you do not need more complex de-bounce code.

The LittlevGL library provides some help with a toggle action, but I chose the simple path of having two buttons where "noisy" touch signals can be ignored.

The problem for me is that if I write the code for you then you will not have learnt anything and it will propagate more questions. The LittlevGL Arduino library is quite young and I see the developers do intent to add more examples so if you are patient then you may find an adaptable example included in the library.

Yes you are 200% right but I don't know what you know but you also don't know many things what I know.
So its no good to underestimate anyone like this. I was just asking you an example of a toggle button for touch screen which I don't know how to do that .
I could buy a ready made touch controller,that is not an issue.its just a learning purpose buy you treat like I'm asking you everything you have.

Couldn't agree more with Bodmer, i've just built an interface using the fantastic LittlevGL and it is very straightforward.
Start with the provided ESP32 example, add in some example code from here - https://docs.littlevgl.com/en/html/object-types/btn.html, tweak it with some help from https://forum.littlevgl.com/ and you'll be flying!

As I explained earlier,I just wanted to learn and see a toggle button on a tft display. I know what I'm doing and which system is good for me.
And now I managed it myself.so this matter is closed now.i don't need any suggestion for this topic anymore Thanks.

okay i was using a ili9341 display with 320x240 resolution and 2.4 inches. i can't get anything running. i have uncommented out the pins and commented the esp8266 values. i added the tft_espi library to arduino via zip file. do i need to update the contents of the zip file and how would i do that. any help would be appreciated from someone that has got a similar config working

@truufseir
Use the IDE library manager to fetch the library. The Arduino IDE does not handle zip files.

Wire the ESP8266 to the display as defined in the Setup.h file within the library and you do not need to change anything.

Hello,
I have a problem with the touch screen function. I tried a lot and each time failed.

The sketch UFT_Demo works but as soon as I try a sketch with the touchscree function, (keypad), nothing.

The keypad appears but neither calibration nor response.

here is the material used:

https://fr.aliexpress.com/item/4000183953417.html?spm=a2g0s.9042311.0.0.5fae6c37CTeXqJ

Here is the wiring: see attachment

Read_user_setup gives this:

11: 27: 00.239 β†’ ets Jun 8 2016 00:22:57
11: 27: 00.239 β†’
11: 27: 00.239 β†’ rst: 0x1 (POWERON_RESET), boot: 0x13 (SPI_FAST_FLASH_BOOT)
11: 27: 00.239 β†’ configsip: 0, SPIWP: 0xee
11: 27: 00.239 β†’ clk_drv: 0x00, q_drv: 0x00, d_drv: 0x00, cs0_drv: 0x00, hd_drv: 0x00, wp_drv: 0x00
11: 27: 00.274 β†’ mode: DIO, clock div: 1
11: 27: 00.274 β†’ load: 0x3fff0018, len: 4
11: 27: 00.274 β†’ load: 0x3fff001c, len: 1216
11: 27: 00.274 β†’ ho 0 tail 12 room 4
11: 27: 00.274 β†’ load: 0x40078000, len: 9720
11: 27: 00.274 β†’ ho 0 tail 12 room 4
11: 27: 00.274 β†’ load: 0x40080400, len: 6352
11: 27: 00.274 β†’ entry 0x400806b8
11: 27: 00.726 β†’
11: 27: 00.726 β†’
11: 27: 00.726 β†’ TFT_eSPI ver = 2.2.0
11: 27: 00.726 β†’ Processor = ESP50
11: 27: 00.726 β†’ Frequency = 240 MHz
11: 27: 00.726 β†’ Transactions = Yes
11: 27: 00.726 β†’ Interface = SPI
11: 27: 00.726 β†’ Display driver = 9341
11: 27: 00.726 β†’ Display width = 240
11: 27: 00.726 β†’ Display height = 320
11: 27: 00.726 β†’
11: 27: 00.726 β†’ MOSI = D-1 (GPIO 23)
11: 27: 00.726 β†’ MISO = D-1 (GPIO 19)
11: 27: 00.726 β†’ SCK = D-1 (GPIO 18)
11: 27: 00.726 β†’ TFT_CS = D8 (GPIO 15)
11: 27: 00.726 β†’ TFT_DC = D4 (GPIO 2)
11: 27: 00.726 β†’ TFT_RST = D2 (GPIO 4)
11: 27: 00.760 β†’
11: 27: 00.760 β†’ TOUCH_CS = D-1 (GPIO 27)
11: 27: 00.760 β†’
11: 27: 00.760 β†’ Font GLCD loaded
11: 27: 00.760 β†’ Font 2 loaded
11: 27: 00.760 β†’ Font 4 loaded
11: 27: 00.760 β†’ Font 6 loaded
11: 27: 00.760 β†’ Font 7 loaded
11: 27: 00.760 β†’ Font 8 loaded
11: 27: 00.760 β†’ Smooth font enabled
11: 27: 00.760 β†’
11: 27: 00.760 β†’ Display SPI frequency = 40.0 MHz
11: 27: 00.760 β†’ Touch SPI frequency = 2.5 MHz
11: 27: 00.760 β†’ [/ code]

Test_Touch_Controller gives this:

11: 30: 32.709 β†’ x: 0 y: 0 z: 4095
11: 30: 32.946 β†’ x: 0 y: 0 z: 4095
11: 30: 33.185 β†’ x: 0 y: 0 z: 4095
11: 30: 33.460 β†’ x: 0 y: 0 z: 4095
11: 30: 33.701 β†’ x: 0 y: 0 z: 4095
11: 30: 33.941 β†’ x: 0 y: 0 z: 4095
11: 30: 34.213 β†’ x: 0 y: 0 z: 4095
11: 30: 34.454 β†’ x: 0 y: 0 z: 4095

An idea?

I’m desperate (dramatization)

Thank you.

Thanks for your links, setup, wiring.

First off. Run all the library examples. Note any problems on paper.

Your wiring looks fine for the TFT.
The Touch Screen will only work if you connect the T_CLK, T_CS, T_DIN, T_OUT pins.
You must have an XPT2046 chip mounted on the pcb at position U2.

Report any problems to the Forum. Quoting the example by name and typing from your paper notes.

Then run the Touch examples. Note any problems on paper.
Specify a GPIO pin for T_CS. The other pins go to the same SPI pins as the TFT.
Life is easier if you use exactly the same wiring as in the examples.

Report any problems to the Forum. Quoting the example by name and typing from your paper notes.

Good Luck.

David.

Thanks for your quick answer.

yes, there is an XPT2046 chip mounted on the pcb at position U2.

here is an excerpt from User_Setup.h

// For ESP32 Dev board (only tested with ILI9341 display)
// The hardware SPI can be mapped to any pins
// VCC
// GND
#define TFT_CS 15 // orange Chip select control pin
#define TFT_RST 4 // white Reset pin (could connect to RST pin) // # define TFT_RST -1 // Set TFT_RST to -1 if display RESET is connected to ESP32 board RST
#define TFT_DC 2 // beige Data Command control pin
#define TFT_MOSI 23 // purple
#define TFT_SCLK 18 // yellow
// LED
#define TFT_MISO 19 /// blue
// ** Touchscreen **
#define TOUCH_CLK 18 // yellow
#define TOUCH_CS 21 // green Chip select pin (T_CS) of touch screen
#define TOUCH_DIN 23 // mauve
#define TOUCH_DO 19 /// purple

Do I have to declare the touchscreen pins β€œT_ or TOUCH_”?

The examples all work except for the TOUCH function.
I never have a screen for calibration (need an SD card?)

I have corrected the colour names. Your Green wire seems to be connected to D27 and not D21 from the Fritzing PNG. I have not checked the module pin numbers against Fritzing.

#define TFT_CS 15 // orange Chip select control pin
#define TFT_RST 4 // white Reset pin (could connect to RST pin) // # define TFT_RST -1 // Set TFT_RST to -1 if display RESET is connected to ESP32 board RST
#define TFT_DC 2 // gray Data Command control pin
#define TFT_MOSI 23 // purple 
#define TFT_SCLK 18 // yellow
// LED
#define TFT_MISO 19 /// blue
// ** Touchscreen **
#define TOUCH_CLK 18 // yellow
#define TOUCH_CS 21 // green Chip select pin (T_CS) of touch screen
#define TOUCH_DIN 23 // purple
#define TOUCH_DO 19 /// blue

I would move the Green wire to D21 which "looks like next to" the Blue wire.
The Touch sketch should work.

Thanks for links and the Fritzing diagram. Especially annotating the wire colours in defines.
Much better than typing !!

David.

YES, it works .... as I have limited skills in programmind (I'm a 50 old beginner)

Thank a lot

I am gobsmacked !!

You have posted well written messages, provided information, provided wiring diagrams, ...

Complete success within about 2.5 hours.
Excellent use of the Forum.

Compare this with posts that withhold information, rats-nest wiring, blurred photos, ...
Sometimes threads go on for weeks and months.

You will be amazed by Bodmer's examples and similar ESP32 projects.

David.