This is an alternative take on the previous article Adafruit ST7789 TFT display with Arduino GFX Library and M5StampS3. This time with a generic display, and a different MCU (ESP32-C3). This one has a single RISC-V core (instead of dual core Xtensa).
So excuse the repeated text, the content has been changed only slightly for the different hardware...
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 ESP32-C3 development board (I purchased one from AliExpress for around two US dollars, very cheap!), with a generic IPS 240x240 TFT display (ST7789 based) I had purchased years ago on eBay. 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.
#include <Arduino.h>
#include <Arduino_GFX_Library.h>
Arduino_DataBus *bus = new Arduino_HWSPI(6/*dc*/, 7/*cs*/,
2/*sclk*/, 3/*mosi*/, 10/*miso*/, &SPI,
true/*is_shared_interface*/);
Arduino_GFX *gfx = new Arduino_ST7789(bus, 11/*rst*/, 1/*r*/,
true/*ips*/, 240, 240);
void setup() {
gfx->begin();
gfx->fillScreen(BLACK);
gfx->setTextColor(WHITE);
}
void loop() {
int x = (int)random(240);
int w = (int)random(240 - 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 GPIO02 (SPI CLK), GPIO03 (SPI MOSI), GPOI06, GPIO11 of the ESP32-C3 connected to SCL, SDA, DC, and RES (Reset) of the display respectively. Also both the VCC and BLK lines of the display are connected to 3V3, and GND is connected commonly between the display and ESP32-C3 board. MISO and CS lines are defined for use in the code, but wiring them was unnecessary and not possible.
TFT ESP32-C3
GND Ground
VCC 3V3
SCL SPI Clock (2)
SDA SPI MOSI (3)
RES (11)
DC (6)
BLK 3V3
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
Important! The board I have will only run its flash chip in DIO mode, so be sure to check all the board options in Tools menu of Arduino IDE before flashing, or if you run into trouble. Also, updating board libraries may reset your options to default so if flashing after a time, check these settings again, and again.
Some models of the ESP32-C3 development board have a serial chip, and some utilize the C3's built in USB support. Be sure to set USB CDC on Boot settings accordingly, and use the BOOT/RST buttons to put into bootloader mode for flashing if necessary.
I found it necessary to unplug and replug the ESP32-C3 board to get the graphic demo to work. Your mileage may vary.