小编
Published2025-09-16
Introduction to SG90 Servo Motors and Basic Arduino Integration
What is an SG90 Servo Motor? The SG90 servo motor is a compact, lightweight, and affordable rotary actuator widely used in robotics, automation, and DIY projects. Unlike standard DC motors, servos provide precise angular control, making them ideal for applications like robotic arms, camera gimbals, or automated door locks. The SG90 operates on a 4.8–6V power supply and can rotate approximately 180 degrees, making it perfect for projects requiring controlled movement.
How Do Servo Motors Work? Servo motors use a closed-loop control system. Inside the SG90, a small DC motor drives a gear train, while a potentiometer (variable resistor) tracks the motor’s current position. This feedback allows the servo to adjust its position accurately based on Pulse Width Modulation (PWM) signals from an Arduino. The PWM signal’s pulse width determines the angle of the servo’s output shaft.
Components Needed To get started, you’ll need:
Arduino Uno or Nano SG90 servo motor Jumper wires Breadboard (optional) 5V power supply (or USB cable for Arduino power)
Wiring the SG90 to Arduino The SG90 has three wires:
Brown (or Black): Ground (GND) Red: Power (VCC, 5V) Yellow (or Orange): Signal (PWM input)
Connect the SG90’s brown wire to Arduino’s GND pin. Attach the red wire to Arduino’s 5V pin. Plug the yellow wire to a PWM-capable digital pin (e.g., pin 9).
Basic Arduino Code for SG90 Control Let’s write a simple sketch to sweep the servo from 0° to 180°:
void setup() { myservo.attach(9); // Connect servo to pin 9 }
void loop() { for (int pos = 0; pos <= 180; pos++) { myservo.write(pos); delay(15); } for (int pos = 180; pos >= 0; pos--) { myservo.write(pos); delay(15); } }
Code Explanation: - The `Servo.h` library simplifies servo control. - `myservo.attach(9)` links the servo to pin 9. - The `loop()` function uses `myservo.write(pos)` to set the servo angle, with `delay(15)` for smooth motion. Testing the Setup Upload the code to your Arduino. The SG90 should sweep back and forth. If it doesn’t move, double-check your wiring and ensure the servo is powered correctly. Why Use External Power? While small servos can run on Arduino’s 5V pin, larger loads or multiple servos may require an external power supply to prevent voltage drops. Troubleshooting Tips - Jittery Movement: Add a delay or capacitor to stabilize power. - Limited Rotation: Ensure the code isn’t restricting the angle (0–180). - Overheating: Avoid forcing the servo beyond its mechanical limits. Project Idea: Automated Plant Waterer Use the SG90 to control a valve in a DIY irrigation system. The servo can open/close based on soil moisture sensor data. --- ### Advanced SG90 Projects and Optimizing Performance Enhancing Servo Control with Potentiometers For manual control, add a potentiometer to adjust the servo angle dynamically. Wiring Additions: - Connect the potentiometer’s outer pins to 5V and GND. - Link the middle pin to Arduino’s analog pin A0. Updated Code:
Servo myservo; int potPin = A0;
void setup() { myservo.attach(9); }
void loop() { int val = analogRead(potPin); val = map(val, 0, 1023, 0, 180); // Convert analog value to angle myservo.write(val); delay(15); }
Explanation: - The potentiometer’s analog input (0–1023) is mapped to 0–180 degrees. - Real-time adjustments let you position the servo precisely. Project Idea: Pan-Tilt Camera Mount Combine two SG90s to create a pan-tilt mechanism. Use joystick inputs or a smartphone app to control the servos. Controlling Multiple Servos Arduino supports up to 12 servos using the `Servo` library, but resource limits may require careful timing. Sample Code for Dual Servos:
Servo servo1; Servo servo2;
void setup() { servo1.attach(9); servo2.attach(10); }
void loop() { servo1.write(90); // Center position servo2.write(180); // Full rotation delay(1000); servo1.write(0); servo2.write(90); delay(1000); } ```
Optimizing Servo Performance
Reduce Jitter: Use servo.writeMicroseconds() for finer PWM control. Power Management: Employ a separate 5V regulator for servos. Avoid Blocking Code: Replace delay() with millis() for non-blocking loops.
Project Idea: Smart Trash Can Lid Program the SG90 to open when a motion sensor detects a hand wave, then close after a few seconds.
Common Mistakes to Avoid
Overloading the servo with heavy attachments. Ignoring voltage/current requirements. Using non-PWM pins for servo signals.
Integrating SG90 with IoT Pair your Arduino with an ESP8266 module to control the servo remotely via Wi-Fi. For example, build a smart curtain opener triggered by a mobile app.
Final Project: Solar Tracker Build a solar panel that follows the sun’s movement using two SG90s and light-dependent resistors (LDRs). The servos adjust the panel’s position to maximize energy absorption.
Conclusion The SG90 servo motor is a versatile tool for Arduino enthusiasts. From basic sweeps to advanced IoT projects, mastering its control opens doors to endless creativity. Start with simple code, experiment with sensors, and gradually tackle complex builds. With the right approach, even beginners can create impressive automation systems!
This guide equips you with the knowledge to harness the SG90’s potential. Ready to bring your ideas to life? Grab your Arduino and start experimenting today!
Update:2025-09-16
Contact Kpower's product specialist to recommend suitable motor or gearbox for your product.