API Reference

pi_servo_hat

Python module for the [SparkFun Pi Servo HAT](https://www.sparkfun.com/products/14328) and [SparkFun Servo pHAT for Raspberry Pi](https://www.sparkfun.com/products/15316). This package should be used in conjunction with the sparkfun_pca9685 package.

class pi_servo_hat.PiServoHat(address=None, debug=None, min_pt=1, max_pt=2)[source]

SparkFun PiServoHat Initialise the qwiic_pca9685 python module at address with i2c_driver.

param address

The I2C address to use for the device. If not provided, the default address is used.

param i2c_driver

An existing i2c driver object. If not provided a driver object is created.

return

Constructor Initialization True- Successful False- Issue loading I2C driver

rtype

Bool

get_pulse_time()[source]

Reads and returns the current min and max pulse times for servo movement

Returns:

tuple: The minimum and maximum pulse times in milliseconds

get_pwm_frequency()[source]

Reads the PWM frequency used on outputs. 50 Hz is recommended for most servos.

Returns

PWM Frequency Range: 24 Hz to 1526 Hz

Return type

Integer

get_servo_position(channel, swing=None)[source]

Reads the specified location for the servo in degrees.

Parameters
  • channel – Channel of Servo to Control Range: 0 to 15

  • swing – Range of Servo Movement 90- 90 Degree Servo 180- 180 Degree Servo

Returns

Esitmated Position (Degrees)

Return type

Float

is_connected()[source]

Is an I2C connection established with the device?

Returns

Device Connection Status True- Connected False- Not Connected

Return type

Bool

move_servo_position(channel, position, swing=None)[source]

Moves servo to specified location in degrees.

Parameters
  • channel – Channel of Servo to Control Range: 0 to 15

  • position – Position (Degrees) Range: Open, but should between 0 and specified servo ‘swing’. The range is not regulated because most servos have extra room for play (i.e. a 90 degree servo may have a +120 degree usable swing). If ‘None’ is specified, the default setting is 90 degrees.

  • swing – Range of Servo Movement 90- 90 Degree Servo 180- 180 Degree Servo

restart()[source]

Soft resets the chip and then clears the MODE1 register to restart the PWM functionality. The PWM frequency is returned to the default 50 Hz setting.

set_duty_cycle(channel, duty_cycle)[source]

Moves servo to specified location based on duty-cycle.

Parameters
  • channel – Channel of Servo to Control Range: 0 to 15

  • duty_cycle – Duty-Cycle (Percentage) Float Range: 0 to 100 (%) Resolution: 1/4096

set_pulse_time(min_pulse_time, max_pulse_time)[source]

Updates the minimum and maximum pulse widths for the servos

Args:

min_pulse_time (float): The new minimum pulse width in ms max_pulse_time (float): The new maximum pulse width in ms

Returns:

bool: Returns true if update was successful, false if an error was encountered

set_pwm_frequency(frequency=None)[source]

Configures the PWM frequency used on outputs. 50 Hz is the default and recommended for most servos.

Parameters

frequency – PWM Frequency Range: 24 Hz to 1526 Hz

Returns

Function Operation True- Successful False- Issue in Execution

Return type

Bool

NOTE: Changing PWM frequency affects timing for servo positioning. Additionally, the servo position needs to be reset for the output control (on all channels).

The output on all channels is initially turned off after the frequency change, but is re-enabled after any of the channels is reconfigured. However, the new PWM frequency will be in affect, so the timing of the outputs on the other channels will be off. (i.e. if a PWM frequency is doubled; the timing of that signal may be halfed.)

sleep()[source]

Set the SLEEP bit to 1, which will unpower the servos. This preserves the life of the servos.

wake()[source]

Set the SLEEP bit to 0, which will power the servos.