
Arduino Quick Start
APIs and example programs related to timed sleep and wakeup of CoreP4X.
3.3.10-aM5CoreP4X0.3.10.3.1CoreP4X uses the ESP32-P4 chip. M5Unified does not support M5.Power.lightSleep() on the ESP32-P4; calling it reports unsupported and returns immediately.
This example provides two sleep entries:
M5.Power.timerSleep(): uses the M5Unified timed sleep flow; wakes up after 5 seconds and restarts.M5.Power.deepSleep(): enters ESP32-P4 deep sleep directly and wakes up using a 5-second ESP timer.Both methods reset on wakeup and run again from setup().
#include <M5Unified.h>
static constexpr int SLEEP_SECONDS = 5;
static constexpr uint64_t SLEEP_TIME_US = 5000000ULL;
M5Canvas canvas(&M5.Display);
static void drawCard(int y, const char* title, const char* detail, uint16_t color)
{
canvas.fillRoundRect(60, y, 360, 96, 16, color);
canvas.drawRoundRect(60, y, 360, 96, 16, TFT_WHITE);
canvas.setTextDatum(middle_center);
canvas.setTextColor(TFT_WHITE, color);
canvas.setFont(&fonts::FreeSansBold18pt7b);
canvas.drawString(title, 240, y + 36);
canvas.setFont(&fonts::FreeSans12pt7b);
canvas.drawString(detail, 240, y + 70);
}
static void drawScreen(const char* status)
{
canvas.fillSprite(TFT_BLACK);
canvas.setTextDatum(middle_center);
canvas.setTextColor(TFT_WHITE, TFT_BLACK);
canvas.setFont(&fonts::FreeSansBold24pt7b);
canvas.drawString("Wakeup", 240, 52);
canvas.setFont(&fonts::FreeSans12pt7b);
canvas.setTextColor(TFT_CYAN, TFT_BLACK);
canvas.drawString("Select sleep mode", 240, 112);
drawCard(164, "Timer Sleep 5s", "M5.Power.timerSleep", TFT_BLUE);
drawCard(298, "Deep Sleep 5s", "M5.Power.deepSleep", TFT_DARKGREEN);
canvas.setTextColor(TFT_ORANGE, TFT_BLACK);
canvas.setFont(&fonts::FreeSans12pt7b);
canvas.drawString(status, 240, 426);
canvas.setTextColor(TFT_DARKGREY, TFT_BLACK);
canvas.setFont(&fonts::FreeSans9pt7b);
canvas.drawString("Tap a card to start", 240, 456);
canvas.pushSprite(0, 0);
}
void setup()
{
auto cfg = M5.config();
cfg.serial_baudrate = 115200;
M5.begin(cfg);
M5.Display.setBrightness(128);
canvas.createSprite(M5.Display.width(), M5.Display.height());
drawScreen("Waiting for input");
}
void loop()
{
M5.update();
if (!M5.Touch.getDetail().wasClicked()) {
delay(20);
return;
}
const auto touch = M5.Touch.getDetail();
if (touch.y >= 164 && touch.y <= 260) {
drawScreen("Starting timer sleep");
Serial.println("Starting timer sleep");
delay(100);
// timerSleep returns only when the sleep request fails.
if (!M5.Power.timerSleep(SLEEP_SECONDS)) {
drawScreen("Timer sleep failed");
Serial.println("Timer sleep failed");
}
} else if (touch.y >= 298 && touch.y <= 394) {
drawScreen("Starting deep sleep");
Serial.println("Starting deep sleep");
delay(100);
// Deep sleep wakes after 5 seconds and restarts the program.
M5.Power.deepSleep(SLEEP_TIME_US, false);
drawScreen("Deep sleep returned");
Serial.println("Deep sleep returned without reset");
}
delay(20);
}After uploading, the screen displays two sleep entries. Tap Timer Sleep 5s to call M5.Power.timerSleep(), and tap Deep Sleep 5s to call M5.Power.deepSleep(). Both methods restart after 5 seconds and display the sleep menu again.
The sleep and wakeup section of CoreP4X uses the Power_Class in the M5Unified library. For more APIs, refer to the documentation below: