KB: Battery level to convert it percentage

Question:

I can measure battery from vbat pin, but when I try to convert it to a percentage, it is not correct.
I use cr2032 do you have an example code about it ?

Answer:

Please refer to ble_bass.c, which supports functions and message handlers including one pertaining to the battery service server and implements this behavior.

void Batt_ServiceAdd_Server(void)
{
    struct bass_db_cfg *db_cfg;
    struct gapm_profile_task_add_cmd *req = KE_MSG_ALLOC_DYN(
        GAPM_PROFILE_TASK_ADD_CMD,
        TASK_GAPM,
        TASK_APP,
        gapm_profile_task_add_cmd,
        sizeof(struct bass_db_cfg));

    /* Fill message */
    req->operation = GAPM_PROFILE_TASK_ADD;
    req->sec_lvl = PERM(SVC_AUTH, DISABLE);
    req->prf_task_id = TASK_ID_BASS;
    req->app_task = TASK_APP;
    req->start_hdl = 0;

    /* Set parameters  */
    db_cfg = (struct bass_db_cfg *)req->param;
    db_cfg->bas_nb = 1;
    db_cfg->features[0] = BAS_BATT_LVL_NTF_SUP;
    db_cfg->batt_level_pres_format[0].description = 0;
    db_cfg->batt_level_pres_format[0].exponent = 0;
    db_cfg->batt_level_pres_format[0].format = 0x4;
    db_cfg->batt_level_pres_format[0].name_space = 1;
    db_cfg->batt_level_pres_format[0].unit = ATT_UNIT_PERCENTAGE;

    /* Send the message */
    ke_msg_send(req);
}

Actually , I’m trying to ask percentage conversion like at below.

if (adc_in_volts >= 3000)
    {
	battLevelPercent = 100;
    }
    else if (adc_in_volts > 2900)
    {
    	battLevelPercent = 100 - ((3000 - adc_in_volts) * 58) / 100;
    }
    else if (adc_in_volts > 2740)
    {
    	battLevelPercent = 42 - ((2900 - adc_in_volts) * 24) / 160;
    }
    else if (adc_in_volts > 2440)
    {
    	battLevelPercent = 18 - ((2740 - adc_in_volts) * 12) / 300;
    }
    else if (adc_in_volts > 2100)
    {
    	battLevelPercent = 6 - ((2440 - adc_in_volts) * 6) / 340;
    }
    else
    {
    	battLevelPercent = 0;
    }

This is not completely available for CR2032 battery. Do you have example like that ? Because as you know that, CR2032 battery voltage curve is not linear. Therefore values will not be correct directly if there is not any linearization.

@ceran.omur

We have not done any sort of CR2032 characterization on our end to help customers with this process.

There are many resources online that provide charts from different battery manufacture displaying their curves, so it would be a matter of quantizing this voltage curve with the mAh at various points to capture the % of mAh that have been used up at each voltage level.

Thank you for using our community forum.