In this tutorial you will learn how to interface a 1.8″ 128×160 TFT display module with a ST7735 display driver with an ESP32 (WEMOS Lolin32 lite).
The instructions and code will work with some minor changes for other ESP32’s and TFT’s as well, as long as the display uses the ST7735 display driver.
Required Parts
You will need an ESP32 and an 1.8 inch TFT display with a resolution of 128×160 pixels and an ST7735 display driver IC. Some cables and a breadboard might come in handy as well.
1.8″ ST7735 TFT Display
ESP32 lite
USB Data Cable
Dupont Wire Set
Breadboard
Makerguides.com is a participant in the Amazon Services LLC Associates Program, an affiliate advertising program designed to provide a means for sites to earn advertising fees by advertising and linking to products on Amazon.com. As an Amazon Associate we earn from qualifying purchases.
1.8″ TFT ST7735 Display Module
The 1.8″ TFT Display Module we are going to use here has as a resolution of 128×160 pixels with 65K RGB colors. The picture below shows the front and back of the display module:
)
The module uses the ST7735S driver chip and is controlled via a SPI-4wire interface. The operating voltage is 3.3V and the display draws around 30mA max.
Note that there are many different versions of this kind of TFT display. Some have an additional SD card or a touch display.
Most importantly, some of them have a logic level converter that allows you to connect the display module to an Arduino that operates on 5V. This one here does not, which means you cannot directly connect it to an Arduino! We use therefore use an ESP32 that operates with CMOS 3.3V logic.
If you want to use an Arduino you need to convert the logic levels. There are various ways to do this (voltage divider, optocoupler, level-shifting IC) but the easiest is to get a level shifting module.
Finally, there are similar displays that use the ST7789 driver IC instead of the ST7735S. The code in this tutorial is for displays with the ST7735S driver IC and will not work for others.
Connecting 1.8″ TFT ST7735 Display with ESP32
Connecting the TFT Display to an ESP32 is easy but the labelling of the pins on the display is a bit confusing. It shows SDA and SCL pins but since it has an SPI interface, these are not the SDA and SCL pins for I2C! Instead, SDA maps to MOSI and SCL to SCLK for SPI.
The following wiring diagram shows you how to connect the 1.8″ TFT ST7735 Display to an WEMOS Lolin32 lite:
And here for your convenience the required connections as a table:
Display | ESP32 |
---|---|
CS / SS | 5 |
RST | 16 |
DC | 17 |
SDA / MOSI | 23 |
SCL / SCLK | 18 |
BKL/BL | 22 |
GND | GND |
VCC | 3.3V |
Make sure to connect VCC of the display to 3.3V! Also you should use the hardware SPI pins for MOSI and SCLK. Depending on the microcontroller the hardware SPI pins will differ. You can find them by selecting your board in the Arduino IDE and then running the code below.
void setup() { Serial.begin(115200); Serial.print("MOSI: "); Serial.println(SDA / MOSI); Serial.print("MISO: "); Serial.println(MISO); Serial.print("SCL / SCLK: "); Serial.println(SCK); Serial.print("CS / SS: "); Serial.println(SS); } void loop() { }
It prints out the pins you need for hardware SPI – specifically MOSI and SCLK. The remaining pins you can select freely and MISO is not used.
Code for TFT ST7735 Display with Adafruit_ST7735 Library
In this section we will use the Adafruit-ST7735 Library to write and draw onto the 1.8″ TFT ST7735 Display. To install it just open the Library Manager, search for “Adafruit-ST7735” and click on the green INSTALL
button:
The installer will probably prompt you to install the dependencies as well. Click on INSTALL ALL
:
With the library installed, we can now test the display. Just compile and upload the following code. It prints the text “Makerguides” with a yellow frame on the display:
#include <Adafruit_GFX.h> #include <Adafruit_ST7735.h> #define TFT_CS 5 #define TFT_RST 16 #define TFT_DC 17 // #define TFT_MOSI 23 // SDA // HW MOSI // #define TFT_SCLK 18 // SCL // HW SCLK // #define TFT_MISO 19 // not used #define TFT_BL 22 // LED back-light Adafruit_ST7735 tft = Adafruit_ST7735(TFT_CS, TFT_DC, TFT_RST); // hardware SPI //Adafruit_ST7735 tft = Adafruit_ST7735(TFT_CS, TFT_DC, TFT_MOSI, TFT_SCLK, TFT_RST); void setup(void) { //tft.initB(); tft.initR(INITR_BLACKTAB); // Init ST7735S chip, black tab //tft.initR(INITR_GREENTAB); // Init ST7735S chip, green tab //tft.setSPISpeed(27000000); tft.setRotation(1); tft.fillScreen(ST77XX_BLACK); tft.setTextColor(ST77XX_WHITE); tft.setTextSize(2); tft.setCursor(15, 50); tft.println("Makerguides"); tft.drawRect(10, 40, 145, 40, ST77XX_YELLOW); } void loop() { }
If you use hardware SPI you will not need the pin definitions for TFT_MOSI
and TFT_SCLK
. However, if you use software SPI you will need them and you will need to pass them to the other constructor for the tft
object:
Adafruit_ST7735 tft = Adafruit_ST7735(TFT_CS, TFT_DC, TFT_MOSI, TFT_SCLK, TFT_RST);
If you compile and upload the code, you should see the following output on your display:
If you don’t see anything, or the colors are wrong, or the text shows artefacts try one of the other initializers for the TFT display in the code:
//tft.initB(); tft.initR(INITR_BLACKTAB); // Init ST7735S chip, black tab //tft.initR(INITR_GREENTAB); // Init ST7735S chip, green tab
The black tab and green tab refer to the colored tabs attached to the screen protection foil these displays usually comes with. For instance, the picture below shows a TFT display with a Green tab:
The color of the tab is important because it often indicates the type of display controller used. The Green Tab is typically for newer or alternative controllers like ILI9341, ST7735, or other specific models. The Black Tab indicates an older or different controller, such as ST7735R or similar variants, which have slightly different specifications.
Finally, you can try to reduce the SPI speed if the text on the display looks corrupted:
tft.setSPISpeed(27000000);
Bye the way, if you want to switch the backlight of the display on or off, you can use the following code:
pinMode(TFT_BL, OUTPUT); digitalWrite(TFT_BL, LOW);
Code for TFT ST7735 Display with TFT_eSPI Library
In the this section, we are going to use the TFT_eSPI library instead of the Adafruit_ST7735 library to control the display. To install it open the Library Manager, search for “TFT_eSPI” and press “INSTALL”. After installation it should look like this:
Next we need to create the correct project folder structure. Open your Arduino IDE and create a project “tft_test
” and save it (Save As …). This will create a folder “tft_test
” with the file “tft_test.ino
” in it. In this folder create another file named “tft_setup.h
“. Your project folder should look like this
If you want to learn more about this setup and other options to configure a TFT display for the TFT_eSPI library have a look at the How to configure TFT_eSPI Library for TFT display tutorial.
After the project folder with the two files is created, copy the following configuration code for the TFT display into the tft_setup.h
file:
// tft_setup.h #define ST7735_DRIVER //#define ST7735_INITB //#define ST7735_GREENTAB //#define ST7735_GREENTAB2 //#define ST7735_GREENTAB3 //#define ST7735_REDTAB #define ST7735_BLACKTAB #define TFT_WIDTH 128 #define TFT_HEIGHT 160 #define TFT_RGB_ORDER TFT_RGB // WEMOLS Lolin32 lite #define TFT_CS 5 #define TFT_RST 16 #define TFT_DC 17 // #define TFT_MOSI 23 // SDA // HW MOSI // #define TFT_SCLK 18 // SCL // HW SCLK // #define TFT_MISO 19 // not used #define TFT_BL 22 // LED back-light #define TFT_BACKLIGHT_ON HIGH #define LOAD_GLCD // Font 1. Original Adafruit 8 pixel font needs ~1820 bytes in FLASH #define LOAD_FONT2 // Font 2. Small 16 pixel high font, needs ~3534 bytes in FLASH, 96 characters #define LOAD_FONT4 // Font 4. Medium 26 pixel high font, needs ~5848 bytes in FLASH, 96 characters #define LOAD_FONT6 // Font 6. Large 48 pixel font, needs ~2666 bytes in FLASH, only characters 1234567890:-.apm #define LOAD_FONT7 // Font 7. 7 segment 48 pixel font, needs ~2438 bytes in FLASH, only characters 1234567890:. #define LOAD_FONT8 // Font 8. Large 75 pixel font needs ~3256 bytes in FLASH, only characters 1234567890:-. #define LOAD_GFXFF // FreeFonts. Include access to the 48 Adafruit_GFX free fonts FF1 to FF48 and custom fonts #define SMOOTH_FONT #define SPI_FREQUENCY 27000000 #define SPI_READ_FREQUENCY 20000000 #define SPI_TOUCH_FREQUENCY 2500000
and the code for the sketch goes into the tft_test.ino
file:
// tft_test.ino #include "tft_setup.h" #include"TFT_eSPI.h" TFT_eSPI tft = TFT_eSPI(); void setup(void) { tft.init(); tft.fillScreen(TFT_BLACK); tft.setRotation(1); tft.setCursor(15, 50, 1); tft.setTextColor(TFT_WHITE, TFT_BLACK); tft.setTextSize(2); tft.println("Makerguides"); tft.drawRect(10, 40, 145, 40, TFT_YELLOW); } void loop() { }
If you compile and upload the code you should see the same output as before:
Similar to the Adafruit_ST7735 library, the tft_setup.h
file for the TFT_eSPI Library offers different initializers depending on the specific TFT display (green tab, black tab, …) you are using. If you are having issues with the display (wrong colors, corrupted text), try one of those first:
//#define ST7735_INITB //#define ST7735_GREENTAB //#define ST7735_GREENTAB2 //#define ST7735_GREENTAB3 //#define ST7735_REDTAB #define ST7735_BLACKTAB
As before, the SPI speed is another important parameter, which is also specified in the tft_setup.h
file:
#define SPI_FREQUENCY 27000000
I tried a common, higher SPI_FREQUENCY
of 40000000 but started to get corrupted text. The picture below shows two screen shots of the TFT display. The first on with a SPI_FREQUENCY
of 27000000 and the second one with a frequency of 40000000.
You can clearly see that the text for the higher SPI frequency becomes corrupted. So be careful with higher speeds and lower the SPI frequency if you are starting to see mangled text.
Finally, the TFT_eSPI Library allows you put the display driver and the display into sleep mode, in addition to switching off the backlight:
pinMode(TFT_BL, OUTPUT); digitalWrite(TFT_BL, LOW); // Switch off Backlight tft.writecommand(ST7735_DISPOFF); // Switch off the display tft.writecommand(ST7735_SLPIN); // Sleep the display driver
The following table shows the power consumption of the TFT display with the different components on or off:
Backlight | Display | Driver | Power |
---|---|---|---|
ON | ON | ON | 15 mA |
OFF | ON | ON | 2 mA |
OFF | OFF | ON | 1.5 mA |
OFF | OFF | OFF | 0.3 mA |
Displaying the text “Makerguides” with the backlight and everything else switched on consumes 15 mA. But if you switch everything off (backlight, display, driver) the power consumptions drops to 0.3 mA! This is great for battery powered project.
Conclusions
In this tutorial you learned how to interface a TFT display module with a ST7735 display driver with an ESP32. We used two different libraries, the Adafruit_ST7735 and the TFT_eSPI library to draw and write text on the TFT display.
If you have any comments, feel free to leave them in the comment section.
Happy Tinkering ; )
Stefan is a professional software developer and researcher. He has worked in robotics, bioinformatics, image/audio processing and education at Siemens, IBM and Google. He specializes in AI and machine learning and has a keen interest in DIY projects involving Arduino and 3D printing.