{"id":24395210,"url":"https://github.com/tasoftch/php-i2c-extension","last_synced_at":"2025-07-08T22:03:31.181Z","repository":{"id":57065376,"uuid":"272289586","full_name":"tasoftch/php-i2c-extension","owner":"tasoftch","description":"A Zend Extension for php to grant access to an existing i2c bus on a linux device (Raspberry Pi and for example the ADS1x15)","archived":false,"fork":false,"pushed_at":"2024-08-10T13:01:12.000Z","size":42,"stargazers_count":6,"open_issues_count":2,"forks_count":3,"subscribers_count":1,"default_branch":"master","last_synced_at":"2025-07-08T22:02:12.280Z","etag":null,"topics":["ads1015","ads1115","i2c-bus","php-i2c","raspberry-pi"],"latest_commit_sha":null,"homepage":"","language":"C","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/tasoftch.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2020-06-14T21:54:26.000Z","updated_at":"2025-03-13T17:34:52.000Z","dependencies_parsed_at":"2024-11-16T01:41:16.724Z","dependency_job_id":"1d24761b-abbb-4883-8913-3243c903f0f3","html_url":"https://github.com/tasoftch/php-i2c-extension","commit_stats":null,"previous_names":[],"tags_count":9,"template":false,"template_full_name":null,"purl":"pkg:github/tasoftch/php-i2c-extension","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/tasoftch%2Fphp-i2c-extension","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/tasoftch%2Fphp-i2c-extension/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/tasoftch%2Fphp-i2c-extension/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/tasoftch%2Fphp-i2c-extension/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/tasoftch","download_url":"https://codeload.github.com/tasoftch/php-i2c-extension/tar.gz/refs/heads/master","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/tasoftch%2Fphp-i2c-extension/sbom","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":264357291,"owners_count":23595575,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["ads1015","ads1115","i2c-bus","php-i2c","raspberry-pi"],"created_at":"2025-01-19T20:26:32.832Z","updated_at":"2025-07-08T22:03:31.128Z","avatar_url":"https://github.com/tasoftch.png","language":"C","funding_links":[],"categories":[],"sub_categories":[],"readme":"# The PHP i2c Extension\n\nI've created this extension to get access to the i2c bus on my raspberry pi.\n\n\n## Prerequisites\n\n- The development module for your version of PHP, i.e, _php7.4-dev_, _php8.2-dev_.\n\n## Installation\n\nChange the configure file on line 5 (_php-config7.4_), to match your installed version of PHP.\nThen, run the following commands to install the extension.\n\n```bin\n$ cd ~\n$ git clone https://github.com/tasoftch/php-i2c-extension.git\n$ cd php-i2c-extension\n$ phpize\n$ ./configure --with-php-config=/usr/bin/php-config7.4 --enable-php-i2c\n$ make\n$ sudo make install\n```\n\nNext, find the location of PHP's INI files on your computer by running the following command.\n\n```bin\n$ php --ini | grep -i \"Configuration File.*Path\"\n```\n\n**Note:** If you're using Microsoft Windows, then run `php --ini` and look for the value of \"Configuration File (php.ini) Path:\".\n\nYou should see a directory path such as _/etc/php/7.4/cli_.\nIn that directory, create a new INI file named _20-i2c.ini_ with the following contents\n\n```ini\n; configuration for php i2c module\nextension=php_i2c\n```\n\nThen, with the file created, test that the I2C extension is loaded by running the following command:\n\n```bash\nphp --ri php_i2c\n```\n\nYou should see the following output:\n\n```bash\n\nphp_i2c\n\nVersion =\u003e 0.8.0\n```\n\n## Usage\n\nThe extension adds five function to the global scope:\n1. ```i2c_open```  \n    This opens the device bus.\n1. ```i2c_select```  \n    This selects an address of a connected chip.\n1. ```i2c_read``` ```i2c_read_byte``` ```i2c_read_2_bytes``` ```i2c_read_3_bytes``` ```i2c_read_4_bytes```      \n    Reads data from the i2c bus.\n1. ```i2c_write``` ```i2c_write_byte``` ```i2c_write_2_bytes``` ```i2c_write_3_bytes``` ```i2c_write_4_bytes```  \n   Writes data to the i2c bus\n1. ```i2c_close```  \n    Closes the bus.\n    \n### Example\n\nI've tested with a Raspberry Pi Model B 3+ and the Adafruit ADS1115 analog to digital converter.\nIt's default i2c address is 0x48.\n\n```php\n\u003c?php\n$fd = i2c_open(\"/dev/i2c-1\");\ni2c_select($fd, 0x48);\n\nfor($e=0;$e\u003c30;$e++) {\n    // Read for 30 times the value between channel AIN_0 and GND, 4.096 V, 128 samples/s\n    i2c_write($fd, 1, [0xc3, 0x85]);\n    // or\n    // i2c_write_2_bytes( 0x01c385 );\n    \n    // Wait for conversion completed\n    usleep(9000);\n    i2c_write($fd, 0);\n    $data = i2c_read($fd, 2);\n\n    $value = $data[0]*256 + $data[1];\n    printf(\"Hex: 0x%02x%02x - Int: %d - Float, converted: %f V\\n\",\n        $data[0], $data[1], $value, (float)$value*4.096/32768.0);\n\n    usleep(500000);\n}\n\ni2c_close($fd);\n```\n\n## Usage PHP\n\nThe package also contains a php wrapper class for i2c.\n\n```bin\n$ composer require tasoft/php-i2c-extension\n```\n\nPlease note that the composer installation does not compile the extension!\nFor compilation use the installation guide described before.\n\nNow the same example can be rewritten as:\n\n```php\n\u003c?php\nuse TASoft\\Bus\\I2C;\n\n$i2c = new I2C(0x48, 1);\nfor($e=0;$e\u003c30;$e++) {\n    // Read for 30 times the value between channel AIN_0 and GND, 4.096 V, 128 samples/s\n    $i2c-\u003ewrite16(1, 0xC385);\n    // Wait for conversion completed\n    usleep(9000);\n    $value = $i2c-\u003ereadRegister16(1);\n\n    printf(\"Hex: 0x%04x - Int: %d - Float, converted: %f V\\n\",\n        $value, $value, (float)$value*4.096/32768.0);\n\n    usleep(500000);\n}\n```\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Ftasoftch%2Fphp-i2c-extension","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Ftasoftch%2Fphp-i2c-extension","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Ftasoftch%2Fphp-i2c-extension/lists"}