Reader small image

You're reading from  Arduino Robotic Projects

Product typeBook
Published inAug 2014
Reading LevelIntermediate
Publisher
ISBN-139781783989829
Edition1st Edition
Languages
Tools
Right arrow
Author (1)
Richard Grimmett
Richard Grimmett
author image
Richard Grimmett

Richard Grimmett has more fun that should be allowed working on robotics projects while teaching Computer Science and Electrical Engineering at Brigham Young University Idaho. He has a Bachelors and Masters degree in Electrical Engineering and a PhD in Leadership Studies. He also has 26 years of experience in the Radar and Telecommunications industries, and even has one of the original brick phones. He has written books on the basics of using the BeagleBone Black for robotics projects, and another for the Raspberry PI and yet another for the Arduino.
Read more about Richard Grimmett

Right arrow

Chapter 7. Controlling Servos with Arduino

In this chapter, you'll learn how to use the basic capability of Arduino to control servo motors.

Servo motors are important because you can use them to create all kinds of useful arms, legs, or even pan-and-tilt mechanisms to make really cool robots that can walk, or pick up things, or move sensors around. You'll then take this to the next level, learning how to add more functionality using a shield to control the speed and direction of a whole set of servos to build a walking hexapod robot.

The basics of a servo motor


Before you begin, you'll need some background on servo motors. Servo motors are somewhat similar to DC motors; however, there is an important difference. While DC motors are generally designed to move in a continuous way—rotating 360 degrees at a given speed—servos are generally designed to move within a limited set of angles. In other words, in the DC motor world, you generally want your motors to spin with continuous rotation speed that you control. In the servo motor world, you want your motor to move to a specific position that you control.

Controlling servos is fairly simple. The device has three wires connected to it: one for the ground connection, one for the drive voltage, and the third is a control signal that expects a pulse-width modulated (PWM) signal. The signal is a square wave that is turned on and off at a set rate, normally at around 500 Hz. The ratio of the length of the time the signal is on to the time the signal is off determines the desired...

Connecting a servo motor directly to Arduino


The first step in connecting a servo motor to Arduino is to actually obtain a servo motor. The following figure shows a typical servo motor, the Hitec HS-311, available at most hobby or RC Control stores:

In order to connect this servo motor to your Arduino, you'll need some of those male-to-male solderless jumper cables that you used in the previous chapters. You'll notice that there are three wires coming from the servo. Two of these supply the voltage and current to the servo. The third provides a control signal that tells the servo where and how to move. You'll connect these three wires to the pins on Arduino. The black wire on the servo is ground; you'll connect that to the GND pin on Arduino. The red wire on the servo is the VCC connection; connect that to the 5 V pin on Arduino. The orange pin is the control pin on the servo; connect that to one of the DIGITAL (PWM~) pins on Arduino, for example, pin 11, as shown in the following figure...

Controlling the servos with a program


Now that the hardware is connected, you'll need to supply the control signal to make your servos move. To control your servo, bring up the Arduino IDE. Make sure that the proper Arduino and port are chosen. Then enter the lines of code as shown in the following screenshot:

This code uses the Servo library that is installed with the standard Arduino IDE. The three sections of code that you'll need to understand are as follows:

  • The global variables servo, servoPin, and angle are used by the program. The Servo data type adds a set of functions so that you can control your servo. This includes the servo.attach(servoPin) and servo.write(angle) functions, which you will use in this program to send the servo to a specific angle. To find out all the different functions that are available, visit http://arduino.cc/en/reference/servo.

  • The setup() function connects the servo functionality to the proper pin and then initializes the serial port.

  • The loop() function reads...

Connecting a servo motor shield to Arduino


The servo motor shield we'll use in this example is available at most online retailers who sell Arduino Uno, and is made by AdaFruit. The following figure shows the servo motor shield connected to the servo wires:

This particular shield can handle up to 16 servos. The important characteristic of this servo shield is the connection on the right-hand side of the shield. In the GND and VCC connections, you'll place your external voltage and current input, allowing Arduino to control many more servos.

Controlling the servo motor shield with a program


Now that your hardware is ready, you'll need to program Arduino to send the proper control signals. To control this shield, you'll need to download a library from Adafruit.

Once you have downloaded the library, you'll need to rename it to install it into the IDE. Look for the Adafruit-PWM-Servo-Driver-Library-master.zip file in the directory you downloaded the file from, and unzip this to the directory where your Arduino library is stored, as shown in the following screenshot:

The Adafruit-PWM-Servo-Driver-Library-master directory should now be in the library directory. You'll need to change the name of this directory to AdafruitServoDriver. The library and its examples are now available. You can open the servo example by navigating to File | Examples | AdafruitServoDriver | servo, as shown...

Summary


You have learned how to control a single servo and an entire set with a servo control shield. Now you can not only build robots that role, but you can also build robots that walk. You know how to easily add even more servos to act as arms, or any number of other functions. However, your robot really can't sense the outside world yet, and is reliant entirely upon someone to control it.

In the next chapter, you'll add sensors so that your robot can avoid/find obstacles and other objects.

lock icon
The rest of the chapter is locked
You have been reading a chapter from
Arduino Robotic Projects
Published in: Aug 2014Publisher: ISBN-13: 9781783989829
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
Richard Grimmett

Richard Grimmett has more fun that should be allowed working on robotics projects while teaching Computer Science and Electrical Engineering at Brigham Young University Idaho. He has a Bachelors and Masters degree in Electrical Engineering and a PhD in Leadership Studies. He also has 26 years of experience in the Radar and Telecommunications industries, and even has one of the original brick phones. He has written books on the basics of using the BeagleBone Black for robotics projects, and another for the Raspberry PI and yet another for the Arduino.
Read more about Richard Grimmett