
Arduino Quick Start
StopWatch power management related APIs and sample programs.
#include <M5Unified.h>
void setup() {
M5.begin();
Serial.begin(115200);
M5.Lcd.setRotation(0);
M5.Lcd.setFont(&fonts::FreeMonoBold12pt7b);
M5.Lcd.setTextDatum(middle_center);
M5.Lcd.drawString("StopWatch Realtime", M5.Lcd.width() / 2, 50);
M5.Lcd.drawString("Battery Status\n", M5.Lcd.width() / 2, 80);
M5.Lcd.setCursor(40, 170);
M5.Lcd.print("Battery Charging:");
M5.Lcd.setCursor(40, 210);
M5.Lcd.print("Battery Level:");
M5.Lcd.setCursor(40, 250);
M5.Lcd.print("Battery Voltage:");
}
void loop() {
M5.update();
bool isCharging = M5.Power.isCharging();
int32_t batteryLevel = M5.Power.getBatteryLevel(); // 0 - 100 %
int16_t batteryVoltage = M5.Power.getBatteryVoltage(); // unit: mV
M5.Lcd.setCursor(290, 170);
M5.Lcd.printf("%s \n", isCharging ? "Yes" : "No");
M5.Lcd.setCursor(290, 210);
M5.Lcd.printf("%d %%\n", batteryLevel);
M5.Lcd.setCursor(290, 250);
M5.Lcd.printf("%d mV\n", batteryVoltage);
delay(2000);
}This program displays whether the battery is charging, the battery level percentage, and the battery voltage on the screen, refreshing every 2 seconds. Due to hardware limitations, StopWatch cannot read the battery current.
The StopWatch battery status section uses the Power_Class from the M5Unified library. For more related APIs, refer to the following document: