Sunday, September 8, 2024

Adafruit ST7789 TFT display with Arduino GFX Library and M5StampS3

 


Attaching a display to a circuit can provide a lot of detailed info and graphical value.  While "a picture is worth a thousand words," an animated display can be entertainment value, textual information can be informative, and add an input device, and the system can be interactive with billions of possibilities.

The tricks in embedded development is choosing the right display, having the right library that supports the display, and figuring out to use it all together with your target embedded system.

Shown above is a minimal footprint ESP32S3 in the form of an M5StampS3 from M5Stack connected to 2.54mm pins, with a rounded corner ST7789 based 280x240 display from Adafruit.  An SPI interface is utilized for communications using the GFX Library for Arduino.  I am using a solderless breadboard to prototype the circuit.  Later I will implement as a more permanent circuit.

Besides power and ground, there are only 4 connections from the ESP32: SCLK, MOSI, DC, and Reset.  In this sample, the TFT select line is permanently selected by connecting to ground.

(The Adafruit display used in this example also includes a MicroSD connector and SPI connections for that as well.  Support for SD is beyond the scope of this article, and would require additional changes to the circuit and code.)

#include <Arduino.h>
#include <Arduino_GFX_Library.h>
Arduino_DataBus *bus = new Arduino_HWSPI(1/*dc*/,
  GFX_NOT_DEFINED/*cs*/, 7/*sclk*/, 5/*mosi*/,
  GFX_NOT_DEFINED/*miso*/, &SPI, true/*is_shared_interface*/);
Arduino_GFX *gfx = new Arduino_ST7789(bus, 3/*rst*/, 1/*r*/,
  true/*ips*/, 240, 320);

void setup() {
  gfx->begin();
  gfx->fillScreen(BLACK);
  gfx->setTextColor(WHITE);
}

void loop() {
  int x = 20 + (int)random(280);
  int w = (int)random(300 - x);
  int y = (int)random(240);
  int h = (int)random(240 - y);  
  int color = (int)random(65536);
  gfx->fillRect(x, y, w, h, color);
  if (random(20)==13)
    delay(100);
}

The wiring corresponds to the code of the databus and gfx initialization parameters, specifically lines 1, 3, 5, 7 of the M5StampS3 connected to DC, RT (reset), SI (serial in), and CK (clock) of the display.  Also the displays V+ line is connected to 5V, and both TC and G (Ground) are connected to ground common to the ESP32 S3.

TFT   StampS3
V+    5V
3V    NC
G     Ground
CK    SPI Clock (7)
SO    NC
SI    SPI MOSI (5)
TC    Ground
RT    (3)
DC    (1)
CC    No Connection
BL    No Connection

It appears there is an overscan issue, beyond the rounded corners.  The gfx object is defining 320x240 display instead of 280x240, and the position and sizes of the rectangles are also interesting here.

Not sure why triangles are sometimes displayed (maybe out of range data?).  That is why there is a 5% chance of a tenth of a second delay to pause for the viewer.

This is just a demo.  You should be able to find much better uses for the display that these random rectangles.

Build details:
  • Arduino IDE 2.3.2
  • esp32 boards 3.0.4
  • GFX Library for Arduino 1.4.7

No comments:

Post a Comment