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
/**
**************************************************
*
* @file Servo_Sweep.ino
* @brief Sweep a hobby servo on channel 0 back and forth using PCA9685.
*
* @authors @ soldered.com
***************************************************/
#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);
}
}
Before you run it, plug the servo into the CH1 header, which is channel 0 in the code: its signal wire on OUT, its power wire on V+ and its ground wire on GND. Then connect a supply suited to your servo to the screw terminal's V+ and GND, because the Qwiic cable does not carry servo power. Hardware Details shows the servo power connections.
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 NoneNative 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
| Type | Name | Description |
|---|---|---|
uint8_t | _address | I2C address of the board. Defaults to PCA9685_DEFAULT_ADDRESS (0x40). |
begin()returns voidInitializes the PCA9685 over I2C and sets the default 50 Hz PWM frequency, which is what hobby servos expect. Call it once in setup().
ReturnsNothing.
setServoPulseRange(uint16_t minPulseUs, uint16_t maxPulseUs)returns voidConfigures 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
| Type | Name | Description |
|---|---|---|
uint16_t | minPulseUs | Pulse width in microseconds mapped to the minimum angle. Library default: 500 us. |
uint16_t | maxPulseUs | Pulse width in microseconds mapped to the maximum angle. Library default: 2500 us. |
setServoAngleRange(float minAngle, float maxAngle)returns voidConfigures the angle range, in degrees, accepted by setServoAngle().
ReturnsNothing.
Parameters
| Type | Name | Description |
|---|---|---|
float | minAngle | Angle in degrees mapped to minPulseUs. Library default: 0. |
float | maxAngle | Angle in degrees mapped to maxPulseUs. Library default: 180. |
setServoAngle(uint8_t channel, float angle)returns voidDrives 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
| Type | Name | Description |
|---|---|---|
uint8_t | channel | Channel number, 0 to 15. |
float | angle | Angle 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.
setServoAngle() assumes setPWMFreq(50), the default begin() sets. The
PWM frequency is shared by all 16 channels: setPWMFreq() takes no channel,
so a call made for an LED changes all 16 outputs too, the servo signal
included. Leave the board at 50 Hz while it drives servos.