Vacancies available. Click here for more information.

I know what you’re thinking! "PHP and Raspberry Pi? Another blog/tutorial about setting up a web server!" Well no, I’ve noticed that there aren't really many applications written in PHP for the Pi, so I thought I’d experiment with that after deciding that I didn’t like C# and being so used to PHP’s structure.

I’ve written a library for managing GPIO connections (General Purpose Input / Output) via a class, which can be bridged into Laravel or Symfony. The reason I’ve enabled it to be bridged into these frameworks is to strengthen its use and widen its capabilities. Some example use cases would be:

  • Turn an LED on with a webpage/API
  • Log a sensor input into a database and use the data to create a pie chart
  • Create a WiFi driven, remote controlled, knife welding robot to protect your home from intruders while you cower under the covers using your phone...

Introduction to GPIO

GPIO is a simple concept, a basic feature of it being the write mode. With GPIO there are several modes in which the pin layout can work. Write is a mode which has two core settings: on and off. Simple right, but how does it work?

Most of the pins on your Raspberry Pi can output at 3.3 volts, which is mapped to a value of 1023. A value of 0 represents 0 volts and a value of 1023 represents the full 3.3 volts. The reason for this is that the specific output voltage could change for different devices, but the mapped value of maximum voltage does not.

To turn an LED on, we first need to declare the pin in the terminal using GPIO:

gpio mode 1 out

Then we can turn the pin on:

gpio 1 1

This will turn pin 1 on.

A really cool concept with GPIO is PWM (Pulse Width Modulation), which allows you to moderate the given output voltage, essentially meaning that you can dim the light! Exciting stuff! I’m going to use PWM in my example with the GPIO Manager:

gpio mode 1 out pwm && gpio 1 1023

This would turn the LED on 'full'.

Here is an example of a cool interactive layout of the Raspberry Pi pin setup.

The GPIO Manager also allows you to add options to your setup, let you use the different pin references, such as BCM and physical pins, which are mentioned in the previous link.

Using GPIO Manager

GPIO Manager does what it says on the tin. It manages your GPIO environment for you, based on configurations, while the service provider will cache the manager so that it’s a constantly live class. This means that values won’t be lost on application close.

The Manager can be found on GitHub.

The Readme provides a guide to installing the Manager with Laravel, which is a fairly straightforward process. Don’t forget to install the GPIO utility for Raspberry Pi too.

In my setup I have 2 LEDs; One green and one blue and I’ve set them up like so:

'pins' => [
    'blue' => [
        'pin' => 1,
        'mode' => 'pwm',
        'defaultValue' => 0,
    'green' => [
        'pin' => 26,
        'mode' => 'write',
        'defaultValue' => 1,

One issue I found when using the GPIO Manager package, was the caching of the Raspberry Pi. When turning the Pi off and on again, the Manager doesn’t set the pins correctly. To avoid this, use php artisan cache:clear whenever you restart or change your GPIO config.

To test that I’ve got my setup working, I’m going to use the following GPIO commands with Laravel. Firstly, to turn the blue light on:

php artisan gpio:set blue 1023
Raspberry Pi Blog 1

And to turn it off again:

php artisan gpio:set blue 0
Raspberry Pi Blog 2

Now to turn on the green LED:

php artisan gpio:set green 1
Raspberry Pi Blog 3

The green LED is in write mode which means it only accepts 1 or 0 rather than the full spectrum, as mentioned earlier in this post.

An example of PWM dimming:

php artisan gpio:set blue 500 && php artisan gpio:set green 1
Raspberry Pi Blog 4
php artisan gpio:set blue 1023

Comparing the image above with the image below, you can see that the blue LED is much dimmer in the above photo.

Raspberry Pi Blog 5

Ta-da! Now let’s make things a little more difficult using controllers.

This is the controller I’m going to use (a more detailed example can be found here)

namespace App\Http\Controllers;
use Illuminate\Http\Request;
use ChickenTikkaMasala\GPIO\GPIOManager;
* Class GPIOManagerExampleController
* @package App\Http\Controllers
* Example class to show how to use GPIO manager
class GPIOManagerExampleController extends Controller
    public $rules = [
        'name' => 'required|string',
        'value' => 'required|numeric',
    * @param Request $request
    * @return \Illuminate\Http\JsonResponse
    public function index(Request $request, GPIOManager $GPIOManager)
        return response()->json($GPIOManager->getDetailedList());
    * @param Request $request
    * @return \Illuminate\Http\JsonResponse
    public function action(Request $request, GPIOManager $GPIOManager)
        $this->validate($request, $this->rules);
        $GPIOManager->{$request->name} = $request->value;
        return response()->json($GPIOManager->getDetailedList());

Remember to set up your routes:

Route::get('/gpio', 'GPIOManagerExampleController@index')->name('gpio.index');
Route::put('/gpio', 'GPIOManagerExampleController@action')->name('gpio.action');

The above controller will allow you to update your Manager using an API. Some might argue that using an API for hardware changes could be a bit slow at responding and you’re right it to some degree, it would be slower than accessing the hardware locally. Having said that, using a library like react-php to create a web socket would increase the speed of the response by nearly 90%, so to all you API haters: cache me outside how bout dat…


So, to conclude, using GPIO with a PHP framework can be extremely easy to implement and easy enough to use. It all depends on how far your imagination can stretch!


Share this post

Subscribe to our newsletter

Call Today

01787 319658/0203 0960 360