An ST7735 color TFT on an Arduino
Advanced
Or see every lesson.
- 455parts on the bench
- 43boards running now
- 1.00xreal time, on every board
The ST7735 is the chip behind the cheap 1.8 inch color screens: 128 by 160 pixels, 65,536 colors, on a red board with an SD card slot on the back. It talks SPI, so it needs a clock, a data line and a few control pins, and the Adafruit ST7735 library does the rest.
The circuit above is an Uno with two color screens: the 1.8 inch ST7735 on the Uno’s hardware SPI pins, and a 1.3 inch ST7789 on pins of its own. Each draws a title, then a small square walks across it in a color that changes as it goes. Press Run and watch the ST7735 light first, then the ST7789 about two seconds later.
What you need
- An Arduino Uno
- A 1.8 inch ST7735 TFT, 128x160 (the red SPI board)
- A 1.3 inch ST7789 TFT, 240x240 (optional, for the second half)
- A breadboard and jumper wires
Wiring
| Part and pin | Goes to | Note |
|---|---|---|
| ST7735 VCC | 5 V | The red board has a regulator for the chip |
| ST7735 GND | GND | |
| ST7735 SCK | Pin 13 | The hardware SPI clock |
| ST7735 SDA | Pin 11 | SPI data (MOSI). It is not I2C, whatever the label says |
| ST7735 CS | Pin 10 | Chip select |
| ST7735 A0 | Pin 9 | What other boards call DC: command or data |
| ST7735 RESET | Pin 8 | |
| ST7735 LED | 5 V | The backlight |
| ST7789 SCL and SDA | A1 and A0 | Software SPI on any pins |
| ST7789 DC and RES | Pins 7 and 6 | This board has no CS pin |
How the screen is driven
The ST7735 holds the whole picture in its own memory, 16 bits a pixel. To draw, the Uno sends a command that sets a window (a range of columns, then a range of rows) and then streams the colors for every pixel in it. That is why a filled square is fast and a full-screen picture is slow: the bytes have to go over the wire either way.
The A0 pin (DC on most other boards) tells the chip which bytes are commands and which are pixels: low for a command, high for data. CS low means "this is for you". The library drives both for you, but if either wire is loose, nothing makes sense to the chip and the screen stays dark.
Colors are 16-bit "565" numbers: 5 bits of red, 6 of green and 5 of blue. ST77XX_GREEN and the other names are ready-made ones, and color565(r, g, b) turns three 0 to 255 values into one, which is how the sketch makes its changing square.
The sketch
Adafruit_ST7735 small(10, 9, 8) takes CS, DC and RESET and uses the Uno’s hardware SPI for the clock and data, pins 13 and 11. initR(INITR_BLACKTAB) wakes the chip and sets it up for the panel, then the usual Adafruit GFX calls draw: fillScreen, setCursor, print, fillRect.
The ST7789 uses the five-number constructor, Adafruit_ST7789 square(-1, 7, A0, A1, 6): no CS (-1), DC on 7, data on A0, clock on A1 and RES on 6. Given its own clock and data pins, the library sends every bit by hand, which works on any pins and is much slower than hardware SPI. That, and the ST7789’s longer start-up, is why it lights about two seconds after the ST7735.
#include <SPI.h>
#include <Adafruit_GFX.h>
#include <Adafruit_ST7735.h>
#include <Adafruit_ST7789.h>
Adafruit_ST7735 small(10, 9, 8);
Adafruit_ST7789 square(-1, 7, A0, A1, 6); // no CS; DC, SDA, SCL, RES
void setup() {
small.initR(INITR_BLACKTAB);
small.fillScreen(ST77XX_BLACK);
small.setTextColor(ST77XX_GREEN);
small.setCursor(4, 4);
small.print("ST7735 128X160");
square.init(240, 240, SPI_MODE3);
square.setRotation(2);
square.fillRect(0, 0, 240, 40, ST77XX_BLUE);
square.setTextColor(ST77XX_WHITE);
square.setTextSize(2);
square.setCursor(8, 12);
square.print("ST7789 IPS");
}
void loop() {
// A block that walks around each screen, in a color that changes as it goes.
static uint8_t step = 0;
uint16_t color = Adafruit_ST7789::color565((uint8_t)(step * 16), 255 - step * 16, 128);
small.fillRect((int16_t)(8 + (step % 8) * 14), 40, 12, 12, color);
square.fillRect((int16_t)(10 + (step % 8) * 28), 120, 24, 24, color);
step = (uint8_t)((step + 1) % 16);
delay(100);
}Black tab, green tab, red tab
The chip’s memory is 132 by 162, a little bigger than the 128 by 160 glass, and makers mount the glass at slightly different spots in it. They mark which with the color of the tab on the screen’s protective film, and the library has an initR setting for each: INITR_BLACKTAB, INITR_GREENTAB and INITR_REDTAB, plus INITR_144GREENTAB for the 1.44 inch square ones.
Pick the wrong one and the picture is shifted: a stripe of noise or black along two edges, or red and blue swapped. If that is what you see, try the other tabs before you suspect the wiring. Peel the film off and lose the tab? Try them in turn; one will draw clean edges.
ST7735 or ST7789?
The ST7789 speaks almost the same commands, so the code barely changes, but it drives bigger, sharper IPS glass: 240 by 240 on the 1.3 inch square boards, 240 by 320 on others. IPS panels look the same from any angle, while the ST7735’s cheaper glass fades when tilted.
Many 1.3 inch ST7789 boards have no CS pin, so the chip listens to every clock on its wires. Put one on the same SPI bus as another screen and it takes that screen’s bytes as its own, which is why the one here has pins of its own. They also want SPI mode 3, the third number in init(240, 240, SPI_MODE3).
For a bigger screen still, the 2.4 and 2.8 inch boards use an ILI9341 at 240 by 320, with touch on many of them. There is a page on it below.
Common mistakes
A dark screen with the backlight on. Usually A0 (DC) or CS is not connected, or connected to the wrong pin. Pull either one in the circuit above and the ST7735 never shows a thing.
Treating SDA and SCK as I2C because of the names. They are SPI: SDA goes to pin 11 (MOSI) and SCK to pin 13 on an Uno.
The wrong initR tab, giving shifted pictures or noisy edges. Try the others.
Driving the signal pins at 5 V for good. The red board’s regulator feeds the chip 3.3 V, but the signal pins go to the chip directly, and the ST7735 is a 3.3 V part. Many people run them from a 5 V Uno anyway; a level shifter, or a 3.3 V board, is the safe way.
Leaving RESET floating. In the circuit above the sketch still starts with RESET unwired, because initR sends a software reset first, but a floating pin on a real board can pick up noise and reset the chip at random. Wire it to a pin or to VCC.
Redrawing the whole screen every loop. A full 128 by 160 fill is 40,960 bytes over the wire; draw only what changed.
Questions
How do I connect an ST7735 to an Arduino Uno?
VCC to 5 V, GND to GND, SCK to 13, SDA to 11, CS to 10, A0 (DC) to 9, RESET to 8 and LED to 5 V. Then Adafruit_ST7735 tft(10, 9, 8) and tft.initR(INITR_BLACKTAB).
Which library do I need for the ST7735?
Adafruit ST7735 and ST7789 Library, with Adafruit GFX for the drawing calls. The same library drives both chips.
Why is my ST7735 screen white or blank?
A white screen is the backlight with nothing drawn: check CS, A0 (DC) and RESET, and that the sketch calls initR. Shifted pictures or noisy edges mean the wrong tab setting.
Can I use the SD card slot on the back?
Yes, on a real board: it shares SCK, MOSI and MISO, with its own CS pin. The simulated board leaves the SD card out.
Keep going
Build this for real
Open the editor, change a value and watch the number move with it. Nothing to install, and no account needed.