Using ST7796S and XPT2046 with hardware SPI

Hi,

I'm working on ESP32 + ST7796S TFT display with XPT2046 touch controller.

  1. There's a problem with running the code with ESP32 which is the touch isn't responsive, I have to touch it multiple times until it response. Where I didn't have this problem with the same setup using Arduino Nano. So, what could be the reason for that ?

  2. I used the ESP32 to get more SPI speed, and I changed the SPI division to 2 to get 8MHz, but I think it runs the same speed as the Arduino Nano. How to get it to run faster ?

  3. I'm using TFT_Touch-master to run XPT2046. The library uses the bit-bang to reading and writing data. My question, Is it a good idea to modify the library to use hardware SPI ?

First off. Please post a link to the actual display that you have bought.

Google finds https://github.com/Bodmer/TFT_Touch which is not really intended for ESP32.

Bodmer has the excellent TFT_eSPI.h library that you can install via the IDE Library Manager. This supports hardware SPI.

I suggest that you run all of his examples to gain familiarity with the library(s).
Then quote the name of the library example that is giving you a problem.

It is never wise to share a single hardware SPI bus with bit-bang and proper hardware SPI devices.

David.

1 Like

http://www.lcdwiki.com/4.0inch_SPI_Module_ST7796

OK, that's an important thing to know. But can you tell me why ? Maybe I can do something and edit the library to improve its work with the ESP32.

I've this library and working on it. But didn't test anything yet.

Didn't get exactly what you mean here. Do you mean it's not a good idea to share SPI bus with bit-bang ?

I have another question, can I run XPT2046 with hardware SPI ?

Thanks for the link. The left hand picture shows the non-Touch version. The right hand picture shows the Touch version i.e. with the XPT2046 chip mounted on the pcb.

The TFT_Touch library is only bit-bang and was written 7 years ago for 2560-style parallel shields.
Yes, you can probably use it but you need separate Touch pins from the TFT and SD pins.

TFT_eSPI is infinitely better than other libraries. Just try it.

Yes, you can put TFT, SD, Touch all on the same hardware SPI bus. Note that XPT2046 requires 2MHz SCK speed. But Bodmer handles the different speeds. TFT runs much faster.

David.

1 Like

I have the one with touch controller.

How to define the CS, RST and DC pins for the TFT and the touch controller?

OK, I found them on User_Setups, I have to say well arranged library.

Is the SD also supported in this library ?

Wire all the SCK pins together,
Wire the MOSI pins together.
...
Separate pins for TFT_CS, SD_CS, Touch_CS.

Please just try the library examples that come with TFT_eSPI.
If you have a problem quote the example by name.
Run the C:\Users\ ... \Documents\Arduino\libraries\TFT_eSPI\examples\Test and diagnostics\Read_User_Setup\Read_User_Setup.ino sketch.

Copy-paste the Serial Terminal to your message.

David.

1 Like

I got this warning as a start:

  // Shift right 1 to drop rounding bit and shift right 8 to divide by 256
  uint16_t r = (((fgR * alpha) + (bgR * (255 - alpha))) >> 9);
  uint16_t g = (((fgG * alpha) + (bgG * (255 - alpha))) >> 9);
  uint16_t b = (((fgB * alpha) + (bgB * (255 - alpha))) >> 9);

  // Combine RGB colours into 24 bits
  return (r << 16) | (g << 8) | (b << 0);
C:\Users\...\OneDrive\Documents\Arduino\libraries\TFT_eSPI\TFT_eSPI.cpp: In member function 'uint32_t TFT_eSPI::alphaBlend24(uint8_t, uint32_t, uint32_t, uint8_t)':
C:\Users\...\OneDrive\Documents\Arduino\libraries\TFT_eSPI\TFT_eSPI.cpp:4446:16: warning: left shift count >= width of type [-Wshift-count-overflow]
   return (r << 16) | (g << 8) | (b << 0);

I installed TFT_eSPI on a brand new Windows11 Laptop via the IDE Library Manager. Edited for MY wiring.

TFT_eSPI ver = 2.4.72
Processor    = ESP32
Frequency    = 240MHz
Transactions = Yes
Interface    = SPI
Display driver = 9341
Display width  = 240
Display height = 320

MOSI    = GPIO 23
SCK     = GPIO 18
TFT_CS   = GPIO 5
TFT_DC   = GPIO 13
TFT_RST  = GPIO 12
TOUCH_CS = GPIO 16

Font GLCD   loaded
Font 2      loaded
Font 4      loaded
Font 6      loaded
Font 7      loaded
Font 8      loaded
Smooth font enabled

Display SPI frequency = 27.00
Touch SPI frequency   = 2.50

Then built and ran TFT_graphictest_one.ino from the examples. (on IDE 1.8.19)

TFT_eSPI library test!
Benchmark                Time (microseconds)
Screen fill              233111
Text                     16225
Lines                    90166
Horiz/Vert Lines         20725
Rectangles (outline)     12912
Rectangles (filled)      478457
Circles (filled)         64942
Circles (outline)        34389
Triangles (outline)      22164
Triangles (filled)       164810
Rounded rects (outline)  21696
Rounded rects (filled)   487335
Done!

The only "warnings" that I got were

In file included from C:\Users\david\OneDrive\Documents\Arduino\libraries\TFT_eSPI\TFT_eSPI.cpp:25:
C:\Users\david\OneDrive\Documents\Arduino\libraries\TFT_eSPI\Processors/TFT_eSPI_ESP32.c: In member function 'bool TFT_eSPI::initDMA(bool)':
C:\Users\david\OneDrive\Documents\Arduino\libraries\TFT_eSPI\Processors/TFT_eSPI_ESP32.c:782:3: warning: missing initializer for member 'spi_bus_config_t::data4_io_num' [-Wmissing-field-initializers]
   };
   ^
C:\Users\david\OneDrive\Documents\Arduino\libraries\TFT_eSPI\Processors/TFT_eSPI_ESP32.c:782:3: warning: missing initializer for member 'spi_bus_config_t::data5_io_num' [-Wmissing-field-initializers]
C:\Users\david\OneDrive\Documents\Arduino\libraries\TFT_eSPI\Processors/TFT_eSPI_ESP32.c:782:3: warning: missing initializer for member 'spi_bus_config_t::data6_io_num' [-Wmissing-field-initializers]
C:\Users\david\OneDrive\Documents\Arduino\libraries\TFT_eSPI\Processors/TFT_eSPI_ESP32.c:782:3: warning: missing initializer for member 'spi_bus_config_t::data7_io_num' [-Wmissing-field-initializers]

Errors are fatal. Warnings are just advisory. I have Warnings set to "all" in Preferences.

1 Like

First, I got redefined pin definitions warnings, then I commented this line:

In file included from C:\Users\wopre\OneDrive\Documents\Arduino\libraries\TFT_eSPI/User_Setup_Select.h:28,
                 from C:\Users\wopre\OneDrive\Documents\Arduino\libraries\TFT_eSPI/TFT_eSPI.h:42,
                 from C:\Users\wopre\OneDrive\Documents\Arduino\libraries\TFT_eSPI\examples\Test and diagnostics\Read_User_Setup\Read_User_Setup.ino:18:
C:\Users\wopre\OneDrive\Documents\Arduino\libraries\TFT_eSPI/User_Setups/Setup36_RPi_touch_ST7796.h:11: warning: "TFT_CS" redefined
 #define TFT_CS   15  // Chip select control pin
 
In file included from C:\Users\wopre\OneDrive\Documents\Arduino\libraries\TFT_eSPI/User_Setup_Select.h:22,
                 from C:\Users\wopre\OneDrive\Documents\Arduino\libraries\TFT_eSPI/TFT_eSPI.h:42,
                 from C:\Users\wopre\OneDrive\Documents\Arduino\libraries\TFT_eSPI\examples\Test and diagnostics\Read_User_Setup\Read_User_Setup.ino:18:
C:\Users\wopre\OneDrive\Documents\Arduino\libraries\TFT_eSPI/User_Setup.h:136: note: this is the location of the previous definition
 #define TFT_CS   PIN_D8  // Chip select control pin D8
 
In file included from C:\Users\wopre\OneDrive\Documents\Arduino\libraries\TFT_eSPI/User_Setup_Select.h:28,
                 from C:\Users\wopre\OneDrive\Documents\Arduino\libraries\TFT_eSPI/TFT_eSPI.h:42,
                 from C:\Users\wopre\OneDrive\Documents\Arduino\libraries\TFT_eSPI\examples\Test and diagnostics\Read_User_Setup\Read_User_Setup.ino:18:
C:\Users\wopre\OneDrive\Documents\Arduino\libraries\TFT_eSPI/User_Setups/Setup36_RPi_touch_ST7796.h:12: warning: "TFT_DC" redefined
 #define TFT_DC    2  // Data Command control pin
 
In file included from C:\Users\wopre\OneDrive\Documents\Arduino\libraries\TFT_eSPI/User_Setup_Select.h:22,
                 from C:\Users\wopre\OneDrive\Documents\Arduino\libraries\TFT_eSPI/TFT_eSPI.h:42,
                 from C:\Users\wopre\OneDrive\Documents\Arduino\libraries\TFT_eSPI\examples\Test and diagnostics\Read_User_Setup\Read_User_Setup.ino:18:
C:\Users\wopre\OneDrive\Documents\Arduino\libraries\TFT_eSPI/User_Setup.h:137: note: this is the location of the previous definition
 #define TFT_DC   PIN_D3  // Data Command control pin
 
In file included from C:\Users\wopre\OneDrive\Documents\Arduino\libraries\TFT_eSPI/User_Setup_Select.h:28,
                 from C:\Users\wopre\OneDrive\Documents\Arduino\libraries\TFT_eSPI/TFT_eSPI.h:42,
                 from C:\Users\wopre\OneDrive\Documents\Arduino\libraries\TFT_eSPI\examples\Test and diagnostics\Read_User_Setup\Read_User_Setup.ino:18:
C:\Users\wopre\OneDrive\Documents\Arduino\libraries\TFT_eSPI/User_Setups/Setup36_RPi_touch_ST7796.h:13: warning: "TFT_RST" redefined
 #define TFT_RST   4  // Reset pin (could connect to RST pin)
 
In file included from C:\Users\wopre\OneDrive\Documents\Arduino\libraries\TFT_eSPI/User_Setup_Select.h:22,
                 from C:\Users\wopre\OneDrive\Documents\Arduino\libraries\TFT_eSPI/TFT_eSPI.h:42,
                 from C:\Users\wopre\OneDrive\Documents\Arduino\libraries\TFT_eSPI\examples\Test and diagnostics\Read_User_Setup\Read_User_Setup.ino:18:
C:\Users\wopre\OneDrive\Documents\Arduino\libraries\TFT_eSPI/User_Setup.h:138: note: this is the location of the previous definition
 #define TFT_RST  PIN_D4  // Reset pin (could connect to NodeMCU RST, see next line)
 
In file included from C:\Users\wopre\OneDrive\Documents\Arduino\libraries\TFT_eSPI/User_Setup_Select.h:28,
                 from C:\Users\wopre\OneDrive\Documents\Arduino\libraries\TFT_eSPI/TFT_eSPI.h:42,
                 from C:\Users\wopre\OneDrive\Documents\Arduino\libraries\TFT_eSPI\examples\Test and diagnostics\Read_User_Setup\Read_User_Setup.ino:18:
C:\Users\wopre\OneDrive\Documents\Arduino\libraries\TFT_eSPI/User_Setups/Setup36_RPi_touch_ST7796.h:30: warning: "SPI_FREQUENCY" redefined
 #define SPI_FREQUENCY  40000000
 
In file included from C:\Users\wopre\OneDrive\Documents\Arduino\libraries\TFT_eSPI/User_Setup_Select.h:22,
                 from C:\Users\wopre\OneDrive\Documents\Arduino\libraries\TFT_eSPI/TFT_eSPI.h:42,
                 from C:\Users\wopre\OneDrive\Documents\Arduino\libraries\TFT_eSPI\examples\Test and diagnostics\Read_User_Setup\Read_User_Setup.ino:18:
C:\Users\wopre\OneDrive\Documents\Arduino\libraries\TFT_eSPI/User_Setup.h:310: note: this is the location of the previous definition
 #define SPI_FREQUENCY  27000000
 
In file included from C:\Users\wopre\OneDrive\Documents\Arduino\libraries\TFT_eSPI/User_Setup_Select.h:28,
                 from C:\Users\wopre\OneDrive\Documents\Arduino\libraries\TFT_eSPI\TFT_eSPI.h:42,
                 from C:\Users\wopre\OneDrive\Documents\Arduino\libraries\TFT_eSPI\TFT_eSPI.cpp:17:
C:\Users\wopre\OneDrive\Documents\Arduino\libraries\TFT_eSPI/User_Setups/Setup36_RPi_touch_ST7796.h:11: warning: "TFT_CS" redefined
 #define TFT_CS   15  // Chip select control pin
 
In file included from C:\Users\wopre\OneDrive\Documents\Arduino\libraries\TFT_eSPI/User_Setup_Select.h:22,
                 from C:\Users\wopre\OneDrive\Documents\Arduino\libraries\TFT_eSPI\TFT_eSPI.h:42,
                 from C:\Users\wopre\OneDrive\Documents\Arduino\libraries\TFT_eSPI\TFT_eSPI.cpp:17:
C:\Users\wopre\OneDrive\Documents\Arduino\libraries\TFT_eSPI/User_Setup.h:136: note: this is the location of the previous definition
 #define TFT_CS   PIN_D8  // Chip select control pin D8
 
In file included from C:\Users\wopre\OneDrive\Documents\Arduino\libraries\TFT_eSPI/User_Setup_Select.h:28,
                 from C:\Users\wopre\OneDrive\Documents\Arduino\libraries\TFT_eSPI\TFT_eSPI.h:42,
                 from C:\Users\wopre\OneDrive\Documents\Arduino\libraries\TFT_eSPI\TFT_eSPI.cpp:17:
C:\Users\wopre\OneDrive\Documents\Arduino\libraries\TFT_eSPI/User_Setups/Setup36_RPi_touch_ST7796.h:12: warning: "TFT_DC" redefined
 #define TFT_DC    2  // Data Command control pin
 
In file included from C:\Users\wopre\OneDrive\Documents\Arduino\libraries\TFT_eSPI/User_Setup_Select.h:22,
                 from C:\Users\wopre\OneDrive\Documents\Arduino\libraries\TFT_eSPI\TFT_eSPI.h:42,
                 from C:\Users\wopre\OneDrive\Documents\Arduino\libraries\TFT_eSPI\TFT_eSPI.cpp:17:
C:\Users\wopre\OneDrive\Documents\Arduino\libraries\TFT_eSPI/User_Setup.h:137: note: this is the location of the previous definition
 #define TFT_DC   PIN_D3  // Data Command control pin
 
In file included from C:\Users\wopre\OneDrive\Documents\Arduino\libraries\TFT_eSPI/User_Setup_Select.h:28,
                 from C:\Users\wopre\OneDrive\Documents\Arduino\libraries\TFT_eSPI\TFT_eSPI.h:42,
                 from C:\Users\wopre\OneDrive\Documents\Arduino\libraries\TFT_eSPI\TFT_eSPI.cpp:17:
C:\Users\wopre\OneDrive\Documents\Arduino\libraries\TFT_eSPI/User_Setups/Setup36_RPi_touch_ST7796.h:13: warning: "TFT_RST" redefined
 #define TFT_RST   4  // Reset pin (could connect to RST pin)
 
In file included from C:\Users\wopre\OneDrive\Documents\Arduino\libraries\TFT_eSPI/User_Setup_Select.h:22,
                 from C:\Users\wopre\OneDrive\Documents\Arduino\libraries\TFT_eSPI\TFT_eSPI.h:42,
                 from C:\Users\wopre\OneDrive\Documents\Arduino\libraries\TFT_eSPI\TFT_eSPI.cpp:17:
C:\Users\wopre\OneDrive\Documents\Arduino\libraries\TFT_eSPI/User_Setup.h:138: note: this is the location of the previous definition
 #define TFT_RST  PIN_D4  // Reset pin (could connect to NodeMCU RST, see next line)
 
In file included from C:\Users\wopre\OneDrive\Documents\Arduino\libraries\TFT_eSPI/User_Setup_Select.h:28,
                 from C:\Users\wopre\OneDrive\Documents\Arduino\libraries\TFT_eSPI\TFT_eSPI.h:42,
                 from C:\Users\wopre\OneDrive\Documents\Arduino\libraries\TFT_eSPI\TFT_eSPI.cpp:17:
C:\Users\wopre\OneDrive\Documents\Arduino\libraries\TFT_eSPI/User_Setups/Setup36_RPi_touch_ST7796.h:30: warning: "SPI_FREQUENCY" redefined
 #define SPI_FREQUENCY  40000000
 
In file included from C:\Users\wopre\OneDrive\Documents\Arduino\libraries\TFT_eSPI/User_Setup_Select.h:22,
                 from C:\Users\wopre\OneDrive\Documents\Arduino\libraries\TFT_eSPI\TFT_eSPI.h:42,
                 from C:\Users\wopre\OneDrive\Documents\Arduino\libraries\TFT_eSPI\TFT_eSPI.cpp:17:
C:\Users\wopre\OneDrive\Documents\Arduino\libraries\TFT_eSPI/User_Setup.h:310: note: this is the location of the previous definition
 #define SPI_FREQUENCY  27000000
 
Sketch uses 292589 bytes (22%) of program storage space. Maximum is 1310720 bytes.
Global variables use 16900 bytes (5%) of dynamic memory, leaving 310780 bytes for local variables. Maximum is 327680 bytes.

But when I comment this line in User_Setup_Select.h, they all go away:

#include <User_Setup.h> // Default setup is root library folder

I don't know if this include is necessary beside the specific User_Setups header which is in my case:
#include <User_Setups/Setup36_RPi_touch_ST7796.h> // Setup file configured for ESP32 and RPi ST7796 TFT with touch

I ran the code with the commented:
#include <User_Setup.h>

And got the following:

TFT_eSPI ver = 2.4.72
Processor    = ESP32
Frequency    = 240MHz
Transactions = Yes
Interface    = SPI
Display driver = 7796
Display width  = 320
Display height = 480

MOSI    = GPIO 23
MISO    = GPIO 19
SCK     = GPIO 18
TFT_CS   = GPIO 15
TFT_DC   = GPIO 2
TFT_RST  = GPIO 4
TOUCH_CS = GPIO 22

Font GLCD   loaded
Font 2      loaded
Font 4      loaded
Font 6      loaded
Font 7      loaded
Font 8      loaded
Smooth font enabled

Display SPI frequency = 40.00
Touch SPI frequency   = 2.50

I uploaded the graphicstest sketch but it didn't work, I'm sure of the wiring.

I tried both:

#include <User_Setups/Setup27_RPi_ST7796_ESP32.h> // ESP32 RPi MHS-4.0 inch Display-B

and

#include <User_Setups/Setup36_RPi_touch_ST7796.h> // Setup file configured for ESP32 and RPi ST7796 TFT with touch

Another thing, does this mean anything ?

I removed the wires and re-wired it again, didn't connect the touch controller for now.

I want to test it with the Arduino nano. What should I do to run it with the nano ?

I haven't found headers for the AVR chips.

Why are you using Setup27_RPi_ST7796_ESP32. when you don't have a RaspberryPi abortion?

I suggest that you select Setup36_RPi_touch_ST7796.h as the "nearest" Setup file and edit it to #undef RPI_DISPLAY_TYPE
I also suggest that you copy the exact pin mapping from Setup36

I can't follow your wires very easily. It is wise to add your wire colours to the Setup. e.g.

#define TFT_MOSI             23    // White

Regarding Nano. TFT_eSPI does not support Nano.
The Red SPI Display requires level-shifters on every signal from the 5V Nano.
If you provide the correct 3.3V signals I can show you suitable ST7796 library(s).

David.

1 Like

I also tried Setup36_RPi_touch_ST7796.h all the same result.

In the picture, there are level shifters between the Nano and the TFT.

I did. I run the LCDWIKI code and it's working.

I'm sure there's something not write in the TFT_eSPI setting, I'm trying to figure and find what I'm doing wrong.

OK, this solved the problem :slight_smile: Thanks man really appreciate your support. And it's fast !

It is not the speed. It is the correct spelling that matters !!

1 Like

What other libraries that suit Arduino boards ?

I have also your Adafruit_ST7796S_kbv-master library.

Bodmer will be faster than Adafruit_ST7796S_kbv
Note that you can increase the "Adafruit" SCK speed with tft.begin(27000000);

David.

1 Like

Yep, but Adafruit_ST7796S_kbv can work on Arduino Nano. But actually started to like running TFT displays on fast boards.

Would this speed work on Arduino AVR boards ?