Extra 5% OFF Use Code: OL05
Free Shipping over ₹999

Interface Servo SG90 with Arduino

Introduction

The SG90 is a small, lightweight servo motor capable of rotating approximately 180 degrees. It can be controlled by sending a Pulse Width Modulation (PWM) signal to set the angle.

Required Components

  • Arduino UNO, Nano
  • SG90 servo motor
  • Jumper wires
  • Breadboard (optional)
  • External power source (optional, if using multiple servos)

Pinout

Circuit Diagram / Wiring

  • Brown wire -> GND (Arduino)
  • Red wire -> 5V (Arduino) or an external 5V power supply if Arduino power is insufficient
  • Orange/Yellow wire -> Pin 9 (Arduino) (PWM-capable pin)

Arduino Code / Programming

To control the servo, you can use the built-in Servo library, which simplifies sending the control signals.

#include <Servo.h>

Servo myServo; // Create a Servo object

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

void loop() {
  // Move the servo to 0 degrees
  myServo.write(0);
  delay(1000); // Wait for 1 second

  // Move the servo to 90 degrees
  myServo.write(90);
  delay(1000); // Wait for 1 second

  // Move the servo to 180 degrees
  myServo.write(180);
  delay(1000); // Wait for 1 second
}

Explanation of the Code

  • Include the Servo Library: The Servo library allows easy control of servo motors.
  • Create a Servo Object: This object represents the servo motor you’re controlling.
  • Attach the Servo: The attach() function specifies the pin used to control the servo.
  • Move the Servo: The write() function sets the servo to a specified angle between 0 and 180 degrees.

Testing and Troubleshooting

  • Power Issues: If the servo behaves erratically, consider using an external power source.
  • Angle Range: The SG90 typically moves between 0 and 180 degrees. If it doesn’t move as expected, check the power connections.
  • Servo Jitter: Make sure your power supply is stable; otherwise, the servo may not hold the position properly.

Common Issues and Solutions

  • Insufficient Power Supply: The SG90 servo motor may not work correctly if the power supply is unstable or insufficient. The servo requires 5V and around 500mA current. If the Arduino is not providing enough power, use an external 5V power source.
    • Solution: Use an external 5V power supply and ensure all grounds are connected. For example, connect the servo’s ground wire to both the Arduino GND and the external power supply GND.
  • Power Spike Issues: Servos can cause voltage spikes that may reset the Arduino or cause the servo to behave erratically.
    • Solution: Add a capacitor (100µF or larger) across the power supply to stabilize the voltage.
  • Servo Not Moving at All: If the servo does not move or jitters, double-check the connections and the code.
    • Solution: Verify that the signal pin is connected to a PWM-capable pin on the Arduino, and ensure the servo is not mechanically blocked.

Recommended Setup Using PCA9685

For using the SG90 servo motor with Arduino, the PCA9685 module is highly recommended, especially when you need to control multiple servos or require more precise control. If you find that your servo is not operating correctly when connected directly to the Arduino or if it is running but not accurately, using the PCA9685 can help resolve these issues. Here are some reasons why the PCA9685 is a good choice:

Multiple Servo Control:

  • The PCA9685 can control up to 16 servos using just two Arduino pins (SDA and SCL). This frees up other pins for additional sensors or modules.

Stable Power Supply:

  • It helps distribute the power supply to the servos more evenly, reducing issues caused by power spikes or voltage drops.

Simplified Wiring:

  • With the PCA9685, you only need to connect the power, ground, and signal lines for each servo to the module, making the setup less cluttered.

Precise Control:

  • The module provides 12-bit PWM resolution, allowing fine control over the servo position.

Improved Accuracy:

  • If your servo is running inaccurately when connected directly to the Arduino, the PCA9685’s precise PWM control can significantly enhance its performance.

Additional Recommendations

Using External Power:

  • When using more than one servo, it’s advisable to use an external 5V power supply to power the servos. Ensure that the power supply can provide sufficient current (at least 1A for every 2-3 servos).

Common Ground:

  • Make sure all devices share a common ground to prevent erratic behavior.

    Leave a Reply

    Your email address will not be published.

    Need Help?