In this guide, we'll learn how a Servo works, the difference between a Positional Servo and Continous Rotation Servo, how to use a Servo with a Raspberry Pi Pico, and what to consider when choosing a Servo.

Transcript

Servos are awesome devices that enable your project to interact with the real world. They are popular amongst makers and professionals alike and you can find them in many industrial applications, even in robotics.

Today we're going to look at how servos work, how to use them and what to consider when choosing a servo. To follow along with the examples in this video, you'll need a Raspberry Pi Pico and either a positional or rotational servo, or both if you have them handy. You'll also need a servo horn, you'll usually get a small bag of them when you purchase a servo. I've put links in the guide to all the hardware I'll be using. Servos come in a unique shape which usually contains a controller, motor and gearbox. Depending on the type, they can be rotated to an exact angle or set to a specific speed. They work using a signal called Pulse Width Modulation or PWM for short, which is a pulsing signal that is sent at high speed from a microcontroller, such as a Raspberry Pi Pico. As the width of the pulse signal changes, so does the angle or speed of the servo.

Note, not all PWM is the same. Some devices like the Raspberry Pi use digital PWM, which can cause jitter in the servo. If you're experiencing this, you might need a servo driver, which I've linked in the guide if you want to check it out. There are two main types of servos, positional and continuous rotation. A positional servo can be rotated to an exact angle, usually between 0 and 180-degrees. These are the most common servos and are used in things like steering in RC cars and joints in robotic arms.

A continuous rotation servo behaves more like a common motor and as the name suggests, turns continuously. With these servos, instead of controlling angle, you control speed and direction. We'll talk a bit more about these later. Let's have a look at how to use a positional servo. Let's start by wiring up the servo with a Raspberry Pi Pico. Servos use three wires, one for ground, one for positive, and one for PWM. Connect the ground wire to a ground pin on the Pico and the positive wire to the VBUS pin on the Pico. Then connect the PWM wire to GPIO16. You can choose any other GPIO if you prefer. Note that if you plan on using the servo for actual work, you should connect it to an external power supply.

To make controlling the servo easier, we will use the MicroPython servo library instead of coding the PWM signals by hand. Plug in the Pico and open up Thonny. From there, go to Tools, then Manage Packages, and search for the MicroPython servo library. Once you see it, click on the library and wait for it to load, then click install. To check that the library is installed, look for the 'lib' folder with 'servo' inside of it in the Raspberry Pi Pico file manager. If you can't see the file explorer, go to view and click 'files'.

Next, head to Example One in the guide and copy and paste the script into a new file in Thonny. Save the script to your Pico and run it. You should see the servo moving back and forth.

Now let's take a look at the script. First, we import time and the servo library. We need time for delays and the servo library to interact with the servo. We create an instance of the servo and pass in the GPIO pin we connected the PWM wire to on the Pico. A variable is created to change the delay. In the script, we can see that it is quite easy to adjust the servo angle later. Next, we start an infinite loop and then we start another loop which steps up from 0 to 180. As the loop counts up, it will change the angle of the servo from 0 to 180. We print the current angle so we can see it on the plotter and then we instruct the servo to physically move to that position using myservo.write(). Finally, we tell the Pico to delay for this many milliseconds. This allows the servo to move and also slows things down so we can actually see them.

Once this loop runs for 180 times, we start a second loop which just does the exact same in reverse. This moves the servo backwards from 180 to 0. So that's pretty cool, but it doesn't give us much control over the servo. Let's have a go at directly controlling the servo to move to specific positions.

Head over to example 2 in the guide and copy-paste that code into a new script. Now run the script, and we can see the servo jumping around to specific angles. Stop the script, and we'll have a look at how it works.

Again we import time and servo, and we set up our servo to the myservo variable. This time we start another infinite loop, but we do something different. We're doing three chunks here that look very similar.

In the first chunk, we set the servo to 90-degrees. 90 is halfway between 0 and 180. So this is our midpoint on the servo. Next, we sleep for one second so that we can see that it's moved and stopped. And we move to the second chunk. This time we move to 0-degrees. This can be thought of as the furthest left position. Again, we sleep for another second so that we can see that it's moved there. Finally, in the last chunk, we move to 180-degrees, which can be thought of as the rightmost position.

Finally, we do one more sleep for one second, and the loop repeats. As an optional side quest, have a go at updating the script with your own angles.Let’s have a look at rotational servos. Under the hood they are controlled the exact same as positional servos using PWM. To see this in action, let's jump back to example one. Stop the Pico and Thonny and unplug the positional servo. Let's replace it with a rotational servo. I'm putting a wheel on mine so we can see it move more accurately.

Open the file from example one back up and we're going to make one small change to slow things down so we can see the changing speed a little easier. What we want to change is the delay ms variable. I'm going to change it to about 100 milliseconds. Let's save that and run the script and observe what happens with a rotational servo. As you can see, the servo sped up in one direction and then it sped back down and then it did the same in reverse, speeding up in the reverse direction and then slowing back down. You may notice that the script was written with position in mind and that's fine. Changing the position in the script is really only changing the underlying PWM signal and for rotational servo PWM determines the speed and direction instead of angular position.

The MicroPython servo library has been designed with only positional servos in mind. If you're working with the rotational servos a lot you might want to use a library that is more focused on them. Like this one. This library claims to be for continuous servos so I would give that a try. Now let's take control of this servo. Stop the currently running script in Thonny and open the script from example two. If you change the example two go back to the guide and copy paste the script again. Run the script again and you will see the servo stop, spin at full speed in one direction and then full speed in the other direction.

Let's have a look at how this works. This is the exact same script. When working with a continuous rotation servo, the process is similar to that of an angular servo. However, instead of setting an angle, we need to set a speed. In the case of an angular servo, we would set 90-degrees to indicate the midpoint. With a continuous rotation servo, the midpoint represents a standing still position where the servo is not moving. When steering hard left or to zero degrees with the angular servo, the servo would move at full speed in one direction, whereas moving to the far right would have the continuous rotation servo spinning in the opposite direction at a full speed. To get a better feel for this, you can update the script with your own values.

Now that we have a grasp of how to use servos, we can focus on choosing the right one for our project. Several factors come into play when choosing a servo. Arguably, the most important aspect is assessing how much power and torque are required. It is crucial to pick a servo that can deliver enough torque to move the intended parts properly. Additionally, you need to ensure that the servo's voltage range aligns with your circuit's capability and that your circuit can provide adequate current to allow proper servo operation. You can check out a guide on how to assess torque and power while selecting a servo for more in-depth details.

The size of the servo is another factor to consider, with standard and micro being the most commonly available options. Micro servos are typically very small, making them perfect for enclosing in tight spaces. Nonetheless, they have smaller motors, which inevitably results in lower torque. In contrast, standard servos are significantly larger than micro servos and can hold larger motors, hence providing higher torque.

Lastly, there are other considerations like plastic or metal, which are worth noting. Gears are an essential part of servos and come in various types, of which plastic and metal gears are the most common. Servos with plastic gears are cheaper compared to those with metal gears, which last longer and are usually found in servos with more torque.

Another important aspect to look for while choosing a servo is the spline size. The spline of a servo is the exposed shaft that you attach your servo horn arm or wheel to. Most servos come with a small bag of accessories that will fit it, but if you are buying additional accessories, make sure they fit the spline size of the servo you are choosing.

Apart from the gears and spline size, you can also find more exotic servos that use things like serial rather than PWM or control. While PWM is by far the most common control interface, other control methods are also available. If you need help choosing a servo for your project or having trouble getting your servos up and running, let us know about it over on the forums. 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.