Skip to content
php-baremetal REGISTRY
Publish a package

php-baremetal/ext-i2c

bm-ext MIT

Baremetal\I2c\Bus + \Device and the device-driver framework. Drivers are packages registering into the i2c_drivers registry; the platform side is an i2c-hal package.

bus: i2c

ext-i2c · Recipe

Scan the bus

List every chip that answers on the bus, with the driver mounted at each address. The first thing to run on new wiring.

A project with ext-i2c only, for a Waveshare ESP32-S3-Touch-LCD-1.85B, whose on-board QMI8658 (0x6B) and PCF85063 (0x51) sit on SDA 11 / SCL 10. On another board, change the board id and the pins.

json composer.json
{
  "name": "acme/i2c-scan",
  "type": "project",
  "repositories": [
    { "type": "composer", "url": "https://repository.php-baremetal.com", "canonical": true }
  ],
  "require": {
    "php-baremetal/conductor": "^1.0",
    "php-baremetal/ext-i2c": "^1.0"
  },
  "config": {
    "allow-plugins": { "php-baremetal/conductor": true },
    "platform": { "php": "8.5.11" }
  },
  "extra": {
    "baremetal": {
      "sdk": "esp32@^2.0",
      "board": "waveshare-esp32-s3-touch-lcd-1.85b",
      "runtime": "init-loop",
      "storage": "embedded",
      "options": { "restart-delay": 3600 }
    }
  }
}

The script runs once and ends; restart-delay keeps the board from restarting for an hour, so the output stays on the console.

php project-src/index.php
<?php
use Baremetal\I2c\Bus;

const SDA = 11;
const SCL = 10;

$bus = new Bus(sda: SDA, scl: SCL);

printf("I2C scan (sda=%d scl=%d):\n", SDA, SCL);
$found = $bus->scan();
foreach ($found as $address => $info) {
    printf("  0x%02X  %s\n", $address, $info['driver'] ?? '(no driver)');
}
printf("%d device(s)\n", count($found));

Output on a bus with just those two chips:

text serial console
I2C scan (sda=11 scl=10):
  0x51  (no driver)
  0x6B  (no driver)
2 device(s)

(no driver) means no driver object has been constructed for that address yet. Construct one first (for example new Qmi8658($bus) from imu-qmi8658) and the scan shows its name.

When it fails

  • Empty result: wrong pins, or the chips are unpowered. Swap SDA and SCL if unsure.
  • I2C scan aborted, bus stuck: eight probes in a row timed out, typically no pull-ups on a bare breakout or a chip holding SDA low. The bus has been reset; fix the wiring and restart.
  • cannot open I2C bus: another Bus already uses one of the pins with a different pair.

Related