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

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

This sample has the magnetometer, `MAG`, measure the magnetic field five times a second for 3 seconds, and prints the latest measurement every second with `pbl_mag_peek()`. Before it starts sampling there is nothing to read: the sensor sleeps until someone sets a sampling interval, and an interval of 0 puts it back to sleep. A listener counts the samples as they come.

On a watch, turn it around to see the field change. The emulator boards have no magnetometer, so the sample uses the simulator there, chosen in its `boards/<board>.roles.yml`, which measures no field.

Run:

`pbl samples samples/drivers/mag`

Boards:

every board

## Sources

src/main.c

```
#include \<errno.h\>#include \<stdint.h\>#include \<stdio.h\>#include \<pbl/drivers/mag.h\>#include \<pbl/kernel/delay.h\>#define INTERVAL\_US 200000U#define LISTEN\_S 3/\* Written by the listener, which runs with the device lock held. \*/static unsigned int s\_samples;static void prv\_sample(const struct pbl\_mag \*mag, const MagData \*sample, void \*user\_data) {s\_samples++;}static const struct pbl\_mag\_listener s\_listener = {.sample = prv\_sample,};int main(void) {uint32\_t interval\_us;MagData field;int ret;/\* The magnetometer sleeps until someone samples it: there is nothing to read yet. \*/ret = pbl\_mag\_peek(MAG, &field);printf("mag: before sampling: %s\n", ret == -ENODATA ? "no data" : "data");pbl\_mag\_set\_listener(MAG, &s\_listener);ret = pbl\_mag\_set\_sampling\_interval(MAG, INTERVAL\_US, &interval\_us);if (ret != 0) {fprintf(stderr, "mag: could not start sampling (%d)\n", ret);return ret;}printf("mag: sampling every %d us\n", (int)interval\_us);for (int i = 0; i \< LISTEN\_S; i++) {unsigned int samples;pbl\_sleep\_ms(1000);ret = pbl\_mag\_peek(MAG, &field);if (ret != 0) {fprintf(stderr, "mag: peek failed (%d)\n", ret);break;}/\* The lock keeps the listener out while reading what it wrote. \*/pbl\_mag\_lock(MAG);samples = s\_samples;pbl\_mag\_unlock(MAG);printf("mag: x=%d y=%d z=%d mG, %d samples so far\n", field.x, field.y, field.z, (int)samples);}/\* An interval of 0 stops sampling, and lets the sensor sleep again. \*/(void)pbl\_mag\_set\_sampling\_interval(MAG, 0U, nullptr);pbl\_mag\_set\_listener(MAG, nullptr);if (ret != 0) {return ret;}printf("mag: %d samples\n", (int)s\_samples);printf("Sample done\n");return 0;}
```

## Output

```
mag: before sampling: no data
mag: sampling every \d+ us
mag: x=-?\d+ y=-?\d+ z=-?\d+ mG, \d+ samples so far
mag: \d+ samples
Sample done
```

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