Goal
SPI (Serial Peripheral Interface) is the fastest and most widely used communication bus for driving a display. This tutorial covers:
- Wiring an SPI TFT display with an ST7789 controller to an ESP32.
- Installing the Adafruit ST7735 and ST7789 library, with the Arduino IDE or PlatformIO.
- Drawing text, shapes and colours on the display.
The workshop setup (XIAO ESP32S3, display on the project PCB) has its own tutorial, with the fixed wiring of the PCB: SPI display and game loop. This one suits any ESP32 board wired on a breadboard.
The SPI bus at a glance
Unlike I2C (2 wires, several slaves by addressing), SPI uses 4 wires and selects its slave with a dedicated pin:
| Wire |
Role |
| MOSI |
Master Out Slave In, data sent by the ESP32 to the display |
| MISO |
Master In Slave Out, data sent back by the peripheral (unused for a write-only display) |
| SCK (or SCLK) |
Clock, generated by the ESP32 |
| CS (Chip Select) |
Selects the active slave, one dedicated wire per peripheral on the bus |
A display usually adds two more pins, outside the standard SPI bus:
| Wire |
Role |
| DC (Data/Command) |
Says whether the byte sent is a command or pixel data |
| RST |
Hardware reset of the display |
This tutorial stays practical. To compare SPI, I2C and UART in detail (speed, number of wires, use cases), see the concept Communication buses.
What you need
- An ESP32-S3 board (Arduino framework), for example the ESP32-S3-DevKitC-1
- An SPI TFT display with an ST7789 controller (the controller is usually given on the back of the display or in its product reference), and its resolution (240 × 320, 240 × 240 or 135 × 240 depending on the model)
- A breadboard and jumper wires
Step 1: Wire the display
Wiring
Connect the pins of the display to the ESP32. On the ESP32-S3, the GPIO matrix routes the SPI bus to almost any pin: the numbers below are an example, which you will find again in the code.
TFT display ESP32-S3
VCC ──────────── 3.3 V
GND ──────────── GND
CS ──────────── GPIO10
DC ──────────── GPIO11
RST ──────────── GPIO12
SCK ──────────── GPIO13
MOSI ──────────── GPIO14
BLK ──────────── 3.3 V (or a GPIO to control the backlight)
On the display, SCK and MOSI are often silkscreened SCL and SDA, as for an I2C bus: they really are the SPI clock and data.
Check the supply voltage your module accepts: most SPI TFT displays run at 3.3 V, the native voltage of the ESP32’s GPIOs. Never apply 5 V to an ESP32 data pin.
On a classic ESP32 (esp32dev board), GPIO6 to 11 are connected to the Flash memory and must never be wired. Use, for example, the pins of the VSPI bus: SCK on GPIO18, MOSI on GPIO23, CS on GPIO5, DC on GPIO16 and RST on GPIO17, and carry these numbers over into the code.
Step 2: Install the library
The Adafruit ST7735 and ST7789 library handles the controller’s protocol. It relies on Adafruit GFX, which provides the drawing functions (text, rectangles, circles, lines) common to all Adafruit displays. There is no configuration file: the pins are passed directly in the code.
With the Arduino IDE
Install the library
In the Library Manager, search for Adafruit ST7735 and ST7789 and install it. Accept the installation of the suggested dependencies (including Adafruit GFX Library).
Add the library to platformio.ini; PlatformIO downloads its dependencies by itself:
[env:esp32-s3-devkitc-1]
platform = espressif32
board = esp32-s3-devkitc-1
framework = arduino
monitor_speed = 115200
lib_deps =
adafruit/Adafruit ST7735 and ST7789 Library@^1.11.0
Before going on, check that the project compiles with an empty main.cpp (only #include <Arduino.h>, setup() and loop()).
Step 3: First primitives
The tft object represents the display. First start the SPI bus on the wired pins, then initialise the display with its resolution. The coordinate system has its origin (0, 0) at the top left: x increases to the right, y downwards.
#include <Arduino.h>
#include <SPI.h>
#include <Adafruit_GFX.h>
#include <Adafruit_ST7789.h>
constexpr int PIN_TFT_CS = 10;
constexpr int PIN_TFT_DC = 11;
constexpr int PIN_TFT_RST = 12;
constexpr int PIN_TFT_SCK = 13;
constexpr int PIN_TFT_MOSI = 14;
Adafruit_ST7789 tft(&SPI, PIN_TFT_CS, PIN_TFT_DC, PIN_TFT_RST);
void setup() {
SPI.begin(PIN_TFT_SCK, -1, PIN_TFT_MOSI, PIN_TFT_CS); // no MISO
tft.init(240, 320); // resolution of YOUR display (width, height)
tft.setSPISpeed(40000000); // 40 MHz
tft.setRotation(1); // landscape (0 to 3 depending on the orientation wanted)
tft.fillScreen(ST77XX_BLACK);
tft.setTextColor(ST77XX_WHITE);
tft.setTextSize(2); // 6 x 8 pixel font, multiplied by 2
tft.setCursor(10, 10);
tft.print("Hello ESP32 !");
tft.drawRect(20, 50, 60, 30, ST77XX_WHITE); // rectangle outline
tft.fillCircle(160, 65, 15, ST77XX_RED); // filled circle
tft.drawLine(0, 100, tft.width(), 100, ST77XX_GREEN);
}
void loop() {}
A badly configured display stays black, with no compilation error. Check in this order: is the backlight (BLK) powered? Do the pins passed to SPI.begin() and to the constructor match the actual wiring? Is the resolution passed to tft.init() that of your display? If your module has no CS pin (or if CS is tied to GND), pass -1 as CS and initialise with tft.init(240, 240, SPI_MODE3): without CS, the ST7789 needs a clock that idles high. Full diagnosis: Troubleshooting an SPI TFT display that shows nothing.
The ILI9341 controller has its own library, Adafruit ILI9341, with the same drawing functions (Adafruit GFX). Only the declaration and the initialisation change: Adafruit_ILI9341 tft(&SPI, PIN_TFT_DC, PIN_TFT_CS, PIN_TFT_RST); (careful, DC before CS) then tft.begin(); instead of tft.init(...). Colours are then written ILI9341_BLACK, ILI9341_WHITE…
You should see the text « Hello ESP32 ! », a rectangle, a red circle and a green line on the display. If the image is shifted or partly scrambled, the resolution passed to tft.init() does not match your display.
The display encodes each colour on 16 bits (5 bits red, 6 bits green, 5 bits blue) rather than the usual 24 bits (RGB888). Constants cover the common colours (ST77XX_BLACK, ST77XX_WHITE, ST77XX_RED, ST77XX_GREEN, ST77XX_BLUE, ST77XX_YELLOW, ST77XX_ORANGE…), and a custom colour is built with:
uint16_t violet = tft.color565(140, 60, 200); // red, green, blue (0-255 each)
tft.fillCircle(80, 65, 15, violet);
Erase before redrawing
Unlike a buffered OLED display (such as the SSD1306), this library draws directly to the display on every call: there is no display() to call to commit a frame. In return, you must explicitly erase a shape before redrawing it elsewhere, or it will leave a trail:
int x = 20;
void loop() {
tft.fillCircle(x, 65, 10, ST77XX_BLACK); // erases the old position
x = (x + 5) % tft.width();
tft.fillCircle(x, 65, 10, ST77XX_WHITE); // draws the new one
delay(30);
}
Erase and redraw only what moves: fillScreen() on every frame would send every pixel of the display over the SPI bus and cause heavy flicker.
Expected result
The display shows text, fixed shapes, and a white circle that moves horizontally in a loop without leaving a trail. This « erase the old position, then draw the new one » pattern is the basis of all animation on a TFT display.
Going further