Skip to main content

Raw PWM control

What this example shows​

Setting a channel's raw on/off tick counts and ramping them over time. This is the board's basic building block: the servo angle helper on the next page works out the tick counts for an angle and then calls setPWM().

The example code​

# FILE: pca9685-basicPWM.py
# AUTHOR: Fran Fodor @ Soldered
# BRIEF: Ramp raw PWM duty cycle on channel 0 of the PCA9685
# LAST UPDATED: 2026-07-24

from pca9685 import PCA9685
from utime import sleep_ms

pwm = PCA9685()
pwm.setPWMFreq(1000) # fast PWM for LEDs/general outputs, not servos

while True:
for duty in range(0, 4096, 32):
pwm.setPWM(0, 0, duty)
sleep_ms(20)

Expected result:

No serial output. Channel 0's duty cycle ramps from 0 to 4064 in steps of 32, taking a little over 2.5 seconds per ramp, then repeats.

Functions used​

PCA9685(i2c=None, address=PCA9685_DEFAULT_ADDRESS)returns None

Constructor. Sets the default 50 Hz PWM frequency, the 500 to 2500 us servo pulse range and the 0 to 180 degree angle range, and keeps the LED All Call address on. If you pass no i2c object, it checks the MicroPython port name (uname().sysname): on the ESP32 and ESP8266 ports it opens I2C bus 0 with SCL on pin 22 and SDA on pin 21, and on boards that report another port name it raises 'Board not recognized, enter I2C pins manually'. Pins 22 and 21 are the module's default, not a property of your board: if your board's Qwiic connector or I2C header uses other pins, create a machine.I2C object on those pins and pass it in.

ReturnsConstructor; no return value.

Parameters

TypeNameDescription
I2Ci2cInitialized machine.I2C object. Optional; without it, pins 22 (SCL) and 21 (SDA) on the ESP32 and ESP8266 ports.
intaddressI2C address of the board. Defaults to PCA9685_DEFAULT_ADDRESS (0x40).
setPWMFreq(freq)returns bool

Sets the PWM frequency shared by all 16 channels. Clamped to the chip's 24-1526 Hz range.

ReturnsTrue on success.

Parameters

TypeNameDescription
floatfreqDesired frequency in Hz, clamped to 24-1526 Hz.
setPWM(channel, on, off)returns bool

Sets the raw tick counts at which a channel's output turns on and off within one PWM period (4096 ticks). Used directly for LEDs and general PWM; setServoAngle() on the next page is a convenience wrapper around this same call.

ReturnsTrue on success, False if channel is out of range.

Parameters

TypeNameDescription
intchannelChannel number, 0 to 15.
intonTick count (0-4095) at which the output turns on.
intoffTick count (0-4095) at which the output turns off.

Putting it together​

The constructor sets a 50 Hz default, but this example immediately raises it to 1000 Hz with setPWMFreq(), which suits an LED or general PWM load better than the 50 Hz hobby servos expect. setPWM() then places both edges of the pulse: with on fixed at 0, off alone controls the duty cycle, so ramping off from 0 to 4064 ramps the output from fully off to nearly fully on.

To hold a channel steady instead of ramping it, call setPWM() once with the tick values you want and stop calling it again; the PCA9685 keeps generating that signal on its own until you change it.