Servo Motor Control with Arduino

In this tutorial we will see how do servo motors work and how to control a small size hobby servo motor with Arduino development board. We’ll also see the simulation of Arduino UNO board with a typical servo motor using Labcenter Electronics Proteus circuit simulation software.

What is a servo motor and how it works?
A servo motor is a type of electric motor that is designed for precise control of position, velocity, and acceleration. It is commonly used in applications that require accurate and controlled motion, such as robotics, CNC machines, and industrial automation.
The most used hobby servo motors are: SG90 Micro Servo and MG996R Servo.
The SG90 micro servo motor is the one used in this tutorial, it is the one shown below with horns and screws:

SG90 Servo motor picture

SG90 Micro servo motor specification:

Operating Voltage4.8 – 6V
Max. Current600mA
Torque11 N.cm @ 4.8V
15 N.cm @ 6V
Speed0.12 sec/60° @ 4.8V
0.10 sec/60° @ 6V
Weight9 g

The SG90 servo motor usually comes with a 3-pin connector that makes it easy to connect to a control circuit or microcontroller.The pins typically include ground (Brown wire), power supply (Red wire), and the control signal (Orange wire).

It’s important to note that there may be slight variations in specifications across different manufacturers, so it’s always a good idea to refer to the specific datasheet or documentation provided by the manufacturer for the most accurate and up-to-date information.

The key feature of a servo motor is its ability to maintain a set position even when external forces try to move it. This is achieved by using a feedback mechanism that constantly measures the motor’s actual position and compares it to a desired position. The motor’s control circuit then adjusts the motor’s position as needed to minimize the difference between the actual and desired positions.

Servo motors are available in a wide range of sizes, power ratings, and performance specifications. They are typically composed of a DC motor, a control circuit, a feedback mechanism (such as a potentiometer or encoder), and a gearbox (which increases the motor’s torque and reduces its speed).

In a closed-loop circuit, the control circuit continuously measures the motor’s actual position using the position sensor and compares it to the desired position. The control circuit then calculates the error signal and adjusts the motor’s position by sending control signals to the motor. This process is repeated until the actual position of the motor matches the desired position.

Servo motor closed loop control system

Servo motors are controlled by a series of pulses that are sent to the motor’s control circuit which is represented in the above control diagram as Input Signal (Target Position). These pulses are typically generated by a microcontroller or other type of controller.
The duration of each pulse determines the position of the servo motor’s shaft. A typical pulse width range for a servo motor is between 1 millisecond and 2 milliseconds, with a pulse width of 1.5 milliseconds representing the center position.

When servo motor control circuit receives a signal from the microcontroller system, it compares target position with actual position and generates an Error. The Error signal is sent to a Controller Circuit that controls the DC motor through H-Bridge circuit. The H-Bridge circuit allows the rotation of DC motor in both directions according to a control signals (sent by the controller circuit).
The Gear System (or gearbox) reduces the speed of the motor and increases its torque. This allows the motor to move a load with high precision and accuracy.

The amount of movement per pulse, or the resolution of the servo motor, depends on the motor’s gear reduction ratio and the number of pulses per second that are sent to the motor.

Servo motors are typically controlled using a series pulses or simply a PWM (Pulse-Width Modulation) signal.
The PWM signal is a square wave signal that varies in duration (pulse width) but has a fixed frequency. The duty cycle, which is the percentage of time the signal is high (on) during each period, determines the position of the servo motor.

A typical PWM signal for a servo motor has a frequency of 50 Hz and a pulse width range of 1 to 2 milliseconds, with a pulse width of 1.5 milliseconds representing the center position. By adjusting the duty cycle of the PWM signal, we can control the position of the servo motor very precisely.
The image below shows a simple relationship between pulse width and servo motor position:

Servo motor control pulse timing position

As shown above, a zero degrees is represented by a pulse width of 1 milliseconds,
A 90 degrees is the center position of the servo motor, it  is represented by a pulse width of 1.5 milliseconds,
A 180 degrees is represented by a pulse width of 2 milliseconds.

This means servo motor angle position is directly proportional to the pulse width.
The following simple animation shows the relationship between servo motor angle and pulse width where pulse width varies between 1ms & 2ms.

Servo motor working animation

Servo Motor Control with Arduino:
Due to its simplicity, the SG90 micro servo motor is used in this tutorial as it can be powered directly from the Arduino board.

Hardware Required:

  • Arduino board
  • SG90 Servo motor
  • 10k Potentiometer
  • Breadboard & Jumper wire

The SG90 servo motor is connected to the Arduino board as follows:
Ground pin (brown wire) is connected to Arduino GND pin.
Power supply positive terminal pin (red wire) is connected to the Arduino’s 5V pin.
Control signal pin (orange wire) is connected to Arduino digital pin 9.

Interfacing SG90 servo motor with Arduino circuit

A potentiometer is used to set servo’s position (angle), its output is connected to Arduino analog channel A0.
Controlling the servo motor with the potentiometer involves using the analog input of the Arduino (A0) to read the potentiometer’s position and mapping that value to control the servo motor. Therefore, the servo gets a position according to the one of the potentiometer.
One end of the potentiometer terminals is connected to 5V pin of the Arduino and the other end is connected to GND pin of the Arduino board.

Servo Motor Control with Arduino Code:
The Arduino IDE already provides a nice built-in library called “Servo” that simplifies servo motor control. This library allows the control of one or more servo motors using one Arduino board.
At the beginning of your Arduino code, the “Servo” library is included as shown below:

Potentiometer output is connected to Arduino analog channel 0, and servo control pin is connected to Arduino digital pin 9:

The Arduino reads analog signal from potentiometer output and converts it to a digital value using board microcontroller built-in ADC (Analog-to-Digital Converter). For the ATmega328P based Arduino boards (UNO, NANO…), the digital value varies between 0 & 1023 (10-bit ADC).

I used the Arduino map() function to scale the ADC output to a value that is useful for the “Servo” library which should be between 0 & 180 (degrees):

We may adjust the mapping in the map() function as needed to match the range of our servo motion, for example we can make the servo moves between angle 60° and 120°.

Another function used in this example is servo.read() which returns current position of the servo.
The lase function is
servo.write(angle) which updates pulse width and therefore servo’s position according to the value of the variable angle (variable angle varies between 0 & 180).

Rest of code is described through comments.

Full Arduino Code:

Servo Motor Control with Arduino Video:
The following video shows my simple DIY circuit of this project where Arduino NANO board is used.

Servo Motor Control with Arduino Proteus Simulation Video:
This project can be easily simulated using Proteus simulation software as shown in the given below video.
Proteus provides a simulation environment that allows us to visualize and test the behavior of our circuit virtually.

Arduino with servo motor Proteus simulation file download:
Proteus simulation file of this project can be downloaded from the below Google Drive link. Use Proteus version 8.15 or higher to open it.
DOWNLOAD


Discover more from Simple Circuit

Subscribe to get the latest posts sent to your email.

1 thought on “Servo Motor Control with Arduino”

  1. Thanks for sharing this informative blog post on controlling the SG90 servo motor with Arduino and Proteus simulation. As an electronics enthusiast, I found your article well-written and easy to follow, making it an excellent resource for beginners and hobbyists.

Leave a Comment

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

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Scroll to Top