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.

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.

27 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).

  5. Just a small thing: the include in my Arduino IDE v 1.8.5 had to be #include (note the capital letter ‘S’), as opposed to the small ‘s’ in example. Otherwise, nice tutorial!

  6. I am using an arduino genuino/ uno and I have the same issue saying (exit status 1
    Error compiling for board Arduino/Genuino Uno.)

    • I had the same issue, but changed the lower case “s” in “servo.h” to an Upper case “S” as “Servo.h”

  7. Sir
    I wanted to know whether I will require to use a separate power source (battery) in my project. I am connecting 2 IR sensors, 1 tower pro servo SG90 and 1 LCD screen with Arduino.

  8. my servo was working properly on this code yesterday but it is not working anymore today did anyone can help me please

    Servo myservo ;

    void setup() {
    // Initially the servo must be stopped
    void loop() {
    // Start turning clockwise
    // Go on turning for the right duration
    // Stop turning
    // Wait for 12h

  9. there is an error in the program the header file name is incorrect

    #include servo.h ====should be==== #include Servo.h

    reference :
    compiler returns an error

  10. Question: how does the stepper remember where it’s at?
    Example, if I move it from 90 to 100 degree and turn of the arduino, next time I power up does it know where 90 degree is or it assumes that current position is 90 degree?

  11. Hi, so I’m trying to make a mini vending machine and I needed motors. This was the only kind I could find and I cut the end part off as I needed straight wires and not that connector thingy. Now I cannot get it to work. I tried every combination possible along with a new 9v battery, and the motor just won’t spin. Anybody know whats wrong? Help!

  12. My servo won’t go the full 180 degrees, it seems to be about 5 degrees short. I’ve tried entering 185 in the program but it doesn’t make any difference. I can rotate the servo by hand past the position it goes to.

  13. I am having no luck heres my error message :
    Arduino: 1.8.9 (Windows Store (Windows 10), Board: “Arduino/Genuino Uno”

    Build options changed, rebuilding all
    ServerMoter:7:10: error: #include expects “FILENAME” or

    #include servo.h //add ” before and after servo.h


    exit status 1
    #include expects “FILENAME” or

    This report would have more information with
    “Show verbose output during compilation”
    option enabled in File -> Preferences.

Leave a Reply

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