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.

Module events

Source Module

Input Event

This Module

Output Event

Sink Module

Power manager module

power_down_event

battery_meas

Source modules for module_state_event

module_state_event

Buttons module

wake_up_event

HID forward module

Motion module

Power manager module

Wheel module

battery_level_event

GATT Battery Service module

module_state_event

Sink modules for module_state_event

Note

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.

Configuration

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.

Set 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 CONFIG_DESKTOP_BATTERY_MEAS_HAS_VOLTAGE_DIVIDER option. 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 battery_def.h file. 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 CONFIG_DESKTOP_BATTERY_MEAS_HAS_ENABLE_PIN option. The number of the pin used for this purpose must be defined as CONFIG_DESKTOP_BATTERY_MEAS_ENABLE_PIN. The implementation uses the GPIO0 port. Because Zephyr’s GPIO driver is used to control this pin, also set the CONFIG_GPIO option.

Implementation details

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.