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
- SG90 Servo Motor
- Arduino Board (e.g., Uno, Mega, Nano)
- Breadboard
- Jumper Wires
- External Power Supply (optional for multiple servos)
- 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
- Connect your Arduino to your computer via USB.
- Open the Arduino IDE and select the correct Board and Port from the Tools menu.
- Upload the code by clicking the Upload button.
- 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
- Robotic arms
- Pan-and-tilt camera mounts
- RC vehicles
- 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!