PHPackages                             tasoft/php-i2c-extension - 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. tasoft/php-i2c-extension

ActiveLibrary[Utility &amp; Helpers](/categories/utility)

tasoft/php-i2c-extension
========================

A simple I2C extension for PHP to read from and write to i2c bus.

v8.2.5(1y ago)71163[2 issues](https://github.com/tasoftch/php-i2c-extension/issues)4MITCPHP &gt;=8.0

Since Jun 18Pushed 1y ago1 watchersCompare

[ Source](https://github.com/tasoftch/php-i2c-extension)[ Packagist](https://packagist.org/packages/tasoft/php-i2c-extension)[ RSS](/packages/tasoft-php-i2c-extension/feed)WikiDiscussions master Synced 3d ago

READMEChangelog (9)DependenciesVersions (10)Used By (4)

The PHP i2c Extension
=====================

[](#the-php-i2c-extension)

I've created this extension to get access to the i2c bus on my raspberry pi.

Prerequisites
-------------

[](#prerequisites)

- The development module for your version of PHP, i.e, *php7.4-dev*, *php8.2-dev*.

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

[](#installation)

Change the configure file on line 5 (*php-config7.4*), to match your installed version of PHP. Then, run the following commands to install the extension.

```
$ cd ~
$ git clone https://github.com/tasoftch/php-i2c-extension.git
$ cd php-i2c-extension
$ phpize
$ ./configure --with-php-config=/usr/bin/php-config7.4 --enable-php-i2c
$ make
$ sudo make install

```

Next, find the location of PHP's INI files on your computer by running the following command.

```
$ php --ini | grep -i "Configuration File.*Path"

```

**Note:** If you're using Microsoft Windows, then run `php --ini` and look for the value of "Configuration File (php.ini) Path:".

You should see a directory path such as */etc/php/7.4/cli*. In that directory, create a new INI file named *20-i2c.ini* with the following contents

```
; configuration for php i2c module
extension=php_i2c
```

Then, with the file created, test that the I2C extension is loaded by running the following command:

```
php --ri php_i2c
```

You should see the following output:

```
php_i2c

Version => 0.8.0
```

Usage
-----

[](#usage)

The extension adds five function to the global scope:

1. `i2c_open`
    This opens the device bus.
2. `i2c_select`
    This selects an address of a connected chip.
3. `i2c_read` `i2c_read_byte` `i2c_read_2_bytes` `i2c_read_3_bytes` `i2c_read_4_bytes`
    Reads data from the i2c bus.
4. `i2c_write` `i2c_write_byte` `i2c_write_2_bytes` `i2c_write_3_bytes` `i2c_write_4_bytes`
    Writes data to the i2c bus
5. `i2c_close`
    Closes the bus.

### Example

[](#example)

I've tested with a Raspberry Pi Model B 3+ and the Adafruit ADS1115 analog to digital converter. It's default i2c address is 0x48.

```
