This post is older than 2 years and might not be relevant anymore
More Info: Consider searching for newer posts

Monitor battery charge in hid keyboard example

The hid keyboard example includes the battery service. But how does it work? I would like to monitor the battery charge. After I use the voltage divider, to which AIN pin should I connect? Where will the battery charge be displayed?

Parents Reply
  • Hi Jorgen,

    The code worked when I tried to increase the range from 0V - 5V.

    What are these hex values 06, 01, ...

    I think I should modify the below function

    static __INLINE uint8_t battery_level_in_percent(const uint16_t mvolts)
    {
        uint8_t battery_level;
    
        if (mvolts >= 3000)
        {
            battery_level = 100;
        }
        else if (mvolts > 2900)
        {
            battery_level = 100 - ((3000 - mvolts) * 58) / 100;
        }
        else if (mvolts > 2740)
        {
            battery_level = 42 - ((2900 - mvolts) * 24) / 160;
        }
        else if (mvolts > 2440)
        {
            battery_level = 18 - ((2740 - mvolts) * 12) / 300;
        }
        else if (mvolts > 2100)
        {
            battery_level = 6 - ((2440 - mvolts) * 6) / 340;
        }
        else
        {
            battery_level = 0;
        }
    
        return battery_level;
    }

    Can you tell me how are the calculations made?

Children
Related