Cellular: Battery

The Battery sample demonstrates how to obtain the following battery related information from the modem of an nRF91 Series DK:

  • Modem battery voltage

  • Modem battery voltage low level notifications

  • Power-off warnings (modem firmware v1.3.1 and higher)

The sample uses the Modem battery library.

Requirements

The sample supports the following development kits:

Hardware platforms

PCA

Board name

Board target

nRF9161 DK

PCA10153

nrf9161dk

nrf9161dk/nrf9161/ns

nRF9160 DK

PCA10090

nrf9160dk

nrf9160dk/nrf9160/ns

nRF9151 DK

PCA10171

nrf9151dk

nrf9151dk/nrf9151/ns

When built for a board target with the */ns variant, the sample is configured to compile and run as a non-secure application with Cortex-M Security Extensions enabled. Therefore, it automatically includes Trusted Firmware-M that prepares the required peripherals and secure services to be available for the application.

Overview

The sample first calls the modem_battery_low_level_handler_set() and modem_battery_pofwarn_handler_set() functions to set the respective handlers, and then sets up the workqueues used to handle the low level battery voltage notifications and the power-off warning notifications. The sample then initializes the Modem library. Next, it enters a state machine that measures the modem battery voltage at every iteration and then executes an activity based on that. The first activity (init_activity()) sets the modem to receive-only mode if the battery voltage drops below the low level threshold or to normal mode if it rises above the threshold and updates the state accordingly.

Then, depending on the modem’s battery voltage, the modem switches mode according to the following conditions:

  • If the modem is in normal mode and the battery voltage drops below the low level threshold, the modem switches to receive-only mode.

  • If the modem is in receive-only mode and the battery voltage is below the low level threshold, the application periodically checks the battery voltage and switches state if it rises above the low level threshold.

  • If the modem is in receive-only mode and the battery voltage is above the low level threshold, the application executes a connectivity evaluation and if the conditions are either normal (7), good (8), or excellent (9), the modem switches to normal mode. When connectivity conditions are poor, the number of packets re-transmitted is higher thus causing an increase in power consumption, which must be avoided when battery voltage is not at an adequately safe level. Full connectivity is recommended when battery voltage is at a sufficient level.

  • If the modem is in normal mode and the battery voltage is above the low level threshold, the application executes some IP traffic (DNS lookup) to maintain an active LTE connection. The modem performs background monitoring of the battery voltage and sends a notification of low level in case it drops below the set threshold.

  • If the modem battery voltage drops below the power-off warning level, the modem is automatically set offline by the hardware.

  • If the modem battery voltage rises above the power-off warning level, the modem is set to receive-only mode and initialized.

Configuration

See Configuring and building for information about how to permanently or temporarily change the configuration.

Configuration options

Check and configure the following configuration options for the sample:

CONFIG_HIGH_BAT_REFRESH_INTERVAL_SEC

Sets the refresh interval (seconds) when the battery voltage is high.

CONFIG_LOW_BAT_REFRESH_INTERVAL_SEC

Sets the refresh interval (seconds) when the battery voltage is low.

CONFIG_POFWARN_REFRESH_INTERVAL_SEC

Sets the refresh interval (seconds) after receiving power-off warning.

Building and running

This sample can be found under samples/cellular/battery in the nRF Connect SDK folder structure.

When built as firmware image for a board target with the */ns variant, the sample has Cortex-M Security Extensions (CMSE) enabled and separates the firmware between Non-Secure Processing Environment (NSPE) and Secure Processing Environment (SPE). Because of this, it automatically includes the Trusted Firmware-M (TF-M). To read more about CMSE, see Processing environments.

To build the sample, follow the instructions in Building an application for your preferred building environment. See also Programming an application for programming steps and Testing and optimization for general information about testing and debugging in the nRF Connect SDK.

Note

When building repository applications in the SDK repositories, building with sysbuild is enabled by default. If you work with out-of-tree freestanding applications, you need to manually pass the --sysbuild parameter to every build command or configure west to always use it.

Testing

After programming the sample to your development kit, complete the following steps to test it:

  1. Connect the kit to the computer using a USB cable. The kit is assigned a COM port (Windows) or ttyACM device (Linux), which is visible in the Device Manager.

  2. Connect to the kit with a terminal emulator (for example, nRF Connect Serial Terminal). See Testing and optimization for the required settings and steps.

  3. Connect the nRF91 Series DK to the Power Profiler Kit II (PPK2) and set up for external power output.

  4. Install the Power Profiler app in the nRF Connect for Desktop.

  5. Connect the Power Profiler Kit II (PPK2) to the PC using a micro-USB cable and connect to it using the App.

  6. Power on or reset your nRF91 Series DK.

  7. Observe that the sample starts, initializes the modem, and changes functional mode depending on the external voltage.

Sample output

The sample shows the following output when battery voltage is high:

Battery sample started
Initializing modem library
Battery voltage: 5019
Setting modem to normal mode...
Normal mode set.
Initializing modem and connecting...
Connected.
Battery voltage: 4977
Executing DNS lookup for 'example.com'...
Battery voltage: 5015
Executing DNS lookup for 'google.com'...
Battery voltage: 4989
Executing DNS lookup for 'apple.com'...
Battery voltage: 4977
Executing DNS lookup for 'amazon.com'...
Battery voltage: 5019
Executing DNS lookup for 'microsoft.com'...

The sample shows the following output when battery voltage drops from a high value to a low value:

Battery voltage: 3191
Executing DNS lookup for 'google.com'...
Battery voltage: 3195
Executing DNS lookup for 'apple.com'...
Battery voltage: 3191
Executing DNS lookup for 'amazon.com'...
Battery voltage: 3195
Executing DNS lookup for 'microsoft.com'...
Battery voltage: 3195
Executing DNS lookup for 'example.com'...
Battery low level: 3191
Battery low level: 3195
Battery low level: 3253
Battery voltage: 3250
Setting modem to RX only mode...
RX only mode set.
Initializing modem and connecting...
Battery low level: 3191
Connected.
Battery low level: 3195

The sample shows the following output when battery voltage rises from a low value to a high value:

Battery low level: 3214
Battery voltage: 3421
Battery voltage: 3421
Energy estimate: 8
Setting modem to normal mode...
Normal mode set.
Battery voltage: 3457
Executing DNS lookup for 'google.com'...
Battery voltage: 3421
Executing DNS lookup for 'apple.com'...
Battery voltage: 3421
Executing DNS lookup for 'amazon.com'...

The sample shows the following output when battery voltage drops to a very low value:

Battery voltage: 3175
Battery voltage: 3175
Modem Event Battery LOW:
******************************************************************
* Attention! Do not attempt to write to NVM while in this state. *
* The NVM operation will sometimes appear to finish successfully *
* without actually being executed at all.                        *
* The modem has been set to Offline.                             *
******************************************************************

Dependencies

This sample uses the following nRF Connect SDK libraries:

It uses the following sdk-nrfxlib library:

In addition, it uses the following secure firmware component: