Wallogit.com
2017 © Pedro Peláez
PHP Class to enable control of the Adafruit PCA9685 16-channel PWM/Servo I2C Interface
PHP Class to enable control of the Adafruit PCA9685 16-channel PWM/Servo I2C Interface, (*1)
I wrote this to allow me to control the Adafruit PCA9685 PWM board to do some custom LED lighting in my living room. The only code I could find for this was in Python. I've been a PHP developer for nearly 20 years, so I naturally wanted to code in something I was more comfortable with., (*2)
It took a lot of reading and piecing together a way to get this working including reviewing quite a bit of the 52-page datasheet for the PCA9685 chip (linked below). In the end, it was worth it as I can now control all the channels of this great little board from a web page on any device in the house., (*3)
As much trouble as I had, I figured others might have the same issues. I decided I'd put the code together as a Composer package and make it available on Packagist. I hope it helps somebody else., (*4)
I'm not going to show how you can get PHP7 & Nginx running on your Raspberry Pi, there are lots of other resources for that kind of thing., (*5)
This code does require that you have already enabled I2C support and installed the i2c-tools., (*6)
For my new (purchased Jan 2017) Raspberry Pi 3, this was easy as I only had to edit the /boot/config.txt file and uncomment the dtparam=i2c_arm=on line and reboot., (*7)
Then I installed the I2C tools:, (*8)
sudo apt-get install -y python-smbus sudo apt-get install -y i2c-tools
You can read up a bit more here: https://learn.adafruit.com/adafruits-raspberry-pi-lesson-4-gpio-setup/configuring-i2c, (*9)
Interface board can be purchased directly from Adafruit here:, (*10)
https://www.adafruit.com/product/815, (*11)
Datasheet for the PCA9685:, (*12)
https://cdn-shop.adafruit.com/datasheets/PCA9685.pdf, (*13)
Installation is best accomplished with Composer (https://getcomposer.org/), (*14)
composer require "phpdreams/pca9685":"dev-master"
require_once(__DIR__ . '/vendor/autoload.php'); use PHPDreams\PCA9685\PCA9685; $pwm = new PCA9685();
I've setup two ways to set the PWM level of any given channel. You can do it by percent or by setting the on and off points., (*15)
$pwm->setPWMpercent($channel, $percent);
Channel is a number from 0 to 15., (*16)
$pwm->setPWM($channel, $countOff, $countOn = 0);
You can set the on and off points in the 4096-count cycle. So setting a channel to turn off at at 2048 would be a 50% duty cycle., (*17)
If you don't set an 'On' point, it will default to 0., (*18)
$pwm->setAll($channel, $countOff, $countOn = 0);
$pwm->setFrequency($frequency);
Defaults to 200Hz refresh., (*19)