
Arduino Quick Start
M5UnitLCD is included in M5GFX and does not need to be installed separately.

draw_function(LovyanGFX* gfx) to draw random circles and squares on Unit LCD. Unit LCD communicates over I2C with a default address of 0x3E and a communication frequency of 400 kHz. When connected to CoreS3's Port A, the corresponding pins are G2 (SDA) and G1 (SCL).#include <M5Unified.h>
#include <M5UnitLCD.h>
M5UnitLCD unit_lcd;
M5Canvas lcd_canvas(&unit_lcd);
void draw_function(LovyanGFX* gfx)
{
int x = rand() % gfx->width();
int y = rand() % gfx->height();
int r = (gfx->width() >> 4) + 2;
uint16_t c = rand();
gfx->fillRect(x - r, y - r, r * 2, r * 2, c);
}
void setup()
{
auto cfg = M5.config();
M5.begin(cfg);
const int sda = M5.getPin(m5::pin_name_t::port_a_sda);
const int scl = M5.getPin(m5::pin_name_t::port_a_scl);
if (!unit_lcd.init(sda, scl, 400000)) {
M5.Display.fillScreen(TFT_RED);
M5.Display.setTextSize(2);
M5.Display.setCursor(12, 32);
M5.Display.println("Unit LCD init failed");
while (true) {
delay(1000);
}
}
unit_lcd.setRotation(3);
lcd_canvas.setColorDepth(16);
lcd_canvas.createSprite(unit_lcd.width(), unit_lcd.height());
lcd_canvas.fillScreen(TFT_BLACK);
int textsize = lcd_canvas.height() / 60;
if (textsize == 0) {
textsize = 1;
}
lcd_canvas.setTextSize(textsize);
}
void loop()
{
int x = rand() % lcd_canvas.width();
int y = rand() % lcd_canvas.height();
int r = (lcd_canvas.width() >> 4) + 2;
uint16_t c = rand();
lcd_canvas.fillCircle(x, y, r, c);
draw_function(&lcd_canvas);
lcd_canvas.pushSprite(0, 0);
unit_lcd.waitDisplay();
delay(50);
}
