Lesson 15 Control a Servo

4492

Overview

In this lesson, we will introduce a new electronic device (Servo) to you, and tell you how to control it with the Arduino UNO.


Components

- 1 * Arduino UNO

- 1 * USB Cable

- 1 * Servo

- Several jumper wires


Principle

1. Servo motor

The servo motor has three wires: power, ground, and signal. The power wire is typically red, and should be connected to the 5V pin on the Arduino board. The ground wire is typically black or brown and should be connected to a ground pin on the Arduino board. Usually the signal pin is yellow, orange or white, and should be connected to a digital pin on the Arduino board. Note that the servo motor draws a considerable amount of power, if you need to drive more than one or two servos, you'll probably need to power them with an extra supply (i.e. not the +5V pin on your Arduino). Be sure to connect the grounds of the Arduino and external power supply together.

2. Servo library

This library allows an Arduino board to control RC (hobby) servo motors. Servos have integrated gears and a shaft that can be precisely controlled. Standard servos allow the shaft to be positioned at various angles, usually between 0 and 180 degrees. Continuous rotation servos allow the rotation of the shaft to be set to various speeds.

3. Key functions:

●attach()

Attach the Servo variable to a pin. Note that in Arduino 0016 and earlier, the Servo library supports only servos on only two pins: 9 and 10.

Syntax:

servo.attach(pin)
servo.attach(pin, min, max)

Parameters:

servo: a variable of type Servo

pin: the number of the pin that the servo is attached to

min (optional): the pulse width, in microseconds, corresponding to the minimum (0-degree) angle on the servo (defaults to 544)

max (optional): the pulse width, in microseconds, corresponding to the maximum (180-degree) angle on the servo (defaults to 2400)

Procedures

Step 1: Build the circuit

1.png


Step 2: Program

_15_servo.ino

/***********************************************************
File name: 15_servo.ino
Description:   The servo motor are rotated to 15 degrees, 30 
               degrees, 45 degrees, 60 degrees, 75 degrees, 
               90 degrees, 75 degrees, 60 degrees, 45 degrees,
               30 degrees, 15 degrees, 0 degrees, and then from 
               0 degrees to 180 degrees and from 180 degrees to
               0 degrees.
Website: www.adeept.com
E-mail: support@adeept.com
Author: Tom
Date: 2015/05/05 
***********************************************************/

#include <Servo.h>
Servo myservo;//create servo object to control a servo

void setup()
{
  myservo.attach(9);//attachs the servo on pin 9 to servo object
  myservo.write(0);//back to 0 degrees 
  delay(1000);//wait for a second
}

void loop()
{  
  myservo.write(15);//goes to 15 degrees 
  delay(1000);//wait for a second

  myservo.write(30);//goes to 30 degrees 
  delay(1000);//wait for a second.33
  
  myservo.write(45);//goes to 45 degrees 
  delay(1000);//wait for a second.33

  myservo.write(60);//goes to 60 degrees 
  delay(1000);//wait for a second.33

  myservo.write(75);//goes to 75 degrees 
  delay(1000);//wait for a second.33

  myservo.write(90);//goes to 90 degrees
  delay(1000);//wait for a second

  myservo.write(75);//back to 75 degrees 
  delay(1000);//wait for a second.33

  myservo.write(60);//back to 60 degrees
  delay(1000);//wait for a second.33

  myservo.write(45);//back to 45 degrees
  delay(1000);//wait for a second.33

  myservo.write(30);//back to 30 degrees
  delay(1000);//wait for a second.33

  myservo.write(15);//back to 15 degrees 
  delay(1000);//wait for a second

  myservo.write(0);//back to 0 degrees 
  delay(1000);//wait for a second
  for(int num=0;num<=180;num++)
  {
     myservo.write(num);//back to 'num' degrees(0 to 180)
     delay(10);//control servo speed
  }
  for(int num=180;num>=0;num--)
  {
     myservo.write(num);//back to 'num' degrees(180 to 0)
     delay(10);//control servo speed 
  }
}



Step 3: Compile the program and upload to Arduino UNO board

Now, you should see the servo rotate from 0 to 180 degrees, and then do it in the opposite direction.

2.jpg


Summary

After learning, you should have known that the Arduino provides a servo library for you to control a servo. By using this library, you can easily control a servo by programming. Just fully play your imagination and make some interesting applications!