
Arduino Quick Start
APIs and example programs for reading battery status on StickC.
#include "M5Unified.h"
void setup()
{
M5.begin();
Serial.begin(115200);
M5.Display.setRotation(1);
M5.Display.setTextColor(TFT_GREEN);
M5.Display.setTextDatum(middle_center);
M5.Display.setTextFont(&fonts::FreeMonoBold9pt7b);
M5.Display.setTextSize(1);
M5.Display.setTextWrap(false);
}
void loop()
{
M5.Display.clear();
bool isCharging = M5.Power.isCharging();
int vol_per = M5.Power.getBatteryLevel();
int vol = M5.Power.getBatteryVoltage();
const int x = M5.Display.width() / 2;
const int y1 = M5.Display.height() / 6;
const int y2 = M5.Display.height() / 2;
const int y3 = M5.Display.height() * 5 / 6;
char levelText[20];
char voltageText[24];
snprintf(levelText, sizeof(levelText), "BAT: %d%%", vol_per);
if (vol > 0) {
snprintf(voltageText, sizeof(voltageText), "VOLT: %dmV", vol);
} else {
snprintf(voltageText, sizeof(voltageText), "VOLT: --");
}
M5.Display.drawString(isCharging ? "CHG: Yes" : "CHG: No", x, y1);
M5.Display.drawString(levelText, x, y2);
M5.Display.drawString(voltageText, x, y3);
delay(2000);
}
The program displays the battery charging state, battery percentage, and battery voltage in mV, refreshing approximately every 2 seconds. If the battery voltage reading is invalid, it displays VOLT: --.
The StickC power functionality uses Power_Class from the M5Unified library. For more related APIs, refer to the documentation below: