Utility Bar

SG90 Servo Arduino Guide

Ethan Zaitchik |

Table of Contents

    The SG90 Micro Servo is a popular tiny servo motor widely used in Arduino and electronics projects. It is lightweight, affordable, and capable of precise position control over 180°. The SG90 operates on 4.8–6V and provides a torque of around 1.8 kg·cm, making it ideal for small robotic arms, pan-tilt cameras, and other hobbyist applications. in this SG90 Arduino tutorial, we will walk you through what the SG90 is, how it works and go over a few code examples.

    There is also a 360° rotation version of the SG90 for projects requiring continuous rotation, such as mini wheels or conveyor mechanisms.

    Explore more servo options in our Servo Collection.

    SG90 servo Arduino tutorial

    How Does a Micro Servo Work?

    A micro servo like the SG90 contains a small DC motor, a set of gears, and a potentiometer. The potentiometer measures the servo's position, allowing the motor to adjust and hold a specific angle. Control is provided via a Pulse Width Modulation (PWM) signal from an Arduino or other microcontroller.

    By changing the pulse width, you can precisely rotate the servo to a desired angle between 0° and 180° (or continuously if using the 360° version). This makes micro servos ideal for applications requiring precise positioning in small spaces.

    SG90 servo PWM signal diagram

    Understanding Rotation: 180° vs 360°

    Standard SG90 servos rotate up to 180°, allowing precise positioning for robotic arms, camera mounts, or animatronics. The 360° version can rotate continuously, functioning more like a motor for wheels or conveyor belts.

    Choosing the correct servo depends on your project: if you need accurate angular positioning, the 180° SG90 is best. For continuous motion tasks, the 360° version provides the flexibility needed.

    SG90 180° vs 360° Zaitronics

    SG90 Features and Specs

    Feature Specification
    Weight 9 g
    Dimensions 22.2 × 11.8 × 31 mm approximately
    Stall torque 1.8 kgf·cm
    Operating speed 0.1 s per 60°
    Operating voltage 4.8 V approximately 5 V
    Dead band width 10 μs
    Temperature range 0 °C to 55 °C
    Position control pulses 0° uses a 1.5 ms pulse for centre position
    90° uses an approximately 2 ms pulse for full right position
    -90° uses an approximately 1 ms pulse for full left position

    When to Use a Motor Driver

    The SG90 servo draws very little current (~200 mA max), so for most small projects it can be powered directly from an Arduino. However, a motor driver can be very helpful in the following situations:

    • You are controlling multiple servos at the same time
    • Your project requires higher torque or greater current than the Arduino can safely provide
    • You want to protect your Arduino from voltage spikes or brownouts

    For single, low-load SG90 projects, a motor driver is optional. For larger setups or projects with multiple servos, a motor driver improves reliability and ensures consistent performance.

    Check out our Motor Drivers Collection or the popular PCA9685 16-Channel PWM Servo Driver for easy servo control.

    SG90 Servo Sweep Example (Automatic Back-and-Forth Motion)

    The following example uses the official Arduino Servo library to make an SG90 micro servo automatically sweep back and forth from 0° to 180° and back, like a classic "testing" or "demo" motion.

    This is a great sketch to verify that your servo is working correctly before connecting sensors or controls.

    This guide assumes basic Arduino knowledge. If you’re just getting started, begin with our Arduino getting started guide for beginners.

    SG90 Servo Wiring Diagram (Arduino Uno)

    Wire Connection
    Servo red wire 5V or external 5V power supply for stability
    Servo brown wire GND
    Servo orange or yellow wire signal Digital pin 9 PWM capable recommended
    SG90 Servo Arduino Uno Wiring Diagram

    Sweep Example for SG90 Servo

    This SG90 sweep code example sketch makes the servo continuously sweep from 0° to 180° and back again.

    /*
     * Ethan Zaitchik
     * SG90 Servo Sweep Example (0° to 180° and back)
     * Full guide: https://zaitronics.com.au/blogs/guides/sg90-servo-arduino-guide
    */
    
    #include <Servo.h>
    
    Servo myservo;  // create servo object to control a servo
    // twelve servo objects can be created on most boards
    
    int pos = 0;    // variable to store the servo position
    
    void setup() {
      myservo.attach(9);  // attaches the servo on pin 9
    }
    
    void loop() {
      // goes from 0 degrees to 180 degrees
      for (pos = 0; pos <= 180; pos += 1) {
        myservo.write(pos);    // tell servo to go to position in variable 'pos'
        delay(15);               // waits 15ms for the servo to reach the position
      }
      
      // goes from 180 degrees to 0 degrees
      for (pos = 180; pos >= 0; pos -= 1) {
        myservo.write(pos);    // tell servo to go to position in variable 'pos'
        delay(15);               // waits 15ms for the servo to reach the position
      }
    }
    

    Code Explanation

    This Arduino sketch demonstrates the classic "sweep" test for a servo motor using the built-in Servo library.

    Here is a detailed line-by-line explanation of the code:

    #include <Servo.h>
    • Includes the standard Arduino Servo library for controlling RC servo motors like the SG90.
    Servo myservo;  // create servo object to control a servo
    • Creates a Servo object named myservo. You can control up to ~12 servos on most Arduino boards.
    int pos = 0;    // variable to store the servo position
    • pos is an integer that keeps track of the current (or target) servo angle.
    void setup() {
      myservo.attach(9);  // attaches the servo on pin 9 to the servo object
    }
    • setup() runs once at startup.
    • attach(9) assigns digital pin 9 to control the servo signal wire.
    for (pos = 0; pos <= 180; pos += 1) {
      myservo.write(pos);
      delay(15);
    }
    • First for loop: Starts at 0°, increases by 1° each time up to 180°.
    • myservo.write(pos) moves the servo to the current angle.
    • delay(15) gives the servo ~15 milliseconds to physically move to the new position (makes motion smooth).
    for (pos = 180; pos >= 0; pos -= 1) {
      myservo.write(pos);
      delay(15);
    }
    • Second for loop: Starts at 180° and decreases back to 0°.
    • The whole process repeats forever inside loop().
    Note: The SG90 servo performs best with a stable 4.8–6V power supply. When connected directly to the Arduino 5V pin, you may notice jitter or weak movement under load. For reliable operation, use an external 5V power supply (with common GND to Arduino).

    SG90 Servo Control with Potentiometer

    This example uses the official Arduino Servo library to control an SG90 micro servo using a potentiometer (knob). Turn the potentiometer to rotate the servo from 0° to 180°.

    Just like the previous example this guide assumes basic Arduino knowledge. If you’re just getting started, begin with our Arduino getting started guide for beginners.

    SG90 Servo + Potentiometer Wiring Diagram (Arduino Uno)

    Component Wire / Pin Connection
    SG90 Servo Red wire 5V or external 5V power supply
    SG90 Servo Brown wire GND
    SG90 Servo Orange or yellow wire signal Digital pin 9 PWM capable
    Potentiometer 10kΩ Outer pin 5V
    Potentiometer 10kΩ Other outer pin GND
    Potentiometer 10kΩ Middle pin wiper Analog pin A0
    SG90 Servo Arduino Uno Potentiometer Wiring Diagram

    Potentiometer Control Example for SG90 Servo

    This sketch reads the potentiometer and maps its value to servo angles (0-180°).

    /*
     * Ethan Zaitchik
     * SG90 Servo + Potentiometer Control Example
     * Full guide: https://zaitronics.com.au/blogs/guides/sg90-servo-arduino-guide
    */
    
    #include <Servo.h>
    
    Servo myservo;  // create servo object to control a servo
    
    int potpin = 0;  // analog pin used to connect the potentiometer (A0)
    int val;         // variable to read the value from the analog pin
    
    void setup() {
      myservo.attach(9);  // attaches the servo on pin 9
    }
    
    void loop() {
      val = analogRead(potpin);           // reads the value of the potentiometer (0-1023)
      val = map(val, 0, 1023, 0, 180);    // scale it to servo range
      myservo.write(val);                 // sets the servo position
      delay(15);                          // waits for the servo to get there
    }
    

    Code Explanation

    This Arduino sketch demonstrates classic servo control using a potentiometer with the built-in Servo library.

    Here is an explanation of the code:

    #include <Servo.h>  // Include the Servo library
    • Includes the standard Arduino Servo library, which provides simple functions for controlling RC servo motors like the SG90.
    Servo myservo;  // create servo object to control a servo
    • Creates a Servo object named myservo. This is your handle to control the servo (you can create multiple for multiple servos).
    int potpin = 0;  // analog pin used to connect the potentiometer (A0)
    int val;         // variable to read the value from the analog pin
    • potpin = 0 refers to analog pin A0.
    • val stores the potentiometer reading temporarily.
    void setup() {
      myservo.attach(9);  // attaches the servo on pin 9 to the servo object
    }
    • setup() runs once at startup.
    • attach(9) connects the servo signal to digital pin 9 (PWM pins are recommended).
    val = analogRead(potpin);           // reads the value of the potentiometer (0-1023)
    • Reads the voltage on A0, returning a value from 0 (0V) to 1023 (5V).
    val = map(val, 0, 1023, 0, 180);    // scale it to use it with the servo (0-180 degrees)
    • The map() function converts the 0–1023 range into 0–180 degrees — perfect for servos.
    myservo.write(val);                 // sets the servo position according to the scaled value
    • write() tells the servo to move to the specified angle (0–180).
    delay(15);                          // waits for the servo to get there
    • A short 15 ms delay smooths movement and reduces jitter.
    Note: For best performance (especially with multiple servos), use an external 5V power supply for the servo (keep GND connected to Arduino GND). The SG90 can draw significant current during movement.

    Alternative Servos

    If the SG90 isn’t quite right for your project, there are several other micro servos you can consider, depending on torque, metal gears, or rotation needs:

    • MG90S Metal Gear Servo – Similar size to the SG90 but with metal gears for increased durability and torque. Great for slightly heavier loads.
    • MG996R Servo – Larger high-torque servo suitable for robotics and heavier mechanical applications.
    • MG995 Servo – Another high-torque option similar to the MG996R, good for projects requiring more strength than the SG90.

    Quick Comparison Table:

    Servo Model Size Torque Gear Type Best Use
    SG90 Micro, lightweight (~9g) ~1.8 kg·cm Plastic Basic, low-load hobby projects
    MG90S Micro, slightly heavier (~13g) ~2.2 kg·cm Metal More durable micro tasks
    MG996R Standard, larger (~55g) ~9–10 kg·cm Metal High-torque, heavier loads

    For a full comparison of these servos and to see which one might best suit your needs, check out our SG90 vs MG90S vs MG996R guide.

    Explore our complete Servo Collection for all available options.

    Applications

    SG90 servos and similar micro servos are extremely versatile. Here are some common projects and applications to inspire your builds:

    • Mini robotic arms for pick-and-place tasks
    • Pan-tilt camera mounts for surveillance or hobby robotics
    • Mini RC vehicles, including cars and boats
    • Animatronics and small moving models for creative projects

    Common Issues & Fixes

    Even small servos like the SG90 can run into problems if not used correctly. Here are the most common issues and SG90 troubleshooting steps:

    • Jittering: Servo vibrates or shakes when holding position. Usually caused by noisy power supply or low-quality PWM signal.
      Fix: Use a stable 5V source and make sure connections are secure.
    • Stalling: Servo fails to move or gets stuck. Often due to overload or mechanical blockage.
      Fix: Reduce load, check for obstructions, or use a higher-torque servo like the MG90S or MG996R.
    • Weak torque: Servo can’t move the load properly. Caused by low voltage or overloading.
      Fix: Supply proper voltage (4.8–6V) and stay within torque limits.
    • Incorrect voltage: Too high can damage the servo, too low can cause stalling.
      Fix: Always check power source, ideally 5V regulated.
    • Library pitfalls: Using the wrong library or improper PWM timings can cause erratic movement.
      Fix: Use the Servo.h library in Arduino and double-check your pulse width values.

    FAQ

    Can I run multiple SG90 servos from the Arduino 5V pin?

    Small projects may work, but for more than 2–3 servos, it’s recommended to use an external power source or a motor driver to avoid overloading the Arduino.

    My servo moves erratically sometimes, why?

    Check your wiring, ensure the PWM signal is stable, and make sure the voltage supply is consistent. Noise or low voltage can cause unpredictable movement.

    Can I use the 360° SG90 for positioning?

    No, 360° SG90 servos are continuous rotation and cannot hold a precise angle. Use a standard 180° SG90 if you need accurate positional control.

    By combining these applications with the servo options above, you can choose the right motor for your project’s size, torque requirements, and motion needs.

    Leave a comment

    // Table of contents