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

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

This sample reads the accelerometer, `ACCEL`, once with `pbl_accel_peek()`, then has it sample every 40 ms for 5 seconds, in batches of up to 10 samples, and counts the shakes and double taps it detects. A listener set with `pbl_accel_set_listener()` gets the batches and events. Batches carry the sensor’s raw samples with how to scale them to milli-g, and the listener runs with the device lock held, so the main thread takes that lock to read what it wrote.

On a watch, move it, shake it or tap it twice while the sample runs. On the emulator the simulator reports the watch lying flat, `z=-1000`, and no events.

Run:

`pbl samples samples/drivers/accel`

Boards:

every board

## Sources

src/main.c

```
#include \<errno.h\>#include \<stdint.h\>#include \<stdio.h\>#include \<pbl/drivers/accel.h\>#include \<pbl/kernel/delay.h\>#include \<pbl/util/math.h\>#define INTERVAL\_US 40000U#define BATCH 10U#define LISTEN\_S 5/\* Written by the listener, which runs with the device lock held. \*/static AccelDriverSample s\_last;static unsigned int s\_samples;static unsigned int s\_shakes;static unsigned int s\_taps;static int16\_t prv\_axis(const AccelRawBatch \*batch, const uint8\_t \*sample, IMUCoordinateAxis axis) {const uint8\_t \*p = sample + batch-\>axis[axis].offset;int16\_t raw = (int16\_t)((uint16\_t)p[0] | ((uint16\_t)p[1] \<\< 8U));return (int16\_t)(batch-\>axis[axis].sign \* (int32\_t)raw \* batch-\>scale\_num / batch-\>scale\_den);}/\* Batches carry the sensor's raw samples, which the batch says how to scale to milli-g. \*/static void prv\_samples(const struct pbl\_accel \*accel, const AccelRawBatch \*batch,void \*user\_data) {const uint8\_t \*last = batch-\>data + (batch-\>num\_samples - 1U) \* batch-\>stride;if (batch-\>num\_samples == 0U) {return;}s\_last.x = prv\_axis(batch, last, AXIS\_X);s\_last.y = prv\_axis(batch, last, AXIS\_Y);s\_last.z = prv\_axis(batch, last, AXIS\_Z);s\_samples += batch-\>num\_samples;}static void prv\_event(const struct pbl\_accel \*accel, const struct pbl\_accel\_event \*event,void \*user\_data) {if (event-\>type == PBL\_ACCEL\_EVENT\_SHAKE) {s\_shakes++;} else if (event-\>type == PBL\_ACCEL\_EVENT\_DOUBLE\_TAP) {s\_taps++;}}static const struct pbl\_accel\_listener s\_listener = {.samples = prv\_samples,.event = prv\_event,};int main(void) {AccelDriverSample now;uint32\_t interval\_us;uint32\_t batch;int ret;ret = pbl\_accel\_peek(ACCEL, &now);if (ret != 0) {fprintf(stderr, "accel: peek failed (%d)\n", ret);return ret;}printf("accel: now x=%d y=%d z=%d mg\n", now.x, now.y, now.z);pbl\_accel\_set\_listener(ACCEL, &s\_listener);ret = pbl\_accel\_set\_sampling\_interval(ACCEL, INTERVAL\_US, &interval\_us);if (ret != 0) {fprintf(stderr, "accel: could not set the sampling interval (%d)\n", ret);return ret;}/\* Up to BATCH samples per report, so the sensor can sleep between them. \*/batch = MIN(BATCH, pbl\_accel\_get\_max\_num\_samples(ACCEL));ret = pbl\_accel\_set\_num\_samples(ACCEL, batch);if (ret != 0) {fprintf(stderr, "accel: could not start sampling (%d)\n", ret);return ret;}printf("accel: sampling every %d us, batches of up to %d\n", (int)interval\_us, (int)batch);/\* Not every sensor detects these: -ENOTSUP is not an error. \*/ret = pbl\_accel\_set\_shake\_detection(ACCEL, true);if (ret != 0 && ret != -ENOTSUP) {fprintf(stderr, "accel: could not enable shake detection (%d)\n", ret);return ret;}ret = pbl\_accel\_set\_double\_tap\_detection(ACCEL, true);if (ret != 0 && ret != -ENOTSUP) {fprintf(stderr, "accel: could not enable double tap detection (%d)\n", ret);return ret;}for (int i = 0; i \< LISTEN\_S; i++) {pbl\_sleep\_ms(1000);/\* The lock keeps the listener out while reading what it wrote. \*/pbl\_accel\_lock(ACCEL);printf("accel: x=%d y=%d z=%d mg, %d samples so far\n", s\_last.x, s\_last.y, s\_last.z,(int)s\_samples);pbl\_accel\_unlock(ACCEL);}(void)pbl\_accel\_set\_shake\_detection(ACCEL, false);(void)pbl\_accel\_set\_double\_tap\_detection(ACCEL, false);(void)pbl\_accel\_set\_num\_samples(ACCEL, 0U);pbl\_accel\_set\_listener(ACCEL, nullptr);printf("accel: %d samples, %d shakes, %d double taps\n", (int)s\_samples, (int)s\_shakes,(int)s\_taps);printf("Sample done\n");return 0;}
```

## Output

```
accel: now x=-?\d+ y=-?\d+ z=-?\d+ mg
accel: sampling every \d+ us, batches of up to \d+
accel: x=-?\d+ y=-?\d+ z=-?\d+ mg, \d+ samples so far
accel: \d+ samples, \d+ shakes, \d+ double taps
Sample done
```

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