pdf-icon

Arduino Quick Start

2. Devices & Examples

6. Applications

Cardputer Battery Status

Cardputer battery status-related APIs and example programs, applicable to both Cardputer and Cardputer-Adv.

Example Program

Compilation Requirements

  • M5Stack board manager version >= 3.2.3
  • Board selection = M5Cardputer
  • M5Cardputer library version >= 1.1.1
  • M5Unified library version >= 0.2.10
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
#include <M5Cardputer.h>

void setup() {
  M5Cardputer.begin();
  M5Cardputer.Display.setFont(&fonts::FreeMonoBold9pt7b);
  M5Cardputer.Display.setCursor(0, 0);

  M5Cardputer.Display.print("   Cardputer (-Adv)\n");
  M5Cardputer.Display.print("    Battery Status\n\n");
  M5Cardputer.Display.print("  Percent:\n");
  M5Cardputer.Display.print("  Voltage:\n");
}

void loop() {
  M5Cardputer.update();

  bool isCharging = M5Cardputer.Power.isCharging();
  int batteryLevel = M5Cardputer.Power.getBatteryLevel();      // 0 - 100 %
  int batteryVoltage = M5Cardputer.Power.getBatteryVoltage();  // unit: mV

  M5Cardputer.Display.setCursor(120, 55);
  M5Cardputer.Display.printf("%3d %%", batteryLevel);
  M5Cardputer.Display.setCursor(120, 72);
  M5Cardputer.Display.printf("%4d mV", batteryVoltage);

  delay(1000);
}

The program will display the battery percentage and battery voltage on the screen, refreshing once per second.

Due to hardware limitations, Cardputer and Cardputer-Adv cannot read battery charging status or battery current information.
Please note that when connecting the Cardputer or Cardputer-Adv to a computer or power source, the switch on the upper side must be turned on to enable charging. Otherwise, the battery will be disconnected and the device will run on external power.

API

The Cardputer Battery status driver uses the Power_Class from the M5Unified library. For more related APIs, refer to the document below:

On This Page