How a servo listens · 04 of 13

4096 counts

The chip cuts every PWM period into 4096 counts and sets each pulse as a number of them. At 50 Hz one count is about 4.88 µs, so 1500 µs is 307 counts. Every one of those numbers rests on the chip's internal clock, which the datasheet gives only as 25 MHz typical.

One period, 4096 counts

The PCA9685 has one counter for all sixteen channels. It counts from 0 to 4095 once per period and starts again. Each channel holds two numbers: the count at which its output goes HIGH, and the count at which it goes LOW. For a servo the first is 0 and the second is the pulse width.

4096 counts, and how long each one is
prescale 121, 50.03 Hz
Frequency asked for50 Hz
Oscillator
Prescale
121
Real frequency
50.03 Hz
One count
4.88 µs
OFF count, 1.5 ms
307
Pulse out
1498 µs (-2)
205 counts between 1000 and 2000 µs. At 50.03 Hz a count is 4.88 µs, so 1500 µs is 307 counts. Most of the 4096 go on the gap between pulses; the part a servo uses is the thin band on the left.

The counter's speed comes from the chip's 25 MHz oscillator divided by a prescaler. The datasheet's formula for it is:

prescale = round(25 000 000 / (4096 × frequency)) − 1

For 50 Hz that is 121. The period is then 20.0 ms (50.03 Hz), and one count is 122 / 25 MHz, about 4.88 µs. So:

PulseCounts
1000 µs205
1500 µs307
2000 µs410

writeMicroseconds() in the Adafruit library does exactly this division and writes the result. Between the two conventional ends there are about 205 steps.

One frequency for all sixteen

There is one prescaler, so every channel runs at the same frequency. The range is 24 Hz (prescale 255) to 1526 Hz (prescale 3). At power-up the prescaler is 30, which is 200 Hz.

The chip also powers up asleep, with its oscillator off, and the prescaler can only be changed while it sleeps. After waking it needs 500 µs before the outputs are right. The library's setPWMFreq() handles all three: sleep, write, wake, wait.

The number nobody can know for you

Every figure on this page assumed the oscillator runs at exactly 25 MHz. The datasheet says 25 MHz typical and gives no tolerance. If the chip runs fast or slow, every pulse on every channel is stretched or shrunk by the same ratio.

Adafruit's servo example passes 27 MHz to setOscillatorFrequency(). That is what Adafruit's example assumes, not a property of this board, and this book does not claim either number for yours. How far off it can matter: were a chip really at 27 MHz while the sketch assumed 25, a command of 1500 µs would come out at about 1390 µs.

Measuring your own chip

Set one channel to a known frequency and measure its period with an oscilloscope or a logic analyser, on the PWM pin and GND. Then:

oscillator = measured_Hz × 4096 × (prescale + 1)

Use the prescale the chip is actually running, which is 121 for a sketch that assumed 25 MHz and asked for 50 Hz. As an example, a reading of 53.0 Hz there means the oscillator is 53.0 × 4096 × 122, about 26.48 MHz. Put that number in the sketch's setOscillatorFrequency() and every channel is corrected at once.

Each chip has its own oscillator, so a board measured once keeps its number. Two boards in one project may need two.

When it does not work

Every servo sits a few degrees off, by the same amount.

The chip's oscillator is not running at the frequency the sketch assumes, which scales every pulse by the same ratio. Measure one channel's period and pass the real oscillator frequency to setOscillatorFrequency(), as this page describes.

I changed the PWM frequency and nothing happened.

The prescaler can only be written while the chip is asleep. The Adafruit library's setPWMFreq() puts it to sleep, writes the prescaler and wakes it again. Code that writes the PRE_SCALE register directly has to do the same.

Why do I see 200 Hz on a scope before my sketch runs?

That is the chip's power-up prescaler, 30, which gives 200 Hz from 25 MHz. The chip also powers up asleep, so nothing comes out until a sketch wakes it. setPWMFreq(50) changes the prescaler to 121.

Can I get finer steps than 4.88 µs?

Not at 50 Hz: the period is always 4096 counts, so the step is the period divided by 4096. That still gives about 205 distinct pulse widths between 1000 and 2000 µs.

Alongside this page
Where this goes next

Two supplies on one board, and the one that decides the voltage on your microcontroller's pins.

V+ and VCC →

Edit this page — content/books/pca9685/4096-counts.mdx

Community

Questions about this product

See what other owners have asked, and read their solutions.

Ask a question ↗

PCA9685 16-Channel Servo Driver Board, USB-C Powered

Loading discussions…

Discuss this article

Ask about this page. The answer stays here, on the page it belongs to, for whoever hits the same wall next.

Browse Modules and blocks on the forum →