Subscribe To Our List

We help electronics hobbyists to create projects related to Micro-controllers. Join our newsletter to get access to new projects first.
Email address
Secure and Spam free...

In this article, you will read about Arduino servo control in detail and also how to control the servo motor manually using the potentiometer and also how to control them by simply uploading code.

What is Servo Motor

Servo motors are DC motors that can be controlled at a particular position. You can also say that servo motors are basically a rotary actuator or linear actuator that allows for their control at specific angle.

Servo motors are available in the field for a long time and they are used in many applications. These are small in size but these are very energy efficient and have many advantages. These can be used in robots, airplanes and in many electronics.

The servo motor shown below is sg90 and we will use it in our project.

sg90

SG90 Servo Motor

Inside of the servo motor

These are actually consist of a circuitry which receives the command and are responsible for their control at particular angle. The circuitry is placed inside the motor unit and it consists of a position able shaft which is fitted with a gear. The servo motor is rotated by an electrical signal which determines that at which angle it will rotate.

To know the working of the servo motor, we will have to take a look inside the servo unit. Inside the servo unit, there is a dc motor, control circuit and a potentiometer. The motor is attached to the control wheel with the help of gears. The potentiometer’s resistance changes with the rotation of motor. The control circuit determines that at which angle and in which direction it will rotate.

Inside servo

Inside of Servo

How it works

The servo motor has actually three wires; one is ground and the other ones are power and signal wire. Electrical pulses or pwm signals are given through the signal wire, which determines the movement of the servo motor. Servo motors move in 180 degrees, 90 degrees in either direction. The 90 degree is defined as the neutral position for the servo motor. The rotor of the servo motor will move in the position based on the pwm signal.

The servo motor accepts a pulse after every 20 milliseconds. The width of the pwm signal will determine that at how much angle the motor will rotate. A pwm signal of 1.5ms will move the motor in 90 degrees, shorter than 1.5ms will move it to 0 degrees and a longer than 1.5ms will move it in 180 degrees.

servo motor duty cycle

Servo Motor Duty Cycle

The servo motors will remain in the position they were moved and if the external force is applied to it to move it in some other direction, then it will resist against it and it will move in the previous direction. The servo motors will not remain in this direction forever, the commands should repeat to keep them in same position.

The actual and the desired position determine the speed of the servo motor. If the motor is near to its desired position, then it will move slowly and if the motor is far, then it will move fast. This is called proportional control.

Circuit Diagram and Explanation

Connect the servo motor with Arduino as shown below.

  • Yellow wire is the signal wire. Connect it to digital pin 2 of Arduino.
  • Brown wire is the ground wire. Connect it to ground of Arduino.
  • Red wire is the power wire. Connect it to 5V pin of Arduino.
arduino servo control

Arduino Servo Control

 

Code

Before uploading the code, download the servo library from here.

#include <Servo.h>   //including the servo library
Servo sg90;          //including a variable for servo named sg90
int servo_pin = 2;

void setup()
{
  sg90.attach(servo_pin);  //Giving the command to arduino to control pin 2 for servo
}

void loop()
{
  sg90.write(0);     // moving the servo at 0 degree
  delay(1000);
  sg90.write(45);     // moving the servo at 45 degree
  delay(1000);
  sg90.write(90);    // moving the servo at 90 degree
  delay(1000);        // wait for 1 second
  sg90.write(135);     // moving the servo at 135 degree
  delay(1000);
  sg90.write(180);   // moving the servo at 180 degrees
  delay(1000);        // wait for 1 sec
  }

The library that we have initialized in the first line contains the commands that we will use in the code like ‘sg90.attach’ and ‘sg90. write’. The ‘sg90.attach( )’ command will tell the Arduino that where we have attached the servo signal pin. The ‘sg90.write( )’ command will move the servo in the direction which will be written in the brackets.

Servo Motor Control using Potentiometer

If you want to control servo motor manually using a potentiometer, then connect the servo motor with Arduino as shown below. The code for this is given at the end.

arduino servo motor

Arduino Servo Control using Potentiometer

Code using Potentiometer

The circuit Diagram for this is given in the circuit diagram section.

In this code, we will read from the potentiometer. The potentiometer will give values from 0 to 1023 to the Arduino but the servo motor only move in 0 to 180 degree direction, so we will have to give the value from 0 o 180. Therefore, we have used the map function which will convert the values from 0 to 1023 into 0 to 180. Now when you will move the potentiometer, then the servo will move according to that.

#include <Servo.h>      //including the servo library
int pot = A0;              //initializing the potentiometer pin at A0
int sg90_pin = 2;    //initializing the servo pin at digital pin 2
int angle;                   //initializing a variable for storing the angle
Servo sg90;               //initializing the variable for servo

void setup()
  sg90.attach(sg90_pin);  //Giving the command to arduino to control pin 2 for servo
}

void loop()
{
  int pot_value = analogRead(pot);                           //pot_value from the potentiometer
  angle = map (pot_value,1023,0,180,0);               //mapping the values from 0-1023 to 0-180
  sg90.write(angle);                                            //Giving the command to move the servo motor
}

Video



If you have any questions, feel free to ask.

JOIN OUR NEWSLETTER
We help electronics hobbyists to create projects related to Micro-controllers. Join our newsletter to get access to new projects first.
We hate spam. Your email address will not be sold or shared with anyone else.

Leave a Reply

Your email address will not be published.

Subscribe To Our List

We help electronics hobbyists to create projects related to Micro-controllers. Join our newsletter to get access to new projects first.
Email address
Secure and Spam free...
Skip to toolbar