top of page

Arduino with Servo motor

A servomotor is a rotatory or linear actuator that allows for precise control of angular or linear position, velocity and acceleration. Servomotor is made up of four components, namely: dc motor coupled to a sensor for position feedback, gears, potentiometer and control circuitry. Servos are used in projects were precise motor control is needed such as in robotics, CNC machinery or automated manufacturing.

servomotor.jpeg

Servomotor

pros
Servomotors are generally used in high-performance alternative to the stepper motor which we will discuss next. They provide high levels of torque at high speed. They also operate at 80-90% efficiency. Servo motors can work in AC or DC drive, and do not suffer from vibration or resonance issues. The encoder and controller are an additional cost, but they optimize the performance of the overall system.

​

cons
A major drawback is that servo motors are more expensive than stepper motors. Apart from the cost addition of its components, it also leads to more frequent maintenance and higher costs.

​

Servomotors are controlled using PWM pulses (with frequency of around 50Hz), the width of the pulse determine the angle of the servo motor. For example, if you want the angular position to be 180 degrees, then pulse duration should be around 2.5miliseconds. The voltage required to run a servomotor should be between 4v-6v. 

​

To work with PWM pulses, we need a microcontroller to handle the operation. That is  where the Arduino come in handy. With the Arduino servo library, we can skip the hardwork of calculating the frequency and pulse width with just a few lined of code. The library also makes it easy to run more than one servomotor using the Arduino.

​

In this project, we will program a servomotor to rotate from angles 0-90-180-90, then repeats the process.

Schematic

Sketch

​

#include <Servo.h>

​

Servo servo1;  // assign a name to your servomotor

void setup() {
   servo1.attach(9);  // define the pin it is connected to
}

​

void loo() {
   servo1.write(0);   // rotate servo to 0 degree
   delay(1000);       // pause for 1 second

​

   servo1.write(90);  // rotate servo to 0 degree
   delay(1000);       // pause for 1 second

​

   servo1.write(180); // rotate servo to 0 degree
   delay(1000);       // pause for 1 second

​

   servo1.write(90);  // rotate servo to 0 degree
   delay(1000);       // pause for 1 second then repeat loop
}

​

To run more than a single motor, just define the next servo and initialize it connected pin:

​

#include <Servo.h>

​

Servo servo1;
Servo servo2;

​

void setup() {
   servo1.attach(9);
   servo2.attach(10);
}

void loo() {
   servo1.write(0);
   servo2.write(0);
   delay(1000);

​

   servo1.write(90);
   servo2.write(90);
   delay(1000);
}

​

Note: if you are using more than 1 servomotor, then you should power them with with an external 6v battery, as Arduino can not provide enough current to power multiple motors. 

bottom of page