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?
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?
Hi,
Unfortunately, the ble_app_hids_keyboard example does not actually measure the battery voltage. It pass a simulated value to the battery service, see battery_level_update(). The ble_app_proximity example implements actual battery voltage measurement, you can use that as reference. Note that the proximity example use VDD as input. If you use a voltage divider, you should change the input to one of AIN0-AIN7 in parameter to NRF_DRV_SAADC_DEFAULT_CHANNEL_CONFIG_SE in adc_configure().
Best regards,
Jørgen
What voltage are you applying to the pin? The example will report 0 percent battery if measured voltage + 270 mV is below 2100 mV.
Hi Jorgen,
Thank you for your reply. I'm applying 3.3V to one of the pins of potentiometer. By varying the resistance, I get a voltage range from 0V to 3.3V which is applied to AIN0. After this is successful, I would like to try it with my battery. But the battery service is constantly stuck at 0 reading.
Did you enable notifications for the battery service? The timer that triggers SAADC sampling is not started until the BLE_BAS_EVT_NOTIFICATION_ENABLED event is received in on_bas_evt().
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?
The function will return a battery percentage from a given input voltage. This is a intended to represent a typical discharge curve for a CR2032 battery. You need to modify the calculations to match your board and battery. You mention that you use voltage divider/potentiometer. Are you sure that the input to the pin after this voltage regulation is high enough to be above 2.1 V?
Yes the voltage goes above 2.1V. I think thats why I notice some changes in hex values. But dont you think they are pretty low? I mean of course I have to modify the above function but stil.
Yes the voltage goes above 2.1V. I think thats why I notice some changes in hex values. But dont you think they are pretty low? I mean of course I have to modify the above function but stil.
If you are seeing changing battery percentage and the measured voltage is not high above 2.1 V, it should be correct. You need to measure what you get in and modify the formula to fit your needs.