• Trendy 25 save up 35% off today  Shop now
  • Supper Value Deals - Save more with coupons
  • Get great devices up to 50% off  View details
Cart 0
No products in the cart.

Servo Control with Arduino: A Beginner’s Guide to Servo Motors

Servo motors are fundamental components in robotics and electronics projects, enabling precise control over angular movement. Whether you’re working on a robotic arm, an RC car, or any project that requires controlled motion, understanding how to control a servo motor with an Arduino is an essential skill. This tutorial will guide you step-by-step through the process, from understanding what a servo motor is to writing code that controls its position.

What is a Servo Motor and How Does It Work?

A servo motor is a rotary actuator or linear actuator that allows for precise control of angular or linear position, velocity, and acceleration. It consists of a motor coupled to a sensor for position feedback. The motor is controlled with a control signal (PWM – Pulse Width Modulation), which determines the amount of movement and direction.

Key Components of a Servo Motor:

  • DC Motor: The core of the servo, responsible for rotation.
  • Gearbox: Reduces the speed of the motor and increases torque.
  • Control Circuit: Receives the PWM signal and adjusts the motor's position accordingly.
  • Position Sensor (Potentiometer): Measures the current angle of the servo and provides feedback to the control circuit.

How a Servo Motor Works:

  • The servo motor receives a PWM signal from the Arduino, which determines the position of the servo’s shaft. The length of the pulse corresponds to the angle the servo will move to. For example, a typical servo motor might accept pulses ranging from 1 millisecond (0°) to 2 milliseconds (180°).
  • The internal control circuit compares the commanded position with the current position, as measured by the potentiometer. If there’s a discrepancy, the motor is driven until the desired position is reached.

Servo motors are typically used in applications where precise positioning is required, such as in robotic arms, RC vehicles, and automated systems.

Wiring a Servo to Arduino

Now that you understand how a servo motor works, let's move on to wiring it up with an Arduino. The setup is straightforward and requires only a few connections.

Components Needed:

  • Arduino Uno (or any compatible Arduino board)
  • Servo motor (e.g., SG90, MG995)
  • Jumper wires
  • External power supply (optional, depending on the servo's power requirements)

Wiring Diagram:

  1. Connect the Servo’s Power Wires:
    • Red Wire (VCC): Connect this wire to the 5V pin on the Arduino (or an external 5V power supply if the servo requires more current).
    • Black/Brown Wire (GND): Connect this wire to the GND pin on the Arduino.
  2. Connect the Servo’s Signal Wire:
    • Yellow/Orange Wire (Signal): Connect this wire to a digital PWM-capable pin on the Arduino, such as pin 9.

Note on Power:

  • Some servos can draw more current than the Arduino can supply, especially under load. In such cases, it’s better to power the servo from an external power supply. Make sure to connect the ground of the external power supply to the ground of the Arduino to establish a common reference.

Writing Code to Control the Servo Position

With the servo motor wired up, the next step is to write the code that will control its position. The Arduino IDE comes with a built-in Servo library that makes it easy to control servo motors.

Step 1: Install the Servo Library

  • The Servo library is included with the Arduino IDE by default. To use it, you simply need to include the library in your sketch.
#include <Servo.h>

Step 2: Declare a Servo Object

  • Create a servo object to control your servo motor.

    Servo myServo;

Step 3: Attach the Servo to a Pin

  • Attach the servo object to the pin that the servo’s signal wire is connected to.
void setup() {
  myServo.attach(9);  // Attaches the servo on pin 9 to the servo object
}

Step 4: Write the Code to Control the Servo

  • In the loop() function, you can write code to control the servo’s position. The myServo.write() function allows you to set the angle of the servo.
void loop() {
  myServo.write(0);    // Set servo to 0 degrees
  delay(1000);         // Wait for a second

  myServo.write(90);   // Set servo to 90 degrees
  delay(1000);         // Wait for a second

  myServo.write(180);  // Set servo to 180 degrees
  delay(1000);         // Wait for a second
}

Explanation:

  • This code rotates the servo to 0 degrees, waits for one second, then moves it to 90 degrees, waits again, and finally moves it to 180 degrees. This process repeats in a loop.

Step 5: Upload and Test

  • Upload the code to your Arduino and observe the servo motor. It should move to the specified positions (0°, 90°, 180°) in sequence.

Exploring Different Ways to Control the Servo

Controlling the servo with hardcoded positions is useful for learning, but in real-world applications, you often need to control the servo dynamically. Let’s explore different ways to control the servo using inputs like a potentiometer or even sensors.

1. Controlling Servo with a Potentiometer

A potentiometer is a variable resistor that you can use to generate an analog input signal. By reading the potentiometer’s value, you can control the servo’s position interactively.

Wiring:

  • Connect the potentiometer’s middle pin to the Arduino’s analog input pin (e.g., A0).
  • Connect the other two pins to 5V and GND, respectively.

Code Example:

#include <Servo.h>

Servo myServo;
int potPin = A0;   // Analog input pin that the potentiometer is attached to

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

void loop() {
  int potValue = analogRead(potPin);   // Reads the value of the potentiometer (0-1023)
  int angle = map(potValue, 0, 1023, 0, 180);  // Maps the potentiometer value to a servo angle (0-180)
  myServo.write(angle);  // Sets the servo position according to the scaled value
  delay(15);  // Small delay for stability
}

Explanation:

  • The potentiometer’s value is read as an analog input (0-1023) and then mapped to a corresponding angle (0-180) using the map() function. The servo motor’s position is updated in real-time as the potentiometer is adjusted.

2. Controlling Servo with Sensors

Servo motors can also be controlled based on sensor input, making them ideal for responsive projects. For example, you could control a servo using an ultrasonic distance sensor to create a simple obstacle-avoiding mechanism.

Wiring:

  • Connect an ultrasonic sensor (e.g., HC-SR04) to the Arduino, with the trigger and echo pins connected to digital pins and the power pins connected to 5V and GND.

Code Example:

#include <Servo.h>

Servo myServo;
const int trigPin = 10;
const int echoPin = 11;

void setup() {
  pinMode(trigPin, OUTPUT);
  pinMode(echoPin, INPUT);
  myServo.attach(9);  // Attaches the servo on pin 9 to the servo object
}

void loop() {
  long duration, distance;
  digitalWrite(trigPin, LOW);
  delayMicroseconds(2);
  digitalWrite(trigPin, HIGH);
  delayMicroseconds(10);
  digitalWrite(trigPin, LOW);
  
  duration = pulseIn(echoPin, HIGH);
  distance = (duration / 2) / 29.1;

  if (distance < 20) {
    myServo.write(0);    // Turn the servo if an object is detected within 20 cm
  } else {
    myServo.write(90);   // Otherwise, set it to the neutral position
  }
  delay(100);
}

Explanation:

  • This code reads the distance to an object using the ultrasonic sensor. If an object is detected within 20 cm, the servo motor turns to 0 degrees; otherwise, it stays at 90 degrees. This can be the basis for more complex behavior, such as obstacle avoidance.

Troubleshooting Common Issues

When working with servo motors, you may encounter some common issues. Here are a few tips to troubleshoot them:

  1. Servo Not Moving:
    • Check all connections, especially the signal wire to the correct Arduino pin.
    • Ensure that the servo is receiving enough power. If it’s a high-torque servo, it might need an external power supply.
    • Verify that the correct library and functions are used in the code.
  2. Unstable or Jittery Movement:
    • Add a small delay in the loop to stabilize the servo’s movement.
    • Ensure that the power supply is adequate and that the servo’s ground is connected to the Arduino’s ground.
  3. Limited Range of Movement:
    • Make sure the servo’s mechanical range is not being restricted by the mounting or connections.
    • Verify that the PWM signal being sent is within the servo’s operational range (typically 0 to 180 degrees).

Expanding Your Servo Control Skills

Once you continue to build your skills with servos, consider exploring these additional topics and projects:

  1. Multiple Servo Control: Learn how to control multiple servos simultaneously using an Arduino. This is particularly useful in projects like robotic arms where several servos are needed to control different joints.
  2. Feedback Control with Servos: Delve into the concept of feedback control where you use additional sensors to fine-tune the position of the servo motor. This is crucial in precision applications like 3D printers and CNC machines.
  3. Wireless Control of Servos: Experiment with wireless modules like Bluetooth or Wi-Fi to control servos remotely. This can be applied in projects like remote-controlled cars or home automation systems.
  4. Building a Robotic Arm: Combine multiple servos to build a simple robotic arm that can perform tasks like picking and placing objects. This project introduces concepts of inverse kinematics and control algorithms.
  5. Servo Control with Advanced Algorithms: Explore more sophisticated control methods, such as PID (Proportional, Integral, Derivative) control, to enhance the accuracy and responsiveness of servo movements in dynamic environments.

Conclusion

Mastering servo control with Arduino is a foundational skill that opens up endless possibilities in robotics and electronics. Whether you're a beginner or an experienced maker, understanding how to interface and control servo motors will significantly enhance your ability to create dynamic and responsive projects. From simple setups using potentiometers to more complex applications involving sensors and feedback loops, servos are versatile components that can bring your ideas to life.

By following this guide, you’ve learned the basics of servo motors, how to wire them up with an Arduino, write the necessary code, and explore various methods of controlling them. As you continue to experiment and build, you'll discover new ways to utilize servos in your projects, taking your robotics and electronics skills to the next level.

Happy building!


icon

Sign up to Newsletter

...and receive ₹25 coupon for first shopping.