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

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

This sample reads the ambient light sensor five times, a second apart. It primes the sensor first with `pbl_als_prime()`, which lets sensors that sample continuously start before the first read, and releases it at the end. The levels are raw counts up to `PBL_ALS_LEVEL_MAX`; `pbl_als_to_lux()` converts them on boards that calibrate their sensor, and returns them unchanged elsewhere.

On a watch, cover the sensor or point it at a light while the sample runs. The emulator’s simulated sensor reads 0.

Run:

`pbl samples samples/drivers/als`

Boards:

every board

## Sources

src/main.c

```
#include \<stdint.h\>#include \<stdio.h\>#include \<pbl/drivers/als.h\>#include \<pbl/kernel/delay.h\>#define READS 5#define PERIOD\_MS 1000int main(void) {int ret = 0;printf("als: levels 0 to %u, lux: %s\n", (unsigned int)PBL\_ALS\_LEVEL\_MAX,pbl\_als\_has\_lux(ALS) ? "yes" : "no");/\* Priming lets sensors that sample continuously start before the first read. \*/pbl\_als\_prime(ALS);for (int i = 0; i \< READS; i++) {uint32\_t level;pbl\_sleep\_ms(PERIOD\_MS);ret = pbl\_als\_read(ALS, &level);if (ret != 0) {fprintf(stderr, "als: read failed (%d)\n", ret);break;}/\* Uncalibrated sensors return the level unchanged. \*/printf("als: level %u, %u %s\n", (unsigned int)level, (unsigned int)pbl\_als\_to\_lux(ALS, level),pbl\_als\_has\_lux(ALS) ? "lux" : "counts");}pbl\_als\_release(ALS);if (ret != 0) {return ret;}printf("Sample done\n");return 0;}
```

## Output

```
als: levels 0 to \d+, lux: (yes|no)
als: level \d+, \d+ (lux|counts)
als: level \d+, \d+ (lux|counts)
als: level \d+, \d+ (lux|counts)
als: level \d+, \d+ (lux|counts)
als: level \d+, \d+ (lux|counts)
Sample done
```

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