Servo Motors, or simply servos, are integrated motors that contain their own power and control circuitry. They're generally available in two flavors: positional or angular. Servos can be accurately driven to specific angles and are often used in radio control equipment for control surfaces and steering. You'll also see them in robotic arms. Continuous rotation or 360 degree servos are essentially a motor with a speed and direction control in a handy package.
G'day! I'm going to show you how to get started driving servos with the PiicoDev Servo Driver and a Raspberry Pi. We'll get these two connected to two kinds of servos and run some example code so you can create angular or continuous rotation motion. Let's get started!
The PiicoDev Servo Driver features the standard PiicoDev connectors for daisy-chaining to other modules. There's four servo channels, a high-power input for providing power to the servos, a power LED to let you know that it's on, and an address switch. This address switch allows you to add up to three additional servo drivers to the same PiicoDev bus. For now, make sure both these switches are in the off position.
To follow along, you'll need a Raspberry Pi that you can run like a desktop computer connected to the internet. You'll also need a PiicoDev adapter for Raspberry Pi and Servo Driver and a PigaVive cable to connect everything together. You'll need at least one servo. Servos come in lots of different shapes, sizes, and types, but I'm going to use these two today. I have an angular servo and a continuous rotation servo.
A note about power: Servos can be really power hungry. The regular PiicoDev bus and cables simply can't provide enough current to drive these motors, so we'll need to connect an external power supply by one of the USB-C connectors. interfaces make sure that SPI is enabled
Adapter like this one or for portability you may want to use a USB battery bank and USB cable. Exactly how much current your project really needs depends on how many servers you drive at the same time, how big those servers are and how much torque they need to deliver. A good starting point is to budget about 0.7 amps per small servo at about 2.4 amps for a large servo.
These are usually packaged with some assorted mounting hardware; these are the servo horns and some fasteners to hold them on and secure the servo. You can even get some special servo wheels which are great for continuous rotation servos. For now, pick your favourite servo horn, press it onto this blind shaft and secure with a screw.
There are four servo channels labelled one through four. Connect your servo to channel one. Servers usually come with a standard three pin connector with connections for ground, power and signal on the server driver; these are labelled negative, positive and sig respectively. Make sure you connect your server correctly as they may be damaged if you plug them in the wrong way around. Your server may have different colored wires to this one; in general, the darkest wire is the negative wire. You can also identify the negative wire on the server connector itself; it's indicated by a small facet or chamfer that runs along one edge of the connector; that's the negative pin.
Connect your picotiv adapter to your Raspberry Pi. For a Raspberry Pi 4, this arrow will point towards the ethernet connector. Connect one end of the applicative cable to the adapter and to connect the other end to your servo driver. Then connect your servo power.
We're ready to boot up the pi. My Pi has booted up and I need to make sure that it's going to work with picadev. In the applications menu, go to preferences, Raspberry Pi configuration and under interfaces make sure that SPI is enabled. to 180 and then we set the servo angle to the current value of the loop variable and then we delay for a quarter of a second
The Interfaces tab makes sure you have I2C enabled. Next, under the Applications tab, go to Programming and for this tutorial, we're going to work in Funny. To program in Thonny go to Tools, Manage Packages and search for Picadev. There it is and make sure you install or upgrade to the latest version.
I'm starting with an example for an angular server. If you're using a continuous rotation server, we'll get to that one next in the article. For this tutorial, find the example for angular servers and copy all of that code into Funny. Ctrl C to copy and Ctrl V to paste and we can press that Green Run button to run the script and the server springs to life immediately.
Nice! Let's take a closer look at the script. We start by importing a delay function and then we import the modules that control the servo. We have the servo driver for this piece of hardware and the PiicoDev server for this piece of hardware. We start by initializing the servo driver. We call the PiicoDev server driver initialization function and we call that controller. So anytime you see a controller in this code, we're referring to this physical servo driver. We use the Picadep servo initialization function and we need to give it two arguments: the controller that it's attached to and the channel of that controller. So here, we're referring to a controller because that's the server driver we just created and we're connected to channel one.
Anytime you see servo in this code, we're referring to this physical server. And now, driving the server to different angles is as easy as setting the servo.angle attribute. The script starts by setting zero degrees, a delay 90-degrees, 180-degrees and then back to zero. That was the stepping of the servo at the start of the script. Finally, we use a for loop to sweep the server more slowly. So we say 4x in range from 0 to 180 and then we set the servo angle to the current value of the loop variable and then we delay for a quarter of a second. here but instead of setting the angle we're setting the speed and the speed can be a value between minus and plus one hundred
Initializing a Servo in this way initializes it with the default timing. Servers are digital devices driven by a pulse and that pulse comes from the servo driver. Different servers from different manufacturers may have different timing requirements and so we can comment out this simple setup and uncomment this more explicit setup. This includes Arguments for minus and Max us. These are the minimum and maximum pulse lengths that are expected by that type of Servo. You can find that information in a server's data sheet. There's also a degrees argument because not all servers are 180-degrees; some might be more and some might be less. If you find that your server is making a buzzing sound at the ends of its travel, it's probably a good idea to have a look at the servo data sheet and update these properties.
To set an angle of 180-degrees in steps of five degrees, we set that angle and then there's a short delay. Let's see that again: step, step, step and then another step. That was the first part and now the slow sweep. And if we change this to a step of one degree (which means we can just remove it because one is default) and we change this to 20 milliseconds, if I run the script again we should get a slower, smoother sweep.
I'm going to save this script as angular servo.py to a PiicoDev directory in my home directory. Let's move on now to the continuous rotation server example. I can unplug my angular server and bring in my continuous rotation server again, making sure it's plugged in the correct way around. In the article, find the continuous rotation server example. I'll add all of that code, copy it, and back in funny open a new script and paste in the code. Click the Green Run button. So we're turning in one direction quickly and then slowly and then the other direction slowly and then quickly. We have a very similar initialization here, but instead of setting the angle, we're setting the speed and the speed can be a value between minus and plus one hundred.
Before we do the two imports as in the previous example, we initialize the controller and we're calling the same PiicoDev server initialization function but we're passing in some different arguments. Controller and channel one are the same, but this time because we're working with a continuous rotation servo, we're passing in midpoint and range both in microseconds. These are similar to the minus and max us arguments, except with continuous rotation servers it's a little easier to think about it in terms of the midpoint and the range.
To set the speed for a continuous rotation server, we just update the dot speed attribute and you can see we set it to one for a fast, 0.2 for slow, negative 0.2 for slow in the reverse direction, and negative 1 for fast reverse. Then we can set a speed of zero for stop.
Now if your servo does not come to a complete stop like mine, you can see it's kind of starting and stopping. You may need to tune it on the back of your server; there may be a tuning potentiometer. If you don't have that, it's okay, there's another way to proceed. But if you have a tuning potentiometer, you can just turn that part when the speed is set to zero to find the zero point and the server comes to a complete stop.
Now let's imagine you don't have one of those tuning potentiometers. Here I have a continuous rotation servo and I've got code running where all we're trying to do is stop the servo. I don't have a tuning potentiometer, so what can I do? You can update this midpoint value. Let's try incrementing it a little bit, 1550. Okay, that makes it a little bit slower. We need to go a little further though, let's go to 1575 and now it's stopped. And that's why with continuous servers we work in terms of midpoint and range. servos and then I can use a for loop to initialize each one
It's not too hard to drive multiple servos. Here, I've connected one angular servo to each channel of the driver. Now, I'll remix the angular servo example. I'll start by removing all the code that we don't need. Each servo needs its own call to the initialization function, so I'll just copy and paste this line another three times and update the channel number for each servo. Now, we have four servo instances in our code that we can control independently.
I'll create an infinite loop that just sets each servo to the same angle in a sequence and there's a delay between each servo. When I run the script, we can see each servo travels 90-degrees in sequence. Pretty cool, right?
Now, it's also possible to daisy-chain up to four servo drivers on the same bus. Recall this first driver has both address switches off. To add the second servo driver, I'll first set a unique address switch configuration by setting address switch number one, address switch number two stays off. Then, daisy-chain the PiicoDev and USB connections and add an additional four servos.
Now, in the code, I'll be explicit with the address switch setting for my first driver just so we don't get confused. Then, create a second instance of PiicoDev servo driver. Here, the address switch argument ASW encodes the state of the address switches. Since address switch 1 is on, I'll put a 1 in the first column of the argument. The second switch is off, so there's a zero.
Now, for the familiar process of initializing the individual servos and this time we're using a different servo driver to tell them apart. I renamed my first driver controller A and the second one is controller B. Eight is a lot of servos to write code for, so I'll put them all in this list called servos and then I can use a for loop to initialize each one.
We can drive both angular and continuous rotation servos using the pikadav servo driver. We can connect these servers to any channel that we like and we can even connect multiple Servo drivers together to drive a lot of servos. For example, we can use a for loop to perform an action for each server, run the code and there's eight servers being controlled individually and look what happens when I drive them all together to produce this nice wavy effect. Now this idea is extensible for up to four Servo drivers for a total of 16 servers. That's heaps of servos so you may need to do some power measurements to make sure your power supply can handle it.
We hope you found this tutorial interesting. If you make something cool from this little starter project, let us know on our forums. That's also the best place to go if you need any help with this guide. Best of luck and until next time, happy making!
Makers love reviews as much as you do, please follow this link to review the products you have purchased.