Skip to main content

Servo sweep

What this example shows​

Driving a hobby servo by angle instead of raw ticks, and sweeping it back and forth across its configured range. This is the board's primary advertised use case.

The example code​

Select Board
Servo_Sweep.ino
#include "PCA9685-SOLDERED.h"

PCA9685 pwm;

void setup()
{
pwm.begin(); // sets 50Hz PWM frequency by default, suitable for hobby servos

// Adjust to match your servo's datasheet if it doesn't reach full range
pwm.setServoPulseRange(500, 2500); // microseconds
pwm.setServoAngleRange(0, 180); // degrees
}

void loop()
{
for (int angle = 0; angle <= 180; angle += 1)
{
pwm.setServoAngle(0, angle);
delay(15);
}

for (int angle = 180; angle >= 0; angle -= 1)
{
pwm.setServoAngle(0, angle);
delay(15);
}
}

Expected result:

No serial output. A hobby servo connected to channel 0 sweeps from 0 to 180 degrees and back, each direction taking a little under 3 seconds, then repeats.

Functions used​

PCA9685(uint8_t _address = PCA9685_DEFAULT_ADDRESS)returns None

Native constructor. The board's I2C address is fixed by the JP1-JP6 solder jumpers, so pass the address that matches how your board is set, or leave it at the default.

ReturnsConstructor; no return value.

Parameters

TypeNameDescription
uint8_t_addressI2C address of the board. Defaults to PCA9685_DEFAULT_ADDRESS (0x40).
begin()returns void

Initializes the PCA9685 over I2C and sets the default 50 Hz PWM frequency, which is what hobby servos expect. Call it once in setup().

ReturnsNothing.

Parameters

This function takes no parameters.

setServoPulseRange(uint16_t minPulseUs, uint16_t maxPulseUs)returns void

Configures the pulse width, in microseconds, that setServoAngle() maps to the minimum and maximum angle. Match this to your servo's own datasheet if it does not reach its full mechanical range at the library's default.

ReturnsNothing.

Parameters

TypeNameDescription
uint16_tminPulseUsPulse width in microseconds mapped to the minimum angle. Library default: 500 us.
uint16_tmaxPulseUsPulse width in microseconds mapped to the maximum angle. Library default: 2500 us.
setServoAngleRange(float minAngle, float maxAngle)returns void

Configures the angle range, in degrees, accepted by setServoAngle().

ReturnsNothing.

Parameters

TypeNameDescription
floatminAngleAngle in degrees mapped to minPulseUs. Library default: 0.
floatmaxAngleAngle in degrees mapped to maxPulseUs. Library default: 180.
setServoAngle(uint8_t channel, float angle)returns void

Drives a hobby servo on the given channel to an angle, clamped to the configured angle range. Internally converts the angle to a pulse width and calls setPWM(), assuming the 50 Hz frequency begin() sets by default.

ReturnsNothing.

Parameters

TypeNameDescription
uint8_tchannelChannel number, 0 to 15.
floatangleAngle in degrees, clamped to the range set by setServoAngleRange().

Putting it together​

setServoPulseRange() and setServoAngleRange() are one-time setup calls: run them once, before the first setServoAngle(). After that, setServoAngle() does the angle-to-pulse-width math and calls setPWM() for you, so you never need to compute tick counts by hand for a servo.

The header documents setServoAngle() as assuming setPWMFreq(50), the default begin() sets. If something else on the same board has since called setPWMFreq() to a different value, for example for an LED on another channel, keep servo channels at 50 Hz rather than relying on setServoAngle() at a different frequency.