{"id":15490192,"url":"https://github.com/t-geindre/php-gpio","last_synced_at":"2025-04-10T18:43:51.560Z","repository":{"id":57067461,"uuid":"60017312","full_name":"t-geindre/php-gpio","owner":"t-geindre","description":"PHP lib to read/write Raspberry PI Gpio","archived":false,"fork":false,"pushed_at":"2016-06-15T08:46:07.000Z","size":135,"stargazers_count":2,"open_issues_count":0,"forks_count":3,"subscribers_count":4,"default_branch":"master","last_synced_at":"2024-10-19T09:16:50.517Z","etag":null,"topics":["gpio","php","raspberry-pi"],"latest_commit_sha":null,"homepage":null,"language":"PHP","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/t-geindre.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2016-05-30T14:43:37.000Z","updated_at":"2024-06-11T09:54:34.000Z","dependencies_parsed_at":"2022-08-24T14:54:06.122Z","dependency_job_id":null,"html_url":"https://github.com/t-geindre/php-gpio","commit_stats":null,"previous_names":[],"tags_count":2,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/t-geindre%2Fphp-gpio","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/t-geindre%2Fphp-gpio/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/t-geindre%2Fphp-gpio/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/t-geindre%2Fphp-gpio/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/t-geindre","download_url":"https://codeload.github.com/t-geindre/php-gpio/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":248271920,"owners_count":21075800,"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":["gpio","php","raspberry-pi"],"created_at":"2024-10-02T07:09:47.960Z","updated_at":"2025-04-10T18:43:51.535Z","avatar_url":"https://github.com/t-geindre.png","language":"PHP","funding_links":[],"categories":[],"sub_categories":[],"readme":"# PHP-GPIO ![master build](https://api.travis-ci.org/t-geindre/php-gpio.svg?branch=master)\n\nA simple library to read/write Raspberry PI GPIOs with PHP.\n\nForked from [ronanguilloux/php-gpio](https://github.com/ronanguilloux/php-gpio) and tested with [Atoum](http://atoum.org/).\n\n## Install\n\nThis library can be installed using [Composer](https://getcomposer.org/):\n\n```sh\n$ composer require tgeindre/php-gpio\n```\n\n## Read/Write Raspberry Pi GPIOs\n\nTo read/write Raspberry Pi GPIOs, use the `PhpGpio\\Gpio` class. The instanciation of this class requires an array of pins numbers you will use.\n\nYou can define a specific list of pins numbers, according to your usage and your Rasberry Pi version, or you can use the `PhpGpio\\Utils\\Pi` class to automaticly find all availables pins:\n\n```php\n\u003c?php\nnamespace myproject;\n\n$pi = new PhpGpio\\Utils\\Pi;\n$pi-\u003egetAvailablePins(); // int array\n```\n\nAccessing to the GPIOs requires root permissions, so make sure your code is running with enought permissions. __Remember__: you should never run your webserver as root.\n\nHere is a simple example of Gpio class usage:\n\n```php\n\u003c?php\nnamespace MyProject;\n\nuse PhpGpio\\GpioInterface;\nuse PhpGpio\\Gpio;\n\n// Both pins are available on all raspi versions\ndefine('PIN_IN', 4);\ndefine('PIN_OUT', 7);\n\n$gpio = new Gpio([PIN_IN, PIN_OUT]);\n\n// First, setup pins with correct directions\n$gpio\n     -\u003esetup(PIN_IN, GpioInterface::DIRECTION_IN) // Makes it readable\n     -\u003esetup(PIN_OUT, GpioInterface::DIRECTION_OUT) // Writeable\n;\n\n// read PIN_IN value and display it\n$value = $gpio-\u003eread(PIN_IN);\nvar_dump($value); // string\n\n// write 1 on PIN_OUT\n$gpio-\u003ewrite(PIN_OUT, GpioInterface::IO_VALUE_ON);\nsleep(1);\n\n// After 1 second, write 0 on PIN_OUT\n$gpio-\u003ewrite(PIN_OUT, GpioInterface::IO_VALUE_OFF);\n\n// Then free all pins\n// (use the unexport() method to free pins one by one)\n$gpio-\u003eunexportAll();\n```\n\nCheck [this page](http://www.raspberrypi-spy.co.uk/2012/06/simple-guide-to-the-rpi-gpio-header-and-pins/) if you need a complete list of availables pins on your Raspberry Pi version.\n\n### Demo\n\nHere is a simple demo using a Raspberry Pi 3 and some LED (click on the image to see the video):\n\n[![Demo video](https://img.youtube.com/vi/ON8dwxP4-fY/1.jpg)](https://www.youtube.com/watch?v=ON8dwxP4-fY)\n\n[Source code](https://gist.github.com/t-geindre/674280ad12f364c4ce386ff73cfe57fc)\n\n## Sensors\n\nCurrently, this library only supports MCP analogic to digital converter (ADC) with 4 or 8 channels:\n\n### MCP3002, MCP3004 \u0026 MCP3008\n\n![mcp3004](https://cloud.githubusercontent.com/assets/6348039/16055086/38463440-3270-11e6-8b36-23c8ea104e91.png)\n\nGiven that the Rapsberry Pi supports only digital inputs, an ADC, such as a MCP, is required to read analogic data.\n\nMCP3002, MCP3004 and MCP3008 work the same way but have dedicated classes for each version:\n * `PhpGpio\\Sensor\\Mcp\\Mcp3002`\n * `PhpGpio\\Sensor\\Mcp\\Mcp3004`\n * `PhpGpio\\Sensor\\Mcp\\Mcp3008`\n\nIt use a SPI interface wich requires, at least, 4 pins:\n * __MISO__ - Master Input Slave Ouput,\n * __MOSI__ - Master Output Slave Input,\n * __CLK__ -  Clock,\n * __CS__ - Channel Select (one dedicated pin for each component using the same SPI interface).\n\nProvided classes use a software communication system so you can use any pins you wich.\n\nHere is the connection schema of those 3 versions of MCP ADC:\n\n![mcp](https://cloud.githubusercontent.com/assets/6348039/16055709/ad264910-3272-11e6-8aad-6ed0617f8c8a.png)\n\nWiring:\n * __VDD__ (power supply) on 3.3V,\n * __VREF__ on 3.3v,\n * __AGND__ on ground (GND),\n * __CLK__ on clock pin,\n * __DOUT__ (digital output) on MISO pin,\n * __DIN__ (digital input) on MOSI pin,\n * __CS__ (channel select) on CS pin,\n * __DGND__ on ground (GND).\n\nNow you are ready to read some data:\n\n```php\nnamespace MyProject;\n\nuse PhpGpio\\Gpio;\nuse PhpGpio\\Sensor\\Mcp\\Mcp3008;\n\n// Defining pins mapping according to your setup\n$pinsMapping = [\n    'MISO' =\u003e 17,\n    'MOSI' =\u003e 8,\n    'CLK' =\u003e 23,\n    'CS' =\u003e 24,\n];\n\n// Setup a Gpio class\n$gpio = new Gpio(array_values($pins));\n\n// Then we can instanciate the MCP class\n$mcp = new Mcp3008(\n    $gpio\n    $mapping['CLK'],\n    $mapping['MOSI'],\n    $mapping['MISO'],\n    $mapping['CS']\n);\n\n// Now let's read some data on the first channel\nwhile (true) {\n    echo $mcp-\u003eread(['channel' =\u003e 0]), \"\\n\";\n    // every second\n    sleep(1);\n}\n```\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Ft-geindre%2Fphp-gpio","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Ft-geindre%2Fphp-gpio","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Ft-geindre%2Fphp-gpio/lists"}