---
title: "I²C Master"
description: "Discrete I²C master that reads a known device at a known address. The pattern: open the bus, init the chip driver, loop reading the sensor every second, close cleanly."
source: https://docs.alplab.ai/sdk/examples/i2c-master
---

# `i2c-master`

Discrete I²C master that reads a known device at a known address. The pattern: open the bus, init the chip driver, loop reading the sensor every second, close cleanly.

Contrasts with [`i2c-scanner`](https://docs.alplab.ai/sdk/examples/i2c-scanner) — the scanner *probes* every 7-bit address for ACKs without knowing what's behind them; this example *reads* a known sensor through its real driver.

Source: [`examples/peripheral-io/i2c-master/`](https://github.com/alplabai/alp-sdk/tree/main/examples/peripheral-io/i2c-master).

## What it does

1. `alp_i2c_open()` on `BOARD_I2C_SENSORS` at 400 kHz (I²C Fast-mode). `BOARD_I2C_SENSORS` is a portable cross-EVK alias from `<alp/board.h>`: it resolves to `ALP_E1M_I2C0` on the E1M EVK and `ALP_E1M_X_I2C0` on the E1M-X EVK.
2. `bmp581_init()` — chip-driver probe with a CHIP_ID check, which catches the "wrong address" case (NACK on probe) up front.
3. `bmp581_set_sampling()` — configures oversampling, output data rate and power mode.
4. Five `bmp581_read_raw()` + `bmp581_compensate()` reads, one per second, printing pressure and temperature without float `printf`.
5. `bmp581_deinit()` + `alp_i2c_close()`.

A NACK on the probe exits gracefully with a diagnostic.

:::note[Changed in alp-sdk v0.17.0: TMP112 → BMP581]
Earlier releases read a TMP112 here. The TMP112 sits on the SoM's `BRD_I2C` management bus, not on `BOARD_I2C_SENSORS` — the bus this example opens — so the example never actually reached it. The BMP581 barometer is the part that **is** on `BOARD_I2C_SENSORS` on the 2626-R2 E1M EVK and the E1M-X EVK, at the same address (the current 2641-R3 E1M EVK fits no BMP581; see the caution below). For the on-module TMP112, see [`v2n-temp-sensor`](https://docs.alplab.ai/sdk/examples/v2n/v2n-temp-sensor) on V2N.
:::

## Hardware

The BMP581 barometer is the on-board sensor on `BOARD_I2C_SENSORS` on the 2626-R2 E1M EVK (U14) and the E1M-X EVK (U14). Its 7-bit address depends on the SDO strap:

| SDO strap | Address | On this bus |
|---|---|---|
| VDDIO | `0x47` | 2626-R2 E1M EVK U14 + E1M-X EVK U14 (this default) |
| GND | `0x46` | (not populated on these EVKs) |

Both of those boards strap SDO to VDDIO, so `BMP581_ADDR_7BIT` works unchanged across them.

:::caution[The current 2641-R3 E1M EVK has no BMP581]
The board preset of alp-sdk v0.17.0 follows the earlier **2626-R2** E1M EVK. The current **2641-R3** E1M EVK (UG-E1M-001) fits no BMP581. On 2641-R3, `0x47` on module `I2C0` is the INA228 `U53` on `CURR_I2C`, which joins the bus only with slide switch `S6` on the module side, so the BMP581 `CHIP_ID` check fails there.
:::

## `board.yaml`

```yaml
som:
  sku: E1M-AEN801

preset: e1m-evk
supported_boards:
  - e1m-evk
  - e1m-x-evk

pins:
  - { e1m: E1M_I2C0, macro: EVK_I2C_BUS_SENSORS, doc: "Shared sensor + IO-expander + INA236 bus" }
cores:
  m55_hp:
    app: ./src
    peripherals:
      - i2c

chips:
  - bmp581

diagnostics:
  log_level: info
```

## Expected output

Real hardware (BMP581 populated, room temperature, sea-level-ish pressure):

```
[i2c-master] open BOARD_I2C_SENSORS @ 400 kHz
[i2c-master] bmp581_init @ 0x47 -> 0 (OK)
[i2c-master] sample 0: 96234 Pa, 23.625 degC
[i2c-master] sample 1: 96231 Pa, 23.687 degC
[i2c-master] sample 2: 96233 Pa, 23.625 degC
[i2c-master] sample 3: 96230 Pa, 23.687 degC
[i2c-master] sample 4: 96232 Pa, 23.750 degC
[i2c-master] done
```

`native_sim` maps `alp-i2c0` to the emul I²C driver with no BMP581 registered as a target, so init gets NACKed:

```
[i2c-master] open BOARD_I2C_SENSORS @ 400 kHz
[i2c-master] bmp581_init @ 0x47 -> -5 (populated? right address?)
[i2c-master] done
```

Either way `[i2c-master] done` latches the harness.

## Troubleshooting

| Symptom | Cause / fix |
|---|---|
| `bmp581_init -> -5` (`ALP_ERR_IO` / NACK) | Chip not populated, wrong address, or the bus is held low (missing pull-ups, stuck target). Run [`i2c-scanner`](https://docs.alplab.ai/sdk/examples/i2c-scanner) to see what actually ACKs. |
| `open failed` (NULL handle) | The `alp-i2c0` devicetree alias isn't set. On `native_sim` check `CONFIG_EMUL=y` / `CONFIG_I2C_EMUL=y` and the shipped overlay. |
| Garbled readings | Wrong bitrate for the bus capacitance. Drop 400 kHz → 100 kHz to confirm. |

## See also

- [`<alp/peripheral.h>` reference — I²C](https://docs.alplab.ai/sdk/api/peripheral)
- [`i2c-scanner`](https://docs.alplab.ai/sdk/examples/i2c-scanner) — discovery companion
- [`i2c-slave`](https://docs.alplab.ai/sdk/examples/i2c-slave) — target-mode companion
- [`i2c-device-hub`](https://docs.alplab.ai/sdk/examples/i2c-device-hub) — every populated device on the bus, one driver each
- [Chip catalogue](https://docs.alplab.ai/sdk/chips)
- [Examples overview](https://docs.alplab.ai/sdk/examples/overview)
