How to interface SG90 servo motor with Arduino

What servo motors are, how do servo motor works & servo working principles are some questions that may arise while working with servo motors and Arduino.

Hello guys! Welcome back to my blog today we going to see how to connect the servo motor with Arduino so let’s begin.

What is Servo Motors??

A Servo motor is a closed-loop precision control motor. it uses a feedback signal to position the angle of the servo motor. Servo motor is seen in a robotic arm or robotics projects, industrial automation, cars, etc. some servo motor rotates from 0 ° to 180° while some rotate from ° to 210 °. 
sg90 servo motor Arduino

We can achieve the desired angle of the servo motor according to the application requirement. The difference between a DC motor & servo motor is that the servo motor is capable of holding position & precise control over rotation & velocity. Servo motor makes the motor rotate at a certain angle or desired angle for a given input signal. A Servo motor can be an AC servo motor or DC servo motor depending upon the requirement of the application.

How do Servo Motors Work??

working principle of servomotor

Suppose you have to rotate the motor at 180° and previously you have rotated 90 °. When you send a command 180° from your Arduino then it will compare the new value with the previous value in our case it's 90 °.so the servo motor will make a difference it will be 90. now servo motor will rotate more than 90° to achieve our desired 180° angle. Also motor rotates the potentiometer which acts as feedback and is fed to the error mechanism.

Rotation of the potentiometer generates some voltage and this voltage is compared with desired voltage to achieve our target position.

Again when a new value is sent by Arduino it will again compare with the previous value to generate a difference between two signals. The comparison of actual angle and desired angle will be compared until the error signal difference will be zero. In our case, the previous value was 180°. This servo motor uses PWM (pulse width modulation) signal to rotate the servo motor.servo motor Arduino code

Servo motor expects to receive pulse at 50HZ every after 20 milliseconds

The width of the pulse is used to determine the angle of the servo motor. If we want to rotate the servo at an angle of 0°, then a pulse width of 1ms is applied.

If we want to rotate the servo at an angle of 90°, then a pulse width of 1.5ms is applied.

If we want to rotate the servo at an angle of 180°, then a pulse width of 2ms is applied.

The duration of the pulse can be varied for different servo motors, it depends upon the manufacturer of the servo.

SG90 servo motor runs between 4.8 – 6 volts (DC). We will be powered up by 5v by Arduino, when the motor is IDLE it will consume 10 mA of current and when there is the rotation of the motor it will consume 100-250 mA.

If you are using a servo motor that consumes more current than 250 mA or you are using more than one servo motor then you should use an external power supply keeping Arduino ground common.

Why do we use servo library instead of directly using PWM pins??

Servo motor expects to receive pulse at 50HZ every after 20 milliseconds 1ms for is fully turned one direction, 1.5ms to rotate at 90 degrees and 2ms is fully 180 degree. On other hand, analogWrite PWM has a full range duty cycle (0 to 100%) at around 490 Hz or 980 Hz. Outputs of Arduino digital pins 3,5,6,9,10,11 does not provide stability and resolution for frequency for timing signal for rotation. If we create manual signals using digitalWrite and delayMicroseconds functions, without using Servo library in the program, then due inaccuracy in timing signal will cause inaccurate rotation which gives difficulty for the user in controlling servo motors.

Servo motor sg90 specifications

  • Rotational degree - 180°
  • Torque - 4.8V (1.80 kg-cm) 
  • Operating voltage - 4.8 V (~5V)
  • Temperature range - 0 ºC – 55 ºC  
  • Weight - 9 gm
  • Speed - 0.1 s/60 degree 

 sg90 servo dimensions

  • Length - 0.91 in (23.1 mm)
  • Width - 0.48 in (12.2 mm) 
  • Height - 1.14 in (29.0 mm)  

Application of Servo motor

  • Robotic arm or robotics projects.
  • Toy cars.
  • CD or DVD player.
  • Conveyor Belts.
  • Metal Cutting & Metal Forming Machines.
  • CNC/printing machines & 3d printers.

To program the servo motor with Arduino we will be using a built-in servo library

#include <Servo.h>

Servo.h gives accessibility to control the servo motor in a much easier way

Servo library includes functions such as

  • attach()
  • write()
  • writeMicroseconds()
  • read()
  • detach()

attach() this function takes Arduino pin as a parameter which we want to connect to servo pin

e.g. - attach(pin)


write() this function takes an angle as a parameter which we want to drive or rotate the servo motor

e.g. - write(angle)


writeMicroseconds() – this function takes pulse width as a parameter that corresponds to angle

e.g. - writeMicroseconds(pulse width in microseconds)


1.5ms is equal to 90 degree

read() - this functions does not take any parameter instead this function returns value. This function tells us at what position or angle our servo motor currently is.

int angle =


detach() - this function takes an Arduino pin as a parameter which we connected had connected to the servo motor. These functions detach the Arduino pin after the program is executed.

Parts Required

  • SG-90 (servo motor module)
  • Jumper wires
  • Breadboard


sg90 servo motor Arduino

Connect the ground pin of the touch sensor to the ground pin of Arduino, the VCC pin to 5v, and the Signal pin to 12. the red wire is VCC (positive voltage)  brown wire is GND (negative voltage) and the orange wire is (PMW) signal pin in the servo motor.


#include <Servo.h>   // includes servo in-built library
Servo myservo;      // create servo object to control a servo
void setup() 
  myservo.attach(12);   // connect signal pin of servo to pin no 12 of Arduino
  Serial.begin(115200); // set baud rate to 115200
void loop() 
  myservo.write(0);       //  rotate servo motor to 0 degree      
  int angle =; // know the current angle of servo motor
  delay(1200);                 // delay of 1200 milliseconds        
   myservo.writeMicroseconds(1500);  //  rotate servo motor to 90 degree using 1.5ms width pulse     
   int angle2 =;      // know the current angle of servo motor
    myservo.write(180);              // rotate servo motor to 180 degree  
    int angle3 =;     // know the current angle of servo motor
   // myservo.detach();              // de-attach the pin of servo                                                      

First, we include an in-built servo library in our code. Then we created an object from the servo library as my servo. Inside the void loop, we have attached pin 12 of Arduino to the servo motor.

In the void loop initially, we rotate the servo motor to 0 degrees, 90 degrees then 180 degrees. We also have used the read() function to know the current angle of the servo motor. And introduce a delay of 1200 milliseconds.  


micro servo sg90

Now we used the serial monitor to observe the output. . To open up the serial monitor, click on Tools > Serial Monitor or use the shortcut (SHIFT + CONTROL + M)You can see output on the Serial monitor. its current angle or position servo is rotating.

How to rotate the servo motor clockwise and anti-clockwise??

#include <Servo.h>   // includes servo in-built library
Servo myservo;      // create servo object to control a servo
void setup() 
  myservo.attach(12);   // connect signal pin of servo to pin no 12 of Arduino
  Serial.begin(115200); // set baud rate to 115200
void loop() 
  for(int i=0;i<=180;i++)
for(int i=180;i>=0;i--)

 Conclusion - 

Today we learn about what is a servo motor, how to use a servo motor with Arduino, the application of a servo motor, how servo motor work with Arduino & working principles of servo motor.

"I hope you find this IoT blog very helpful to you. In the upcoming lesson, we will see more about IoT sensors till then bye. See you all in my next blog."

Close Menu