Introduction to Controlling Servo Motor with Arduino

A Servo motor is a motor controlled based on angles. The motor is given the target angle and it uses an internal feedback mechanism to move to the desired angle. The motor is comprised of the common 5V DC motors, gears, and a potentiometer to determine its position. The motor can then correct any movement based on the potentiometer output. The feedback mechanism enables the servo to make precise angle movements. Servo motors are used to move robot arms, move sensor position, rotate objects. Some of the common application of a servo is in CNC machines where they are used with stepper motors to achieve the desired motion.

In this practical, we will move the servo motor through a 180-degree motion and then move it back to the home position. We will also expand on that by adding a potentiometer as a control device. The servo motor will move based on the position of the potentiometer.

Hardware

Arduino board (Arduino UNO/Mega)

Servo motor

Jumper wires

Potentiometer

Circuit

Most servo motors have three basic connections:

Black/Brown: Ground wire

Red: Positive wire (around 5V)

Yellow/white: control wire

In this practical, we will connect the ground wire to the Arduino ground pin, the power wire to the Arduino 5V pin, and the control wire to Arduino digital pin. The circuit is as follows

Code

#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

  }

}

The servo motor should slowly move to a 180-degree position and slowly move back to the home position (0 degrees).

Let’s Add a Potentiometer

Now we will add a potentiometer and use it to control the position of the servo motor. Arduino will read the potential meter position from the middle pin, map the values to angles and then control the servo motor based on the angle generated. Connect the circuit as shown below

Code

#include <Servo.h>//Servo library

Servo servo_test;//initialize a servo object for the connected servo 

int angle = 0;   

int potentiometer = A0;// initialize the A0analog pin for potentiometer

 

void setup ()

{

servo_test.attach(9); // attach the signal pin of servo to pin9 of Arduino

}

void loop ()

{

angle = analogRead(potentiometer);// reading the potentiometer value between 0 and 1023

angle = map (angle, 0, 1023, 0, 179) ;// scaling the potentiometer value to angle value for servo between 0 and 180)

servo_test.write(angle); //command to rotate the servo to the specified angle

delay (10) ;//allow the servo to move to the desired angle

}

You may also like...

Leave a Reply

Your email address will not be published. Required fields are marked *