PCA9685 16-channel servo driver
A 16-channel, 12-bit PWM board that runs sixteen servos from two I2C wires, with the Adafruit PWM Servo Driver library built in.
The blue board with sixteen servo headers along one edge. It makes sixteen servo pulses, or sixteen PWM outputs of any duty, from two I2C wires, so an Uno can run a robot arm, a hexapod or a camera rig without timing a single pulse itself. Robot arm kits use it for exactly that.
Pins
| Pin | What it does |
|---|---|
VCC |
The chip's logic supply, 2.3 V to 5.5 V: the board's 5 V or 3.3 V. |
GND |
Ground, shared with the board and the servo supply. |
SDA, SCL |
The I2C bus. The board has 10k pull-ups on both. |
OE |
Output enable, active low. Pulled low on the board: leave it unwired. |
V+ |
The servos' supply, from its own 5 V source. The chip does not use it. |
PWM0 to PWM15 |
Each channel's signal: the yellow pin of its servo header. |
On the board each channel is a three-pin header, and its red and black pins
are V+ and GND. Here each channel is its signal pin, and a servo's red and
brown wires go to the same V+ and GND.
Using it
The Adafruit PWM Servo Driver library is built in:
#include <Wire.h>
#include <Adafruit_PWMServoDriver.h>
Adafruit_PWMServoDriver pwm = Adafruit_PWMServoDriver(); // address 0x40
void setup() {
pwm.begin();
pwm.setPWMFreq(50); // the servo frame
}
void loop() {
pwm.writeMicroseconds(0, 1500); // channel 0 to the middle
pwm.setPWM(1, 0, 205); // channel 1: high from count 0 to 205
}
A period is 4096 counts. At 50 Hz a count is about 4.9 us, so a 1 ms to 2 ms servo pulse is 205 to 410 counts.
Things the real chip makes you get right
- It wakes up asleep, with the oscillator off and every channel full off.
begin()wakes it; a sketch that writes the registers by hand has to clearSLEEPinMODE1first, then wait half a millisecond. - The frequency can only be set while it is asleep.
setPWMFreq()puts the chip to sleep, writes the prescale and wakes it again. A prescale written while awake is ignored. - Auto-increment is off out of reset. Without it, the four bytes of a
channel's
ONandOFFcounts all land in one register.begin()turns it on. - Outputs change at the end of the transfer that wrote them, not byte by byte.
The oscillator
The datasheet's oscillator is 25 MHz typical, and real chips run a few
percent off, which is why the library has setOscillatorFrequency(). The
simulated chip runs at exactly 25 MHz, so leave the library at its default.
A sketch copied from a tutorial that sets 27 MHz gets pulses about 8 percent
shorter than it asked for.
What the board shows
Under each channel the board prints the pulse width it is putting out, in microseconds, and the chip prints the frequency, or "sleep" while the oscillator is off.
What it does not model
The spread of real oscillators, the external clock input, the sub-addresses and the software reset call. The all-call address 0x70 does work.
See it in a project
The Pick and place arm on a PCA9685 runs a robot arm from one.