marsman2020
Posts: 72
Joined: Sun May 20, 2012 11:26 pm

I2C 16 Channel PWM/Servo Breakout - Working

Thu Jun 21, 2012 6:45 pm

I have successfully attached an NXP PCA9685 16-channel PWM controller breakout (http://www.adafruit.com/products/815) to my Pi, and used the userspace i2cset command to change the registers around and move a servo.

I used the new armel Debian Wheezy with bootc's 3.2.20 r1 kernel from http://www.bootc.net/projects/raspberry-pi-kernel/

I have to say compared to dealing with the spaghetti code of different Arduino libraries for running I2C, etc, it was very easy. Attach device, modprobe i2c-dev to get userspace i2c interfaces, run i2cdetect to make sure it was showing up on address 0x40, and then set registers by using

Code: Select all

i2cset <I2C-BUS> <DEVICE ADDRESS> <REGISTER ADDRESS> <VALUE>
I used a small breadboard power supply (http://www.cutedigi.com/tools/power-sup ... upply.html) to provide 3V3 to the VCC and 5V to the server drive V+ on the breakout board, without taxing Pi's power supply. Tied the ground on the Pi to the ground on the power supply and the breakout board, connected up the SDA and SCL lines, and everything...just worked.

I may write a small python library for the PCA9685, if folks are interested.

Tanga
Posts: 30
Joined: Tue Jan 03, 2012 11:18 pm

Re: I2C 16 Channel PWM/Servo Breakout - Working

Fri Jun 22, 2012 7:11 am

Very keen to see where you go with this. The first step towards a pi powered robot. I just ordered this breakout:
http://www.sparkfun.com/products/10616
for another project, so won't be able to use your code if I keep going that way, but I'm keen to read about what you do as robotics is still pretty abstract and difficult to understand. I just ordered one of these robots, however:
http://www.kickstarter.com/projects/arc ... open-robot
and would be keen to see if I can hook it up to the pi for added connection options and processing power. :)
Raspberry Pi (Raspbian) Tutorials by a fellow noob:
http://noobsquared.blogspot.com.au/

marsman2020
Posts: 72
Joined: Sun May 20, 2012 11:26 pm

Re: I2C 16 Channel PWM/Servo Breakout - Working

Fri Jun 22, 2012 4:32 pm

A fellow makerspace member just did a large (144 channels) PWM driver setup with 3 SPI interface shift register 48 channel PWM drivers, for a massive LED art project.

After watching what was required to make the SPI chips work (and helping a little with getting it running), I decided to go with I2C for the servo application. If I build a rocker bogie mars-rover style robot, there will be a maximum of 10 servos for the driveline and maybe 2 for some other attachments. Electrically and timing-wise I2C is more complicated because of only using 1 wire that the master and the slave pull low/leave high to transmit data, but the userspace tools and python_smbus bindings in Linux seem to be easier to use then SPI devices (mainly because I2C/SMBus is extensively used in computers for sensors)

I do have a SPI LCD backpack also from Adafruit that I want to try, but I need a 3.3V-5V level shifter on the SPI bus to make that work.

marsman2020
Posts: 72
Joined: Sun May 20, 2012 11:26 pm

Re: I2C 16 Channel PWM/Servo Breakout - Working

Fri Jun 22, 2012 11:21 pm

I'm going to push forward with making a PiBot using this controller.

An early parts list/description. The goal is to keep things inexpensive and use what I have around the house.

Chassis:
<Insert easily available thin plate building material here>

Power:
-1 4-AA battery holder
-1 1-AA battery holder
(NiMH cells vary from 1.4V at startup to 1.1v when they are close to dead. 5 will yield 7V-5.5V)
-1 MIC29300-5.0WT 5V, 3A low-dropout linear regulator (http://www.digikey.com/product-search/e ... 76-1121-Nd)
-1 MIC29310-3.3WT 3V3, 3A low-dropout linear regulator (http://www.digikey.com/scripts/dksearch ... 76-2228-nd)
(The 5V regulator will power the Pi, 4 servos, and the Wifi adapter. The 3V3 regulator will power the PWM controller chip and any other future 3V3 I2C devices)
-5x Eneloop 1800mAh AA NiMH Cells

Mobility:
-4 Continuous Rotation Servos (http://www.pololu.com/catalog/product/1248)
-4 Wheels (http://www.pololu.com/catalog/product/1193)

Communications:
-1 TBD USB wifi adapter on a USB extension cord, with the 5V lines cut and attached to the regulator.

Sensors:
TBD

Control:
-Raspberry Pi
-Adafruit 16 Channel PWM Breakout

User avatar
seregus
Posts: 13
Joined: Wed Jul 18, 2012 1:29 pm

Re: I2C 16 Channel PWM/Servo Breakout - Working

Tue Aug 14, 2012 3:54 pm

Hi,

I was just about to give up on my idea to use Raspberry as a heart and brain for my robot project, but randomly found your post!
It looks like it's exactly what I needed to make my robot move and even add a servo to move it's camera using Linux, not real-time OS!

If I may ask - did you accomplished writing up some library for controlling the PWM outputs for this board? If you positive on this, is it okay if I'll ask you to share your code? In any case thank you very much for your post - it was very helpfull!

rfincher1
Posts: 14
Joined: Sat May 26, 2012 5:09 am

Re: I2C 16 Channel PWM/Servo Breakout - Working

Mon Sep 10, 2012 9:19 pm

There is now a tutorial from Adafruit on connecting their 16 channel pwm board to the Pi:

http://learn.adafruit.com/adafruit-16-c ... pberry-pi/

It has a Python library.

Rick

sgo
Posts: 2
Joined: Sat Oct 13, 2012 11:03 am

Re: I2C 16 Channel PWM/Servo Breakout - Working

Sat Oct 13, 2012 11:06 am

Good Idea !

By experience it may be risky to power the Servo and the controlling logic from the same LDO.

(I already made a project with a simple DC/DC converter + two servo and a microcontroller. The current surge on the servo caused so slight voltage decrease and as such brown out and random code on the microcontroller !)

gideon
Posts: 8
Joined: Fri Jun 21, 2013 5:46 pm

Re: I2C 16 Channel PWM/Servo Breakout - Working

Sun Jun 23, 2013 6:09 pm

marsman2020 wrote:I have successfully attached an NXP PCA9685 16-channel PWM controller breakout (http://www.adafruit.com/products/815) to my Pi, and used the userspace i2cset command to change the registers around and move a servo..
Any idea if it would work to control two 5-6v DC motors? The tutorial seems to say it won't?

joerob
Posts: 7
Joined: Sun May 19, 2013 1:22 am

Re: I2C 16 Channel PWM/Servo Breakout - Working

Tue Jun 25, 2013 1:31 am

You seem to have some idea how to use I2C. I am presently very confused. There seem to be several Python support libraries. Which one to use? Why? Some of the samples require root permissions to run, which makes no sense to me. Most examples are tied closely to a specific chip such as MCP23008 and lack generality.

You stated:
userspace i2cset command
I have no idea what that means. Where did you start? What library examples did you use?

As to your motor question, the Adafruit board is just a PWM generator. You still need a motor controller chip. You send the PWM to the motor chip along with 2 logic lines for direction and breaking. You could use an MCP23008 or GPIO for the logic. A common work-around employs servos modified for continous rotation. Servos have the motor controller built-in so that PWM alone is sufficient.

Torsten1
Posts: 4
Joined: Wed Mar 11, 2015 4:16 pm

Re: I2C 16 Channel PWM/Servo Breakout - Working

Wed Mar 11, 2015 7:15 pm

marsman2020 wrote:I have successfully attached an NXP PCA9685 16-channel PWM controller breakout (

Code: Select all

i2cset <I2C-BUS> <DEVICE ADDRESS> <REGISTER ADDRESS> <VALUE>
.

Hello

can you please explain which values to use for <REGISTER ADDRESS> and <VALUE>
to control a servo ?

BR/Torsten

BMS Doug
Posts: 4423
Joined: Thu Mar 27, 2014 2:42 pm
Location: London, UK

Re: I2C 16 Channel PWM/Servo Breakout - Working

Wed Mar 11, 2015 10:38 pm

Torsten1 wrote:
marsman2020 wrote:I have successfully attached an NXP PCA9685 16-channel PWM controller breakout (

Code: Select all

i2cset <I2C-BUS> <DEVICE ADDRESS> <REGISTER ADDRESS> <VALUE>
.

Hello

can you please explain which values to use for <REGISTER ADDRESS> and <VALUE>
to control a servo ?

BR/Torsten
The easiest method is to use the adafruit libraries
rfincher1 wrote:There is now a tutorial from Adafruit on connecting their 16 channel pwm board to the Pi:

http://learn.adafruit.com/adafruit-16-c ... pberry-pi/

It has a Python library.

Rick
If you don't want to use the adafruit libraries you can find a full description of the address registers in the datasheet (pages 9 to 13).
Doug.
Building Management Systems Engineer.

Torsten1
Posts: 4
Joined: Wed Mar 11, 2015 4:16 pm

Re: I2C 16 Channel PWM/Servo Breakout - Working

Thu Mar 12, 2015 1:03 pm


If you don't want to use the adafruit libraries you can find a full description of the address registers in the datasheet (pages 9 to 13).
Thank's!

so ok address space for Port 0 is 0x06 0x07 0x08 0x09

first i have to set the MODE1
i2cset -y 2 0x40 0x00 0

Prescale value default is 0x1e.
i looked into some scripts they calculate the prescale like this, so I have done with PHP

$freq = 60;
$prescaleval = 25000000.0;
$prescaleval /= 4096.0;
$prescaleval /= $freq;
$prescaleval -= 1.0;

$prescale = round($prescaleval,0 ,PHP_ROUND_HALF_UP);

$oldmode = 0x1e;
$newmode = ($oldmode & 0x7F) | 0x10;
(NOW PLAN WHY ?)

know set the new prescale
i2cset -y 2 0x40 0x00 $newmode
i2cset -y 2 0x40 0xFE $prescale
i2cset -y 2 0x40 0x00 $oldmode
$neu = $oldmode | 0x80;
i2cset -y 2 0x40 0x00 $neu

so the new prescale mode is 0x65 from i2cget -y 2 0x40 0xFE
example from code https://github.com/kaosat-dev/adafruit- ... ver.coffee

and then set the port 0
i2cset -y 2 0x40 0x06 0
i2cset -y 2 0x40 0x07 0
i2cset -y 2 0x40 0x08 88
i2cset -y 2 0x40 0x09 2

But nothing move ;(

User avatar
joan
Posts: 16159
Joined: Thu Jul 05, 2012 5:09 pm
Location: UK

Re: I2C 16 Channel PWM/Servo Breakout - Working

Thu Mar 12, 2015 1:26 pm

Torsten1 wrote:
...
But nothing move ;(
All sorts of things could be wrong.

A photo and description of your connections may help.

Torsten1
Posts: 4
Joined: Wed Mar 11, 2015 4:16 pm

Re: I2C 16 Channel PWM/Servo Breakout - Working

Mon Mar 16, 2015 5:15 pm

Hello again

so it works, but i can only controll 90deg.
By hand I can rotate the servo 180 deg.

Making PCA9685 go to sleep
/usr/sbin/i2cget -y 2 0x40 0
0x00

set sleep bit while preserving other bits and ensuring that restart bit is set to 0 when writing to register
/usr/sbin/i2cset -y 2 0x40 0 16
Setting frequency to 60 Hz. Calcualting prescale value prescale=round( 25000000/(4096 * frequency)) - 1 from frequency as 101(PCA9685 clock is at 25MHz or 25000000 Hz)
/usr/sbin/i2cset -y 2 0x40 254 101
/usr/sbin/i2cset -y 2 0x40 0 0x00

usleep 5000
/usr/sbin/i2cset -y 2 0x40 0 128
Setting Channel 0 PWM on at 0 step, off at 150 step in 0 to 4095 steps at 60 Hz
/usr/sbin/i2cset -y 2 0x40 6 0
/usr/sbin/i2cset -y 2 0x40 7 0
/usr/sbin/i2cset -y 2 0x40 8 150
/usr/sbin/i2cset -y 2 0x40 9 0
Setting Channel 0 PWM on at 3 step, off at 0 step in 0 to 4095 steps at 60 Hz
/usr/sbin/i2cset -y 2 0x40 6 3
/usr/sbin/i2cset -y 2 0x40 7 0
/usr/sbin/i2cset -y 2 0x40 8 0
/usr/sbin/i2cset -y 2 0x40 9 0

JasonBourne68
Posts: 1
Joined: Wed Jan 27, 2016 1:15 pm

Re: I2C 16 Channel PWM/Servo Breakout - Working

Wed Jan 27, 2016 1:21 pm

Should this:

Setting Channel 0 PWM on at 3 step, off at 0 step in 0 to 4095 steps at 60 Hz

Not be something like this:

Setting Channel 0 PWM on at 0 step, off at 600 step in 0 to 4095 steps at 60 Hz ?

So:

/usr/sbin/i2cset -y 2 0x40 6 3

should be:

/usr/sbin/i2cset -y 2 0x40 6 0

And:

/usr/sbin/i2cset -y 2 0x40 8 0

Should be:

/usr/sbin/i2cset -y 2 0x40 8 600

phpdreams
Posts: 2
Joined: Fri Jan 13, 2017 5:06 am
Location: Mountain Home, Arkansas USA

Re: I2C 16 Channel PWM/Servo Breakout - Working

Tue Feb 28, 2017 4:59 am

I know this is a fairly old post, but some of the info in this thread helped me along the way. I just put together a PHP Class/Package that will control the Adafruit PCA9685 PWM/Servo board quickly and easily. You can get it along with instructions here:

Git Hub: https://github.com/phpdreams/pca9685

Hope it helps!

Bill

Return to “Automation, sensing and robotics”