English
English
简体中文
日本語
pdf-icon

Arduino Quick Start

2. Devices & Examples

5. Extensions

6. Applications

StopWatch Power

StopWatch power management related APIs and sample programs.

Example

Compilation Requirements

  • M5Stack Board Manager Version >= 3.3.7
  • Board Option = M5StopWatch
  • M5Unified Library Version >= 0.2.15
  • M5GFX Library Version >= 0.2.21
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 32 33 34 35 36 37
#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.

API

The StopWatch battery status section uses the Power_Class from the M5Unified library. For more related APIs, refer to the following document:

On This Page