How to Use the SG90 Servo Motor with Arduino

How to Use the SG90 Servo Motor with Arduino

The SG90 servo motor is a small and lightweight servo motor commonly used in robotics, RC vehicles, and other projects requiring precise angle control. In this tutorial, we’ll show you how to interface the SG90 servo motor with an Arduino.

What You Will Need

  1. SG90 Servo Motor
  2. Arduino Board (e.g., Uno, Mega, Nano)
  3. Breadboard
  4. Jumper Wires
  5. External Power Supply (optional for multiple servos)
  6. A computer with the Arduino IDE installed

Step 1: Understanding the SG90 Servo Motor

The SG90 servo motor has three pins:

SG90 Pin Function
Brown Ground (GND)
Red Power (VCC)
Orange Signal (PWM)

Note: The servo motor operates at 4.8V-6V. If you’re powering multiple servos, use an external power supply.

Step 2: Wiring the SG90 to Arduino

Here’s how to connect the SG90 servo motor to the Arduino Uno:

SG90 Pin Arduino Pin
Brown GND
Red 5V
Orange Pin 9

Step 3: Install the Servo Library

The Arduino IDE includes a built-in Servo library, so you don’t need to install anything extra.

Step 4: Upload the Code

Here’s a simple example code to control the SG90 servo motor:

#include <Servo.h>

Servo myServo; // Create a Servo object

void setup() {
  myServo.attach(9); // Attach the servo to pin 9
  Serial.begin(9600);
  Serial.println("SG90 Servo Test");
}

void loop() {
  for (int angle = 0; angle <= 180; angle += 1) { // Sweep from 0 to 180 degrees
    myServo.write(angle);  // Set servo to angle
    delay(15);             // Wait for the servo to reach the position
  }

  for (int angle = 180; angle >= 0; angle -= 1) { // Sweep from 180 to 0 degrees
    myServo.write(angle);  // Set servo to angle
    delay(15);             // Wait for the servo to reach the position
  }
}

Step 5: Test the Setup

  1. Connect your Arduino to your computer via USB.
  2. Open the Arduino IDE and select the correct Board and Port from the Tools menu.
  3. Upload the code by clicking the Upload button.
  4. Once the code is uploaded, the servo motor will start sweeping from 0 to 180 degrees and back continuously.

Step 6: Controlling the Servo Dynamically

You can also control the servo dynamically using the Serial Monitor. Here’s an example:

#include <Servo.h>

Servo myServo;

void setup() {
  myServo.attach(9);
  Serial.begin(9600);
  Serial.println("Enter an angle (0-180):");
}

void loop() {
  if (Serial.available()) {
    int angle = Serial.parseInt(); // Read the angle from Serial Monitor
    if (angle >= 0 && angle <= 180) {
      myServo.write(angle);
      Serial.print("Servo set to: ");
      Serial.println(angle);
    } else {
      Serial.println("Invalid angle. Enter a value between 0 and 180.");
    }
  }
}

Troubleshooting

  • Servo not moving: Double-check the wiring and ensure the correct pin is defined in the code.
  • Jittering servo: Use an external power supply to provide stable power.
  • Servo overheating: Avoid holding the servo in a position for a long time under load.

Applications of SG90 Servo

  1. Robotic arms
  2. Pan-and-tilt camera mounts
  3. RC vehicles
  4. Automated door openers

Conclusion

You’ve successfully interfaced the SG90 servo motor with Arduino and controlled its position. The SG90’s versatility and ease of use make it ideal for various DIY and robotics projects. Experiment with different control methods and create your own servo-powered projects!

Leave a comment

Notice an Issue? Have a Suggestion?
If you encounter a problem or have an idea for a new feature, let us know! Report a problem or request a feature here.