
Arduino Quick Start
APIs and example programs related to Chain DualKey Power management.
#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: