Battery measurement module¶
Use the Battery measurement module to periodically measure the battery voltage and send the
battery_level_event event that informs about the current battery level.
See the Overview: Firmware architecture for more information about the event-based communication in the nRF Desktop application and about how to read this table.
The Battery measurement module uses Zephyr’s ADC driver to measure voltage. For this reason, set the following options:
CONFIG_ADC- The module’s implementation uses Zephyr ADC driver.
CONFIG_ADC_ASYNC- The module’s implementation uses asynchronous calls.
CONFIG_ADC_NRFX_SAADC- The module’s implementation uses nrfx SAADC driver for nRF52 MCU series.
CONFIG_DESKTOP_BATTERY_MEAS to enable the module.
Also, make sure to define
CONFIG_DESKTOP_BATTERY_MEAS_POLL_INTERVAL_MS, that is the amount of time between the subsequent battery measurements (in ms).
If the measurement circuit contains a voltage divider made of two resistors, set the
The following defines specify the values of these two resistors (in kOhm):
CONFIG_DESKTOP_BATTERY_MEAS_VOLTAGE_DIVIDER_UPPER- Upper resistor.
CONFIG_DESKTOP_BATTERY_MEAS_VOLTAGE_DIVIDER_LOWER- Lower resistor.
The module measures the voltage over the lower resistor.
The battery voltage is converted to the state of charge (in percentage level units) according to the lookup table defined in the
You can find this file the in board-specific directory in the application configuration directory.
Remember to also define the following battery parameters (otherwise, the default values will be used):
CONFIG_DESKTOP_BATTERY_MEAS_MIN_LEVEL- Battery voltage in mV that corresponds to the 0% battery level.
CONFIG_DESKTOP_BATTERY_MEAS_MAX_LEVEL- Battery voltage in mV that corresponds to the 100% battery level.
CONFIG_DESKTOP_VOLTAGE_TO_SOC_DELTA- Difference in mV between the adjacent elements in the conversion lookup table.
If a pin is used to enable the battery measurement, enable the
The number of the pin used for this purpose must be defined as
The implementation uses the
Because Zephyr’s GPIO driver is used to control this pin, also set the
k_work_delayable starts the asynchronous voltage measurement and resubmits itself.
When it is processed the next time, it reads the measured voltage.
This read sequence is repeated periodically.
When the system goes to the low-power state, the work is canceled.
During the first voltage readout, the Analog to Digital Converter is automatically calibrated to increase the measurement accuracy.