How to Control Servo Motor Using Arduino

Posted on

Serial Servo Motor

Servo Motor Control Using Arduino – In some projects, we will control a servo motor via serial communication. Using Arduino we can easily do this.

Servo is a motor that can be controlled to rotate to a certain level. In general, this servo motor can rotate from 0 degrees to 180 degrees. Some can rotate up to 360 degrees. This motor has three wires. Two of them are + and – and one is for data. The data cable is used to control the rotation of the servo motor.

This servo motor is a major component in robots, robot servo, RC cars and RC planes. To control this servo motor is very easy.

With the help of the Servo.h library that has been provided on the Arduino IDE, we only need to enter the number of degrees of motor rotation, the Arduino servo motor will rotate according to the number of degrees we want.

Popular servo used for this tutorial are Servo Tower Pro MG996R, Tower Pro SG5010, AX12 Dynamixel, Tower Pro SG90, Tower Pro MG90, Savox 1268SG, Savox 1267, Savox 1251, Savox 1256, Savox 1258.

PWM On Servo Motor

Servo motors work on the basis of pulse width modulation (PWM). PWM is a method of getting an analog or voltage value from a digital value. A voltage of 0-5 volts is depicted as a digital value of 0-255.

However, to illustrate how large the cycle is, the PWM servo uses percent. Consider the following example:

Arduino pins only produce a maximum voltage of 5 volts. So, a small pulse width will result in a swinging voltage of 0-5 volts.

PWM Duty Cycle

The wider the pulse in the signal cycle, the greater the resulting analog voltage. Write the value if we use the “analogWrite ()” command as follows:

  • analogWrite (0) = 0% = 0 volts
  • analogWrite (64) = 25% = 1.25 volts
  • analogWrite (127) = 50% = 2.5 volts
  • analogWrite (191) = 75% = 3.75 volts
  • analogWrite (255) = 100% = 5 volts

The frequency used in the cycle is 50Hz.

How To Rotate The Servo To A Specific Position?

Now we will try to rotate the Arduino servo motor. Generally to drive a servo motor a maximum cycle time of 2ms is required. 1ms for 0 °, 1.5ms for 90 ° and 2ms for 180 °. Look at the following picture:

PWM Duty Cycle

We will cover the program that will be used in this tutorial, but first feel free to connect the Arduino and the Servo Motor as shown in this image.

How to Control Servo Motor Using Arduino

Servo Motor Program

1. Servo Drive Basic Program

The command to drive this servo motor on the Arduino IDE is “analogWrite ()”. Here is a basic servo program:

#include <Servo.h> 

Servo myservo;

void setup() 
  myservo.attach(9); //Pin PWM
  myservo.write(90); //90 is degree 

void loop() {}

2. Convert Program Char to Int

If we send a serial monitor value, 90 for example, with the aim of rotating the servo to 90 degrees, this won’t work. Because the value we receive from the serial monitor is a char character .Meanwhile, the value that determines the motor position is an integer data type.

To change the data type from char to int we can use the command “Serial.parseInt ()”. For a tutorial on converting data from a serial monitor, read here.

Following are the basic commands to convert the value from the serial monitor to an integer value:

void setup(){

void loop(){
  while (Serial.available() != 0){
    int val = Serial.parseInt();

Servo Control Using Serial Monitor

We will use the arduino as the basic program of the servo controller and convert the above values to drive the servo motor, as follows:

#include <Servo.h>

Servo myServo;

void setup() {

void loop() {
  while (Serial.available() != 0) {
    int val = Serial.parseInt();

The above programs have been tried and found with success. Above is a basic guide on how servo works.

If you understand this, chances are you can understand how to control high-speed, high-speed servo and hybrid servo drives such as 5kg servo motor, 15kg servo motor, 20kg servo motor, 30kg servo motor, 100kg servo motor.

Famous manufacturers such as Tecnica servo, Bosch servo, Lenze servo, Elmo Motion, Beckhoff Servo Drive, Omron Servo, Servo Parker, Lenze 9400, Kuka servo motor, Mitsubishi servo drive, Siemenst sinamic v90, ABB servo motor, Allen Bradley servo motor, Hiwin servo, Panasonic mbdjt2210 servo drive.

Thank you for visiting the Piko Chip website. May be this article is useful.

1 comment

Leave a Reply