This guide will help you get started with a PiicoDev® Servo Driver module. We'll take a tour of the module and walk through some examples to drive common servos. We'll even drive multiple servos independently - up to a maximum of 16!

Transcript

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 drive servos with the PiicoDev Servo Driver and a micro bit. We'll connect this hardware together to run both positional and continuous rotation servos.

The 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 of course need a micro bit, a PiicoDev adapter for micro bit, servo driver, and PiicoDev cable to connect everything together. You'll need a power supply to power your servos because they're quite power hungry. This is a 5 volt 3 amp power supply with USB-C connector. And of course, you'll need at least one servo.

Servos come in all different shapes and sizes. I'm going to work with both a positional and a continuous rotation servo. Servos 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 and press it onto the splined. to the PiicoDev directory and open PiicoDev unified and PiicoDev server.py

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. Plug your MicroBit into the adapter, connect your PDF cable to the adapter and connect the other end to your servo driver. Connect to your computer with USB and finally connect the servo power.

A note about power: Servers 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 via one of the USBC connectors. 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.

We're going to program in Thonny today. If you've never used Thonny with the MicroBit before, check the guide for more help with getting started. If you already know how to use Thony, proceed to the download PiicoDev module section. Select your Dev board and right click PiicoDev unified, select save link as. I'm going to save this to a PiicoDev directory in my documents. Do the same with picardevserver.py. Open Thony, navigate to the PiicoDev directory and open PiicoDev unified and PiicoDev server.py.

To where you saved your files and connect to your micro bit, select both files, right click and upload to micro bit. Now return to the article and find the example for driving angular servers. I'm going to start with my angular server. If you only have a continuous rotation server, we'll get to that example next.

Find the example code and select all of that code, copy with control C and into a new script in funny paste that code. Click the Green Run button and if prompted to save, I'll save this to my MicroBit as main.py and the server sprung to life.

Let's have a look at that again. The server jumps around in about 90 degree increments, sweeps back and then slowly sweeps again. Let's take a look at the code to see what's going on. We start the script by importing a sleep function and importing the PiicoDev server modules. We initialize the pikadev servo driver using the initialization function and that returns a driver object. We call this controller wherever you see controller in this code, we're referring to this physical Servo driver that we call the PiicoDev server initialization function. That takes Arguments for the servo driver that we need to attach to, that's this controller and the channel number that the server is plugged into. In this case, we're in channel number one, so we put a one in the next argument. This returns a PiicoDev Servo object that we simply call Servo. So wherever you see Servo in this code, we're referring to the physical Servo plugged into controller at Channel One.

Controlling the servo angle is really easy, we just set the servo dot angle attribute. We start by setting it to zero, there's a short delay, we set it to 90-degrees, another short delay, 180-degrees and then back to zero. That's that stepped Behavior at the start of the script. Then after another delay, we sweep the servo slowly. directions

From 0 to 180-degrees, we use a for loop for X in range from 0 to 180 in steps of five. So our angle is going to jump in small steps of five degrees and then we just set that servo angle to that value and do a short sleep. Now we could remix this code. I'll delete everything except the first two assignments to angle, so 0 and 90-degrees. We could make a while true loop, highlight everything, tab it into the true lift and now when we run the code this will just go from 0 to 90-degrees and back forever.

When we initialize a server in this way, this is the default initialization that uses some default server parameters. If your server has an angle range different to 180-degrees or it's making some funny noises, you probably need to stop and change the way that you set up the servo. If I comment out that setup and uncomment this setup, this is the more explicit server initialization that includes the minimum pulse length in microseconds, the maximum pulse length in microseconds and the angular range of the servo in degrees. This is all information that you can get from a server's data sheet.

Now these default values are generally safe for most servers but not every server from every manufacturer behaves in the same way. This tuning information is literally the timing information that gets sent down the signal wire. We won't dwell too much on exactly what it means, but suffice to say these numbers ought to match the specifications for the servo in its data sheet.

I've swapped my angular server for my continuous rotation server. It's time to look at how to drive a continuous rotation server. Jump back to the article, find the example for continuous rotation servers, highlight all that example code and paste it over the top of all the code in Main. Nothing for it but to run the code. Here the server runs in both directions.

There are two paths forward if your servo has a tuning potentiometer. You can use a screwdriver to finally dial that potentiometer until the server comes to a rest. If your servo does not have a tuning potentiometer, we can still tune the servo using code. Try making small adjustments up or down to the midpoint_us argument. This is the midpoint of the servo signal in microseconds. It may be that we just need to tune this a little higher or a little lower until we find that zero point.

In this example, I pretended I couldn't use the potentiometer and found the zero point at 1550 microseconds. It will help with your tuning if you remove all of the other code so that the only command issued to your servo is the stop command.

Let's take a closer look at this code. We begin with the same imports and a very similar setup except this time for PiicoDev servo. We're working with the midpoint and range arguments. These are similar to the min and max arguments from the angular example except with continuous rotation servos it's a little easier to think about things as the midpoint and the range.

That's because the midpoint is the point where the servo will stop spinning. For continuous rotation servos, we use the dot speed attribute and this accepts values between negative one and one. Here we set speed to 1 which is fast in the forward direction, 0.2 to slow things down, and then we go to negative 0.2 to go the same speed but in reverse. Finally, we go fast reverse and the script ends with speed equals zero to stop.

Now it's not too hard to drive multiple servos too. Here I've connected one angular servo to each channel.

I'll start by remixing the Angular Servo example. I'll remove all the code that we don't need. Each real server 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 server. Now we have four server instances in our code that we can control independently.

I'll create an infinite loop that just sets each server to the same angle in a sequence and there's a delay between each Servo. When I run the script, we can see each server 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 server 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 server driver to tell them apart. I renamed my first driver controller A and the second one is controller B.

Eight is a lot of servers to write code for, so I'll put them all in this list called servers and 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!

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. And so there you have it, controlling servers with the PiicoDev server driver and a micro bit.

If you make anything cool from these starter projects or you just have some questions, let us know on our forums. We're full-time makers and happy to help. Until next time, happy making!

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.