Skip to content

Instantly share code, notes, and snippets.

@palaniraja
Created July 27, 2025 19:45
Show Gist options
  • Save palaniraja/639883660a1a2af12e6b30752c3eeab4 to your computer and use it in GitHub Desktop.
Save palaniraja/639883660a1a2af12e6b30752c3eeab4 to your computer and use it in GitHub Desktop.
240x240 TFT panel with M5StickC Plus2 & GC9A01
// #include <M5GFX.h>
#include <LovyanGFX.hpp>
#include <Arduino.h>
#define CHK_PIN 25
#define SCL_SCLK 26
#define SDA_MOSI 25
#define CS_CS 32
#define DC_DC 33
// M5StickC. Plus 2
// Default display - ST7789V2 / 135 x 240
// G15 - TFT_MOSI
// G13 - TFT_CLK
// G14 - TFT_DC
// G5 - TFT_CS
// G12 - TFT_RST
// G27 - TFT_BL
// HY2.0 - 4P
// GND - 5V - G32 - G33
// TOP
// GND - 5Vout - G26 - G36(in)/G25(out) - G0 - BAT - 3Vout - 5Vin
int counter = 0;
// M5GFX display;
class LGFX : public lgfx::LGFX_Device
{
lgfx::Panel_GC9A01 _panel_instance;
lgfx::Bus_SPI _bus_instance;
public:
LGFX()
{
{ // SPI bus config
auto cfg = _bus_instance.config();
cfg.spi_host = VSPI_HOST;
cfg.spi_mode = 0;
cfg.freq_write = 40000000;
cfg.freq_read = 16000000;
cfg.spi_3wire = false;
cfg.use_lock = true;
cfg.dma_channel = 1;
cfg.pin_sclk = SCL_SCLK;
cfg.pin_mosi = SDA_MOSI;
cfg.pin_miso = -1;
cfg.pin_dc = DC_DC;
_bus_instance.config(cfg);
_panel_instance.setBus(&_bus_instance);
}
{ // Panel config
auto cfg = _panel_instance.config();
cfg.pin_cs = CS_CS;
cfg.pin_rst = -1;
cfg.pin_busy = -1;
cfg.panel_width = 240;
cfg.panel_height = 240;
cfg.offset_x = 0;
cfg.offset_y = 0;
cfg.offset_rotation = 0;
cfg.dummy_read_pixel = 8;
cfg.dummy_read_bits = 1;
cfg.readable = false;
cfg.invert = true;
cfg.rgb_order = false;
cfg.dlen_16bit = false;
cfg.bus_shared = true;
_panel_instance.config(cfg);
}
setPanel(&_panel_instance);
}
};
LGFX display;
int cx, cy;
void setup(void) {
Serial.begin(115200);
// display.init();
// display.setFont(&fonts::FreeSans18pt7b);
// display.setTextDatum(textdatum_t::middle_center);
// display.drawString("Hello.", display.width() / 2, display.height() / 2);
// display.startWrite();
display.init();
display.setRotation(0);
display.fillScreen(TFT_BLACK);
// border, -1 radius needed with my display
cx = display.width() / 2;
cy = display.height() / 2;
int radius = cx-1;
// Draw yellow circle border (thin line)
display.drawCircle(cx, cy, radius, TFT_YELLOW);
display.fillCircle(cx, cy, radius - 3, TFT_BLACK);
// display.drawString("Hello GC9A01!", 10, 10);
display.setFont(&fonts::Font4);
display.setTextSize(2);
// Centered string
display.setTextColor(TFT_YELLOW);
display.setTextDatum(middle_center);
display.drawString("Hello!", cx, cy);
// pinMode(CHK_PIN, OUTPUT); // this would mess up lgfx
}
void loop(void) {
display.startWrite();
display.clear();
display.setTextDatum(middle_center);
if (counter%2) {
display.setTextColor(TFT_RED);
display.drawString("High", cx, cy);
// digitalWrite(CHK_PIN, HIGH);
} else {
display.setTextColor(TFT_GREEN);
display.drawString("Low", cx, cy);
// digitalWrite(CHK_PIN, LOW);
}
Serial.printf("counter: %d\n", counter);
counter++;
display.endWrite();
vTaskDelay(1000 / portTICK_PERIOD_MS);
}
[env:default]
board = m5stick-c
platform = espressif32
; force_verbose = true
monitor_speed = 115200
monitor_filters =
time
framework = arduino
lib_deps =
m5stack/M5GFX@^0.2.9
lovyan03/LovyanGFX@^1.1.12
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment