Tutorial: How To Control the Tower Pro SG90 Servo with Arduino UNO

I write this tutorial to show you how to control the direction, position, and speed of the SG90 9G Micro servo motor with the Arduino UNO board. I know that if you’re a hobbyist with some experience in robots is very easy to control this servo motor, but like always, you couldn’t miss anything in the field. Even so, if this tutorial is boring for you, here is a list of cheap projects for Arduino UNO.

Tutorial: How To Control the Tower Pro SG90 Servo with Arduino UNO

Tutorial: How To Control the Tower Pro SG90 Servo with Arduino UNO

How the tutorial is organized:

  1. I start the tutorial with a short overview of the parts needed to control the servo motor;
  2. Then I did an overview of the Tower Pro SG90 servo;
  3. I break down into components the servo motor to show you the parts;
  4. Finally, I went to the practice part to show you how to wire TowerPro SG90, writes the Arduino sketch, and some hacks for the servo motor.

A servo motor allows a precise control of the angular position, velocity, and acceleration. It’s like you’re at the steering wheel of your car. You control precisely the speed of the car and the direction.

This very strict control of the angular position, velocity, and acceleration can’t be done without a sensor for position feedback. This sensor sounds the alarm when the motor is spinning. But even so, there is something more sophisticated that controls all the stages of the servo motor. It’s a dedicated controller that makes the tiny things inside the servo to move with military precision.

A feedback sensor, a controller, and a motor. I’m just asking – why we need a servo motor? These servos are essential parts if we need to control the position of objects, rotate sensors, move arms and legs, drive wheels and tracks, and more.

1. Hardware Required

This is not a complex project, but we still need some parts. Luckily, all the components are cheap and available worldwide.

To control the TowerPro SG90 servo, you will need the following parts:

2. The Tower Pro SG90 Servo

The Tower Pro SG90 servo is one of the cheapest servo motors that you can find on the market. Even it’s cheap, less than $5, don’t try to rotate the servo motor by hand because this may damage the motor.

Let’s make a short overview of the SG90 specifications.

You need torque to control the position of an object, for example, and this little box that weight 0.32 oz (9.0 g) can provide at 4.8V a torque of 25.0 oz-in (1.80 kg-cm). At 4.8V, the speed of the servo is 0.12 sec/60°. All these specifications are really impressive for this little plastic box. Well, there are other high-end servos with a bit more muscle than the SG90 servo, but these high-end servos can’t beat the price of the SG90 servo. SG90 is cheap enough to throw away when they break.

3. Take a look inside of the SG90 micro servo

Inside SG90 servo

Inside SG90 servo


Inside the micro servo, you will find the pieces from the above image. The top cover hosts the plastic gears while the middle cover hosts a DC motor, a controller, and the potentiometer.

4. How to wire TowerPro SG90, the Arduino sketch, and some hacks

Considering that you already have an Arduino UNO, the SG90 servo, and the six wires, your circuit should look like this (plus bonus the scheme):

Arduino SG Servo Motor Control Schema

Arduino SG Servo Motor Control Schema

Arduino SG Servo Motor Control Circuit

Arduino SG Servo Motor Control Circuit

It would be something to note here. The servo motor has three leads, with one more than a DC motor. Each lead has a color code. So you have to connect the brown wire from the micro servo to the GND pin on the Arduino. Connect the red wire from the servo to the +5V on the Arduino. And finally, connect the orange wire from the SG90 servo to a digital pin (pin 9) on the Arduino.

Arduino makes the things simple. In this tutorial, I use the SG90 servo powered directly from the Arduino via USB. And you can do the same.

The Arduino sketch

Below you can find the Arduino sketch that controls the servo’s direction, the position of the motor and the speed of the SG90 servo. Before reaching the Arduino code, I want to write few words about the file. This library makes our life easier. It contains all the functions required for controlling the SG90 servo.

/*
Into Robotics
*/
 
#include servo.h  //add '<' and '>' before and after servo.h
 
int servoPin = 9;
 
Servo servo;  
 
int servoAngle = 0;   // servo position in degrees
 
void setup()
{
  Serial.begin(9600);  
  servo.attach(servoPin);
}
 
 
void loop()
{
//control the servo's direction and the position of the motor

   servo.write(45);      // Turn SG90 servo Left to 45 degrees
   delay(1000);          // Wait 1 second
   servo.write(90);      // Turn SG90 servo back to 90 degrees (center position)
   delay(1000);          // Wait 1 second
   servo.write(135);     // Turn SG90 servo Right to 135 degrees
   delay(1000);          // Wait 1 second
   servo.write(90);      // Turn SG90 servo back to 90 degrees (center position)
   delay(1000);

//end control the servo's direction and the position of the motor


//control the servo's speed  

//if you change the delay value (from example change 50 to 10), the speed of the servo changes
  for(servoAngle = 0; servoAngle < 180; servoAngle++)  //move the micro servo from 0 degrees to 180 degrees
  {                                  
    servo.write(servoAngle);              
    delay(50);                  
  }

  for(servoAngle = 180; servoAngle > 0; servoAngle--)  //now move back the micro servo from 0 degrees to 180 degrees
  {                                
    servo.write(servoAngle);          
    delay(10);      
  }
  //end control the servo's speed  
}

You can also hack the SG 90 micro servo

The SG90 micro servo can turn your robot wheel. And this is great since the shapes of the servo box help you attach it to a robot chassis. But there is a problem with the rotation of the SG90 servo. It’s about the rotation that reach a maximum 180 degrees.

In this case, you need to hack it to have a continuous rotation. This is a delicate operation that requires time and cautions. Before proceeding any operation, make sure you have extra money or other micro servo if you’re doing this wrong.

This tutorial shows you step-by-step how to hack a Tower Pro SG 90 micro servo for continuous rotation.

And in the end, I hope this tutorial helps you guys learn how to control the Tower Pro SG 90 micro servo with Arduino UNO. Also, don’t forget to share the tutorial on social networks to help others learn to control their micro servo.

Disclaimer: This post contains affiliate links.

Commerce Content is independent of articles and advertising, and if you buy something through our posts, I may get a small share of the sale.

10 comments » Write a comment

  1. Thank you for this excelent tutorial, I did, and works perfect, I am traing to activate a servo mg996R by arduino uno, with no succes.

    Thank you again

  2. Great post! Thanks for the tutorial.

    Ps: There is a little typo in the code, where you set the angle to 45 degrees, your comment says it’s set to 50. Just a tiny mistake. 🙂

    • Mg995 is relatively heavy motor to be powered directly from microcontroller. The power must be supplied separately. I typically use 4aa NiMh battery pack with each cell upto 2100mAh for couple of mg995 motors and it works very well.

  3. Hi,
    I try to control servo motors with PWM. I need a 20ms periode and a flexibal duty cycle from 900µs to 2ms to set the direction of the servo. I use the LPC 2148. can u give me any suggestion i m new to ARM. please give me any reference code if you have.
    thanks in advance

  4. The (greater than/less than) around the Servo.h makes it be treated as a html tag, vanishing it. Might be just in chrome (or the Blink rendering engine in general), I vaguely feel like this has changed at some point or otherwise differed. Replace with </> to fix (or fix whatever is supposed to do that automatically).

Leave a Reply

Required fields are marked *.