Demonstrated here is the fast way to control either small 9-gram servos or a much more powerful 15KG servo using a GPIO Pin on a Raspberry Pi.

Transcript

Hey gang, Tim here at Core Electronics and today we're controlling a small servo and a big servo using a Raspberry Pi.

Servos are an immensely useful way to turn electrical energy into a rotational or linear motion with high efficiency and with great precision. The servos in this video are all standard rotary actuator servos that can rotate 180-degrees or 270-degrees. The code supplied here will work with most rotary actuator servos and can be adjusted to maximize the effectiveness of the result.

It is worth noting there are also similar looking continuous rotation servos. Much as their name implies, these servos can spin continuously and are controlled by a Raspberry Pi differently, with control over the speed and direction instead of position.

The Raspberry Pi has enough current output to control at least two small 9 gram servos directly. When you start controlling bigger and many servos, they will have a higher current demand than the GPIO pins can supply, so an outside power source will be required for those situations.

This guide will be split into two sections, running a small servo and running a large servo. If you're going to control a whole bunch of servos, check out the Raspberry Pi HAT from Adafruit that will let you control up to 16 really easily.

Now with that, let's start by controlling this small servo. On the table before me is everything you need to make this work. A small servo. Here I have the Makeblock 9 gram micro servo with a 180 degree range. A microSD card flashed with Raspberry Pi OS. A official Raspberry Pi 4 official power supply. Three jumper wires that are male to female. And naturally, a Raspberry Pi. Here I'm using a Raspberry Pi 4, but any Raspberry Pi microprocessor will work.

Connect...To connect the Raspberry Pi to the small servo, start by connecting the ground wire of the servo to a ground pin on the Raspberry Pi. The ground wire is usually black or dark brown. Next, connect the 5 volt power wire (usually red) to the 5 volt pin on the Raspberry Pi. In this example, we will use GPIO18 (pin number 12) as the data pin to control the direction and angle of the servo. The data wire on the servo is usually orange or white.

Insert the microSD card into the Raspberry Pi. Once everything is connected, let's get the code running on the Raspberry Pi. Open a Python interface like Thonny and copy and paste the provided code. You can find the exact code on the article page linked below.

Save and run the code using the buttons in the Python interface. You should be able to hear and see the servo moving immediately.

Now let's take a quick look at the code. It starts by importing a feature called "angular servo" from a library called GPIO0. The next line imports the "sleep" function. Then, a variable called "servo" is created and given details. The data pin is set to GPIO18, and the minimum and maximum pulse width values are defined.

Servo control is achieved by sending the servo a pulse width modulated (PWM) signal. This signal consists of a series of repeating pulses with variable widths, where the width of the pulse determines the angle of the servo arm. Different servos may expect different PWM lengths.

If your 180-degree servo does not rotate fully right away, you can adjust the min and max pulse width values by increments of 0.0001 until you achieve full rotation. If you have full rotation and do not hear any gear straining, you have successfully set up the servo.You have found the best values. As each server has unique PWM specifications, another way of learning this value can come from the data sheet and often at the bottom of our product pages.

Back to the code. It then sets up an endless loop using a while true statement. In this loop, the servo will go from the min angle to the min angle to the max angle with two seconds per second. From the min angle to the min angle to the max angle with two seconds pause between each movement. The angle is decided by degrees and this is going to do that forever. Feel free to alter this code as this is where the real creativity can occur. The servo could actuate for any reason. You just need to code it in.

So with this now working, let's move on to the second part of this guide and run this larger servo. Everything on the table is what you need to run this large servo. This servo in particular is a DF robot metal geared 15 kilogram servo that can turn 270-degrees. You're also going to need a 5 volt DC 4 amp power supply, a DC power jack adapter, jumper cables, male to female and male to male, and a small screwdriver. A Raspberry Pi power supply and naturally a Raspberry Pi and a microSD card with Raspberry Pi OS flash to it.

You're going to connect the ground pin of the Raspberry Pi to the ground wire of the small servo. Black wire to the dark brown black wire most commonly seen on big servos by connecting the two black wires to the DC power jack. This will attach the ground to the power supply as well. Then connect the power wire of the large servo, commonly a red or dark brown color, to the 5 volts of the external DC power supply.

To send the data signal, I will use again the GPIO18, which is pin number 12 as the data pin telling.The servo controls the direction and angle of rotation. This is typically done through an orange or white wire. Now that everything is connected for the larger servo, we need to add power to the system. Before doing so, let's get the code running on the Raspberry Pi. Open up the Python interpreter and copy and paste the code for the large servo, which can be found on the article page. Save the file and start the code by pressing the button. Once the DC power jack is connected, the servo will come alive and start running.

Let's take a closer look at the code. It is similar to the previous code, but there are some differences due to the servo's unusual range of movement, which is 270-degrees. To accommodate this, we adjust the line where the servo variable is created, specifying the minimum and maximum angles. The code in the endless loop is also adjusted accordingly. Additionally, the minimum and maximum pulse widths are different for this servo. To find the appropriate pulse range, refer to the data sheet and adjust the values until the full range of motion is achieved. Remember to divide the numbers from the data sheet by one million to match the code.

If you only have one power plug for this setup, you can use a two-way 2.1mm DC barrel jack splitter and a DC to USB Type-C adapter. This allows you to connect multiple devices to the power source.In this tutorial, I will show you how to control large and small servos directly from a Raspberry Pi. To do this, you will need a power supply that can provide enough current for both the Raspberry Pi and the servo.

First, connect one end of the power supply to the USB-C port on the Raspberry Pi. This will power the Raspberry Pi itself.

Next, connect the other end of the power supply to the servo. This will provide power to the servo.

Now, you have everything you need to control servos from a Raspberry Pi. In this tutorial, I will be using two specific servos, but there are many other great servos available.

For example, you can find servos that rotate 300-degrees or even have internal clutches to protect the gears if the servo is overloaded.

So, with the servo twisting away and hopefully some ideas brewing, I hope you found this tutorial helpful. 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.