English
English
简体中文
日本語

Arduino Quick Start

2. Devices & Examples

5. Extensions

6. Applications

CoreP4X Battery Status

APIs and example programs related to the battery and charging status of CoreP4X.

Example Program

Compilation Requirements

  • M5Stack Board Manager version >= 3.3.10-a
  • Board selection = M5CoreP4X
  • M5Unified library version >= 0.3.1
  • M5GFX library version >= 0.3.1

CoreP4X samples the battery and VBUS voltage through M5PM1. The charging status is read from M5IOE1 IO6 (CHG_STAT); low indicates charging. The charge current level is controlled by M5PM1 PYG1: 180mA by default, and 650mA when the output is low.

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 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117
#include <M5Unified.h>

static const char* chargeStateText(int state)
{
  // 0: not charging, 1: charging, 2: unknown
  switch (state) {
    case 0: return "Not charging";
    case 1: return "Charging";
    default: return "Unknown";
  }
}

static int readChargeLimit(void)
{
  uint8_t gpioBits = 0;
  if (!M5.Power.M5pm1.getGPIOInputBits(&gpioBits)) {
    return 0;
  }

  // PYG1 high-Z: 180mA / PYG1 low: 650mA
  return (gpioBits & (1U << 1)) ? 180 : 650;
}

static void drawBattery(int level, int voltageMv, int vbusMv,
                        int chargeMa, int state)
{
  const bool charging = state == 1;
  const int centerX = M5.Display.width() / 2;

  M5.Display.startWrite();
  M5.Display.fillScreen(TFT_BLACK);
  M5.Display.setTextDatum(middle_center);
  M5.Display.setFont(&fonts::FreeSansBold24pt7b);
  M5.Display.setTextColor(TFT_WHITE, TFT_BLACK);
  M5.Display.drawString("Battery Status", centerX, 48);

  // The body and terminal together are centered on the 480 x 480 display.
  M5.Display.drawRect(151, 96, 160, 78, TFT_WHITE);
  M5.Display.fillRect(315, 120, 14, 30, TFT_WHITE);

  const int width = constrain(level, 0, 100) * 154 / 100;
  if (width > 0) {
    M5.Display.fillRect(155, 100, width, 70, charging ? TFT_GREEN : TFT_WHITE);
  }

  if (charging && (millis() / 400) % 2 == 0) {
    M5.Display.drawRect(145, 90, 190, 90, TFT_GREEN);
  }

  char line[48];
  M5.Display.setFont(&fonts::FreeSans18pt7b);
  snprintf(line, sizeof(line), "Level: %d%%", level);
  M5.Display.drawString(line, centerX, 210);
  snprintf(line, sizeof(line), "Voltage: %d mV", voltageMv);
  M5.Display.drawString(line, centerX, 258);
  snprintf(line, sizeof(line), "VBUS: %d mV", vbusMv);
  M5.Display.drawString(line, centerX, 306);

  if (chargeMa > 0) {
    snprintf(line, sizeof(line), "Charge limit: %d mA", chargeMa);
  } else {
    snprintf(line, sizeof(line), "Charge limit: Unknown");
  }
  M5.Display.setTextColor(TFT_CYAN, TFT_BLACK);
  M5.Display.drawString(line, centerX, 354);

  snprintf(line, sizeof(line), "State: %s", chargeStateText(state));
  M5.Display.setTextColor(charging ? TFT_GREEN : TFT_WHITE, TFT_BLACK);
  M5.Display.drawString(line, centerX, 402);
  M5.Display.endWrite();
}

void setup()
{
  auto cfg = M5.config();
  cfg.serial_baudrate = 115200;
  M5.begin(cfg);

  M5.Display.setBrightness(128);

  const int level = M5.Power.getBatteryLevel();
  const int voltageMv = M5.Power.getBatteryVoltage();
  const int vbusMv = M5.Power.getVBUSVoltage();
  const int chargeMa = readChargeLimit();
  const int state = static_cast<int>(M5.Power.isCharging());
  drawBattery(level, voltageMv, vbusMv, chargeMa, state);
}

void loop()
{
  M5.update();

  static uint32_t lastRefresh = 0;
  const uint32_t now = millis();
  if (now - lastRefresh >= 1000) {
    lastRefresh = now;

    const int level = M5.Power.getBatteryLevel();
    const int voltageMv = M5.Power.getBatteryVoltage();
    const int vbusMv = M5.Power.getVBUSVoltage();
    const int chargeMa = readChargeLimit();
    // Charge status is read from M5IOE1 IO6.
    const int state = static_cast<int>(M5.Power.isCharging());

    drawBattery(level, voltageMv, vbusMv, chargeMa, state);
    Serial.printf("Battery: %d mV, level: %d%%, state: %s\n",
                  voltageMv, level, chargeStateText(state));
    Serial.printf("VBUS: %d mV, charge limit: ", vbusMv);
    if (chargeMa > 0) {
      Serial.printf("%d mA\n", chargeMa);
    } else {
      Serial.println("unknown");
    }
  }

  delay(20);
}

The screen shows the battery icon, remaining level, battery voltage, VBUS voltage, the current charge current limit level, and the charging state; while charging, the frame blinks and the level bar is displayed in green. The serial monitor also outputs the battery voltage, level, charge level, charging state, and VBUS voltage.

Note
The charge level here comes from the current selection of M5PM1 PYG1 and represents the charge current limit setting, not a real-time measurement of the charging current.

API

The battery status section of CoreP4X uses the Power_Class in the M5Unified library. For more related APIs, refer to the documentation below:

Page Tools
PDF
On This Page