ST7796S 4 inch TFT
The 4 inch 320x480 SPI TFT with an XPT2046 touch panel: an ST7796S that takes RGB565 like the ILI9341, on bigger glass.
It has fourteen pins down its edge, in the same order as the 2.8 inch ILI9341 touch board and the 3.5 inch ILI9488. Aliases: ST7796, ST7796S, 4 inch TFT, 4.0 inch LCD, MSP4021.
To touch it, press on the picture while the circuit runs, and drag to move your finger. Let go and the finger lifts.
Pins
| Pin | What it does |
|---|---|
VCC, GND |
Supply and ground. The board runs from 3.3 V, or 5 V through its regulator. |
CS |
The display's chip select, active low. |
RST |
The display's reset, active low. |
DC |
The display's command or data line. |
MOSI, SCK |
The display's SPI data in and clock. |
LED |
The backlight. Low puts it out; left unwired, it is on. |
MISO |
The display's data out, never driven here. |
T_CLK, T_CS, T_DIN, T_DO |
The XPT2046 touch controller's SPI bus and chip select. |
T_IRQ |
Low while the panel is pressed. |
The touch pins may be left unwired when the sketch does not read the touch.
Properties
None.
What the model gets right
RGB565. The ST7796S takes COLMOD 0x55 and two bytes a pixel over SPI, like the
ILI9341, so a full-screen fill moves a third fewer bytes than on the ILI9488. With
TFT_eSPI on an ESP32 here, fillScreen() takes 192 ms.
The start-up. TFT_eSPI's ST7796 sequence unlocks the extended registers with
0xF0 0xC3 and 0xF0 0x96, sets them, and locks them again with 0x3C and 0x69.
The part takes all of it; those registers set analog voltages the simulation does not
have, so they change nothing on the glass.
Rotation. setRotation() writes MADCTL, and the picture turns: 320x480 in
rotations 0 and 2, 480x320 in 1 and 3.
The touch. The XPT2046 is the 2.8 inch board's: readings come from where your finger is on the glass, about 200 at one edge and 3900 at the other.
What it does not model
Reads: nothing answers on MISO, so readPixel() gives black. The IPS versions of
the board, which need TFT_INVERSION_ON: this one is the TN glass, and that define
shows a negative on it. Vertical scrolling, partial mode, the gamma tables and the
BGR bit.
Code
#define ST7796_DRIVER
#define TFT_MOSI 23
#define TFT_SCLK 18
#define TFT_CS 15
#define TFT_DC 2
#define TFT_RST 4
#include <TFT_eSPI.h>
TFT_eSPI tft = TFT_eSPI();
void setup() {
tft.init();
tft.setRotation(1); // 480x320
tft.fillScreen(TFT_NAVY);
tft.setTextColor(TFT_WHITE, TFT_NAVY);
tft.drawString("ST7796S, 4 inch", 10, 10, 4);
}
void loop() {}
TFT_eSPI reads its driver and pins from User_Setup.h; in Mokxi they are #defines
above the #include, with the same names. Touch is TOUCH_CS and getTouch(), or
XPT2046_Touchscreen.h on a chip select of its own.
Common mistakes
ILI9488_DRIVER on this board: the 18-bit pixels come out in the wrong colors. Use
ST7796_DRIVER. TFT_INVERSION_ON on a TN board, which shows a negative.
See it in action
ESP32 ST7796S touch paint at /templates: an ESP32 DevKit draws a palette on the 4 inch board and paints where you press. Press Run and draw on the glass.