小编
Published2025-09-16
Introduction to the SG90 Servo and Arduino
The SG90 servo motor is a compact, affordable, and versatile component widely used in robotics, automation, and DIY projects. Whether you’re building a robotic arm, a camera gimbal, or an automatic pet feeder, understanding how to control this servo with an Arduino is a foundational skill. In this guide, you’ll learn how to wire the SG90 to an Arduino Uno, write a basic program to control its movement, and explore practical applications.
The SG90 is a 9g micro servo that operates on 4.8–6V DC. It rotates approximately 180 degrees (90° in each direction) and uses Pulse Width Modulation (PWM) to determine its angle. Its lightweight design and precise positioning make it ideal for projects where space and accuracy matter.
Arduino Uno or compatible board SG90 servo motor Jumper wires (male-to-male) Breadboard (optional) USB cable for Arduino
Wiring the SG90 Servo to Arduino
The SG90 has three wires:
Brown (Ground): Connects to Arduino’s GND pin. Red (Power): Connects to Arduino’s 5V pin. Orange/Yellow (Signal): Connects to a PWM-capable digital pin (e.g., Pin 9).
Step-by-Step Wiring Guide:
Plug the SG90’s brown wire into the Arduino’s GND pin. Connect the red wire to the 5V pin. Attach the orange/yellow wire to Pin 9.
Note: For multiple servos, use an external power supply to avoid overloading the Arduino’s 5V regulator.
Writing Your First Arduino Program for the SG90
The Arduino IDE includes a built-in Servo library, simplifying servo control. Let’s create a basic program to sweep the servo from 0° to 180°.
Servo myServo; // Create a servo object int pos = 0; // Initial position
void setup() { myServo.attach(9); // Attach servo to Pin 9 }
void loop() { // Sweep from 0° to 180° for (pos = 0; pos <= 180; pos += 1) { myServo.write(pos); delay(15); // Adjust speed here } // Sweep back from 180° to 0° for (pos = 180; pos >= 0; pos -= 1) { myServo.write(pos); delay(15); } }
#### How It Works: - `#include `: Imports the Servo library. - `Servo myServo`: Declares a servo object. - `myServo.attach(9)`: Links the servo to Pin 9. - `myServo.write(pos)`: Sends the target angle to the servo. Upload the code, and your SG90 will start sweeping smoothly! --- ### Understanding PWM and Servo Control The SG90 relies on PWM signals to set its angle. PWM works by sending rapid pulses where the pulse width (duration) determines the servo’s position. For the SG90: - 1ms pulse: 0° position. - 1.5ms pulse: 90° position. - 2ms pulse: 180° position. The Arduino’s `Servo` library handles these calculations automatically, so you only need to specify the desired angle. --- ### Troubleshooting Common Issues 1. Jittery Movement: Ensure the power supply is stable. Add a capacitor (10µF) between the servo’s power and ground wires. 2. Servo Doesn’t Move: Check connections. Verify the signal pin matches the code. 3. Overheating: Avoid forcing the servo beyond its mechanical limits. --- ### Advanced Control: Using a Potentiometer for Manual Adjustment Now that you’ve mastered basic movement, let’s integrate a potentiometer to control the servo angle manually. This mimics real-world applications like steering mechanisms or adjustable sensors. #### Additional Components: - 10kΩ potentiometer --- ### Wiring the Potentiometer 1. Connect the potentiometer’s middle pin to Arduino’s A0. 2. Attach the other two pins to 5V and GND. --- ### Code for Potentiometer Control
Servo myServo; int potPin = A0; // Potentiometer on A0
void setup() { myServo.attach(9); }
void loop() { int potValue = analogRead(potPin); // Read potentiometer (0–1023) int angle = map(potValue, 0, 1023, 0, 180); // Convert to angle (0–180°) myServo.write(angle); delay(15); } ```
analogRead(potPin): Reads the potentiometer’s voltage (0–5V) as a value between 0 and 1023. map(): Converts the 0–1023 range to 0–180°, which the servo understands.
Turn the potentiometer knob, and the servo will follow!
Project Ideas to Level Up Your Skills
Robotic Arm: Combine multiple servos to create a programmable arm. Sun Tracker: Use light sensors to make the servo follow sunlight. Smart Dustbin: Automate lid opening with a motion sensor.
Optimizing Servo Performance
Power Management: For multiple servos, use a dedicated 6V battery pack. Reduce Delay: Replace delay() with millis() for non-blocking code in complex projects. Calibration: Adjust map() values if your servo doesn’t reach 0° or 180°.
Conclusion: Your Journey Begins Here
You’ve now learned how to wire, program, and troubleshoot the SG90 servo with Arduino. These skills form the backbone of countless automation projects. Experiment with different sensors, code logic, and mechanical designs to unlock the full potential of this tiny motor.
Remember, every expert was once a beginner. Keep tinkering, and soon you’ll be building systems that move, sense, and interact with the world!
Update:2025-09-16
Contact Kpower's product specialist to recommend suitable motor or gearbox for your product.