pdf-icon

Arduino Quick Start

2. Devices & Examples

6. Applications

Station Battery

Station battery related APIs and sample programs to read status.

Example

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
#include "M5Unified.h" void setup() { auto cfg = M5.config(); M5.begin(cfg); M5.Display.setTextDatum(middle_center); M5.Display.setTextColor(TFT_BLACK); M5.Display.setTextFont(&fonts::FreeSansOblique12pt7b); M5.Display.setTextSize(1); } void loop() { M5.Display.clear(TFT_WHITE); bool isCharging = M5.Power.isCharging(); int vol_per = M5.Power.getBatteryLevel(); int vol = M5.Power.getBatteryVoltage(); int cur = M5.Power.getBatteryCurrent(); M5.Display.setCursor(0, 30); M5.Display.printf("Charging: %s \n\n", isCharging ? "Yes" : "No"); M5.Display.setCursor(0, 60); M5.Display.printf("Bat_level: %d%%", vol_per); M5.Display.setCursor(0, 90); M5.Display.printf("Bat_voltage: %d%mV", vol); M5.Display.setCursor(0, 120); M5.Display.printf("Bat_current: %d%mA", cur); delay(2000); } 

This program displays charging status, battery percentage, voltage, and current on screen, refreshing every 2 seconds.

API

Station power section uses the Power_Class from the M5Unified library. For more related APIs, refer to the documentation below: