Setting up and Using the Adafruit 16-Channel Servo HAT for Raspberry Pi

Updated 16 February 2023

This guide will teach you how to control up to 992 5V Servos through the GPIO pins on your Raspberry Pi! But more practically, without stacking multiple HATS (Hardware Attached on Top), this guide will teach exactly how to control sixteen 5V servos using a single Raspberry Pi single-board computer. This is all possible via the Adafruit Servo HAT which will act as a PWM expander, taking up only 2 GPIO pins and communicating through I2C. To learn a whole bunch about I2C communication with the Raspberry Pi check this is the guide for you. The setup created in this guide will be able to send out 16 unique PWM signals. PWM signals are great for servos (both standard and continuous) and can be used for a myriad of other purposes (like LED light control, motor speed, and even the heat output of a heater). If you want to learn more about PWM signals check out this guide right here to know more! See the contents of the guide below.

-      What You Need
-      Hardware Assembly
-      Terminal Commands
-      Simple Code for Standard Servo
-      Simple Code for Continous Servo
    Where to Now
-      Downloads

If you only want to control one or two servos with your Raspberry Pi come to have a look at this guide Controlling Standard Servos with the Raspberry Pi. If you instead want to use your Arduino to control a whole bunch of servos come to take a look at the Arduino Servo Shield by Adafruit. This is a similar setup that directly attaches to the top of the Arduino. 

The Adafruit Servo HAT Next to a Raspberry Pi 4 Model B  
If you happen to be holding a Raspberry Pi board and not quite sure what it is? Then check out this guide Raspberry Pi Generations to identify it. As always if you have any questions, queries, thoughts, or comments please let me know!


What You Need

Below is a list of everything you will need to create a similar system in your Maker-Verse.

- Any Raspberry Pi Single Board Computer with a Micro-SD Card Flashed with Raspberry PI OS - A guide to flash Micro-SD cards can be found here.

Adafruit Servo HAT - Keep in mind that the headers will need to be soldered onto it to function correctly.

Standard Servo - the Makeblock 9g Micro Servo can turn 180 degrees in total and is a great example. Here is another 9g Servo but made by DFRobot.

- Continuous Servo - the FEETECH FS90R Continuous servo is a great example.

Raspberry Pi 4 Official Power Supply

5V DC 4A Power Supply - for use with the Servo Hat as the Raspberry Pi GPIO Pins will not supply enough current to activate multiple (3 ) Servos.


Hardware Assembly

To start, attach the Hat to the top of the Raspberry Pi by lining up the headers of the HAT with the GPIO pins of the Raspberry Pi. See this happening in the image below. Take care that all the pins are correctly lined up and that the HAT is fully seated.

Connecting up the HAT to the Raspberry Pi- Make sure to line up the GPIO Correct 
With that complete attach the desired Servos to the HAT. Take careful note to connect them correctly. The ground pin side on the HAT is the one closest to the edge of the board. Black or Dark Brown is the normal colour for ground wires on Servos. Red is the normal colour for Positive Wires on Servos. The Data wire which tells the direction/speed/angle of the Servo the go-to is normally an Orange or White colour. Below shows an image of a single servo correctly connected onto the 0 port of the Servo HAT. In code, we will refer and identify the ports by these numbers.

Showing a single Servo connected perfectly in port 0 
With that complete, insert a micro-SD card flashed with Raspberry Pi OS and hook it up as a desktop computer. Add a mouse, keyboard, HDMI to a monitor. Also, power up the HAT with a 5V 4 Amp DC connector. Then power up the Raspberry Pi System by plugging in a USB-C connector and adding power to the servo HAT by plugging in the 5V DC power 4A power supply. See all of these parts, as well as the mouse and keyboard, attached together below.

All part connected together (including the two power supplies)


Terminal Commands

Some packages will need to be installed to your fresh version of Raspberry Pi OS. This will allow correct communication between the Adafruit Hat and your servos. To install these packages we will need to connect our system to the internet, do so by pressing the WIFI button on the top right of your screen and going through the procedure. You will also need to enable I2C connection. To do this open up the Raspberry Pi Configuration menu (found using the top left menu and scrolling over preferences) and enable the I2C Connection found under the Interfaces tab. After enabling reset the Raspberry Pi to lock in the changes. 

Now with your system rebooted and connected to the internet, open a new terminal window by pressing the black button on the top left of the screen. This will open up a terminal window. See the image below of this happening and a big red arrow pointing towards the terminal button that was pressed.

Terminal console Location

This terminal window will enable us to download from the internet the exact packages we require. So now type and enter the following lines into the terminal to get all the packages that you will need. If prompted, type and enter | Y | to continue/confirm installations. See above video to watch these commands being downloaded.

sudo apt-get install python-smbus

sudo apt-get install i2c-tools

sudo pip3 install adafruit-circuitpython-servokit

With that complete you will have everything you need to start controlling your servos with this HAT and your Raspberry Pi single-board computer.


Simple Code for Standard Servo

Below is a simple code that will run a single standard servo that has been attached to the 0 connector point. If you have connected it to spot 5 you would alter any instance of | kit.servo[0] | to | kit.servo[5] |. See the image of the setup further below. You can copy and paste this into either Thonny or Geany (both are just Python Language Interpreter Software) save it and then run it. You can also download this script at the bottom of this page, name of the script is | Basic-Servo-Control.py |

#Here we will import all the extra functionality desired
from time import *
from adafruit_servokit import ServoKit

#Below is an initialising statement stating that we will have access to 16 PWM channels of the HAT and to summon them we will use | kit |
kit = ServoKit(channels=16)

#Below desides the initial angle that the servo which is attatched to Port 0 will be. In this case we will make it zero degrees.
kit.servo[0].angle = 0

#Below will create an infinite loop
while True:
    
#Below will make the system wait for 3 seconds
    sleep(3)

#Below will rotate the Standard servo to the 180 degree point
    kit.servo[0].angle = 180
#Below will make the system wait for 3 seconds
sleep(3) 
#Below will rotate the Standard servo to the 180 degree point
kit.servo[0].angle = 0

All part connected together (including the two power supplies)


Simple Code for Continuous Servo

Below is the simple code that will run a continuous servo using this Adafruit HAT. You can copy and paste this into either Thonny or Geany (both are just Python Language Interpreter Software) save it and then run it. You can also download this script at the bottom of this page, name of the script is | Basic-Servo-Continuous-Control.py |. As a note, if you find it running when you have set the throttle to 0 the best way to fix the problem is to adjust the Potentiometer with a screwdriver (found on the bottom of most common servos). Rotate the potentiometer with the throttle set at 0 and adjust until the continuous servo stops rotating. At that point, it will be correctly dialed in. See the continuous servo and the underneath of the continuous servo highlighting that potentiometer in the image below. 

Continuous Servo Yay

#Here we will import all the extra functionality desired
from time import * from adafruit_servokit import ServoKit
#Below is an initialising statement stating that we will have access to 16 PWM channels of the HAT and to summon them we will use | kit | kit = ServoKit(channels=16) #Below desides the initial speed that the continuous servo which is attatched to Port 0 will be. In this case we will make it 0 which should not move.
kit.continuous_servo[0].throttle = 0
#Below will create an infinite loop while True:
#Below will make the system wait 1 second sleep(1)
#Below will rotate the servo clockwise at max speed kit.continuous_servo[0].throttle = 1
#Below will make the system wait 2 seconds sleep(2)
#Below will rotate the servo counter-clockwise at half speed kit.continuous_servo[0].throttle = -0.5


Where to Now

Let's get crazy with this and control All the Servos all the time! See the script to do this below. Jumping into the script you can see I have just expanded on the previous to send out PWM signals through each port with a 0.2-second delay between each movement. You can copy and paste below into either Thonny or Geany (both are just Python Language Interpreter Software) save it and then run it. You can also download this script at the bottom of this page, name of the script is | Max-Servo-Control.py |. This will use every single PWM signal that this HAT can produce to control 16 different servos independently. Check the image directly below for this all set up. 

All the Servos all the time!

#Below will import all the extra functionality desired and create the initialising statement for access to all 16 PWM signals.
from time import * from adafruit_servokit import ServoKit kit = ServoKit(channels=16)
#Below decides the angle of all servos kit.servo[0].angle = 0 kit.servo[1].angle = 0 kit.servo[2].angle = 0 kit.servo[3].angle = 0 kit.servo[4].angle = 0 kit.servo[5].angle = 0 kit.servo[6].angle = 0 kit.servo[7].angle = 0 kit.servo[8].angle = 0 kit.servo[9].angle = 0 kit.servo[10].angle = 0 kit.servo[11].angle = 0 kit.servo[12].angle = 0 kit.servo[13].angle = 0 kit.servo[14].angle = 0 kit.servo[15].angle = 0
#Below creates an infinite loop while True:
#Below rotates each servo one by one kit.servo[0].angle = 180 sleep(.2) kit.servo[1].angle = 180 sleep(.2) kit.servo[2].angle = 180 sleep(.2) kit.servo[3].angle = 180 sleep(.2) kit.servo[4].angle = 180 sleep(.2) kit.servo[5].angle = 180 sleep(.2) kit.servo[6].angle = 180 sleep(.2) kit.servo[7].angle = 180 sleep(.2) kit.servo[8].angle = 180 sleep(.2) kit.servo[9].angle = 180 sleep(.2) kit.servo[10].angle = 180 sleep(.2) kit.servo[11].angle = 180 sleep(.2) kit.servo[12].angle = 180 sleep(.2) kit.servo[13].angle = 180 sleep(.2) kit.servo[14].angle = 180 sleep(.2) kit.servo[15].angle = 180 sleep(.2)

Now I just need 61 more Adafruit 16 Channel HATS and 976 more 5V Servos and we can max this baby out!


Downloads

Below is a download link for all the scripts utilised in this guide. For ideas on where to go next just check out this!

Attachment - Servo-HAT-Control-Code-new.zip

Have a question? Ask the Author of this guide today!

Please enter minimum 20 characters

Your comment will be posted (automatically) on our Support Forum which is publicly accessible. Don't enter private information, such as your phone number.

Expect a quick reply during business hours, many of us check-in over the weekend as well.

Comments


Loading...
Feedback

Please continue if you would like to leave feedback for any of these topics:

  • Website features/issues
  • Content errors/improvements
  • Missing products/categories
  • Product assignments to categories
  • Search results relevance

For all other inquiries (orders status, stock levels, etc), please contact our support team for quick assistance.

Note: click continue and a draft email will be opened to edit. If you don't have an email client on your device, then send a message via the chat icon on the bottom left of our website.

Makers love reviews as much as you do, please follow this link to review the products you have purchased.