USD

Servo Motors and Control with Arduino Platforms

By John LeDuc

Contributed By Digi-Key Electronics

In this article, we’ll discuss the following:

(click the link below to skip to any section that suits your needs)

What is a servo motor?

How do they function? (Basic driving methods & techniques)

How to select a servo motor for your application

Working with Arduino platforms

Books & reference links

What is a servo motor?

What is a servo motor, where are they used, what are they made of and, how do they function?

A servo motor is a motor whose shaft turns to position something based off a control signal. They are typically used to steer remote control airplanes by adjusting the wing flaps, flight position for drones, controlling valves used in flow control or continuous drive of wheels for robots. They can be used to position or adjust almost anything you can think of. They consist of a plastic housing which contains a DC motor, a control circuit and a few gears for torque (see Figure 1).

Image of Parallax’s Servo Motor showing the exposed DC motor and gears

Figure 1: Parallax’s Servo Motor # 900-00005-ND showing the exposed DC motor and gears which turn a potentiometer that provides feedback to the control IC for positioning the shaft. A notch on part of a gear limits rotation to 180 degrees. For continuous rotation, the notch can be removed.

How do they function?

Servo motor control of the shaft position comes from using a pulse width modulation signal (PWM) to turn the shaft clockwise or counter clockwise, depending on the pulse width of the signal. Typically, a pulse width of 1 ms will rotate the shaft clockwise and a 2 ms pulse will rotate the shaft counter clockwise. To position the shaft ½ way, or in the middle, a 1.5 ms pulse typically works. You will need 20 ms between each pulse. Figure 2 below shows the timing for each position.

Image of PWM Timing for different shaft positions

Figure 2: PWM Timing for different shaft positions

Image of wiring connections as shown in Parallax’s datasheet

Figure 3: The wiring connections as shown in Parallax’s datasheet.

How to select a servo motor for your application

Determine if you need limited (max of 180 degrees) or continuous rotation. You should also note the amount of torque the shaft needs to be for your application along with the speed of rotation, which is rated in RPM (revolutions per minute).

If you are building a moving object, a robot for instance, continuous rotation servos are typically used to drive the wheels. On the other hand, limited rotation servos can be used for applications such as positioning a model airplane wing flap for flight control, a water valve, a switch, or for assisting a hand grip used for a robotic arm.

Image of Parallax’s High Speed Servo Motor

Figure 4: Parallax’s High Speed Servo Motor # 900-00025-ND which provides continuous rotation of 150 RPM @ 6 VDC

The following chart lists some of the hobby servos available. For more details and a larger selection click this link.

Limited or Continuous Rotation Digi-Key Part number Torque - Rated (oz.-in) RPM Operating DC Voltage Size L/W/H Inches (mm)
Limited 900-00005-ND 38 Limited to 180° 4-6 2.2/0.8/1.6
(56/19/41)
CR 900-00008-ND 38 50 4-6 2.2/0.8/1.6
(56/19/41)
CR 900-00025-ND 22 180 6-8 2.2/0.8/1.6
(56/19/41)
Limited 1568-1320-ND 16.6 Limited to 160° 4.8-6 0.91/0.46/1.14
(23/11.74/29)
CR 1528-1496-ND 44.52 56 4.8-6 2.1/0.8/1.5
(55/20/37)
CR 1597-1197-ND 25.2 100 4.8-6 0.90/0.452/0.94
(23/11.5/24)

Working with Arduino platforms

(if you are new to Arduino – see this article link to learn more)

One of the most popular Arduino boards is known as the “UNO”. There are several popular versions from 3rd party suppliers we distribute, for example Adafruit, SparkFun, Seeed, DFRobot, etc. One of my personal favorites is the Red Board from SparkFun (they made some nice improvements over the original “UNO” board). I’ve also used Adafruit’s “Flora” which is a round circuit board that is cost effective and easy to use. To view all of Digi-Key’s Arduino boards, click this link.

Image of Arduino brand UNO and SparkFun’s Red Board (UNO)

Figure 5: On the left is the Arduino brand UNO and SparkFun’s Red Board (UNO) on the right.

If you are already familiar with using Arduino boards, you may skip the following paragraph:

My description of Arduino is as follows - an IC chip and/or board that uses a standard C based language within a popular IDE (Integrated Development Environment) called Arduino IDE where you can create code/software know as sketches to make the main IC chip (in popular boards it is the ATMEGA328 Microcontroller) activate the I/O pins to do something. There is feedback you can get on screen in the IDE (using the serial command) to show what the IC is doing with data – similar to using Windows Hyper Terminal mode to see the serial feedback from the outside world. Tremendous online support is available along with a ton of books and sample code on the internet. Arduino is always updating their IDE, so make sure you download the latest version. They have a lot of libraries of sample code and functions to make the board do what it does. The Arduino boards as they are called, have standard pin outs and have developed a standard called shields to add different functionality to the board – here is a link to the varieties available from Digi-Key.

Let’s get into driving a servo motor using the Red Board:

First the latest version of the Arduino IDE was downloaded and the program started. Then the Red Board was hooked up.

This board uses a mini-B to A USB cable which serves to power the board and download your sketch or written code to the onboard MCU. In this example, standard library example called “Servo- Sweep” was used to drive my servo (see Figure 6).

Image of where the Servo Sweep program is located

Figure 6: Where the Servo Sweep program is located.

 

/* Sweep

 by BARRAGAN <http://barraganstudio.com>

 This example code is in the public domain.

 

 modified 8 Nov 2013

 by Scott Fitzgerald

 http://www.arduino.cc/en/Tutorial/Sweep

*/

#include <Servo.h>

 

Servo myservo;                                                // create servo object to control a servo

// twelve servo objects can be created on most boards

int pos = 0;                                                           // variable to store the servo position

 

void setup()    {

    myservo.attach(9);                                      // attaches the servo on pin 9 to the servo object

}

 

void loop()   {

    for (pos = 0; pos <= 180; pos += 1)         // goes from 0 degrees to 180 degrees

                                                                                // in steps of 1 degree

{                                                                             

    myservo.write(pos);                                 // tell servo to go to position in variable 'pos'

    delay(15);                                                   // waits 15ms for the servo to reach the position

  }

 

  for (pos = 180; pos >= 0; pos -= 1)           // goes from 180 degrees to 0 degrees

 {            

    myservo.write(pos);                                  // tell servo to go to position in variable 'pos'

    delay(15);                                         // waits 15ms for the servo to reach the position

  }

}

Figure 7: Code for the sweep program. This program is by BARRAGAN modified by Scott Fitzgerald (noted in the code above).

The USB cable was then plugged into the board and then the “Arduino UNO” board was selected under “Tools” at the top left of the IDE. In the same menu, Port was selected to verify that the program picked up which COM port the board was connected to. Finally, the download button on the top left (small arrow pointing right ward) was clicked. After a few seconds, the Red Board flashed some LEDs (indicating the compiled program was being downloaded to the board).

The servo motor was then connected to a separate power supply with the ground tied to the board’s ground (USB power can only drive so much current and the servo is a bit noisy with inductive pulses that are not good for the board).

To control the motor, the PWM input needs to be connected to the board. By looking at the code above, the PWM output is attached to pin 9. Using Figure 3 above, use the white lead or the middle wire, and attach it to pin 9 of the board.

Now it’s time to run the program. The program starts out by zeroing the shaft clockwise as far as it can go and slowly sweeps it counter clockwise 180 degrees and then back the other way - clockwise to zero. An oscilloscope was used to measure the PWM pulse width as it went through the program. It was found that 1.5 ms was correct for 90 degrees and close to 600 µs for zero or all the way clockwise. At 180 degrees, or all the way counter-clockwise, the PWM pulse was close to 2.2 ms. The program worked well and you can change some of the values to see various effects such as locked positions or that changing the delay to a smaller value makes the sweep speedup.

Books & reference links

There are a variety of resources for you to learn more about working with motors online. Here are a few links to help you get further into making something with electronics; Engineering Resources, Maker to Market, and books about electronics.

Disclaimer: The opinions, beliefs, and viewpoints expressed by the various authors and/or forum participants on this website do not necessarily reflect the opinions, beliefs, and viewpoints of Digi-Key Electronics or official policies of Digi-Key Electronics.

About this author

John LeDuc

John LeDuc, Digital Business Project Manager at Digi-Key Electronics, started his career in 1984 assisting Digi-Key customers with technical questions, reviewing and adding products to the catalog. He specialized in supporting the INS8073 Tiny Basic demo board from National Semiconductor. John is now enhancing the digital experience of our engineering customers by collecting and generating unique ideas to make our website better. He holds an Associate degree in Electronics Technology and is a “Midnight Engineer”, tinkering with electronics in his spare time and creating unique designs with his 3D printer.

About this publisher

Digi-Key Electronics

Digi-Key Electronics, based in Thief River Falls, Minn., is a global, full-service provider of both prototype/design and production quantities of electronic components, offering more than six million products from over 750 quality name-brand manufacturers at Digi-Key.