English
English
简体中文
日本語
pdf-icon

Arduino Quick Start

2. Devices & Examples

5. Extensions

6. Applications

PaperColor Wakeup Sleep & Wake

PaperColor sleep and wake-related APIs and example code.

Example Code

Build Requirements

  • M5Stack Board Manager version >= 3.2.7
  • Board option = M5PaperColor
  • M5Unified library version >= 0.2.15
  • M5GFX library version >= 0.2.21
  • M5PM1 library version >= 1.0.1

RTC Wakeup for ESP32-S3

Uses the RTC interrupt signal to wake up the ESP32-S3. During this process, the M5PM1 power management chip remains in standby mode.

cpp
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44
#include <M5Unified.h>

M5Canvas canvas(&M5.Display);

static void drawEpdSleepScreen()
{
    const int w = M5.Display.width();
    const int h = M5.Display.height();

    canvas.fillSprite(WHITE);
    canvas.setTextDatum(middle_center);

    canvas.setFont(&fonts::FreeSansBold18pt7b);
    canvas.setTextColor(RED);
    canvas.drawString("Press Top BtnA", w / 2, h / 2 - 20);

    canvas.setTextColor(GREEN);
    canvas.drawString("Sleep 5s, then wakeup", w / 2, h / 2 + 20);

    canvas.pushSprite(0, 0);
}

void setup(void)
{
    auto cfg          = M5.config();
    cfg.clear_display = false;
    M5.begin(cfg);

    M5.Display.setEpdMode(epd_mode_t::epd_quality);

    canvas.createSprite(M5.Display.width(), M5.Display.height());
    drawEpdSleepScreen();
}

void loop(void)
{
    M5.update();

    if (M5.BtnA.wasPressed()) {
        M5.Power.timerSleep(5);
    }

    delay(10);
}

The sleep and wake functionality in this example uses Power_Class from the M5Unified library. For additional APIs, refer to:

M5PM1 Timer Wakeup

After the device powers on, press Button A to configure a 10-second timer to trigger a power-on event, then the device shuts down and waits to be woken up.

cpp
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82
#include <M5Unified.h>
#include <M5PM1.h>
#include <Wire.h>

static constexpr uint32_t POWERON_SECONDS = 10;
static constexpr uint32_t POWEROFF_SECONDS = 10;

M5PM1 pm1;
M5Canvas canvas(&M5.Display);

static bool pm1_ready = false;

static void drawScreen(const char* status)
{
    const int w = M5.Display.width();
    const int h = M5.Display.height();

    canvas.fillSprite(WHITE);
    canvas.setTextDatum(top_left);
    canvas.setTextColor(BLACK);

    canvas.setFont(&fonts::FreeSansBold12pt7b);
    canvas.drawString("M5PM1 TIMER", 16, 24);

    canvas.setFont(&fonts::FreeSansBold9pt7b);
    canvas.setTextColor(BLUE);
    canvas.drawString("BtnA: power on after 10s", 16, 86);

    canvas.setTextColor(pm1_ready ? GREEN : RED);
    canvas.drawString(status, 16, h - 40);

    canvas.pushSprite(0, 0);
}

void setup(void)
{
    auto cfg = M5.config();
    cfg.clear_display = false;
    M5.begin(cfg);

    Serial.begin(115200);
    M5.Display.setEpdMode(epd_mode_t::epd_quality);

    canvas.createSprite(M5.Display.width(), M5.Display.height());

    m5pm1_err_t err = pm1.begin(&M5.In_I2C, M5PM1_DEFAULT_ADDR, M5PM1_I2C_FREQ_100K);
    pm1_ready       = (err == M5PM1_OK);

    if (pm1_ready) {
        pm1.setLdoEnable(true);
    }

    if (pm1_ready) {
        Serial.println("PM1 init ok");
        drawScreen("PM1 ready");
    } else {
        Serial.printf("PM1 init failed: %d\n", (int)err);
        drawScreen("PM1 init failed");
    }
}

void loop(void)
{
    M5.update();

    if (!pm1_ready) {
        delay(20);
        return;
    }

    if (M5.BtnA.wasPressed()) {
        Serial.printf("BtnA: timer power on in %lu s\n", POWERON_SECONDS);
        m5pm1_err_t err = pm1.timerSet(POWERON_SECONDS, M5PM1_TIM_ACTION_POWERON);
        if (err != M5PM1_OK) {
            Serial.printf("timerSet POWERON failed: %d\n", (int)err);
            drawScreen("BtnA failed");
        }
        delay(1000);
        pm1.shutdown();
    }
    delay(20);
}

For more information on PaperColor - M5PM1, refer to:

On This Page