Arduino Quick Start
APIs and example program for Air Quality sleep and wakeup functionality.
#include <M5Unified.h>
#include <esp_sleep.h>
void setup() {
auto cfg = M5.config();
M5.begin(cfg); // Initialize display, buttons, RTC, etc.
M5.Display.fillScreen(TFT_WHITE);
M5.Display.setTextSize(2);
M5.Display.setTextColor(TFT_BLACK);
M5.Display.setTextDatum(middle_center);
esp_sleep_wakeup_cause_t cause = esp_sleep_get_wakeup_cause();
int cx = M5.Display.width() / 2;
int cy = M5.Display.height() / 2;
if (cause == ESP_SLEEP_WAKEUP_TIMER) {
M5.Display.drawString("Woken up!", cx, cy - 20);
M5.Display.drawString("Press BtnA", cx, cy + 20);
} else {
M5.Display.drawString("Press BtnA", cx, cy - 20);
M5.Display.drawString("to sleep 5s", cx, cy + 20);
}
}
void loop() {
M5.update(); // Refresh button state
// Press BtnA to enter 5-second timed sleep
if (M5.BtnA.wasPressed()) {
int cx = M5.Display.width() / 2;
int cy = M5.Display.height() / 2;
M5.Display.fillScreen(TFT_WHITE);
M5.Display.setTextDatum(middle_center);
M5.Display.drawString("Sleeping...", cx, cy);
delay(500);
M5.Power.timerSleep(5); // Wake up after 5 seconds
}
}
After uploading, press the button to see the following effects:
The power section uses the Power_Class
from the M5Unified library. For more related APIs, refer to the following documentation: