小编
Published2025-09-16
Introduction to the SG90 Servo and Arduino
The SG90 servo motor is a compact, affordable, and versatile component that has become a favorite among Arduino enthusiasts. Whether you’re building a robot, automating a smart home device, or creating interactive art, this tiny servo packs a punch. In this guide, you’ll learn how to harness its power using Arduino, write efficient code, and troubleshoot common issues.
What Makes the SG90 Servo Special?
The SG90 is a 9g micro servo motor capable of rotating approximately 180 degrees (90° in each direction). It operates on 4.8V to 6V DC, making it compatible with most Arduino boards. Its lightweight design and precise angular control make it ideal for projects requiring accurate movement, such as:
Robotic arms Camera pan-tilt systems Automated plant waterers RC vehicle steering
To follow along, gather these components:
Arduino Uno or Nano SG90 servo motor Jumper wires (male-to-male) Breadboard (optional) External 5V power supply (recommended for high torque)
Wiring the SG90 to Arduino
The SG90 has three wires:
Brown (Ground): Connect to Arduino’s GND pin. Red (VCC): Connect to Arduino’s 5V pin. Orange/Yellow (Signal): Connect to a PWM-enabled digital pin (e.g., D9).
Pro Tip: For heavy loads, use an external power supply for the servo to prevent Arduino from resetting due to voltage drops.
Basic Arduino Code for SG90 Control
Let’s start with a simple sketch to sweep the servo from 0° to 180°.
void setup() { myservo.attach(9); // Signal pin connected to D9 }
void loop() { for (int pos = 0; pos <= 180; pos += 1) { myservo.write(pos); delay(15); } for (int pos = 180; pos >= 0; pos -= 1) { myservo.write(pos); delay(15); } }
Code Breakdown: - The `Servo.h` library simplifies servo control. - `myservo.attach(9)` initializes the servo on pin 9. - The `loop()` function uses `myservo.write(pos)` to set the angle, with `delay(15)` for smooth motion. #### Troubleshooting Common Issues 1. Servo Jitter: Add a delay between movements or use a capacitor (10µF) across the servo’s power lines. 2. Incorrect Angles: Calibrate the servo using `myservo.writeMicroseconds()` for finer control (500µs = 0°, 2500µs = 180°). 3. Overheating: Avoid continuous rotation; let the servo rest between cycles. #### Project Idea: Smart Trash Can Lid Put your skills to the test by building a hands-free trash can. Use an ultrasonic sensor to detect motion and program the SG90 to open the lid automatically! --- ### Advanced SG90 Servo Control and Creative Projects Now that you’ve mastered the basics, let’s explore advanced techniques and real-world applications. #### Precision Control with `writeMicroseconds()` The `Servo.h` library allows microsecond-level control, offering better accuracy than degree-based angles. Here’s how to use it:
void setup() { myservo.attach(9); }
void loop() { myservo.writeMicroseconds(500); // 0° position delay(1000); myservo.writeMicroseconds(1500); // 90° position delay(1000); myservo.writeMicroseconds(2500); // 180° position delay(1000); }
Why This Matters: Some servos have non-standard pulse ranges. This method ensures compatibility and precise adjustments. #### Using Potentiometers for Manual Control Add a potentiometer to adjust the servo angle in real time: Wiring: - Potentiometer’s outer pins to 5V and GND. - Middle pin to Arduino’s A0. Code:
Servo myservo; int potPin = A0;
void setup() { myservo.attach(9); }
void loop() { int val = analogRead(potPin); val = map(val, 0, 1023, 0, 180); myservo.write(val); delay(20); } ```
Building a Servo-Driven Pan-Tilt System
Combine two SG90 servos to create a camera mount that tracks movement or follows the sun!
Attach one servo horizontally (pan) and another vertically (tilt). Use joystick inputs or light sensors to control angles. Secure the servos with a 3D-printed or cardboard frame.
Avoiding Servo Burnout: Best Practices
Power Management: Always power the servo separately when driving multiple motors. Mechanical Limits: Physically prevent the servo from rotating beyond 180° to avoid gear damage. Smooth Transitions: Use for loops with small angle increments instead of abrupt jumps.
Project Idea: Automated Pet Feeder
Create a timed feeder using an Arduino, SG90 servo, and real-time clock (RTC) module. Program the servo to rotate at specific times, releasing food into a bowl.
Conclusion: Unleash Your Creativity!
The SG90 servo and Arduino are a match made in DIY heaven. From simple sweeping motions to complex robotics, the possibilities are endless. Experiment with sensors, integrate IoT modules, or combine multiple servos for multi-axis projects.
Final Pro Tip: Share your projects on platforms like GitHub or Instructables to inspire others and get feedback. Happy tinkering!
This guide equips you with the knowledge to tackle SG90-Arduino projects confidently. Whether you’re a beginner or a seasoned maker, the journey from basic code to advanced automation starts here!
Update:2025-09-16
Contact Kpower's product specialist to recommend suitable motor or gearbox for your product.