---
title: "Battery"
canonical_url: https://developer.repebble.com/pebbleos/docs/samples/drivers/battery/
last_updated: 2026-11-17
sdk_version: 4.44
---
# Battery

Source: https://developer.repebble.com/pebbleos/docs/samples/drivers/battery/

This sample reads the battery three times, a second apart: its voltage, current and temperature, whether USB power is connected, whether the charger charges and in which phase. It sets a connection handler too, which prints USB power coming or going while it runs.

The battery is read through the charger of the board, its PMIC, which the sample enables in its `boards/` files. Plug or unplug the charging cable while it runs to see the handler and the charge state change.

Run:

`pbl samples samples/drivers/battery`

Boards:

every board

## Sources

src/main.c

```
#include \<stdint.h\>#include \<stdio.h\>#include \<pbl/drivers/battery.h\>#include \<pbl/kernel/delay.h\>#define READS 3static const char \*const s\_status[] = {[BatteryChargeStatusUnknown] = "unknown",[BatteryChargeStatusComplete] = "complete",[BatteryChargeStatusTrickle] = "trickle",[BatteryChargeStatusCC] = "constant current",[BatteryChargeStatusCV] = "constant voltage",};static void prv\_usb\_changed(bool is\_connected) {printf("battery: USB power %s\n", is\_connected ? "connected" : "removed");}int main(void) {battery\_init();/\* Called from a work queue whenever USB power comes or goes. \*/battery\_set\_connection\_handler(prv\_usb\_changed);for (int i = 0; i \< READS; i++) {BatteryConstants c;BatteryChargeStatus status;int ret;ret = battery\_get\_constants(&c);if (ret != 0) {fprintf(stderr, "battery: measurement failed (%d)\n", ret);return ret;}ret = battery\_charge\_status\_get(&status);if (ret != 0) {fprintf(stderr, "battery: charge status failed (%d)\n", ret);return ret;}printf("battery: %d mV, %d uA, %d mC\n", (int)c.v\_mv, (int)c.i\_ua, (int)c.t\_mc);printf("battery: USB %s, %s, charge %s\n",battery\_is\_usb\_connected() ? "connected" : "disconnected",battery\_charge\_controller\_thinks\_we\_are\_charging() ? "charging" : "not charging",s\_status[status]);pbl\_sleep\_ms(1000);}battery\_set\_connection\_handler(nullptr);printf("Sample done\n");return 0;}
```

## Output

```
battery: -?\d+ mV, -?\d+ uA, -?\d+ mC
battery: USB (dis)?connected, (not )?charging, charge [a-z]+
battery: -?\d+ mV, -?\d+ uA, -?\d+ mC
battery: USB (dis)?connected, (not )?charging, charge [a-z]+
battery: -?\d+ mV, -?\d+ uA, -?\d+ mC
battery: USB (dis)?connected, (not )?charging, charge [a-z]+
Sample done
```

Generated from PebbleOS [9e782207f](https://github.com/coredevices/pebbleos/commit/9e782207f53f8f1c02f7b13ea40cdc092d7bab3d). This page is maintained in the pebbleos repository: [docs/samples/drivers/battery.md](https://github.com/coredevices/pebbleos/blob/9e782207f53f8f1c02f7b13ea40cdc092d7bab3d/docs/samples/drivers/battery.md).
