16 Servos and 1 Raspberry Pi! These single board computers are exceptional and here is the best way to control a whole bunch of 5V servos with them.

Transcript

Hey gang, Tim here at Core Electronics. Today, I'm going to give you all the knowledge you need to control 16 servos using a Raspberry Pi via this 16-channel PWM HAT from Adafruit.

The Raspberry Pi is an exceptional single board computer, but one thing it isn't great at is controlling lots of DC motors. And these kinds of servos all have DC motors wearing inside them that make them work. DC motors need very specific and repetitive timing pulses to operate. These pulses are referred to as pulse width modulation or PWM. I have a link down in the description of a guide all about PWM so if you're interested in knowing more, just hit it up.

So this HAT swoops into the rescue, adding the capability to control 16 servos with absolutely perfect timing whilst utilizing only two pins for I2C communication. To learn about I2C communication with the Raspberry Pi, check out the guide I have linked in the description.

Here in the table is everything you will need. Naturally, you'll need the Adafruit 16-channel PWM servo hat. Keep in mind that these headers need to be soldered on for the HAT to function correctly. I'm going to use a Raspberry Pi 4 model B, but any Pi with easy access to GPIO will be perfect for this setup. You will also want a microSD card that has been flashed with the Raspberry Pi OS, a power supply, a monitor, HDMI cord, and keyboard and mouse. Simply everything you would need to run this Single-Board Computer like a desktop machine.

We also have this 5 volt 4 amp DC power supply which we will use to power the HAT and attached servos. Powering servos can take a lot of power and by utilizing two power supplies, we prevent the Raspberry Pi from ever being starved of electricity. And of course 5 volt servos. Lots of 5 volt servos.

Now, the fastest way to get up and running with this Adafruit HAT is as follows. Line up the headers, and solder them into place to fix the HAT to the top of the Raspberry Pi. Then connect the power and I2C cables to the HAT and Raspberry Pi. Finally, attach your servos.

That's it for now, I hope you found this informative and helpful. If you have any questions, feel free to ask in the comment section below and I'll do my best to get back to you as soon as possible.need to make use of the Adafruit hat. Once you've pasted in the first line of code, hit enter and wait for it to finish installing before moving on to the next line. Repeat this process for all three lines of code. Once all packages are installed, you will need to reboot your Raspberry Pi. You can do this by clicking on the raspberry icon on the top left of the screen and then clicking on 'Shut Down'. Once it has fully shut down, you can unplug the USB-C connector and the power supply to the hat. At this point, you can connect up to four servos to the Adafruit servo HAT by simply plugging them into the PWM pins marked zero through three. The servos will remain at their current position until you give them a new command. You can do this by using the appropriate Python library or a third-party program like Scratch or Snap4Arduino. If you have any issues or questions, feel free to reach out to our technical support team for assistance. To start, let's run the first script named "basic servo control.py". This will activate the attached standard servo. I'm going to right-click it and select "Open with Thonny IDE". Thonny IDE is just a Python interpreter software, and you can use whatever software is your preference. With the script open, let's press the big green play button, and we should see the servo spring to life.

Jumping into the code quickly, you can see at the start that time and the Adafruit servo kit library have both been imported to this script. After this, there's an initializing statement. What's written here will mean that...

You're also going to want to enable I2C communication on your Raspberry Pi. You do this by going up to the top left, going into preferences, clicking on Raspberry Pi configuration, opening up Raspberry Pi configuration, clicking across to interfaces, and enabling I2C. Press "OK" and give it a quick reboot.

All the scripts that I utilize in this video can be found and downloaded at the bottom of the full article, link to that in the description. Then it's just a simple matter of unzipping that downloaded file onto your desktop or wherever you deem appropriate.

If prompted, type and enter "Y" to continue the installation process. Each line starts with "sudo", which means each line is going to be run with admin privileges. The first two lines will provide the framework for the HAT to work correctly with your Raspberry Pi. As you can see right here, an error message has popped up. This is because this particular package has been made obsolete by the most recent Bullseye OS. You only need to type this first line if you're using an older OS version, and the final line provides everything needed to control this HAT through Python code. Once that last one is complete, you'll have everything you need to control your servos with ease.We have full access to the 16 PWM channels of the Raspberry Pi hat. After this initializing line, we dive straight into deciding the angle of the servo using the kit dot servo with the index number in brackets, which in this case, connected to channel zero is zero. Then by adding on the dot angle to the end of that line, it will know that we want to alter the angle of the servo, and in this particular case, we've made it zero. By making this equal zero, this line is telling the servo to rotate to its zero position.

After this, I created an infinite loop using a while true statement. Once it enters this loop, any lines that are tabbed are going to be repeated until I manually tell the script to stop. In this loop, I start by telling the program to sleep for three seconds, and then I use the exact same kit dot servo zero dot angle from before, but this time I turn it to 180. This is going to rotate the servo through to its full arc of freedom to the 180 degree, sleeping again, and then returning itself back to the zero angle.

Say you instead wanted to control a continuous servo, the game plan using this HAT is very similar. Just to clarify, whilst a regular servo only turns over a narrow range with precise control over position, a continuous rotation server has a shaft that spins continuously with control over the speed and the direction.

So let's start by replacing the standard servo with this continuous one I have right here. We're going to take care to attach it to the top of the HAT exactly the same way with the ground side closest to the edge of the hat. With that done, let's open up the script basic servo continuous control dot py in the same way as before into Thonny IDE.

Then, as soon as I press the big green run button just like so, our continuous servo springs to life. Jumping into the script, you can see it looks very similar to the one before. In fact, the first three lines are exactly the same.

The next line sets up the continuous servo profile for the zero PWM port and gives it a throttle value of zero. Throttle values that it accepts can be from negative one to one. Negative values will drive the continuous servo in the reverse direction. With a correctly calibrated continuous servo, this right here making it zero will make this stand still.

Then a while true infinite loop is created just like before and we then start rotation at max speed. Then we have a two-second pause and then the next line is half speed but in reverse.

As a note, when utilizing these little servos, if you find it running when you have the throttle set to zero, the best way to fix the problem is to adjust the potentiometer found at the bottom. You will need to use a screwdriver to do this. You're going to rotate this potentiometer with the throttle set at zero, and you're going to adjust it until the continuous servo stops rotating. Then rerun this code to check that everything is working properly.

The final code I'm going to show you is going to take a little bit of setting up, so bear with me just for a sec. Before me are my battalion of standard servos. Keep in mind that any of these can be any type or size of servo so long as they're 5 volts, they are compatible with this Adafruit hat.

I've created one final script called max servo control dot py. I'm going to open it up the same way as before and run it now. Very satisfying.

In regards to connections, I've just filled up each one of these PWM channels. Looking at the code, you can see that the first three lines are exactly the same. Then we set up each servo for each of the index numbers all the way up to 15 to equal the angle zero.

Then we sleep for two seconds, then we create this variable called t which equals 0.5. Then we create our "while true" infinite loop, and we create a for loop that rotates each of them as fast as they can in one direction but for only 5 seconds. After that, we move the servo back to 0-degrees and then we rotate it as fast as we can in the opposite direction for five seconds.

Overall, the function of this code is to cycle through all of the servos connected to the Adafruit HAT and move them in opposite directions for five seconds each. It is a very effective way to test all servos at once and ensure they are working correctly. While true statement. This is our infinite loop that's just going to keep going around and around and around. And the very first thing it does is it focuses on the servo attached to the channel zero and it tells that servo to go at an angle 180 degree and then sleep t and t is that 0.5.

Then it tells that same servo to then go to the angle zero. So we're just flicking it and then flicking it back. Then I'm telling the machine to sleep t divided by two so half t which is 0.25 seconds.

It then does that same thing for the servo attached to the index number one does the same for the servo index number two and so on so forth. Then after all of that it goes t equals t divided by 1.5 which shortens this t time and then it repeats once again.

Hopefully this has got you thinking and willing to jump into this world. If you only need to control one or two servos or one with a unique PWM requirement through your Raspberry Pi I've also created a guide to do that. Hit up the description to jump into it and with that until next time stay cozy.

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.