English
English
简体中文
日本語

Arduino Quick Start

2. Devices & Examples

5. Extensions

6. Applications

CoreP4X M5PM1 & M5IOE1 Power Management

1. Multi-Level Power Switching Design

CoreP4X integrates the Stamp Timer Power2 (M5PM1) and M5IOE1, and implements multi-level power switching with hardware circuits. Each level corresponds to a different controller operating state and power state for peripherals and interfaces. You can switch the related power enables according to runtime requirements, turning off unused peripherals to reduce overall power consumption.

M5PM1 / M5IOE1 Driver Libraries

The M5PM1 / M5IOE1 driver libraries make it easy to configure their pin functions for low-power wakeup and peripheral power control.

Note
CoreP4X's multi-level power switching design is not a fully series structure: the power inputs of L1 and L2/L3A both come from the L0 source and can be controlled independently; but L3B is switched by M5IOE1 IO12 through the MT9700 from P4_3V3_L2, so turning off L2 also cuts L3B. L1 is output by the LDO of M5PM1; after M5PM1 powers on, L1 and L2/L3A are enabled automatically, and M5Unified further enables L3B through M5IOE1 during initialization.

L0

At this power level, the battery keeps M5PM1, the RTC, and the power button powered. As long as the battery is not depleted, 3V3_L0 remains on, and M5PM1 supports power on/off via the power button and RTC interrupt wakeup.

L1

At this power level, power for the IMU and the wakeup logic is enabled. This level uses the LDO3V3_EN_PP of M5PM1 to output 3V3_L1, which can be switched with the following APIs.

pm1.setLdoEnable(true);  // L1 ON
pm1.setLdoEnable(false); // L1 OFF

The INT1 of the LSM6DS3TR-C is connected to M5PM1 G0. To wake M5PM1 through the IMU, keep L1 powered and then put M5PM1 into sleep:

pm1.setLdoEnable(true);
pm1.ldoSetPowerHold(true);
pm1.setLedEnLevel(true);
pm1.shutdown();

L2 / L3A

At this power level, the ESP32-P4, M5IOE1, the touch panel (TP), and the Grove (PORT.A) interface are powered. This level uses the DCDC3V3_EN_PP of M5PM1 to output 3V3_L2, which can be switched with the following APIs.

pm1.setDcdcEnable(true);  // L2 / L3A ON
pm1.setDcdcEnable(false); // L2 / L3A OFF

When the ESP32-P4 is asleep, the power state is L2; when the ESP32-P4 is running or has exited sleep, the power state is L3A. The ESP32-P4 can put M5PM1 into sleep to cut its own power (L2 -> L1/L0).

L3B

Level L3B covers the operating state of peripherals such as audio, IR, Ethernet, microSD, USB, and LCD. The shared 3V3_L3B (P4_3V3_L3B) is controlled by M5IOE1 IO12 and supplies the IR, Ethernet PHY, microSD, and M5-Bus 3.3V; the power for audio, USB-A, and USB-C OTG is independently controlled by IO1, IO2, and IO4, the speaker amplifier is enabled by IO3, and the LCD is enabled by IO10 with power from 3V3_L2. M5Unified enables the shared 3V3_L3B power automatically during initialization.

Peripherals Controlled by M5IOE1

M5IOE1 IO1 IO2 IO3 IO4 IO5 IO6 IO8 IO9/PWM1 IO10 IO11 IO12 IO13
Audio codec PYB_AU_PWR_EN
USB-A Host 5V PYB_USBA_OUT_EN
Speaker amplifier PYB_SPK_PWR_EN
USB-C OTG 5V PYB_P4USBC_OUT_EN
PORT.A 5V PYB_GROVE_PWR_EN
Charge status PY_CHG_STAT
Touch reset PYB_TP_RST
LCD PYB_LCD_BL_PWM PYB_LCD_PWR_EN PYB_LCD_RST
3V3_L3B PYB_L3B_EN
microSD detect PYB_SD_DEC
  • IO1 (PYB_AU_PWR_EN): enables the audio 3.3V (AU_VBUS_L3) supply for the VDD of the ES8311, ES7210, microphones, and AW8737A.
  • IO2 (PYB_USBA_OUT_EN): controls the 5V output of the USB-A Host port.
  • IO3 (PYB_SPK_PWR_EN): enables or disables the AW8737A speaker amplifier (SHDN).
  • IO4 (PYB_P4USBC_OUT_EN): controls the 5V output of the USB-C OTG port.
  • IO5 (PYB_GROVE_PWR_EN): controls the 5V output of PORT.A; the port power of PORT.A belongs to the L2/L3A level.
  • IO6 (PY_CHG_STAT): reads the charging status; low indicates charging.
  • IO8 (PYB_TP_RST): controls the touch controller reset.
  • IO9/PWM1 (PYB_LCD_BL_PWM): uses PWM1 to control the LCD backlight brightness.
  • IO10 (PYB_LCD_PWR_EN): controls the LCD power.
  • IO11 (PYB_LCD_RST): controls the LCD reset.
  • IO12 (PYB_L3B_EN): controls the shared peripheral power of IR, Ethernet PHY, microSD, and others.
  • IO13 (PYB_SD_DEC): reads the microSD card detection status; low indicates the card is inserted.

The peripheral power control can be implemented with the M5IOE1 API below:

ioe1.pinMode(M5IOE1_PIN_12, OUTPUT);
ioe1.digitalWrite(M5IOE1_PIN_12, HIGH); // 3V3_L3B ON
ioe1.digitalWrite(M5IOE1_PIN_12, LOW);  // 3V3_L3B OFF

M5PM1-Controlled Functions

M5PM1 PYG0 PYG1 PYG2 PYG4
IMU wakeup PYG0_IMU_INT
Charge current level PYG1_CHG_PROG
Interrupt output PYG2_IRQ
RTC wakeup PYG4_RTC_INT
  • PYG0 (PYG0_IMU_INT): receives the INT1 interrupt of the LSM6DS3TR-C for motion wakeup.
  • PYG1 (PYG1_CHG_PROG): controls the battery charge current level.
  • PYG2 (PYG2_IRQ): M5PM1 interrupt output, connected to ESP32-P4 GPIO0.
  • PYG4 (PYG4_RTC_INT): receives the RX8130CE interrupt for RTC wakeup.

2. Charge Current Level Control

The charge current of CoreP4X is controlled by PYG1 (M5PM1_GPIO_NUM_1) of M5PM1 and can be set to 180mA (default) or 650mA.

PYG1 Pin State Configuration Charge Current
Low (LOW) Output + push-pull, output low 650mA
High impedance (Hi-Z / floating) Input + no pull (default) 180mA

Set the 650mA Charge Level

Set M5PM1_GPIO_NUM_1 to output + push-pull mode and output low:

pm1.gpioSetFunc(M5PM1_GPIO_NUM_1, M5PM1_GPIO_FUNC_GPIO);
pm1.gpioSetMode(M5PM1_GPIO_NUM_1, M5PM1_GPIO_MODE_OUTPUT);
pm1.gpioSetPull(M5PM1_GPIO_NUM_1, M5PM1_GPIO_PULL_NONE);
pm1.gpioSetDrive(M5PM1_GPIO_NUM_1, M5PM1_GPIO_DRIVE_PUSHPULL);
pm1.gpioSetOutput(M5PM1_GPIO_NUM_1, 0);

You can also use gpioSet() to complete the configuration at once; the third parameter is the output level and must be 0:

pm1.gpioSet(M5PM1_GPIO_NUM_1, M5PM1_GPIO_MODE_OUTPUT, 0,
            M5PM1_GPIO_PULL_NONE, M5PM1_GPIO_DRIVE_PUSHPULL);

Set the 180mA Charge Level

Set M5PM1_GPIO_NUM_1 to input + no pull mode:

pm1.gpioSetFunc(M5PM1_GPIO_NUM_1, M5PM1_GPIO_FUNC_GPIO);
pm1.gpioSetMode(M5PM1_GPIO_NUM_1, M5PM1_GPIO_MODE_INPUT);
pm1.gpioSetPull(M5PM1_GPIO_NUM_1, M5PM1_GPIO_PULL_NONE);
Note
The 650mA level is held by the low level of the PYG1 pin. After a short press of the power button (M5PM1 reset), a double-click power-off, entering download mode, or a power loss, PYG1 returns to high impedance and the charge current returns to the default 180mA.

3. M5PM1 Sleep

Manual Sleep

M5PM1 can be put into sleep manually by the program to reduce overall power consumption. By default, a direct shutdown falls back to L0, where only M5PM1 and the RTC powered by 3V3_L0 keep working.

pm1.shutdown();

In scenarios such as IMU wakeup, RTC wakeup, or ESP32-P4 sleep, keep the required power domains enabled before M5PM1 enters sleep. For example, when waking up through the IMU, L1 must remain powered for the LSM6DS3TR-C:

pm1.setLdoEnable(true);
pm1.ldoSetPowerHold(true);
pm1.setLedEnLevel(true);
pm1.shutdown();
Note
shutdown() cuts the whole device power only when running on battery. When testing sleep with USB power, the system may keep running; disconnect the USB cable for the test.

I2C Idle Sleep

Both M5PM1 and M5IOE1 support automatic I2C idle sleep. After entering sleep, the controller's first communication is only used to wake the chip; that communication may fail, and the effective communication usually completes on the next attempt.

m5pm1_err_t setI2cSleepTime(uint8_t seconds);
m5ioe1_err_t setI2cSleepTime(uint8_t sleepTime);
pm1.setI2cSleepTime(5);  // M5PM1 idle sleep after 5 s
ioe1.setI2cSleepTime(5); // M5IOE1 idle sleep after 5 s

4. M5PM1 Timer

M5PM1 supports a timer function; when the countdown ends, it can perform power-on, power-off, reset, and other actions.

m5pm1_err_t timerSet(uint32_t seconds, m5pm1_tim_action_t action);
typedef enum {
    M5PM1_TIM_ACTION_STOP = 0b000,     // Stop, no action
    M5PM1_TIM_ACTION_FLAG = 0b001,     // Set flag only
    M5PM1_TIM_ACTION_REBOOT = 0b010,   // System reboot
    M5PM1_TIM_ACTION_POWERON = 0b011,  // Power on
    M5PM1_TIM_ACTION_POWEROFF = 0b100  // Power off
} m5pm1_tim_action_t;

Timed Power-On / Power-Off Example

Example description: after the device starts, two touch cards are displayed. Tap Reboot 3 s and the device powers off immediately, then powers on automatically after 3 seconds; tap Power off 5 s and M5PM1 turns off the whole device power after 5 seconds.

Note
Before using this example, if IMU or RTC wakeup has been configured, disable the wakeup enables for G0 / G4 first (pm1.gpioSetWakeEnable(M5PM1_GPIO_NUM_0, false);, pm1.gpioSetWakeEnable(M5PM1_GPIO_NUM_4, false);) and clear the wakeup flags (pm1.clearWakeSource(0x7F);). Otherwise, M5PM1 may be woken up early by the IMU or RTC interrupt, and the timer will not work properly.
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 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106
#include <M5Unified.h>
#include <M5PM1.h>

M5PM1 pm1;

static void drawCard(int y, const char* title, const char* detail, uint16_t color)
{
    M5.Display.fillRoundRect(60, y, 360, 100, 16, color);
    M5.Display.drawRoundRect(60, y, 360, 100, 16, TFT_WHITE);
    M5.Display.setTextDatum(middle_center);
    M5.Display.setTextColor(TFT_WHITE, color);
    M5.Display.setFont(&fonts::FreeSansBold18pt7b);
    M5.Display.drawString(title, 240, y + 38);
    M5.Display.setFont(&fonts::FreeSans12pt7b);
    M5.Display.drawString(detail, 240, y + 72);
}

static void drawMenu(void)
{
    M5.Display.fillScreen(TFT_BLACK);
    M5.Display.setTextDatum(middle_center);
    M5.Display.setTextColor(TFT_CYAN, TFT_BLACK);
    M5.Display.setFont(&fonts::FreeSansBold24pt7b);
    M5.Display.drawString("Power Timer", 240, 52);

    drawCard(122, "Reboot 3 s", "Shutdown then power on", TFT_BLUE);
    drawCard(272, "Power off 5 s", "Timer shuts down the device", TFT_ORANGE);

    M5.Display.setTextColor(TFT_DARKGREY, TFT_BLACK);
    M5.Display.setFont(&fonts::FreeSans9pt7b);
    M5.Display.drawString("Tap a card to start", 240, 430);
}

static void drawMessage(const char* title, const char* detail, uint16_t color)
{
    M5.Display.fillScreen(TFT_BLACK);
    M5.Display.setTextDatum(middle_center);
    M5.Display.setTextColor(color, TFT_BLACK);
    M5.Display.setFont(&fonts::FreeSansBold24pt7b);
    M5.Display.drawString(title, 240, 188);
    M5.Display.setTextColor(TFT_WHITE, TFT_BLACK);
    M5.Display.setFont(&fonts::FreeSansBold12pt7b);
    M5.Display.drawString(detail, 240, 258);
}

void setup(void)
{
    delay(500);
    M5.begin();
    Serial.begin(115200);
    M5.Display.setBrightness(128);

    // Initialize PM1
    m5pm1_err_t err = pm1.begin(&M5.In_I2C, M5PM1_DEFAULT_ADDR, M5PM1_I2C_FREQ_100K);
    if (err == M5PM1_OK) {
        Serial.println("PM1 initialization successful");
    } else {
        Serial.printf("PM1 initialization failed, error code: %d\n", err);
        drawMessage("M5PM1 Error", "Check the internal I2C bus", TFT_RED);
        while (true) {
            delay(1000);
        }
    }
    pm1.setSingleResetDisable(false);

    pm1.gpioSetWakeEnable(M5PM1_GPIO_NUM_0, false);
    pm1.gpioSetWakeEnable(M5PM1_GPIO_NUM_4, false);
    pm1.clearWakeSource(0x7F);
    drawMenu();
}

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

    if (!M5.Touch.getDetail().wasClicked()) {
        delay(20);
        return;
    }

    const auto touch = M5.Touch.getDetail();
    if (touch.y >= 122 && touch.y <= 222) {
        m5pm1_err_t err = pm1.timerSet(3, M5PM1_TIM_ACTION_POWERON);
        if (err != M5PM1_OK) {
            Serial.printf("Power-on timer failed, error code: %d\n", err);
            drawMessage("Timer failed", "M5PM1 did not accept the timer", TFT_RED);
            return;
        }

        Serial.println("Set power-on timer: 3s");
        drawMessage("Powering off", "Auto power-on in 3 s", TFT_BLUE);
        delay(700);
        if (pm1.shutdown() != M5PM1_OK) {
            drawMessage("Shutdown failed", "Disconnect USB and retry", TFT_RED);
        }
    } else if (touch.y >= 272 && touch.y <= 372) {
        m5pm1_err_t err = pm1.timerSet(5, M5PM1_TIM_ACTION_POWEROFF);
        if (err == M5PM1_OK) {
            Serial.println("Set power-off timer: 5s");
            drawMessage("Power off armed", "Shutdown in 5 s", TFT_ORANGE);
        } else {
            Serial.printf("Power-off timer failed, error code: %d\n", err);
            drawMessage("Timer failed", "M5PM1 did not accept the timer", TFT_RED);
        }
    }
}

After a successful upload, tap the upper card to test the 3-second timed power-on, and tap the lower card to test the 5-second timed power-off. After power-off, single-click the M5PM1 power button to power on again.

Example effect demonstration:

5. IMU Wakeup

LSM6DS3TR-C Driver Library

IMU to M5PM1 Wakeup

After the power switches to L1 Mode, the LSM6DS3TR-C is powered by 3V3_L1, while M5PM1 continues to work from the L0 power. After configuring the IMU Wake-up interrupt, M5PM1 enters sleep while keeping the L1 output (3V3_L1) powered to keep the LSM6DS3TR-C working.

At this point, flipping or moving the device triggers the LSM6DS3TR-C INT1 interrupt. INT1 is connected to M5PM1 G0, and M5PM1 restarts the system after detecting the rising edge.

After M5PM1 wakes up, it runs the L0, L1, and L2/L3A power-on sequence again, and the ESP32-P4 re-executes initialization.

Example description: after the device boots, tap the screen to enable the LSM6DS3TR-C Wake-up interrupt, configure M5PM1 G0 rising-edge wakeup, and keep the L1 power, then turn off M5PM1. At this point, flipping or moving the device wakes M5PM1, and the ESP32-P4 powers on again and executes setup().

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
#include <M5Unified.h>
#include <M5PM1.h>
#include <Wire.h>
#include <Adafruit_LSM6DS3TRC.h>

Adafruit_LSM6DS3TRC imu;
M5PM1 pm1;

static void drawStatus(const char* title, const char* detail, uint16_t color)
{
    M5.Display.fillScreen(TFT_BLACK);
    M5.Display.setTextDatum(middle_center);
    M5.Display.setTextColor(color, TFT_BLACK);
    M5.Display.setFont(&fonts::FreeSansBold24pt7b);
    M5.Display.drawString(title, 240, 190);
    M5.Display.setTextColor(TFT_WHITE, TFT_BLACK);
    M5.Display.setFont(&fonts::FreeSansBold12pt7b);
    M5.Display.drawString(detail, 240, 264);
}

void setup(void)
{
    delay(500);
    M5.begin();
    Serial.begin(115200);

    Wire1.setPins(M5.getPin(m5::pin_name_t::in_i2c_sda), M5.getPin(m5::pin_name_t::in_i2c_scl));
    M5.Display.setBrightness(128);

    // Initialize PM1
    m5pm1_err_t err = pm1.begin(&M5.In_I2C, M5PM1_DEFAULT_ADDR, M5PM1_I2C_FREQ_100K);
    if (err == M5PM1_OK) {
        Serial.println("PM1 initialization successful");
        pm1.gpioSetFunc(M5PM1_GPIO_NUM_0, M5PM1_GPIO_FUNC_GPIO);
        pm1.gpioSetMode(M5PM1_GPIO_NUM_0, M5PM1_GPIO_MODE_INPUT);
        pm1.gpioSetPull(M5PM1_GPIO_NUM_0, M5PM1_GPIO_PULL_DOWN);
        pm1.gpioSetWakeEnable(M5PM1_GPIO_NUM_0, true);
        pm1.gpioSetWakeEdge(M5PM1_GPIO_NUM_0, M5PM1_GPIO_WAKE_RISING);
    } else {
        Serial.printf("PM1 initialization failed, error code: %d\n", err);
    }
    pm1.setSingleResetDisable(false);

    // Check if sensor is connected and initialize
    while (!imu.begin_I2C(LSM6DS_I2CADDR_DEFAULT, &Wire1)) {
        Serial.println("Error: LSM6DS3TR-C not connected, check wiring and I2C address!");
        delay(1000);
    }
    imu.configIntOutputs(false, false);  // Push-pull, active high
    Serial.println("LSM6DS3TR-C initialization successful");

    drawStatus("IMU Wakeup", "Tap to power off", TFT_CYAN);
}

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

    if (M5.Touch.getDetail().wasClicked()) {
        imu.enableWakeup(true, 0, 5);
        imu.configInt1(false, false, false, false, true);
        Serial.println("LSM6DS3TR-C Wake-up interrupt enabled");

        drawStatus("Powering off", "Move to wake", TFT_ORANGE);
        delay(500);

        // Shutdown and keep L1 for the IMU
        pm1.setLdoEnable(true);
        pm1.ldoSetPowerHold(true);
        pm1.setLedEnLevel(true);
        pm1.shutdown();
    }

    delay(20);
}

Example effect demonstration:

IMU to ESP32-P4 Wakeup

The G2 interrupt output of M5PM1 is connected to LPG0 (GPIO0) of the ESP32-P4. After the LSM6DS3TR-C INT1 triggers M5PM1 G0, M5PM1 pulls GPIO0 low through G2, which can be used as a deep-sleep wakeup source for the ESP32-P4.

Implementation steps:

  1. Configure M5PM1 G0 as an input; this pin is connected to the INT1 output of the LSM6DS3TR-C.
  2. Configure M5PM1 G2 as the interrupt output (IRQ) pin. When G0 detects an IMU interrupt, G2 outputs low.
  3. Configure ESP32-P4 GPIO0 as a falling-edge interrupt input; for deep sleep, it can be configured as an EXT1 wakeup source.
  4. Flip or move the device: triggers LSM6DS3TR-C INT1 → M5PM1 G0 → M5PM1 G2 → ESP32-P4 GPIO0, and the program handles the interrupt.

Example description: after the device boots, tap the screen to configure the Adafruit Wake-up, the M5PM1 G0 input interrupt, and the G2 IRQ output. After moving the device, GPIO0 generates a falling edge and the screen displays the motion detection result.

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 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113
#include <M5Unified.h>
#include <M5PM1.h>
#include <Wire.h>
#include <Adafruit_LSM6DS3TRC.h>
#include "driver/rtc_io.h"

Adafruit_LSM6DS3TRC imu;
M5PM1 pm1;

static void drawStatus(const char* title, const char* detail, uint16_t color)
{
    M5.Display.fillScreen(TFT_BLACK);
    M5.Display.setTextDatum(middle_center);
    M5.Display.setTextColor(color, TFT_BLACK);
    M5.Display.setFont(&fonts::FreeSansBold24pt7b);
    M5.Display.drawString(title, 240, 190);
    M5.Display.setTextColor(TFT_WHITE, TFT_BLACK);
    M5.Display.setFont(&fonts::FreeSansBold12pt7b);
    M5.Display.drawString(detail, 240, 264);
}

void setup(void)
{
    delay(500);
    M5.begin();
    Serial.begin(115200);

    Wire1.setPins(M5.getPin(m5::pin_name_t::in_i2c_sda), M5.getPin(m5::pin_name_t::in_i2c_scl));
    M5.Display.setBrightness(128);

    // Initialize PM1
    m5pm1_err_t err = pm1.begin(&M5.In_I2C, M5PM1_DEFAULT_ADDR, M5PM1_I2C_FREQ_100K);
    if (err == M5PM1_OK) {
        Serial.println("PM1 initialization successful");
        pm1.irqClearGpioAll();
        pm1.irqClearSysAll();
        pm1.irqClearBtnAll();

        pm1.irqSetGpioMaskAll(M5PM1_IRQ_MASK_ENABLE);
        pm1.irqSetSysMaskAll(M5PM1_IRQ_MASK_ENABLE);
        pm1.irqSetBtnMaskAll(M5PM1_IRQ_MASK_ENABLE);
        pm1.irqSetGpioMask(M5PM1_IRQ_GPIO0, M5PM1_IRQ_MASK_DISABLE);

        pm1.gpioSetFunc(M5PM1_GPIO_NUM_0, M5PM1_GPIO_FUNC_GPIO);
        pm1.gpioSetMode(M5PM1_GPIO_NUM_0, M5PM1_GPIO_MODE_INPUT);
        pm1.gpioSetPull(M5PM1_GPIO_NUM_0, M5PM1_GPIO_PULL_DOWN);

        pm1.gpioSetMode(M5PM1_GPIO_NUM_2, M5PM1_GPIO_MODE_OUTPUT);
        pm1.gpioSetDrive(M5PM1_GPIO_NUM_2, M5PM1_GPIO_DRIVE_PUSHPULL);
        pm1.gpioSetOutput(M5PM1_GPIO_NUM_2, 1);
        pm1.gpioSetFunc(M5PM1_GPIO_NUM_2, M5PM1_GPIO_FUNC_IRQ);
    } else {
        Serial.printf("PM1 initialization failed, error code: %d\n", err);
    }
    pm1.setSingleResetDisable(false);

    // Check if sensor is connected and initialize
    while (!imu.begin_I2C(LSM6DS_I2CADDR_DEFAULT, &Wire1)) {
        Serial.println("Error: LSM6DS3TR-C not connected, check wiring and I2C address!");
        delay(1000);
    }
    imu.configIntOutputs(false, false);  // Push-pull, active high
    Serial.println("LSM6DS3TR-C initialization successful");

    drawStatus("IMU IRQ", "Tap to enable interrupt", TFT_CYAN);
}

volatile bool pm1IrqTriggered = false;

void ARDUINO_ISR_ATTR pm1_irq_handler()
{
    pm1IrqTriggered = true;
}

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

    if (pm1IrqTriggered) {
        pm1IrqTriggered = false;
        bool wakeEvent = imu.shake();
        pm1.clearWakeSource(0x7F);
        pm1.irqClearGpioAll();
        pm1.irqClearSysAll();
        pm1.irqClearBtnAll();

        Serial.printf("LSM6DS3TR-C wake event: %s\n", wakeEvent ? "true" : "false");
        drawStatus("IMU IRQ", "Motion detected", TFT_GREEN);
    }

    if (M5.Touch.getDetail().wasClicked()) {
        imu.enableWakeup(true, 0, 5);
        imu.configInt1(false, false, false, false, true);
        pm1.clearWakeSource(0x7F);
        pm1.irqClearGpioAll();
        pm1.irqClearSysAll();
        pm1.irqClearBtnAll();

        Serial.println("LSM6DS3TR-C Wake-up interrupt enabled");
        drawStatus("IMU IRQ", "Move the device", TFT_ORANGE);

        // Use either option below.
        pinMode(GPIO_NUM_0, INPUT_PULLUP);
        attachInterrupt(GPIO_NUM_0, pm1_irq_handler, FALLING);

        // esp_sleep_enable_ext1_wakeup_io(1ULL << GPIO_NUM_0, ESP_EXT1_WAKEUP_ANY_LOW);
        // rtc_gpio_pullup_en(GPIO_NUM_0);
        // Serial.println("Going to sleep now");
        // esp_deep_sleep_start();
    }

    delay(20);
}

Example effect demonstration:

6. RTC Wakeup

The RX8130CE is powered by the always-on 3V3_L0 rail; the RTC keeps counting after M5PM1 sleeps. The RTC /IRQ is connected to M5PM1 G4, and can be used to wake M5PM1, or to wake the ESP32-P4 through M5PM1 G2.

RTC to M5PM1 Wakeup

The VIO and VDD of the RX8130CE are both connected to the always-on 3V3_L0 rail. After M5PM1 enters sleep, the RTC keeps counting, so RTC wakeup does not depend on keeping L1 powered.

After configuring the RTC timer interrupt, the /IRQ output of the RX8130CE triggers a falling-edge wakeup through M5PM1 G4. After M5PM1 wakes up, it runs the L0, L1, and L2/L3A power-on sequence again, and the ESP32-P4 re-executes initialization.

Example description: after the device boots, tap the screen; the RTC is set to trigger a timer interrupt after 5 seconds, and M5PM1 G4 is configured as a falling-edge wakeup. After M5PM1 shuts down, the RTC keeps counting and restarts the ESP32-P4 when the time is reached.

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
#include <M5Unified.h>
#include <M5PM1.h>

M5PM1 pm1;

static void drawStatus(const char* title, const char* detail, uint16_t color)
{
    M5.Display.fillScreen(TFT_BLACK);
    M5.Display.setTextDatum(middle_center);
    M5.Display.setTextColor(color, TFT_BLACK);
    M5.Display.setFont(&fonts::FreeSansBold24pt7b);
    M5.Display.drawString(title, 240, 190);
    M5.Display.setTextColor(TFT_WHITE, TFT_BLACK);
    M5.Display.setFont(&fonts::FreeSansBold12pt7b);
    M5.Display.drawString(detail, 240, 264);
}

void setup(void)
{
    delay(500);
    M5.begin();
    Serial.begin(115200);
    M5.Display.setBrightness(128);

    // Initialize PM1
    m5pm1_err_t err = pm1.begin(&M5.In_I2C, M5PM1_DEFAULT_ADDR, M5PM1_I2C_FREQ_100K);
    if (err == M5PM1_OK) {
        Serial.println("PM1 initialization successful");
        pm1.gpioSetFunc(M5PM1_GPIO_NUM_4, M5PM1_GPIO_FUNC_GPIO);
        pm1.gpioSetMode(M5PM1_GPIO_NUM_4, M5PM1_GPIO_MODE_INPUT);
        pm1.gpioSetPull(M5PM1_GPIO_NUM_4, M5PM1_GPIO_PULL_UP);
        pm1.gpioSetWakeEnable(M5PM1_GPIO_NUM_4, true);
        pm1.gpioSetWakeEdge(M5PM1_GPIO_NUM_4, M5PM1_GPIO_WAKE_FALLING);
    } else {
        Serial.printf("PM1 initialization failed, error code: %d\n", err);
    }
    pm1.setSingleResetDisable(false);

    if (!M5.Rtc.isEnabled()) {
        Serial.println("RTC initialization failed");
    }

    drawStatus("RTC Wakeup", "Tap to sleep for 5 s", TFT_CYAN);
}

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

    if (M5.Touch.getDetail().wasClicked()) {
        M5.Rtc.clearIRQ();

        if (M5.Rtc.setTimerIRQ(5000)) {
            Serial.println("RTC IRQ enabled successfully");
            drawStatus("Powering off", "Wake in 5 s", TFT_ORANGE);
            delay(500);

            // RX8130CE is powered by 3V3_L0
            pm1.shutdown();
        } else {
            Serial.println("Failed to enable RTC IRQ");
            drawStatus("RTC Wakeup", "Timer setup failed", TFT_RED);
        }
    }

    delay(20);
}

Example effect demonstration:

RTC to ESP32-P4 Wakeup

The RTC wakeup configuration steps are similar to the IMU to ESP32-P4 wakeup, except that RTC wakeup does not need to configure the LSM6DS3TR-C interrupt, but still relies on the chain RTC /IRQ → M5PM1 G4 → M5PM1 G2 → ESP32-P4 GPIO0.

Implementation steps:

  1. Configure M5PM1 G4 as an input; this pin is connected to the /IRQ output of the RX8130CE.
  2. Configure M5PM1 G2 as the interrupt output (IRQ) pin. When G4 detects an RTC interrupt, G2 outputs low.
  3. Configure ESP32-P4 GPIO0 as a falling-edge interrupt input; for deep sleep, it can be configured as an EXT1 wakeup source.
  4. When the RTC timer is reached, the RTC IRQ is triggered → M5PM1 G4 → M5PM1 G2 → ESP32-P4 GPIO0, and the program handles the interrupt.

Example description: after the device boots, tap the screen to set the RX8130CE to output an interrupt after 5 seconds. When the interrupt arrives, M5PM1 G2 pulls GPIO0 low, the ESP32-P4 enters the falling-edge interrupt handler and updates the display; tapping the screen again re-arms the timer.

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 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100
#include <M5Unified.h>
#include <M5PM1.h>
#include "driver/rtc_io.h"

M5PM1 pm1;

static void drawStatus(const char* title, const char* detail, uint16_t color)
{
    M5.Display.fillScreen(TFT_BLACK);
    M5.Display.setTextDatum(middle_center);
    M5.Display.setTextColor(color, TFT_BLACK);
    M5.Display.setFont(&fonts::FreeSansBold24pt7b);
    M5.Display.drawString(title, 240, 190);
    M5.Display.setTextColor(TFT_WHITE, TFT_BLACK);
    M5.Display.setFont(&fonts::FreeSansBold12pt7b);
    M5.Display.drawString(detail, 240, 264);
}

void setup(void)
{
    delay(500);
    M5.begin();
    Serial.begin(115200);
    M5.Display.setBrightness(128);

    // Initialize PM1
    m5pm1_err_t err = pm1.begin(&M5.In_I2C, M5PM1_DEFAULT_ADDR, M5PM1_I2C_FREQ_100K);
    if (err == M5PM1_OK) {
        Serial.println("PM1 initialization successful");
        pm1.irqClearGpioAll();
        pm1.irqClearSysAll();
        pm1.irqClearBtnAll();

        pm1.irqSetGpioMaskAll(M5PM1_IRQ_MASK_ENABLE);
        pm1.irqSetSysMaskAll(M5PM1_IRQ_MASK_ENABLE);
        pm1.irqSetBtnMaskAll(M5PM1_IRQ_MASK_ENABLE);
        pm1.irqSetGpioMask(M5PM1_IRQ_GPIO4, M5PM1_IRQ_MASK_DISABLE);

        pm1.gpioSetMode(M5PM1_GPIO_NUM_4, M5PM1_GPIO_MODE_INPUT);
        pm1.gpioSetPull(M5PM1_GPIO_NUM_4, M5PM1_GPIO_PULL_UP);

        pm1.gpioSetMode(M5PM1_GPIO_NUM_2, M5PM1_GPIO_MODE_OUTPUT);
        pm1.gpioSetDrive(M5PM1_GPIO_NUM_2, M5PM1_GPIO_DRIVE_PUSHPULL);
        pm1.gpioSetFunc(M5PM1_GPIO_NUM_2, M5PM1_GPIO_FUNC_IRQ);
    } else {
        Serial.printf("PM1 initialization failed, error code: %d\n", err);
    }
    pm1.setSingleResetDisable(false);

    delay(200);
    M5.Rtc.clearIRQ();
    delay(200);
    drawStatus("RTC IRQ", "Tap to start timer", TFT_CYAN);
}

volatile bool pm1IrqTriggered = false;

void ARDUINO_ISR_ATTR pm1_irq_handler()
{
    pm1IrqTriggered = true;
}

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

    if (pm1IrqTriggered) {
        pm1IrqTriggered = false;
        M5.Rtc.setTimerIRQ(0);
        M5.Rtc.clearIRQ();
        pm1.clearWakeSource(0x7F);
        pm1.irqClearGpioAll();
        pm1.irqClearSysAll();
        pm1.irqClearBtnAll();

        Serial.println("PM1 IRQ triggered");
        drawStatus("RTC IRQ", "Timer wake detected", TFT_GREEN);
    }

    if (M5.Touch.getDetail().wasClicked()) {
        if (M5.Rtc.setTimerIRQ(5000)) {
            Serial.println("RTC IRQ enabled successfully");
            drawStatus("RTC IRQ", "Wait for IRQ", TFT_ORANGE);

            // Use either option below.
            pinMode(GPIO_NUM_0, INPUT_PULLUP);
            attachInterrupt(GPIO_NUM_0, pm1_irq_handler, FALLING);

            // esp_sleep_enable_ext1_wakeup_io(1ULL << GPIO_NUM_0, ESP_EXT1_WAKEUP_ANY_LOW);
            // rtc_gpio_pullup_en(GPIO_NUM_0);
            // Serial.println("Going to sleep now");
            // esp_deep_sleep_start();
        } else {
            Serial.println("Failed to enable RTC IRQ");
            drawStatus("RTC IRQ", "Timer setup failed", TFT_RED);
        }
    }

    delay(20);
}

Example effect demonstration:

7. M5IOE1 Peripheral Control

The M5IOE1 of CoreP4X controls the power and configuration of peripherals such as IR, audio, USB, PORT.A, and LCD, and reads the charge and microSD status.

M5IOE1 Pin CoreP4X Function
IO1 Audio codec power enable
IO2 USB-A Host 5V output enable
IO3 Speaker amplifier power enable
IO4 USB-C OTG 5V output enable
IO5 PORT.A 5V output enable
IO6 Charge status input; low indicates charging
IO8 Touch controller reset
IO9/PWM1 LCD backlight PWM
IO10 LCD power enable
IO11 LCD reset
IO12 3V3_L3B shared peripheral power enable
IO13 microSD card detection input
Note
Because M5Unified currently does not support reinitializing the display separately while running, the example does not control IO8, IO10, IO11, and IO12. After these pins are pulled low, touch, LCD, or shared peripherals require reinitialization to recover.

Example description: the program switches the PORT.A, USB-A, USB-C, speaker, and audio power in sequence, then demonstrates the LCD backlight, and reads the charging status and microSD card detection status. Each step updates the screen state and waits 1 second, making it easy to observe the actual effect of the power switches.

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 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121
#include <M5Unified.h>
#include <M5IOE1.h>

M5IOE1 ioe1;
M5Canvas canvas(&M5.Display);

#define IOE_AUDIO_EN M5IOE1_PIN_1
#define IOE_USBA_EN  M5IOE1_PIN_2
#define IOE_SPK_EN   M5IOE1_PIN_3
#define IOE_USBC_EN  M5IOE1_PIN_4
#define IOE_GROVE_EN M5IOE1_PIN_5
#define IOE_CHG_STAT M5IOE1_PIN_6
#define IOE_SD_DET   M5IOE1_PIN_13

static void show(const char* line1, const char* line2)
{
    canvas.fillSprite(TFT_BLACK);
    canvas.setTextDatum(middle_center);
    canvas.setTextColor(TFT_CYAN, TFT_BLACK);
    canvas.setFont(&fonts::FreeSansBold24pt7b);
    canvas.drawString("Power Test", 240, 58);
    canvas.setTextColor(TFT_WHITE, TFT_BLACK);
    canvas.setFont(&fonts::FreeSansBold18pt7b);
    canvas.drawString(line1, 240, 206);
    canvas.setTextColor(TFT_ORANGE, TFT_BLACK);
    canvas.setFont(&fonts::FreeSansBold12pt7b);
    canvas.drawString(line2, 240, 270);
    canvas.pushSprite(0, 0);
}

void setup(void)
{
    delay(500);
    M5.begin();
    Serial.begin(115200);
    M5.Display.setBrightness(128);

    // Initialize IOE1 (CoreP4X uses address 0x4F)
    const m5ioe1_err_t ioeErr = ioe1.begin(&M5.In_I2C, M5IOE1_DEFAULT_ADDR_2, M5IOE1_I2C_FREQ_100K);
    if (ioeErr != M5IOE1_OK) {
        Serial.printf("M5IOE1 init failed: %d\n", ioeErr);
        while (true) {
            delay(1000);
        }
    }

    ioe1.pinMode(IOE_AUDIO_EN, OUTPUT);
    ioe1.pinMode(IOE_USBA_EN, OUTPUT);
    ioe1.pinMode(IOE_SPK_EN, OUTPUT);
    ioe1.pinMode(IOE_USBC_EN, OUTPUT);
    ioe1.pinMode(IOE_GROVE_EN, OUTPUT);
    ioe1.pinMode(IOE_CHG_STAT, INPUT_PULLUP);
    ioe1.pinMode(IOE_SD_DET, INPUT_PULLUP);
    ioe1.setPwmFrequency(1000);

    canvas.createSprite(480, 480);
    Serial.println("Peripheral power test begin");
}

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

    // PORT.A 5V
    show("PORT.A 5V", "OFF");
    ioe1.digitalWrite(IOE_GROVE_EN, LOW);
    delay(1000);
    show("PORT.A 5V", "ON");
    ioe1.digitalWrite(IOE_GROVE_EN, HIGH);
    delay(1000);

    // USB-A Host 5V
    show("USB-A 5V", "OFF");
    ioe1.digitalWrite(IOE_USBA_EN, LOW);
    delay(1000);
    show("USB-A 5V", "ON");
    ioe1.digitalWrite(IOE_USBA_EN, HIGH);
    delay(1000);

    // USB-C OTG 5V
    show("USB-C OTG 5V", "OFF");
    ioe1.digitalWrite(IOE_USBC_EN, LOW);
    delay(1000);
    show("USB-C OTG 5V", "ON");
    ioe1.digitalWrite(IOE_USBC_EN, HIGH);
    delay(1000);

    // Speaker amplifier power
    show("Speaker power", "OFF");
    ioe1.digitalWrite(IOE_SPK_EN, LOW);
    delay(1000);
    show("Speaker power", "ON");
    ioe1.digitalWrite(IOE_SPK_EN, HIGH);
    delay(1000);

    // Audio codec power
    show("Audio power", "OFF");
    M5.Speaker.end();
    ioe1.digitalWrite(IOE_AUDIO_EN, LOW);
    delay(1000);
    show("Audio power", "ON");
    ioe1.digitalWrite(IOE_AUDIO_EN, HIGH);
    M5.Speaker.begin();
    delay(1000);

    // LCD backlight PWM
    show("Backlight", "25%");
    ioe1.analogWrite(M5IOE1_PWM_CH1, 64);
    delay(1000);
    show("Backlight", "100%");
    ioe1.analogWrite(M5IOE1_PWM_CH1, 255);
    delay(1000);

    // Charger and SD status
    const int chargeState = ioe1.digitalRead(IOE_CHG_STAT);
    const int sdState = ioe1.digitalRead(IOE_SD_DET);
    show(chargeState == LOW ? "CHG: Charging" : "CHG: Idle",
         sdState == LOW ? "SD: Inserted" : "SD: Removed");
    Serial.printf("CHG_STAT: %d, SD_DET: %d\n", chargeState, sdState);
    delay(1000);
}

After a successful upload, the program demonstrates PORT.A, USB-A, USB-C, speaker, audio, and LCD backlight control in sequence, and reads the charging status and microSD card detection status; each step outputs the current state on the screen and serial port.

Page Tools
PDF
On This Page