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 Uno: A Comprehensive Guide

小编

Published2025-09-16

Introduction to SG90 Servo and Basic Arduino Integration

The SG90 servo motor is a compact, lightweight, and versatile component widely used in robotics, automation, and DIY projects. When paired with an Arduino Uno, it becomes a powerful tool for creating dynamic movements, from simple door openers to advanced robotic arms. In this guide, we’ll explore how to interface the SG90 with Arduino Uno, write efficient code, and troubleshoot common issues.

What is an SG90 Servo Motor?

The SG90 is a 9g micro servo motor capable of rotating approximately 180 degrees. It operates on 4.8V to 6V DC and uses Pulse Width Modulation (PWM) to control its position. Unlike continuous rotation servos, the SG90 moves to specific angles, making it ideal for precise applications like camera panning, valve control, or steering mechanisms.

Components Needed

Arduino Uno SG90 servo motor Jumper wires Breadboard (optional) 5V power supply (or USB cable)

Wiring the SG90 to Arduino Uno

Power Connections: Connect the SG90’s brown wire (ground) to Arduino’s GND pin. Attach the red wire (VCC) to Arduino’s 5V pin. Signal Connection: Plug the orange/yellow wire (signal) into a PWM-enabled digital pin (e.g., D9).

Note: If powering multiple servos, use an external 5V supply to avoid overloading the Arduino’s voltage regulator.

Basic Arduino Code for SG90 Control

The Arduino IDE’s built-in Servo.h library simplifies servo control. Below is a basic code to sweep the servo from 0° to 180°:

```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 += 1) { myservo.write(pos); // Move servo to position 'pos' delay(15); // Wait for the servo to reach the position } for (int pos = 180; pos >= 0; pos -= 1) { myservo.write(pos); delay(15); } }

#### How the Code Works 1. Library Inclusion: The `Servo.h` library provides pre-defined functions for servo control. 2. Servo Object: `myservo` is an instance of the `Servo` class. 3. Attachment: `myservo.attach(9)` links the servo to pin 9. 4. Sweep Logic: The `for` loops increment/decrement the servo position, creating a sweeping motion. #### Common Issues and Fixes - Jittery Movement: Add a capacitor (10µF) between the servo’s power and ground wires. - Servo Doesn’t Move: Check wiring (signal pin must be PWM-capable). - Overheating: Ensure the servo isn’t mechanically blocked or overloaded. #### Project Idea 1: Automated Plant Watering System Use the SG90 to control a valve in a DIY irrigation system. The servo can rotate to open/close a water channel based on soil moisture sensor data. --- ### Advanced Projects and Custom Servo Control Now that you’ve mastered the basics, let’s dive into advanced projects and explore techniques like external power management, multi-servo control, and integrating sensors. #### Using External Power for Servos When using multiple servos or high-torque applications, power them via an external 5V supply: 1. Connect the external supply’s positive terminal to the servo’s VCC (red wire). 2. Link the external ground to both the servo’s GND (brown wire) and Arduino’s GND. 3. Keep the signal wire (orange/yellow) connected to the Arduino. #### Controlling Multiple Servos The Arduino Uno can handle up to 12 servo motors using the `Servo.h` library. Here’s a code snippet for dual-servo control:

cpp

include

Servo servo1; Servo servo2;

void setup() { servo1.attach(9); servo2.attach(10); }

void loop() { servo1.write(90); // Set servo1 to 90° servo2.write(45); // Set servo2 to 45° delay(1000); }

#### Project Idea 2: Smart Dustbin with Motion Sensor Build a hands-free dustbin using an SG90 servo and an ultrasonic sensor: 1. Mount the servo on the dustbin lid. 2. Program the Arduino to open the lid when the sensor detects a hand nearby. 3. Close the lid after a set delay. Code Snippet:

cpp

include

include

define TRIGGER_PIN 2

define ECHO_PIN 3

define MAX_DISTANCE 20 // in cm

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

void setup() { lidServo.attach(9); lidServo.write(0); // Initial position: closed }

void loop() { int distance = sonar.ping_cm(); if (distance <= 10) { lidServo.write(90); // Open lid delay(3000); // Wait 3 seconds lidServo.write(0); // Close lid } } ```

Project Idea 3: Solar Tracker with LDR Sensors

Create a solar panel tracker using two SG90 servos and Light-Dependent Resistors (LDRs):

Mount servos on X and Y axes. Use LDRs to detect light intensity. Adjust servo positions to align the panel with the brightest light source.

Troubleshooting Advanced Issues

Servo Not Responding to Sensor Input: Verify sensor connections and calibrate threshold values. Erratic Behavior: Ensure no power supply fluctuations. Use a separate power source for sensors.

Conclusion

The SG90 servo and Arduino Uno form a dynamic duo for prototyping and automation. From basic angle control to complex sensor-driven systems, the possibilities are endless. Experiment with different code parameters, integrate additional components like potentiometers or Bluetooth modules, and share your creations with the maker community!

This guide equips you with the knowledge to harness the SG90’s potential. Whether you’re a hobbyist or an engineer, these skills will elevate your projects to the next level. Happy tinkering!

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.