pdf-icon

Arduino Quick Start

2. Devices & Examples

6. Applications

Chain DualKey Power

APIs and example programs related to Chain DualKey Power management.

Example Program

Build Requirements

  • M5Stack Board Manager version ≥ 3.2.4
  • Board option = M5ChainDualKey
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
#define PIN_ADC_CHRG 9
#define PIN_ADC_BATT 10
#define PIN_ADC_VBUS 2

float chgVoltage;
float batVoltage;
float usbVoltage;

void setup() {
  pinMode(PIN_ADC_CHRG, INPUT);
  pinMode(PIN_ADC_BATT, INPUT);
  pinMode(PIN_ADC_VBUS, INPUT);

  Serial.begin(115200);
}

void loop() {
  chgVoltage = analogRead(PIN_ADC_CHRG) / 4095.0 * 3.3;
  batVoltage = analogRead(PIN_ADC_BATT) / 4095.0 * 3.3 * 1.51;
  usbVoltage = analogRead(PIN_ADC_VBUS) / 4095.0 * 3.3 * 1.51;

  if (chgVoltage >= 1.4 && chgVoltage <= 1.8) {
    Serial.println("Battery is charging");
  } else if (chgVoltage > 1.8 && chgVoltage <= 2.4) {
    Serial.println("Battery is full");
  } else if (chgVoltage > 3.0) {
    Serial.println("Battery is not charging");
  } else {
    Serial.println("Battery charging status is unknown");
  }

  Serial.printf("Battery voltage: %.4f V\n", batVoltage);  // Unit: V
  Serial.printf("    USB voltage: %.4f V\n", usbVoltage);  // Unit: V

  Serial.println();
  delay(1000);
}

This program detects the battery charging status, battery voltage, and USB input voltage and outputs them to the serial port monitor once per second:

About Battery Charging
As long as an external power source is connected, the battery will charge regardless of the switch position.
On This Page