English
English
简体中文
日本語

Arduino Quick Start

2. Devices & Examples

5. Extensions

6. Applications

Chain Captain M5PM1 & M5IOE1 Power Management

1. Multi-Level Power Switching Design

Chain Captain integrates the M5PM1 and M5IOE1 with hardware circuits to implement multi-level power switching. Each level corresponds to a different controller operating state and power state for peripherals and interfaces. You can switch the controller state and related power enables as needed, 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
Chain Captain's multi-level power switching design is not a series structure. The power inputs for the L1 ~ L3B switches all originate from the L0 source rather than from the preceding power level, allowing each level to be controlled independently. The levels are classified by peripheral power supply and consumption. After M5PM1 powers on, L1, L2, and L3A are enabled automatically (DCDC3V3_EN_PP, LDO3V3_EN_PP, and CHG_EN_PP are enabled by default). During M5Unified initialization, M5IOE1 and M5PM1 are then controlled to enable L3B, supplying or enabling the remaining peripherals.

L0

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

L1

At this power level, power is enabled for the IMU, RTC, and wakeup logic. This level uses the M5PM1 LDO3V3_EN_PP (i.e. PM_3V3_L1_EN) to output 3V3_L1, which can be controlled with the following APIs.

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

In addition, BMI270 INT1 is connected to M5PM1 G4, and the RX8130CE interrupt is connected to M5PM1 G2. To wake M5PM1 from the IMU or RTC, 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, power is enabled for the ESP32-S3, the M5IOE1 expander, system peripherals, and key pull-ups. This level uses the M5PM1 DCDC3V3_EN_PP (i.e. PM_3V3_L2_EN) to output 3V3_L2, which can be controlled with the following APIs.

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

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

L3B

Level L3B controls power or related settings for peripherals such as the LCD, audio, IR, RGB LED, 15Pin Header 5V, and Grove / Chain 5V.

Peripherals Controlled by M5IOE1

M5IOE1 IOEG1 IOEG2 IOEG3 IOEG4 IOEG5 IOEG6 IOEG8 IOEG9 IOEG10 IOEG11 IOEG12
LCD TFT_RST TFT_BL_EN TFT_L3B_EN
IR IR_L3B_EN
Charge CHG_STA
BOOST BOOST_L3B_EN
Audio CODER_L3B_EN
HEADER HEADER5V_EN
RGB LED G_LED B_LED R_LED
  • IOEG1 (TFT_RST): LCD reset
  • IOEG2 (IR_L3B_EN): IR IR_3V3_L3B power enable
  • IOEG3 (CHG_STA): charge status input; low indicates charging
  • IOEG4 (BOOST_L3B_EN): BOOST_5V boost enable
  • IOEG5 (CODER_L3B_EN): audio codec CODER_3V3_L3B power enable
  • IOEG6 (HEADER5V_EN): 15Pin Header 5V output enable
  • IOEG7 (ADDR_SET): tied to ground through a 10K resistor; the M5IOE1 address is fixed at 0x4F
  • IOEG8/IOEG9/IOEG10 (G/B/R_LED): RGB LED PWM output
  • IOEG11 (TFT_BL_EN): LCD backlight enable
  • IOEG12 (TFT_L3B_EN): LCD TFT_3V3_L3B power enable

The audio path also includes the AW8737A speaker amplifier enable controlled by the ESP32-S3 G21 (G21_SPK_L3B_EN); IOEG5 only controls the CODER_3V3_L3B supply for the codec and microphone.

Peripheral power can be controlled independently with the M5IOE1 APIs below:

ioe1.pinMode(M5IOE1_PIN_2, OUTPUT);
ioe1.digitalWrite(M5IOE1_PIN_2, HIGH);  // IR power on

5V Outputs Controlled by M5PM1

The 5V output switches of the Grove and Chain interfaces are controlled by M5PM1 PMG0 and PMG3:

M5PM1 PMG0 PMG3
Grove 5V PMG0_GROVE5V_EN
Chain 5V PMG3_CHAIN5V_EN
  • PMG0 (PMG0_GROVE5V_EN): Grove 5V output enable
  • PMG3 (PMG3_CHAIN5V_EN): Chain 5V output enable

The 5V outputs of Grove, Chain, and the 15Pin Header share one BOOST_5V branch: IOEG4 (BOOST_L3B_EN) enables the SY7088DGC to boost VSYS to 5V, and PMG0, PMG3, and IOEG6 then turn on the 5V output switches for Grove, Chain, and Header respectively. Therefore, BOOST_5V must be enabled before turning on any 5V output.

The M5Unified cfg.output_power option defaults to true, so Grove and Chain 5V are enabled by default after M5.begin(). They can be controlled with the following APIs:

M5.Power.setExtOutput(true);  // Grove / Chain 5V on
M5.Power.setExtOutput(false); // Grove / Chain 5V off, input mode
Note
If the device is powered through the 5V pins of Grove A, Chain, or the 15Pin Header, turn off the corresponding 5V output (use M5.Power.setExtOutput(false, ...) for Grove/Chain, and M5IOE1 IO6 for Header), or set cfg.output_power to false, to avoid back-driving the external power supply.

2. M5PM1 Sleep

Manual Sleep

M5PM1 can be put into sleep mode by the program to reduce overall power consumption. By default, setting sleep directly falls back to L0, where only M5PM1 and the RTC powered by 3V3_L0 remain powered.

pm1.shutdown();

In some special use cases (for example, IMU or RTC wakeup, or ESP32-S3 SoC sleep), M5PM1 may enter sleep while keeping some power levels enabled for wakeup sources or state retention. For such applications, configure the power switch pin states of the corresponding levels and activate state retention before calling pm1.shutdown().

Note
shutdown() only cuts the whole device power when running on battery; when USB is connected, the system may keep running. Disconnect the USB cable when testing sleep and wakeup.

I2C Idle Sleep

M5PM1 can be configured to enter sleep automatically when I2C is idle, reducing overall power consumption. After it enters sleep, the first communication from the ESP32-S3 is used to wake M5PM1, so that transaction may fail; the effective communication completes on the next transaction.

m5pm1_err_t setI2cSleepTime(uint8_t seconds);

3. M5PM1 Timer

M5PM1 supports a timer that performs operations such as power-on, power-off, or reset when the countdown ends.

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: After startup, use KEYB / KEYC to select the timer action (power on again after 3 seconds / power off after 5 seconds), and press KEYA to confirm. When power-on is selected, the device powers off immediately and powers on automatically after 3 seconds. When power-off is selected, the device powers off after 5 seconds and can be turned on again by a single click of the power button.

Note
If IMU or RTC wakeup has been configured before using this example, disable the wakeup enables for G2 / G4 (gpioSetWakeEnable(M5PM1_GPIO_NUM_2 / M5PM1_GPIO_NUM_4, false)) and clear the wakeup flags (clearWakeSource(0x7F)) first. Otherwise, M5PM1 may be woken up by the IMU or RTC, 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
#include <M5Unified.h>
#include <M5PM1.h>

M5PM1 pm1;

struct TimerItem {
    const char* title;
    uint32_t seconds;
    m5pm1_tim_action_t action;
};

static const TimerItem ITEMS[] = {
    {"Power ON  3s", 3, M5PM1_TIM_ACTION_POWERON},
    {"Power OFF 5s", 5, M5PM1_TIM_ACTION_POWEROFF},
};

static const size_t ITEM_COUNT = sizeof(ITEMS) / sizeof(ITEMS[0]);
static size_t itemIndex = 0;

static void drawMenu(const char* status)
{
    M5.Display.clear();
    M5.Display.setTextDatum(middle_center);
    M5.Display.setFont(&fonts::FreeSansBold18pt7b);
    M5.Display.setTextColor(TFT_CYAN, TFT_BLACK);
    M5.Display.drawString("Timer", 120, 40);
    M5.Display.setFont(&fonts::FreeSans9pt7b);

    for (size_t i = 0; i < ITEM_COUNT; ++i) {
        M5.Display.setTextColor(i == itemIndex ? TFT_GREEN : TFT_WHITE, TFT_BLACK);
        M5.Display.drawString(ITEMS[i].title, 120, 100 + i * 36);
    }

    M5.Display.setTextColor(TFT_DARKGREY, TFT_BLACK);
    M5.Display.drawString("B: prev  C: next", 120, 180);
    M5.Display.setTextColor(TFT_WHITE, TFT_BLACK);
    M5.Display.drawString("A: OK", 120, 205);

    if (status) {
        M5.Display.setTextColor(TFT_ORANGE, TFT_BLACK);
        M5.Display.drawString(status, 120, 228);
    }
}

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

    M5.Display.setRotation(0);
    M5.Display.setBrightness(128);

    // Initialize PM1
    const m5pm1_err_t err = pm1.begin(&M5.In_I2C, M5PM1_DEFAULT_ADDR, M5PM1_I2C_FREQ_100K);
    if (err != M5PM1_OK) {
        Serial.printf("M5PM1 init failed: %d\n", err);
        while (true) {
            delay(1000);
        }
    }

    drawMenu(nullptr);
    Serial.println("KEYB: previous, KEYC: next, KEYA: OK");
}

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

    if (M5.BtnB.wasClicked()) {
        itemIndex = (itemIndex + ITEM_COUNT - 1) % ITEM_COUNT;
        drawMenu(nullptr);
    }

    if (M5.BtnC.wasClicked()) {
        itemIndex = (itemIndex + 1) % ITEM_COUNT;
        drawMenu(nullptr);
    }

    if (M5.BtnA.wasClicked()) {
        const TimerItem& item = ITEMS[itemIndex];
        Serial.printf("Set timer: %s\n", item.title);
        drawMenu(item.action == M5PM1_TIM_ACTION_POWERON ? "Reboot in 3s"
                                                          : "Power off in 5s");
        pm1.timerSet(item.seconds, item.action);
        if (item.action == M5PM1_TIM_ACTION_POWERON) {
            pm1.shutdown();
        }
    }

    delay(20);
}

Example effect demonstration:

4. IMU Wakeup

BMI270 Driver Library

IMU Wakeup of M5PM1

Power switches to L1 mode, and only the BMI270, RX8130CE RTC, and M5PM1 remain powered. After IMU wakeup is configured, M5PM1 enters sleep while retaining the L1 output (3V3_L1) required for the BMI270 to operate.

You can then flip or move the device to trigger the BMI270 INT1 interrupt and wake M5PM1 for a restart.

After M5PM1 wakes, it runs the L0, L1, and L2/L3A power-up sequence again, and the ESP32-S3 repeats initialization.

Example: After startup, click KEYA to enable the BMI270 AnyMotion interrupt, configure M5PM1 G4 wakeup, and retain L1 power, then shut down M5PM1. Flip or move the device to wake M5PM1; the ESP32-S3 powers up again and runs 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 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94
#include <M5Unified.h>
#include <M5PM1.h>
#include <Wire.h>
#include "SparkFun_BMI270_Arduino_Library.h"

BMI270 imu;
M5PM1 pm1;

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

    M5.Display.setRotation(0);
    M5.Display.setBrightness(128);

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

    // 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.gpioSetWakeEnable(M5PM1_GPIO_NUM_4, true);
        pm1.gpioSetWakeEdge(M5PM1_GPIO_NUM_4, M5PM1_GPIO_WAKE_FALLING);  // Falling edge
    } else {
        Serial.printf("PM1 initialization failed, error code: %d\n", err);
    }
    pm1.setSingleResetDisable(false);

    // Initialize BMI270
    while (imu.beginI2C(BMI2_I2C_PRIM_ADDR, Wire1) != BMI2_OK) {
        Serial.println("Error: BMI270 not connected, check wiring and I2C address!");
        delay(1000);
    }
    imu.disableFeature(BMI2_ANY_MOTION);
    Serial.println("BMI270 initialization successful");

    M5.Display.clear();
    M5.Display.setTextDatum(middle_center);
    M5.Display.setFont(&fonts::FreeSansBold18pt7b);
    M5.Display.setTextColor(TFT_CYAN, TFT_BLACK);
    M5.Display.drawString("IMU Wakeup", 120, 40);
    M5.Display.setFont(&fonts::FreeSans9pt7b);
    M5.Display.setTextColor(TFT_WHITE, TFT_BLACK);
    M5.Display.drawString("A: arm & sleep", 120, 110);
    M5.Display.drawString("Shake to wake", 120, 145);
}

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

    if (M5.BtnA.wasPressed()) {
        int8_t ret = imu.enableFeature(BMI2_ANY_MOTION);
        // Optional
        // bmi2_sens_config config;
        // config.type = BMI2_ANY_MOTION;
        // config.cfg.any_motion.threshold = 0xA0;  // 1LSB equals to 0.48mg. Default is 83mg. Lower is more sensitive
        // config.cfg.any_motion.duration = 0x0A;   // 1LSB equals 20ms. Default is 100ms.
        // ret |= imu.setConfig(config);

        bmi2_int_pin_config intPinConfig;
        intPinConfig.pin_type = BMI2_INT1;
        intPinConfig.int_latch = BMI2_INT_NON_LATCH;
        intPinConfig.pin_cfg[0].lvl = BMI2_INT_ACTIVE_LOW;
        intPinConfig.pin_cfg[0].od = BMI2_INT_PUSH_PULL;
        intPinConfig.pin_cfg[0].output_en = BMI2_INT_OUTPUT_ENABLE;
        intPinConfig.pin_cfg[0].input_en = BMI2_INT_INPUT_DISABLE;
        ret |= imu.setInterruptPinConfig(intPinConfig);
        ret |= imu.mapInterruptToPin(BMI2_ANY_MOTION_INT, BMI2_INT1);
        if (!ret) {
            Serial.println("BMI270 AnyMotionInterrupt enabled successfully");
        } else {
            Serial.println("Failed to enable BMI270 AnyMotionInterrupt");
        }

        M5.Display.clear();
        M5.Display.setTextDatum(middle_center);
        M5.Display.setFont(&fonts::FreeSansBold18pt7b);
        M5.Display.setTextColor(TFT_ORANGE, TFT_BLACK);
        M5.Display.drawString("Power OFF", 120, 90);
        M5.Display.setFont(&fonts::FreeSans9pt7b);
        M5.Display.setTextColor(TFT_WHITE, TFT_BLACK);
        M5.Display.drawString("Shake to wake", 120, 140);
        delay(1000);

        // Shutdown
        pm1.setLdoEnable(true);
        pm1.ldoSetPowerHold(true);
        pm1.setLedEnLevel(true);
        pm1.shutdown();
    }
}

Example effect demonstration:

IMU Wakeup of ESP32-S3

The M5PM1 G1 interrupt output pin is connected to the ESP32-S3 G14 on the board. After the BMI270 INT1 triggers the G4 interrupt, M5PM1 pulls G14 low through G1, forming a chained wakeup signal to wake the ESP32-S3. The steps are as follows:

  1. Configure M5PM1 G4 as an input; this pin is connected to the BMI270 INT1 output.
  2. Configure M5PM1 G1 as the interrupt output (IRQ) pin. When G4 detects an IMU interrupt, G1 outputs a low level.
  3. Configure the ESP32-S3 G14 as a falling-edge interrupt input; in deep sleep it can be configured as an EXT1 wakeup source.
  4. Flip or move the device: BMI270 INT1 -> M5PM1 G4 -> M5PM1 G1 IRQ -> ESP32-S3 G14, and the program handles the interrupt.

Example: After startup, click KEYA to configure the BMI270 AnyMotion, the M5PM1 G4 input interrupt, and the G1 IRQ output. After the device is moved, G4 triggers the G1 IRQ, G14 sees a falling edge, and the screen shows 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 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132
#include <M5Unified.h>
#include <M5PM1.h>
#include <Wire.h>
#include "SparkFun_BMI270_Arduino_Library.h"
#include "driver/rtc_io.h"

BMI270 imu;
M5PM1 pm1;

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

    M5.Display.setRotation(0);
    M5.Display.setBrightness(128);

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

    // 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_1, M5PM1_GPIO_MODE_OUTPUT);
        pm1.gpioSetDrive(M5PM1_GPIO_NUM_1, M5PM1_GPIO_DRIVE_PUSHPULL);
        pm1.gpioSetFunc(M5PM1_GPIO_NUM_1, M5PM1_GPIO_FUNC_IRQ);
    } else {
        Serial.printf("PM1 initialization failed, error code: %d\n", err);
    }
    pm1.setSingleResetDisable(false);

    while (imu.beginI2C(BMI2_I2C_PRIM_ADDR, Wire1) != BMI2_OK) {
        Serial.println("Error: BMI270 not connected, check wiring and I2C address!");
        delay(1000);
    }
    imu.disableFeature(BMI2_ANY_MOTION);
    Serial.println("BMI270 initialization successful");

    M5.Display.clear();
    M5.Display.setTextDatum(middle_center);
    M5.Display.setFont(&fonts::FreeSansBold18pt7b);
    M5.Display.setTextColor(TFT_CYAN, TFT_BLACK);
    M5.Display.drawString("IMU IRQ", 120, 40);
    M5.Display.setFont(&fonts::FreeSans9pt7b);
    M5.Display.setTextColor(TFT_WHITE, TFT_BLACK);
    M5.Display.drawString("A: enable interrupt", 120, 110);
    M5.Display.drawString("Shake to trigger", 120, 145);
}

volatile bool pm1IrqTriggered = false;

void ARDUINO_ISR_ATTR pm1_irq_handler()
{
    pm1IrqTriggered = true;
}

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

    if (pm1IrqTriggered) {
        pm1IrqTriggered = false;

        Serial.println("PM1 IRQ triggered");

        uint16_t status = 0;
        imu.getInterruptStatus(&status);
        Serial.printf("BMI270 interrupt status: 0x%04X\n", status);

        M5.Display.setTextDatum(middle_center);
        M5.Display.setFont(&fonts::FreeSans9pt7b);
        M5.Display.setTextColor(TFT_GREEN, TFT_BLACK);
        M5.Display.drawString("IRQ triggered", 120, 195);
    }

    if (M5.BtnA.wasPressed()) {
        pm1.irqClearGpioAll();
        pm1.irqClearSysAll();
        pm1.irqClearBtnAll();
        int8_t ret = imu.enableFeature(BMI2_ANY_MOTION);
        // Optional
        // bmi2_sens_config config;
        // config.type = BMI2_ANY_MOTION;
        // ret |= imu.getConfig(&config);
        // config.cfg.any_motion.threshold = 0xE0;  // 1LSB equals to 0.48mg. Default is 83mg. Lower is more sensitive
        // config.cfg.any_motion.duration = 0x0A;   // 1LSB equals 20ms. Default is 100ms.
        // ret |= imu.setConfig(config);

        bmi2_int_pin_config intPinConfig;
        intPinConfig.pin_type = BMI2_INT1;
        intPinConfig.int_latch = BMI2_INT_NON_LATCH;
        intPinConfig.pin_cfg[0].lvl = BMI2_INT_ACTIVE_LOW;
        intPinConfig.pin_cfg[0].od = BMI2_INT_PUSH_PULL;
        intPinConfig.pin_cfg[0].output_en = BMI2_INT_OUTPUT_ENABLE;
        intPinConfig.pin_cfg[0].input_en = BMI2_INT_INPUT_DISABLE;
        ret |= imu.setInterruptPinConfig(intPinConfig);
        ret |= imu.mapInterruptToPin(BMI2_ANY_MOTION_INT, BMI2_INT1);
        if (!ret) {
            Serial.println("BMI270 AnyMotionInterrupt enabled successfully");
        } else {
            Serial.println("Failed to enable BMI270 AnyMotionInterrupt");
        }

        M5.Display.clear();
        M5.Display.setTextDatum(middle_center);
        M5.Display.setFont(&fonts::FreeSansBold12pt7b);
        M5.Display.setTextColor(TFT_WHITE, TFT_BLACK);
        M5.Display.drawString("Now shake!", 120, 120);

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

        // esp_sleep_enable_ext0_wakeup(GPIO_NUM_14, 0);  // 0 = Low
        // rtc_gpio_pullup_en(GPIO_NUM_14);
        // Serial.println("Going to sleep now");
        // esp_deep_sleep_start();
    }
}

Example effect demonstration:

5. RTC Wakeup

RTC Wakeup of M5PM1

Power switches to L1 mode, and only the RX8130CE RTC, BMI270, and M5PM1 remain powered. After the RTC timed wakeup is configured, M5PM1 enters sleep while retaining the L1 power required by the RTC to operate.

The RTC timer can be configured to wake the system; the RTC /IRQ interrupt is connected to M5PM1 G2 (PMG2_RTC_INT), triggering an M5PM1 wakeup and powering the ESP32-S3 again.

After M5PM1 wakes, it runs the L0, L1, and L2/L3A power-up sequence again, and the ESP32-S3 repeats initialization.

Example: After startup, click KEYA to configure the RX8130CE to output an interrupt after 5 seconds, configure M5PM1 G2 as a falling-edge wakeup, and call shutdown(). When the RTC timer expires, M5PM1 powers the controller again, and the ESP32-S3 runs from setup() again.

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

M5PM1 pm1;

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

    M5.Display.setRotation(0);
    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.gpioSetWakeEnable(M5PM1_GPIO_NUM_2, true);
        pm1.gpioSetWakeEdge(M5PM1_GPIO_NUM_2, M5PM1_GPIO_WAKE_FALLING);  // Falling edge
    } else {
        Serial.printf("PM1 initialization failed, error code: %d\n", err);
    }
    pm1.setSingleResetDisable(false);

    M5.Display.clear();
    M5.Display.setTextDatum(middle_center);
    M5.Display.setFont(&fonts::FreeSansBold18pt7b);
    M5.Display.setTextColor(TFT_CYAN, TFT_BLACK);
    M5.Display.drawString("RTC Wakeup", 120, 40);
    M5.Display.setFont(&fonts::FreeSans9pt7b);
    M5.Display.setTextColor(TFT_WHITE, TFT_BLACK);
    M5.Display.drawString("A: sleep for 5 s", 120, 110);
}

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

    if (M5.BtnA.wasPressed()) {
        M5.Rtc.clearIRQ();

        if (M5.Rtc.setTimerIRQ(5000)) {  // 5s later wakeup
            Serial.println("RTC IRQ enabled successfully");
            M5.Display.clear();
            M5.Display.setTextDatum(middle_center);
            M5.Display.setFont(&fonts::FreeSansBold18pt7b);
            M5.Display.setTextColor(TFT_ORANGE, TFT_BLACK);
            M5.Display.drawString("Power OFF", 120, 90);
            M5.Display.setFont(&fonts::FreeSans9pt7b);
            M5.Display.setTextColor(TFT_WHITE, TFT_BLACK);
            M5.Display.drawString("Wake in 5 s", 120, 140);
            delay(500);

            // Shutdown
            pm1.setLdoEnable(true);
            pm1.ldoSetPowerHold(true);
            pm1.setLedEnLevel(true);
            pm1.shutdown();
        } else {
            Serial.println("Failed to enable RTC IRQ");
        }
    }
}

Example effect demonstration:

RTC Wakeup of ESP32-S3

The configuration steps for waking the ESP32-S3 from the RTC are similar to those for waking M5PM1 from the RTC. The difference is that the RTC interrupt is not used to power the whole device, but follows the chain RTC IRQ -> M5PM1 G2 -> M5PM1 G1 IRQ -> ESP32-S3 G14.

The steps are as follows:

  1. Configure M5PM1 G2 as an input; this pin is connected to the RX8130CE /IRQ output.
  2. Configure M5PM1 G1 as the interrupt output (IRQ) pin. When G2 detects an RTC interrupt, G1 outputs a low level.
  3. Configure the ESP32-S3 G14 as a falling-edge interrupt input; in deep sleep it can be configured as an EXT1 wakeup source.
  4. When the RTC timer expires: RTC IRQ -> M5PM1 G2 -> M5PM1 G1 IRQ -> ESP32-S3 G14, and the program handles the interrupt.

Example: After startup, click KEYA to configure the RX8130CE to output an interrupt after 5 seconds. When the interrupt arrives, M5PM1 G1 pulls G14 low, and the ESP32-S3 enters the falling-edge interrupt handler and updates the screen. Click KEYA again to set the timer again and repeat the test.

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;

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

    M5.Display.setRotation(0);
    M5.Display.setBrightness(128);
    M5.Display.clear();
    M5.Display.setTextDatum(middle_center);
    M5.Display.setFont(&fonts::FreeSansBold18pt7b);
    M5.Display.setTextColor(TFT_CYAN, TFT_BLACK);
    M5.Display.drawString("RTC IRQ", 120, 40);
    M5.Display.setFont(&fonts::FreeSans9pt7b);
    M5.Display.setTextColor(TFT_WHITE, TFT_BLACK);
    M5.Display.drawString("A: sleep for 5 s", 120, 110);

    // 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_GPIO2, M5PM1_IRQ_MASK_DISABLE);
        pm1.gpioSetMode(M5PM1_GPIO_NUM_2, M5PM1_GPIO_MODE_INPUT);
        pm1.gpioSetPull(M5PM1_GPIO_NUM_2, M5PM1_GPIO_PULL_UP);

        pm1.gpioSetMode(M5PM1_GPIO_NUM_1, M5PM1_GPIO_MODE_OUTPUT);
        pm1.gpioSetDrive(M5PM1_GPIO_NUM_1, M5PM1_GPIO_DRIVE_PUSHPULL);
        pm1.gpioSetFunc(M5PM1_GPIO_NUM_1, 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);
}

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.irqClearGpioAll();
        pm1.irqClearSysAll();
        pm1.irqClearBtnAll();

        Serial.println("PM1 IRQ triggered");
        M5.Display.setTextDatum(middle_center);
        M5.Display.setFont(&fonts::FreeSans9pt7b);
        M5.Display.setTextColor(TFT_GREEN, TFT_BLACK);
        M5.Display.drawString("IRQ triggered", 120, 195);
    }

    if (M5.BtnA.wasPressed()) {
        if (M5.Rtc.setTimerIRQ(5000)) {  // Set Timer IRQ
            Serial.println("RTC IRQ enabled successfully");
            M5.Display.clear();
            M5.Display.setTextDatum(middle_center);
            M5.Display.setFont(&fonts::FreeSansBold12pt7b);
            M5.Display.setTextColor(TFT_WHITE, TFT_BLACK);
            M5.Display.drawString("Wait for IRQ...", 120, 120);

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

            // esp_sleep_enable_ext0_wakeup(GPIO_NUM_14, 0);  // 0 = Low
            // rtc_gpio_pullup_en(GPIO_NUM_14);
            // Serial.println("Going to sleep now");
            // esp_deep_sleep_start();
        } else {
            Serial.println("Failed to enable RTC IRQ");
        }
    }
}

Example effect demonstration:

6. M5PM1 / M5IOE1 Peripheral Control

M5IOE1 controls the power and configuration of peripherals such as IR, audio, Header 5V, RGB LED, and the LCD backlight, and reads the charging status; M5PM1 controls the 5V outputs of the Grove and Chain interfaces.

M5IOE1 Pin Chain Captain Function
IO1 LCD reset
IO2 IR power enable
IO3 Charge status input; low means charging
IO4 BOOST_5V boost enable
IO5 Audio codec power enable
IO6 15Pin Header 5V output enable
IO7 Address setting; tied to ground through 10K resistor
IO8 RGB LED green PWM
IO9 RGB LED blue PWM
IO10 RGB LED red PWM
IO11 LCD backlight enable
IO12 LCD 3.3V power enable
Note
Because M5Unified does not currently support reinitializing the display separately, the example does not control IO1 (TFT_RST) or IO12 (TFT_L3B_EN); after these two pins are pulled low, the display requires reinitialization to recover.

The Grove and Chain 5V outputs controlled by M5PM1 are as follows:

M5PM1 Pin Chain Captain Function
PMG0 Grove 5V output enable
PMG3 Chain 5V output enable

The 5V outputs of Grove, Chain, and Header are supplied by BOOST_5V, which is generated by IOEG4 (BOOST_L3B_EN), so the example enables BOOST_5V before switching the 5V outputs.

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 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174
#include <M5Unified.h>
#include <M5PM1.h>
#include <M5IOE1.h>

M5PM1 pm1;
M5IOE1 ioe1;

#define IOE_IR_EN    M5IOE1_PIN_2
#define IOE_CHG_STA  M5IOE1_PIN_3
#define IOE_BOOST_EN M5IOE1_PIN_4
#define IOE_AUDIO_EN M5IOE1_PIN_5
#define IOE_HEADER5V M5IOE1_PIN_6
#define IOE_LED_G    M5IOE1_PIN_8
#define IOE_LED_B    M5IOE1_PIN_9
#define IOE_LED_R    M5IOE1_PIN_10
#define IOE_BL_EN    M5IOE1_PIN_11

#define PM1_GROVE5V  M5PM1_GPIO_NUM_0
#define PM1_CHAIN5V  M5PM1_GPIO_NUM_3

static void show(const char* line1, const char* line2)
{
    M5.Display.clear();
    M5.Display.setTextDatum(middle_center);
    M5.Display.setFont(&fonts::FreeSansBold18pt7b);
    M5.Display.setTextColor(TFT_CYAN, TFT_BLACK);
    M5.Display.drawString("Power Test", 120, 40);
    M5.Display.setFont(&fonts::FreeSans9pt7b);
    M5.Display.setTextColor(TFT_WHITE, TFT_BLACK);
    M5.Display.drawString(line1, 120, 110);
    M5.Display.drawString(line2, 120, 145);
}

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

    M5.Display.setRotation(0);
    M5.Display.setBrightness(128);

    // Initialize PM1
    const m5pm1_err_t pm1Err = pm1.begin(&M5.In_I2C, M5PM1_DEFAULT_ADDR, M5PM1_I2C_FREQ_100K);
    if (pm1Err == M5PM1_OK) {
        pm1.setSingleResetDisable(false);
        pm1.pinMode(PM1_GROVE5V, OUTPUT);
        pm1.pinMode(PM1_CHAIN5V, OUTPUT);
    } else {
        Serial.printf("M5PM1 init failed: %d\n", pm1Err);
    }

    // Initialize IOE1 (Chain Captain 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_IR_EN, OUTPUT);
    ioe1.pinMode(IOE_BOOST_EN, OUTPUT);
    ioe1.pinMode(IOE_AUDIO_EN, OUTPUT);
    ioe1.pinMode(IOE_HEADER5V, OUTPUT);
    ioe1.pinMode(IOE_LED_G, OUTPUT);
    ioe1.pinMode(IOE_LED_B, OUTPUT);
    ioe1.pinMode(IOE_LED_R, OUTPUT);
    ioe1.pinMode(IOE_BL_EN, OUTPUT);
    ioe1.pinMode(IOE_CHG_STA, INPUT);

    // RGB LED needs push-pull outputs (open-drain by default).
    ioe1.setDriveMode(IOE_LED_R, M5IOE1_DRIVE_PUSHPULL);
    ioe1.setDriveMode(IOE_LED_G, M5IOE1_DRIVE_PUSHPULL);
    ioe1.setDriveMode(IOE_LED_B, M5IOE1_DRIVE_PUSHPULL);

    ioe1.setPwmFrequency(1000);
    ioe1.analogWrite(M5IOE1_PWM_CH3, 128);  // backlight 50%

    Serial.println("Peripheral power test begin");
}

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

    // IR power
    show("IR power", "OFF");
    ioe1.digitalWrite(IOE_IR_EN, LOW);
    delay(1000);
    show("IR power", "ON");
    ioe1.digitalWrite(IOE_IR_EN, HIGH);
    delay(1000);

    // BOOST_5V feeds the Grove / Chain / Header 5V branches
    show("BOOST_5V", "ON");
    ioe1.digitalWrite(IOE_BOOST_EN, HIGH);
    delay(1000);

    // Grove 5V output (M5PM1 G0)
    show("Grove 5V", "OFF");
    pm1.digitalWrite(PM1_GROVE5V, LOW);
    delay(1000);
    show("Grove 5V", "ON");
    pm1.digitalWrite(PM1_GROVE5V, HIGH);
    delay(1000);

    // Chain 5V output (M5PM1 G3)
    show("Chain 5V", "OFF");
    pm1.digitalWrite(PM1_CHAIN5V, LOW);
    delay(1000);
    show("Chain 5V", "ON");
    pm1.digitalWrite(PM1_CHAIN5V, 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.end();
    M5.Speaker.begin();
    delay(1000);

    // 15Pin Header 5V
    show("Header 5V", "ON");
    ioe1.digitalWrite(IOE_HEADER5V, HIGH);
    delay(1000);
    show("Header 5V", "OFF");
    ioe1.digitalWrite(IOE_HEADER5V, LOW);
    delay(1000);

    // RGB LED
    show("RGB LED", "Red");
    ioe1.analogWrite(M5IOE1_PWM_CH4, 255);
    ioe1.analogWrite(M5IOE1_PWM_CH2, 0);
    ioe1.analogWrite(M5IOE1_PWM_CH1, 0);
    delay(1000);
    show("RGB LED", "Green");
    ioe1.analogWrite(M5IOE1_PWM_CH4, 0);
    ioe1.analogWrite(M5IOE1_PWM_CH2, 255);
    ioe1.analogWrite(M5IOE1_PWM_CH1, 0);
    delay(1000);
    show("RGB LED", "Blue");
    ioe1.analogWrite(M5IOE1_PWM_CH4, 0);
    ioe1.analogWrite(M5IOE1_PWM_CH2, 0);
    ioe1.analogWrite(M5IOE1_PWM_CH1, 255);
    delay(1000);
    show("RGB LED", "Off");
    ioe1.analogWrite(M5IOE1_PWM_CH4, 0);
    ioe1.analogWrite(M5IOE1_PWM_CH2, 0);
    ioe1.analogWrite(M5IOE1_PWM_CH1, 0);
    delay(1000);

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

    // Charge status
    const int chargeState = ioe1.digitalRead(IOE_CHG_STA);
    show("CHG_STA", chargeState == LOW ? "CHARGING" : "NOT CHARGING");
    Serial.printf("CHG_STA: %d\n", chargeState);
    delay(1000);

    show("BOOST_5V", "OFF");
    ioe1.digitalWrite(IOE_BOOST_EN, LOW);
    delay(1000);
}

After power-on, the example demonstrates the control of the IR power, BOOST_5V, Grove / Chain 5V, audio power, Header 5V, RGB LED, and LCD backlight in sequence, and reads the charging status. Each step outputs the current state on the screen and the serial monitor.

Page Tools
PDF
On This Page