PHPackages                             php-io-extensions/i2c - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [Utility &amp; Helpers](/categories/utility)
4. /
5. php-io-extensions/i2c

ActivePhp-ext[Utility &amp; Helpers](/categories/utility)

php-io-extensions/i2c
=====================

PHP-Controllable I2C Extension

v0.1.7(4w ago)011MITCPHP &gt;=8.3

Since May 6Pushed 4w agoCompare

[ Source](https://github.com/php-io-extensions/i2c)[ Packagist](https://packagist.org/packages/php-io-extensions/i2c)[ RSS](/packages/php-io-extensions-i2c/feed)WikiDiscussions main Synced 1w ago

READMEChangelogDependenciesVersions (8)Used By (0)

I2C
===

[](#i2c)

[![PHP](https://camo.githubusercontent.com/91e2ff786d2fba1edf015025006e0156a071320b3662eaf2c50f39d4bb4b2369/68747470733a2f2f696d672e736869656c64732e696f2f62616467652f7068702d253345253344382e332d626c7565)](https://www.php.net)[![Zephir](https://camo.githubusercontent.com/5add3cf7b546a9aa97c8f3d9f5caeda8c9fdf1bd0f90151d5b3e8e25e591ed83/68747470733a2f2f696d672e736869656c64732e696f2f62616467652f7a65706869722d302e31392532422d6f72616e6765)](https://docs.zephir-lang.com/latest/en/installation)[![Platform](https://camo.githubusercontent.com/ec3ffbe89f8d1f152dec06cd451833acef4ded894b500c724cb70033418ec0be/68747470733a2f2f696d672e736869656c64732e696f2f62616467652f706c6174666f726d2d6c696e75782d6c6967687467726579)](https://www.kernel.org)

PHP-controllable Linux I²C extension built with Zephir.

The I2C extension allows PHP to communicate with I²C devices on Linux — selecting slave addresses, performing raw reads and writes, and executing combined transactions. Useful for sensors, displays, ADCs, and other hardware connected to an I²C bus.

```
git clone https://github.com/DeptOfScrapyardRobotics/I2C
cd I2C
# Raspberry Pi / Debian Trixie:
bash install-debian-trixie.sh
# NVIDIA JetPack 6 (Jetson):
bash install-jetpack6.sh
```

Requirements
------------

[](#requirements)

- PHP 8.3+ with development headers (`php-dev` / `php-devel`)
- [Zephir](https://docs.zephir-lang.com/latest/en/installation) 0.19+
- [FD extension](https://github.com/DeptOfScrapyardRobotics/FD) — provides raw integer file descriptors via `Fd\FD::open()`
- Linux with I²C userspace support (`/dev/i2c-N`)
- `i2c-dev` kernel module loaded

Installation
------------

[](#installation)

Install Zephir if you haven't already:

```
composer global require phalcon/zephir
```

Install the [FD extension](https://github.com/DeptOfScrapyardRobotics/FD) first — I2C requires raw integer file descriptors that PHP's stream layer cannot provide:

```
git clone https://github.com/DeptOfScrapyardRobotics/FD
cd FD && bash install.sh
```

Then clone and build this extension:

```
git clone https://github.com/DeptOfScrapyardRobotics/I2C
cd I2C
# Raspberry Pi / Debian Trixie:
bash install-debian-trixie.sh
# NVIDIA JetPack 6 (Jetson):
bash install-jetpack6.sh
```

The install script handles the full workflow: ensures `linux/i2c-dev.h` headers are present, loads the `i2c-dev` kernel module, clean → build → copy `.so` → write `30-i2c.ini` into all detected `conf.d` directories → verify `php -m` → reload php-fpm if present.

To use a specific Zephir binary:

```
# Raspberry Pi / Debian Trixie:
ZEPHIR_BIN=/path/to/zephir bash install-debian-trixie.sh
# NVIDIA JetPack 6 (Jetson):
ZEPHIR_BIN=/path/to/zephir bash install-jetpack6.sh
```

API
---

[](#api)

All methods are static. File descriptors are plain integers. Use the [FD extension](https://github.com/DeptOfScrapyardRobotics/FD) to open and close them — PHP's stream-based `fopen` does not produce the raw integer FDs the kernel ioctls require.

```
use Fd\FD;
use I2c\I2CUse;
use I2c\I2CConfig;

$fd = FD::open('/dev/i2c-1', 2); // O_RDWR = 2
// ... use $fd with I2CUse and I2CConfig methods ...
FD::close($fd);
```

### `I2c\I2CUse`

[](#i2ci2cuse)

Basic I/O operations on an I²C bus file descriptor.

---

#### `slave(int $fd, int $address): int`

[](#slaveint-fd-int-address-int)

Selects the slave device to communicate with. Must be called before `read()` or `write()`. Returns `0` on success, `-1` on failure.

---

#### `read(int $fd, int $bytes_to_read): string`

[](#readint-fd-int-bytes_to_read-string)

Reads raw bytes from the currently selected slave. Returns the bytes as a binary string, or `""` on error.

---

#### `write(int $fd, string $data, int $bytes_to_write): int`

[](#writeint-fd-string-data-int-bytes_to_write-int)

Writes raw bytes to the currently selected slave. Returns the number of bytes written, or `-1` on failure.

---

### `I2c\I2CConfig`

[](#i2ci2cconfig)

Configuration and advanced transaction operations on an I²C bus file descriptor.

---

#### `tenbit(int $fd, int $enable): int`

[](#tenbitint-fd-int-enable-int)

Enables (`1`) or disables (`0`) 10-bit slave addressing. Returns `0` on success, `-1` on failure.

---

#### `pec(int $fd, int $enable): int`

[](#pecint-fd-int-enable-int)

Enables (`1`) or disables (`0`) SMBus Packet Error Checking. Returns `0` on success, `-1` on failure.

---

#### `funcs(int $fd): int`

[](#funcsint-fd-int)

Returns the adapter's functionality bitmask, or `-1` on failure. Compare against `I2C_FUNC_*` values from [`linux/i2c.h`](https://elixir.bootlin.com/linux/latest/source/include/uapi/linux/i2c.h).

---

#### `rdwr(int $fd, array $messages): int`

[](#rdwrint-fd-array-messages-int)

Performs a combined read/write transaction in a single atomic ioctl. Returns the number of messages transferred on success, or `-1` on failure.

Each element of `$messages` is an associative array:

KeyTypeDescription`addr``int`Slave address`flags``int``I2C_M_*` flags — `0` = write, `0x0001` = read`data``string`Raw bytes; length determines bytes transferred---

Usage
-----

[](#usage)

```
use Fd\FD;
use I2c\I2CUse;
use I2c\I2CConfig;
```

I2C\_M flag values for `rdwr`. PHP does not define these — pass the values directly or define your own constants:

ConstantValueDescription`I2C_M_WR``0x0000`Write to slave`I2C_M_RD``0x0001`Read from slave`I2C_M_TEN``0x0010`Use 10-bit addressing`I2C_M_NOSTART``0x4000`Skip repeated START condition---

### Basic read/write

[](#basic-readwrite)

```
$fd = FD::open('/dev/i2c-1', 2); // O_RDWR

I2CUse::slave($fd, 0x48);
I2CUse::write($fd, "\x00", 1);

$data = I2CUse::read($fd, 2);

FD::close($fd);
```

---

### Combined transaction (write then read, no STOP)

[](#combined-transaction-write-then-read-no-stop)

```
$fd = FD::open('/dev/i2c-1', 2);

$messages = [
    ['addr' => 0x48, 'flags' => 0x0000, 'data' => "\x00"],
    ['addr' => 0x48, 'flags' => 0x0001, 'data' => "\x00\x00"],
];

I2CConfig::rdwr($fd, $messages);

FD::close($fd);
```

---

### Query adapter capabilities

[](#query-adapter-capabilities)

```
$fd = FD::open('/dev/i2c-1', 2);

$funcs = I2CConfig::funcs($fd);
echo "Adapter funcs bitmask: {$funcs}\n";

FD::close($fd);
```

---

### 10-bit addressing

[](#10-bit-addressing)

```
$fd = FD::open('/dev/i2c-1', 2);

I2CConfig::tenbit($fd, 1);
I2CUse::slave($fd, 0x1A2);
I2CUse::write($fd, "\x00", 1);
$data = I2CUse::read($fd, 4);

FD::close($fd);
```

License
-------

[](#license)

Copyright © Project Saturn Studios, LLC. All rights reserved.

###  Health Score

39

—

LowBetter than 84% of packages

Maintenance94

Actively maintained with recent releases

Popularity7

Limited adoption so far

Community6

Small or concentrated contributor base

Maturity43

Maturing project, gaining track record

 Bus Factor1

Top contributor holds 100% of commits — single point of failure

How is this calculated?**Maintenance (25%)** — Last commit recency, latest release date, and issue-to-star ratio. Uses a 2-year decay window.

**Popularity (30%)** — Total and monthly downloads, GitHub stars, and forks. Logarithmic scaling prevents top-heavy scores.

**Community (15%)** — Contributors, dependents, forks, watchers, and maintainers. Measures real ecosystem engagement.

**Maturity (30%)** — Project age, version count, PHP version support, and release stability.

###  Release Activity

Cadence

Every ~1 days

Total

7

Last Release

28d ago

### Community

Maintainers

![](https://avatars.githubusercontent.com/u/10563160?v=4)[Angel Gonzalez](/maintainers/projectsaturnstudios)[@projectsaturnstudios](https://github.com/projectsaturnstudios)

---

Top Contributors

[![projectsaturnstudios](https://avatars.githubusercontent.com/u/10563160?v=4)](https://github.com/projectsaturnstudios "projectsaturnstudios (8 commits)")

### Embed Badge

![Health badge](/badges/php-io-extensions-i2c/health.svg)

```
[![Health](https://phpackages.com/badges/php-io-extensions-i2c/health.svg)](https://phpackages.com/packages/php-io-extensions-i2c)
```

###  Alternatives

[moodle/moodle

Moodle - the world's open source learning platform

7.1k86.6k28](/packages/moodle-moodle)

PHPackages © 2026

[Directory](/)[Categories](/categories)[Trending](/trending)[Changelog](/changelog)[Analyze](/analyze)
