We have ST7796 driven LCD modules. Display has diameter 4″ and resolution 480×320. This module has also resistive touch screen, but in this post we wont cover touch controls, just using LCD.
Our module can be powered with 5V but data lines are not 5V tolerant. Since we will use ESP8266 there is no problem, but if You’d like to connect it to some older Arduino – then logic level converter is a must.
Ok, so connections ar as follow:
|ESP8266 Arduino pins
|LCD module pin
OK, so we have connected it. Now software part. There are two libraries which can be used. One is Arduino GFX and second is TFT_eSPI. I got both running, but while Arduino GFX was working well I was unable to display image. Displaying text works w/o problems, just image fails despite of different formats tries. Since TFT_eSPI was working OK I have stopped trying GFX display my image.
At the end I’m attaching Platformio project with two targets: one using Arduino GFX and second using TFT_eSPI. You can easily start from there. From
main.cpp depending on platformio environment is included one of two files
ST7796 with TFT_eSPI (etft.h)
This library I would probably use in own project – first seems to be working better with pictures, has interesting examples and it’s a bit faster. On ESP32 it promises working with DMA, could be interesting to see what can be done in terms of speed.
Configuring library depends on environment. For Platformio it’s done through variables set in
platformio.ini. You need set build flags, first is
USER_SETUP_LOADED is set, then library does not include configs, and all needed parameters can be set through next defines. Complete list for connection presented earlier is:
If your preferred environment is Arduino IDE, then you need to update files in
libraries folder. Find TFT_eSPI installation folder and there is a directory
UserSetups. For ST7796 closest one is number 28. You just need to comment out
#define RPI_DISPLAY_TYPE. Then just include file right after
Remember, that after You update TFT_eSPI library (or IDE will do it on your behalf) changes will be lost. As library autor recommends you can create new folder in Arduino sketches folder and place there copy of setup and include it form sketch like this:
So, after You configured library to use proper config You can start working with LCD. In case you can not get display running at all or display is distorted, you can see
setupDebug.h file how to display onto Serial current library configuration. Just include file and all
config. Then You can verify if correct pins are configured in library.
All operations on LCD are through object, You need to create:
TFT_eSPI tft = TFT_eSPI();
For me LCD init was done as follow:
Have you noticed that standard orientation for LCD is size 320×480 (
setRotation you can adjust how all is being displayed. There are 4 rotations, two for vertical and two for horizontal.
For sake of this example I have stored image in flash, as PROGMEM variable. If You want to convert image into variable you may use this file to C array converter online. On attached screenshot You can see what settings was used to get correct image on screen:
Image is being pushed to display by
pushImage it can be done in one step.
ST7796 and Arduino GFX library (libGFX.h)
Second target in attached project used Arduino GFX library. This library is a bit slower, but (especially for less advanced users) easier to configure. Using it on ESP8266 we will relay on hardware SPI controller and thus first we create data bus object. Since it uses predefined SPI pins on ESP You need only provide two parameters. These are pins used for Data/Command (DC) and Chip Select (CS). Next using this bus object you create instance of class supporting ST7796 controller:
Arduino_DataBus *bus = new Arduino_HWSPI(D3 /* DC */, D2 /* CS */);
Arduino_GFX *gfx = new Arduino_ST7796(bus, D4 /* RST */);
As I wrote before I was not able to get images on screen, so example just clears LCD and writes classic Hello world sentence. That’s it.
For both libraries example code is attached in archive for download.