Arduino Tutorial Series : Lesson 07 - Servo Motors



Let's make things moving...

So far we made some progress on arduino programming, but things get more interesting when the project have some moving parts. So let's add servo motors to our project.

Servo Motors

Servo motors are a kind of motors where we can control the rotation angle. This means we can give an exact angle to the motor to rotate and stop at. 

Servo motors have internal circuit which is used to keep track of the rotation angle and maintain it according to a reference given by our arduino. So, we only need to give the reference and every other thing will be taken care by the servo motor itself. 


Contorlling servo motor with Arduino

Hardware Setup

For our tutorial we will use Tower Pro - Micro Servo



Basically a servo motor has three wires,

  • Power (Red)
  • GND (Black/Brown)
  • Control/Signal (Yellow)


Connect power to 5v pin of Arduino and GND to GND pin. Connect control pin to Digital pin 9 of the Arduino.

WARNING

It is better to give power to servo motor from outside source instead of Arduino 5v pin. If the servo is large you will surely need to have a power source. Excess current draw by motors can cause damage to the Arduino.

Code

#include <Servo.h>

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

void setup() {
  myServo.attach(9);  // attaches the servo on pin 9 to the servo object
}

void loop() {
  myServo.write(30);                  // sets the servo position to 30
  delay(1000);                           // waits for 1s
 myServo.write(60);                  // sets the servo position to 60
  delay(1000);                           // waits for 1s
 myServo.write(120);                  // sets the servo position to 120
  delay(1000);                           // waits for 1s

}


 For the first time in out tutorial series we are using a Library here.
As you can see at the very beginning of the code we have imported ' Servo.h ' library. The Servo library contains all the thing we need to control a servo motor.

First we need to create a servo object,

Servo myServo;

In setup() function we have initialized the servo control pin,

myServo.attach(9);

This attaches Digital pin 9 to myServo object for controlling the servo.
Then we can use following command to rotate the servo to certain angle.

Syntax:

myServo.write(ANGLE);


Potentiometer servo control

We can use our previous tutorial knowledge to control servo motor using a potentiometer input.



#include <Servo.h>

Servo myServo;  // create servo object 

int potpin = 0;  // analog pin used to connect the potentiometer
int val;    // variable to read the value from the analog pin

void setup() {
  myServo.attach(9);  // attaches the servo on pin 9 to the servo object
}

void loop() {
  val = analogRead(potpin);            // reads the value of the potentiometer (value between 0 and 1023)
  val = map(val, 0, 1023, 0, 180);     // scale it to use it with the servo (value between 0 and 180)
  myServo.write(val);                  // sets the servo position according to the scaled value
  delay(15);                           // waits for the servo to get there
}


Hope you got a good idea about servo motor control with arduino, Try new things with servo motors. See you with the next tutorial ;)

No comments:

Powered by Blogger.