---
title: "Sensors"
canonical_url: https://developer.repebble.com/pebbleos/docs/samples/drivers/sensor/
last_updated: 2026-12-03
sdk_version: 4.44
---
# Sensors

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

This sample walks every device of the [sensor class](https://developer.repebble.com/pebbleos/docs/drivers/sensor.md) with `pbl_sensor_next()`, and reads each type it measures once with `pbl_sensor_read()`. `PBL_SENSOR_SPEC()` of just the type asks for the whole channel: the X, Y and Z of a vector, in the device frame, or the value of a scalar. Values come in SI units times a multiplier the sample picks so that each prints with 3 decimals: mm/s² for m/s², nT for µT, Pa for kPa.

It enables `CONFIG_SENSOR` and no role, so it gets the driver of every sensor the board’s devicetree enables, whether the firmware uses it or not, and runs on boards with any mix of them. On the emulator it reads the simulated accelerometer, die temperature sensor and magnetometer, the last enabled in its `boards/qemu_emery.conf`: the emulator boards leave it off, having no magnetometer.

It then turns the console prompt on, with the `sensor` command of the [shell](https://developer.repebble.com/pebbleos/docs/subsystems/shell.md), `CONFIG_SENSOR_SHELL`, to drive the rest of the class by hand: attributes and the values they take, events, a flush, and the batches and events of a sensor printed as they come.

```
> sensor list
0: lsm6dso +shake
  accelerometer x 0
  accelerometer y 0
  accelerometer z 0
> sensor avail 0 accelerometer sampling-frequency
12500 26000 52000 104000 208000
> sensor stream 0 on
> sensor attr 0 accelerometer sampling-frequency 26000
26000
> sensor attr 0 accelerometer report-latency 500000
499993
accelerometer: 13 frames 38461 us apart, at 5046966 us: x=1.710 y=3.998 z=-8.867 m/s2
accelerometer: 13 frames 38483 us apart, at 5548095 us: x=1.721 y=4.105 z=-8.816 m/s2
> sensor event 0 shake on
shake: +z at 6714569 us
> sensor attr 0 accelerometer sampling-frequency 0
0
> sensor stream 0 off
```

Run:

`pbl samples samples/drivers/sensor`

Boards:

every board

## Sources

src/main.c

```
#include \<errno.h\>#include \<stdint.h\>#include \<stdio.h\>#include \<stdlib.h\>#include \<pbl/console/console.h\>#include \<pbl/drivers/sensor.h\>static const char \*const s\_types[PBL\_SENSOR\_TYPE\_COUNT] = {[PBL\_SENSOR\_ACCEL] = "accelerometer",[PBL\_SENSOR\_ANGL\_VEL] = "gyroscope",[PBL\_SENSOR\_MAGN] = "magnetometer",[PBL\_SENSOR\_TEMP] = "thermometer",[PBL\_SENSOR\_PRESSURE] = "barometer"};/\* Values are asked for at 1000 times the unit printed, which gets 3 decimals. \*/static const struct {const char \*unit;uint32\_t scale;} s\_units[PBL\_SENSOR\_TYPE\_COUNT] = {[PBL\_SENSOR\_ACCEL] = {"m/s2", 1000U},[PBL\_SENSOR\_ANGL\_VEL] = {"rad/s", 1000U},[PBL\_SENSOR\_MAGN] = {"uT", 1000000000U},[PBL\_SENSOR\_TEMP] = {"C", 1000U},[PBL\_SENSOR\_PRESSURE] = {"kPa", 1U}};static const char \*prv\_milli(char \*buf, size\_t size, int32\_t val) {(void)snprintf(buf, size, "%s%u.%03u", val \< 0 ? "-" : "", (unsigned int)(llabs(val) / 1000),(unsigned int)(llabs(val) % 1000));return buf;}static void prv\_print\_values(const char \*name, int type, const int32\_t \*val) {char x[16], y[16], z[16];if (pbl\_sensor\_num\_values(PBL\_SENSOR\_SPEC(type)) == 3U) {printf("%s: %s: x=%s y=%s z=%s %s\n", name, s\_types[type], prv\_milli(x, sizeof(x), val[0]),prv\_milli(y, sizeof(y), val[1]), prv\_milli(z, sizeof(z), val[2]), s\_units[type].unit);} else {printf("%s: %s: %s %s\n", name, s\_types[type], prv\_milli(x, sizeof(x), val[0]),s\_units[type].unit);}}static void prv\_read(const struct pbl\_sensor \*sensor, enum pbl\_sensor\_type type) {const char \*name = pbl\_sensor\_get\_name(sensor);int32\_t val[3];int ret;/\* No modifier: the whole vector of a vector type, the plain channel of a scalar one. \*/ret = pbl\_sensor\_read(sensor, PBL\_SENSOR\_SPEC(type), s\_units[type].scale, val, nullptr);/\* A saturated value is still a value. \*/if (ret != 0 && ret != -ERANGE) {fprintf(stderr, "%s: %s: read failed (%d)\n", name, s\_types[type], ret);return;}prv\_print\_values(name, type, val);}int main(void) {const struct pbl\_sensor \*sensor = nullptr;unsigned int count = 0U;/\* Every sensor the board describes, whether a role points at it or not. \*/while ((sensor = pbl\_sensor\_next(sensor)) != nullptr) {count++;if (!pbl\_sensor\_is\_ready(sensor)) {printf("%s: not ready\n", pbl\_sensor\_get\_name(sensor));continue;}for (int t = 0; t \< PBL\_SENSOR\_TYPE\_COUNT; t++) {if (pbl\_sensor\_has\_type(sensor, t)) {prv\_read(sensor, t);}}}printf("%u sensors\n", count);/\* The sensor shell command does the rest by hand. \*/pbl\_console\_enable\_prompt();printf("Sample done\n");return 0;}
```

## Output

```
\w+: (accelerometer|gyroscope|magnetometer|thermometer|barometer): 
\d+ sensors
Sample done
```

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