Pi Servo pHAT (v2) Hookup Guide

Contributors: QCPete, santaimpersonator
Favorited Favorite 6

Python Examples

Note: This section includes examples in reference to the Python package. If you are looking for the original example code, it has been moved to the following archived section.

The example code for this product is located in the GitHub repository for the Python package; it is also hosted with the ReadtheDocs documentation:

To run the examples, simple download or copy the code into a file. Then, open/save the example file (if needed) and execute the code in your favorite Python IDE. For example, with the default Python IDLE click Run > Run Module or use the F5 key. To terminate the example use the Ctrl + C key combination.

Example 1

We will only cover the first example; however, we will also be breaking down how the code works and explaining how it functions.

Import Dependencies

The first part of the code, imports the required dependencies to operate.

import pi_servo_hat
import time

Initialize Constructor

This line instantiates an object for the device.

test = pi_servo_hat.PiServoHat()

Soft Restart

This line soft resets the PCA9685 chip, clears the MODE1 register, and returns the PWM frequency to the default 50 Hz.


Test Run

This section of the code moves the servo arm to 0° by outputting a 1 ms PWM signal, pausing for a second, then it moves the arm to 90° by outputting a 2 ms PWM signal, and holds the position for a second.

# Moves servo position to 0 degrees (1ms), Channel 0
test.move_servo_position(0, 0)

# Pause 1 sec

# Moves servo position to 90 degrees (2ms), Channel 0
test.move_servo_position(0, 90)

# Pause 1 sec

Note: The inputs for the servo position will be approximate, as it is limited to the resolution of the PWM signal. Users can move to Example 3 for a demonstration of this.


The final section of the code repeatedly sweeps the servo arm from 0° to 90° and then back. The code iterates with ~1° increments and prints the input position.

while True:
    for i in range(0, 90):
        test.move_servo_position(0, i)
    for i in range(90, 0, -1):
        test.move_servo_position(0, i)

Note: As nothing is perfect, the servo position may vary from the expected input. This limitation is usually due to the accuracy of the servo; however, part of this can be attributed to the timing on the PWM signal. (The PWM signal to the servo is affected by the resolution of the output and the PWM frequency; however, these factors are negligible in comparison to the servo accuracy.)

With servos, you get what you pay for. High quality performance servos cost significantly more than the cheaper alternatives. Various factors of their performance, including size, torque, response time, and position accuracy attribute to cost. Therefore, if you are using a cheaper alternative you may need to compensate in the software.

Additionally, with some servos, the full range of the servo arm rotation extends beyond the expected 90° or 180°. The final commented section of the code can be used to take advantage of this factor; however, it should be used with caution as it may permanently damage or destroy the servo.