Home Industry InsightServo
Looking for a suitable motor? Looking for a suitable motor?
Looking for a suitable motor?

Mastering the SG90 Servo Motor with Arduino: A Comprehensive Guide for Beginners

小编

Published2025-09-16

Introduction to the SG90 Servo Motor

The SG90 servo motor is a compact, lightweight, and affordable component that has become a staple in robotics, automation, and DIY projects. Whether you’re building a robotic arm, a solar tracker, or a custom camera slider, this 9g micro servo offers precise angular control—perfect for beginners and experts alike. But how do you make it work with an Arduino? Let’s dive in!

What Makes the SG90 Special?

The SG90 operates on a 4.8–6V power supply and provides a 180-degree rotation range, making it ideal for applications requiring limited but accurate movement. Its three wires (power, ground, and signal) simplify connections, while its internal potentiometer ensures closed-loop feedback for maintaining position.

Components You’ll Need

To get started, gather these essentials:

Arduino Uno or Nano SG90 servo motor Jumper wires Breadboard (optional) USB cable for Arduino 5V power source (for external power, if needed)

Wiring the SG90 to Arduino

Connecting the SG90 to an Arduino is straightforward:

Power (Red Wire): Connect to the Arduino’s 5V pin. Ground (Brown/Black Wire): Link to the Arduino’s GND pin. Signal (Yellow/Orange Wire): Attach to a PWM-enabled digital pin (e.g., D9).

⚠️ Caution: Avoid powering the servo directly from the Arduino for prolonged use, as it may overload the board. Use an external 5V supply for heavy-duty projects.

Basic Arduino Code for SG90

Let’s write a simple sketch to sweep the servo from 0° to 180° and back. This introduces you to the Servo library and PWM control.

```cpp

include

Servo myservo; // Create a servo object

void setup() { myservo.attach(9); // Attach servo to pin 9 }

void loop() { for (int pos = 0; pos <= 180; pos++) { myservo.write(pos); // Move to position 'pos' delay(15); // Wait for the servo to reach the position } for (int pos = 180; pos >= 0; pos--) { myservo.write(pos); delay(15); } }

#### Code Breakdown - `#include `: Imports the Servo library for easy motor control. - `Servo myservo`: Declares a servo object. - `myservo.attach(9)`: Assigns the servo signal to pin 9. - `myservo.write(pos)`: Sends the target angle to the servo. Upload this code, and your SG90 should sweep smoothly! ### Troubleshooting Common Issues 1. Jittery Movement: Add a delay between movements or use a capacitor (10µF) across the servo’s power lines. 2. Servo Doesn’t Move: Check wiring—ensure the signal pin is correctly connected. 3. Overheating: Avoid forcing the servo beyond its mechanical limits. ### Project Idea #1: Automated Plant Waterer Use the SG90 to control a small valve or lever for watering plants. Combine it with a soil moisture sensor to automate your garden! --- ### Advanced SG90 Control Techniques Now that you’ve mastered the basics, let’s explore advanced methods to elevate your projects. #### Using Potentiometers for Manual Control Add a potentiometer to adjust the servo angle in real time. Here’s how: 1. Connect the potentiometer’s middle pin to Arduino’s A0. 2. Update the code to read the analog input and map it to 0–180°:

cpp

include

Servo myservo; int potPin = A0;

void setup() { myservo.attach(9); }

void loop() { int val = analogRead(potPin); // Read potentiometer (0–1023) int angle = map(val, 0, 1023, 0, 180); // Map to servo range myservo.write(angle); delay(15); }

#### Joystick-Controlled Pan-Tilt Mechanism Pair two SG90s with a joystick module to create a pan-tilt system for cameras or sensors. Connect the joystick’s X and Y axes to analog pins and map their values to servo angles. ### Project Idea #2: Smart Dustbin with Motion Sensor Build a hands-free dustbin using an ultrasonic sensor and SG90: 1. Mount the sensor to detect when a hand is near. 2. Program the servo to lift the lid automatically.

cpp

include

include

define TRIGGER_PIN 12

define ECHO_PIN 11

define MAX_DISTANCE 15 // in cm

Servo lidServo; NewPing sonar(TRIGGERPIN, ECHOPIN, MAX_DISTANCE);

void setup() { lidServo.attach(9); lidServo.write(0); // Start with lid closed }

void loop() { int distance = sonar.ping_cm(); if (distance > 0 && distance < 10) { lidServo.write(90); // Open lid delay(3000); // Keep open for 3 seconds } else { lidServo.write(0); // Close lid } delay(100); } ```

Power Management Tips

For multi-servo setups, use a 5V DC power supply with a common ground to the Arduino. Incorporate a PCA9685 PWM driver for controlling up to 16 servos simultaneously.

Mechanical Considerations

Gear Protection: Avoid forcing the servo beyond its limits to prevent gear stripping. Mounting: Use screws or adhesive to secure the servo, especially in high-vibration projects.

Why the SG90 and Arduino Are a Perfect Match

The Arduino’s simplicity and the SG90’s affordability make them a powerhouse duo for prototyping. From interactive art installations to home automation, the possibilities are endless.

Final Thoughts

You’ve now unlocked the fundamentals of SG90 servo control with Arduino. Experiment with sensors, integrate multiple servos, and share your creations with the maker community. Remember, every great project starts with a single servo sweep!

This guide equips you with the knowledge to harness the SG90’s potential. Ready to innovate? Grab your Arduino, wire up a servo, and start turning your ideas into motion! 🚀

Update:2025-09-16

Contact a motor expert for product recommendation.
Contact a motor expert for product recommendation.

Powering The Future

Contact Kpower's product specialist to recommend suitable motor or gearbox for your product.