PHP Classes

PHP Led Raspberry PI: Control led lights to turn on and off

Recommend this page to a friend!
  Info   View files Example   View files View files (4)   DownloadInstall with Composer Download .zip   Reputation   Support forum (1)   Blog (2)    
Last Updated Ratings Unique User Downloads Download Rankings
2021-09-15 (11 months ago) RSS 2.0 feedNot enough user ratingsTotal: 138 All time: 9,143 This week: 213Up
Version License PHP version Categories
led_raspberry 1.0.0Custom (specified...5PHP 5, Hardware
Description Author

This class can control led lights to turn on and off.

It can access system variables in a Raspberry PI device to perform several types of operations to control led lights. Currently, it can:

- Turn on and off the led lights
- Set the control variables like export, unexport, direction

Innovation Award
PHP Programming Innovation award nominee
August 2021
Number 7
Raspberry PI is a popular platform for creating small hardware devices that can be controlled by software written in many languages, including PHP.

This package demonstrates how to control a Raspberry PI-based LED light system using PHP.

Manuel Lemos
Picture of Rafael Martin Soto
  Performance   Level  
Name: Rafael Martin Soto <contact>
Classes: 13 packages by
Country: Spain Spain
Age: 48
All time rank: 256967 in Spain Spain
Week rank: 25 Up2 in Spain Spain Up
Innovation award
Innovation award
Nominee: 7x

Winner: 4x



Class for manage directly leds on raspberry pi in PHP

We can access GPIO ports directly with fwrite() for power on|off leds, without use shell_exec() function. The result is a fasted library to access GPIO ports. This one is written to manage LEDS, but with a litlle changes you can use it for other components (buttons, relay, ....)


- A minimum (minimum, minimum, minimum requeriments is needed). Tested on:

- Simple Raspberry pi (B +	512MB	700 MHz ARM11) with Raspbian Lite PHP7.3 (i love this gadgets)  :heart_eyes:

# FILES: There are 2 files:

led.class.php -> Master class. This file is the main file that you need to include in your code.

example.php -> Code with example use of the class

# INSTALLATION: A lot of easy :smiley:. It is written in PURE PHP. Only need to include the files. Tested on basic PHP installation

     require_once( 'led.class.php' );


- Create the variable with led class on GPIO 18:

    $Led18 = new Led( '18' );

- Power ON GPIO led 18:


- Power OFF GPIO led 18:


- Unexport GPIO port 18:




  • You can pass $fpUnexport & $fpExport file handles if you have more than 1 led. In this way, only 1 file will be openned for Export and Unexport. If not params given, every led will open and close individually files for export and unexport. With $fpUnexport & $fpExport params yo will get a better performance.

Without file handles:

     $Led18 = new Led( '18' ); // Without file handles
     // Here your code using power on off on leds as you want
     // ......
     // Remember do Unexport at the end of your code

With file handles:

     $fpUnexport = fopen( '/sys/class/gpio/unexport', 'w' );
     $fpExport   = fopen( '/sys/class/gpio/export', 'w' );
     $Led18      = new Led( '18', $fpUnexport, $fpExport); // With file handles
     // Here your code using power on off on leds as you want
     // ......
     // Remember do Unexport & close files at the end of your code
     fclose( $fpUnexport );
     fclose( $fpExport );

Power ON led:


Power OFF led:


Unexport GPIO PORT:



     require_once( 'led.class.php' );
     // Create the variable with led class on GPIO 18:
     $Led18 = new Led( '18' );
     // Power ON GPIO led 18:
     // Wait 5 seconds
     //Power OFF GPIO led 18:
     //Unexport GPIO port 18:

Of course. You can use it freely :vulcan_salute::alien:

By Rafa.

@author Rafael Martin Soto

@author {@link Inatica}

@blog {@link Rafael Martin's Blog}

@since July 2021

@version 1.0.0

@license GNU General Public License v3.0

  Files folder image Files  
File Role Description
Accessible without login Plain text file example.php Example Example script
Plain text file led.class.php Class Class source
Accessible without login Plain text file LICENSE Lic. License text
Accessible without login Plain text file Doc. Documentation

 Version Control Unique User Downloads Download Rankings  
This week:0
All time:9,143
This week:213Up
User Comments (1)
Great work overall!
11 months ago (Filip Komar)
For more information send a message to info at phpclasses dot org.