Reader small image

You're reading from  Mastering Arduino

Product typeBook
Published inSep 2018
Reading LevelBeginner
PublisherPackt
ISBN-139781788830584
Edition1st Edition
Languages
Tools
Right arrow
Author (1)
Jon Hoffman
Jon Hoffman
author image
Jon Hoffman

Jon Hoffman has over 25 years of experience in the field of information technology. Over these years, Jon has worked in the areas of system administration, network administration, network security, application development, and architecture. Currently, Jon works as a senior software engineer for Syn-Tech Systems. Jon has developed extensively for the iOS platform since 2008. This includes several apps that he has published in the App Store, apps that he has written for third parties, and numerous enterprise applications. He has also developed mobile applications for the Android and Windows platforms. What really drives Jon the challenges that the field of information technology provides and there is nothing more exhilarating to him than overcoming a challenge. Some of Jon's other interests are spending time with his family, robotic projects, and 3D printing. Jon also really enjoys Tae Kwon Do, where he and his oldest daughter Kailey earned their black belts together early in 2014, Kim (his wife) earned her black belt in December 2014, and his youngest daughter Kara is currently working towards her black belt.
Read more about Jon Hoffman

Right arrow

Servo Motors

When power is supplied to a brushed DC motor, it will begin to continuously spin until the power is cut off. This makes brushed DC motors very good for such items as turning the wheels on a robot or the blades on a fan. There are times when we need more precise control over how much the motor turns. For example, to control a robotic arm, we would need the motors to turn at a precise amount to put the arm where it needs to be. For applications like this, we can use a servo motor.

In this chapter, you will learn:

  • How to control a servo motor
  • How to use the Arduino servo library
  • How to power a servo motor

Introduction

The types of servo motors that we will use with the Arduino are pretty small, but most have fairly high torque and are very energy efficient. This allows us to use these motors for industrial-grade applications such as robotic arms, conveyor belts, autofocus lenses in cameras and even solar-tracking systems for solar panels.

A servo motor is made up of a DC motor, which does the actual work; a potentiometer, which controls the amount of power going to the motor; control circuitry, which controls the movement of the motor and gears. The following photograph shows a servo motor connected to a robotic claw:

A servo motor contains three wires for the control signal, power and ground. The signal wire is usually orange or yellow. The power is the usual red, and the ground wire is usually brown or black.

Some smaller servo motors can use the 5V out on the Arduino...

Components needed

In this chapter, you will need the following components:

  • One Arduino Uno or compatible board
  • One servo motor (the code has been tested with the MG996R servo. However, any standard servo should work)
  • One potentiometer
  • One 4 AA battery holder with batteries to power the servo motor
  • Jumper wires
  • One breadboard

Circuit diagrams

The following diagram shows how to connect the servo motor with the Arduino:

In this project, we will use the potentiometer to control the position of the servo motor. Notice that the potentiometer uses the 5V power source from the Arduino while the servo motor uses 6V (4 × 1.5V) from the batteries; however, the two power sources share a common ground. Now let's look at the code to control the servo motor.

Code

Both the Arduino IDE and Web Editor come with a servo library that we can use simply by including the header file. The following code will do this:

#include <Servo.h>

Next, we need to define the pin that the servo motor and the potentiometer are connected to. The following code will connect the signal wire to the digital 3 pin and the potentiometer to the analog 0 pin on the Arduino:

#define SERVO0_POT 0
#define SERVO0_OUT 3 

Now we need to define an instance of the Servo type as shown in the following line:

Servo servo0;

Within the setup function, we need to call the attach() method from the servo instance to initialize the instance and to tell it what pin the servo is attached to. The following code shows this:

void setup() {
  servo0.attach(SERVO0_OUT);
}

We will want to define a function that will read the potentiometer and set the position of the servo position...

Challenge

For the challenge in this chapter, you will need a 6 DOF (Degree of Freedom) robotic arm. The following image show what a 6 DOF robotic arm looks like:

For this challenge, you will need to figure out how to wire the remaining servo motors to the Arduino and the correct power configuration. You can order 6 DOF robotic arm kits from Amazon or eBay. Go to their site and do a search for 6 DOF robotic arm. Prices for these kits varies greatly depending on the size and power of the arm/claw. You can get the robotic arms prebuilt, or as a kit that you need to build yourself.

Summary

In this chapter, we learned how a servo motor works and how we can control them with an Arduino. We also saw what components make up a servo motor. In the next chapter, we will see how to use a relay board.

lock icon
The rest of the chapter is locked
You have been reading a chapter from
Mastering Arduino
Published in: Sep 2018Publisher: PacktISBN-13: 9781788830584
Register for a free Packt account to unlock a world of extra content!
A free Packt account unlocks extra newsletters, articles, discounted offers, and much more. Start advancing your knowledge today.
undefined
Unlock this book and the full library FREE for 7 days
Get unlimited access to 7000+ expert-authored eBooks and videos courses covering every tech area you can think of
Renews at $15.99/month. Cancel anytime

Author (1)

author image
Jon Hoffman

Jon Hoffman has over 25 years of experience in the field of information technology. Over these years, Jon has worked in the areas of system administration, network administration, network security, application development, and architecture. Currently, Jon works as a senior software engineer for Syn-Tech Systems. Jon has developed extensively for the iOS platform since 2008. This includes several apps that he has published in the App Store, apps that he has written for third parties, and numerous enterprise applications. He has also developed mobile applications for the Android and Windows platforms. What really drives Jon the challenges that the field of information technology provides and there is nothing more exhilarating to him than overcoming a challenge. Some of Jon's other interests are spending time with his family, robotic projects, and 3D printing. Jon also really enjoys Tae Kwon Do, where he and his oldest daughter Kailey earned their black belts together early in 2014, Kim (his wife) earned her black belt in December 2014, and his youngest daughter Kara is currently working towards her black belt.
Read more about Jon Hoffman