Skip to content

Check battery of Mao King Unit

Yihui Xiong edited this page Oct 27, 2017 · 1 revision

The Mao King Unit has build-in battery. We can use the microcontroller ATmega32U4 of the respeaker core to read battery voltage and charging status.

The battery voltage can be read from pin A1 and The charging status is detected by Pin A5 (0 = charging). The example code is like:

begin()
{
    pinMode(A5, INPUT_PULLUP);
    pinMode(A1, INPUT);
}

loop()
{
    int is_charging = 1 - digitalRead(A5);
    int16_t capacity = analogRead(A1) - 460;
    if (capacity > 255) {
        capacity = 255;
    } else if (capacity < 0) {
        capacity = 0;
    }
}

You can also use battery example of the respeaker arduino library. See https://github.com/respeaker/respeaker_arduino_library/blob/master/examples/battery/battery.ino