pdf-icon

Arduino Guide

Unit OLED Arduino Tutorial

1. Preparation

2. Example Program

Example Explanation
The Unit OLED is a 1.3-inch OLED expansion screen unit. In this example, the CoreS3 main controller will use the PORT.A expansion interface to control and implement scrolling text display. Other M5Stack controllers can adapt by modifying the SDA, SCL pin configuration when creating the M5UnitOLED display(2, 1, 400000); instance.

#include <M5Unified.h>
#include <M5GFX.h>
#include <M5UnitOLED.h>

M5UnitOLED display(2, 1, 400000);  // SDA, SCL, FREQ

M5Canvas canvas(&display);

static constexpr char text[]    = "Hello world ! Hello Unit OLED ! ";
static constexpr size_t textlen = sizeof(text) / sizeof(text[0]);
int textpos                     = 0;
int scrollstep                  = 2;

void setup(void)
{
    M5.begin();
    M5.Display.setFont(&fonts::lgfxJapanMinchoP_32);
    M5.Display.setTextDatum(middle_center);
    M5.Display.drawString("Unit OLED Test", M5.Display.width() / 2, M5.Display.height() / 2);

    display.init();
    display.setRotation(1);
    canvas.setColorDepth(1);  // mono color
    canvas.setFont(&fonts::lgfxJapanMinchoP_28);
    canvas.setTextWrap(false);
    canvas.setTextSize(2);
    canvas.createSprite(display.width() + 64, 72);
}

void loop(void)
{
    int32_t cursor_x = canvas.getCursorX() - scrollstep;
    if (cursor_x <= 0) {
        textpos  = 0;
        cursor_x = display.width();
    }

    canvas.setCursor(cursor_x, 0);
    canvas.scroll(-scrollstep, 0);
    while (textpos < textlen && cursor_x <= display.width()) {
        canvas.print(text[textpos++]);
        cursor_x = canvas.getCursorX();
    }
    display.waitDisplay();
    canvas.pushSprite(&display, 0, (display.height() - canvas.height()) >> 1);
}

3. Compile and Upload

    1. Download Mode: Different devices may require entering download mode before programming. The steps for this may vary depending on the main control device. For more details, refer to the Arduino IDE Getting Started Guide , which includes specific download tutorial lists for each device.
  • For CoreS3, press and hold the reset button (for about 2 seconds) until the internal green LED lights up, and then release it. This indicates the device is in download mode and ready for programming.

    1. Select the device port and click the compile and upload button in the top left corner of Arduino IDE, then wait for the program to compile and upload to the device.

4. Hello World

Use the CoreS3's PORT.A to control the Unit OLED and display scrolling text.

On This Page