Monday, February 13, 2017

Controlling 16 rc servos with the Adafruit 16-Channel 12-bit PWM/Servo Driver PCA9685 with Orange Pi Lite over I2C


I'll use the command line to do all things, not going into details of the PCA9685 (see my previous post for details), and I'm assuming you have an Orange Pi Lite with armbian installed.



The cabling is shown for reference:
Orange Pi Lite - I2C bus 0

First thing to get are the i2c-tools:
apt-get install i2c-tools

Then you can probe the i2c bus and should see the device already:
i2cdetect -y 0
     0  1  2  3  4  5  6  7  8  9  a  b  c  d  e  f
00:          -- -- -- -- -- -- -- -- -- -- -- -- --
10: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
20: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
30: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
40: 40 -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
50: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
60: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
70: 70 -- -- -- -- -- -- --
The address 0x40 is the chip address, while 0x70 is an additional address of the chip that appears on the bus, it will vanish after modifying the MODE0 register. For details see the datasheet.

Initializing the PCA9685 chip and setting PWM values:
#
#init
#
i2cset -y 0 0x40 0x00 16  #sleep
i2cset -y 0 0x40 0xFE 121 #50 Hz
i2cset -y 0 0x40 0x00 32  #auto-increment
sleep 1s
i2cset -y 0 0x40 0x00 `expr 32 + 128` #auto-increment and restart
#
#write values to the first three channels
#
#auto-increment
i2cset -y 0 0x40 0x06 0 0 255 0 i
#auto-increment
i2cset -y 0 0x40 0x0A 0 0 255 2 i
#four commands
i2cset -y 0 0x40 0x0E 0
i2cset -y 0 0x40 0x0F 0
i2cset -y 0 0x40 0x10 255
i2cset -y 0 0x40 0x11 1
With auto-increment it is possible to write all three channels at once, starting at address 0x06:
i2cset -y 0 0x40 0x06 0 0 255 0 0 0 255 2 0 0 255 1 i
That's it.

Actual numbers for my servos:
i2cset -y 0 0x40 0x06 0 0 240 1 i #max
i2cset -y 0 0x40 0x06 0 0  71 1 i #center
i2cset -y 0 0x40 0x06 0 0 132 0 i #min

1 comment:

  1. Thanks for your post. I use a PIC micro and program in assembly language but no real translation from C code was needed. I tweaked the PWM rate to get exactly 50-Hz for my board and eliminated the "sleep" command and the first "auto-increment" command.

    ReplyDelete