Maker Pro
Arduino

Servo Motor Control with Arduino Uno

July 19, 2024 by Rachana Jain
Share
banner

In this comprehensive guide, you'll learn how to interface a servo motor with an Arduino Uno.

Servo motors are indispensable in the world of robotics, automation, and hobby electronics, due to their ability to provide precise control over angular movement. Unlike traditional DC motors that rotate continuously, servo motors are designed to move to a specific angle and hold that position with accuracy and stability. This unique feature makes them important in applications ranging from robotic arms and camera gimbals to remote-controlled aircraft and intricate model systems.

Overview of Servo Motor

Servo motors are widely used in robotics, automation, and control systems for their precision in motion and position control. They are small, efficient, and relatively simple to interface with microcontrollers, making them popular among hobbyists and engineers alike. Unlike regular DC motors, which rotate continuously, servo motors rotate to a specific angle and hold that position until instructed otherwise. This makes them ideal for applications where precise angular positioning is required, such as in robotic arms and automation.

Servo Motor Working

The working principle of a servo motor revolves around closed-loop control, which involves feedback systems to regulate the motor's position. Here’s a simplified breakdown of its working:

  1. Control Signal: The position of the servo motor is determined by a Pulse Width Modulated (PWM) signal, typically provided by a microcontroller like the Arduino. This signal consists of a series of pulses, with the width of each pulse determining the angle to which the servo should rotate.
  2. DC Motor and Gearbox: Inside the servo motor, there is a small DC motor connected to a gear train. The gears reduce the speed and increase the torque, allowing for precise control over the angular position.
  3. Feedback Mechanism: A potentiometer (variable resistor) is connected to the output shaft of the servo. This potentiometer provides a feedback voltage that varies with the shaft’s position.
  4. Control Circuit: The control circuit compares the feedback voltage from the potentiometer with the input control signal (PWM). If there is a difference, it adjusts the motor's direction and speed to reduce this error, thus positioning the shaft correctly.

PWM Control Signal

The PWM control signal typically has a frequency of 50Hz (20ms period). The position of the servo motor’s shaft is determined by the width of the pulse:

  • 1ms Pulse Width: Positions the servo at 0 degrees.
  • 1.5ms Pulse Width: Positions the servo at 90 degrees (neutral position).
  • 2ms Pulse Width: Positions the servo at 180 degrees.

Servo Motor Pinout

A standard servo motor has three wires:

  1. Power (VCC): Usually red, this wire connects to the positive supply voltage, typically 5V.
  2. Ground (GND): Usually black or brown, this wire connects to the ground of the circuit.
  3. Signal: Usually yellow, orange, or white, this wire carries the PWM signal from the microcontroller to control the servo position.

Interfacing Arduino with Servo Motors

Interfacing a servo motor with an Arduino Uno is straightforward thanks to the built-in Servo library, which simplifies the process of generating the necessary PWM signal.

Components Required

  • Arduino Uno board
  • Servo motor 
  • Jumper wires

Circuit Diagram

  1. Connect the VCC wire of the servo to the 5V pin on the Arduino.
  2. Connect the GND wire of the servo to a GND pin on the Arduino.
  3. Connect the Signal wire of the servo to one of the PWM-capable digital pins on the Arduino (e.g., pin 9).

Code

To control the servo motor, we’ll use the Arduino Servo library, which abstracts the complexities of generating the PWM signal.

#include <Servo.h>
Servo myServo;  // Create a servo object to control a servo

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

void loop() {
  // Rotate the servo from 0 to 180 degrees
  for (int angle = 0; angle <= 180; angle++) {
    myServo.write(angle);  // Move the servo to the current angle
    delay(15);             // Wait for the servo to reach the position
  }
  
  // Rotate the servo from 180 back to 0 degrees
  for (int angle = 180; angle >= 0; angle--) {
    myServo.write(angle);  // Move the servo to the current angle
    delay(15);             // Wait for the servo to reach the position
  }
}

Video

If you want to learn how to make a servo tester checkout How to Control Servo Motor with Arduino

Author

Avatar
Rachana Jain

I'm an avid Arduino and electronics enthusiast with a passion for tinkering, experimenting, and bringing innovative ideas to life. From creating custom circuits to coding intricate projects, I thrive on the thrill of turning concepts into reality.

Related Content

Comments


You May Also Like