Interfacing Servo Motor With Arduino

by Rachana Jain in Circuits > Arduino

141 Views, 0 Favorites, 0 Comments

Interfacing Servo Motor With Arduino

How to Interface Servo Motor with Arduino UNO | Servo Control Arduino Code | Arduino Projects

Servo motors allow for precise control of position and speed. Their ability to provide accurate and reliable movement makes them indispensable in applications requiring fine control and complex, coordinated motions. They are widely used in robotics and automation for tasks such as assembling components, painting, welding, and performing delicate operations.

In this project, we will learn how to interface a servo motor with an Arduino Uno microcontroller. 

Supplies

  • Arduino Uno
  • Servo Motor (SG90)
  • Jumper Wires
  • Breadboard (optional)

Understanding Servo Motors

20240428_104020.jpg

Servo motors are precise electric motors used in various applications requiring precise control of angular or linear position. They are essential components in fields such as robotics, industrial automation, and aerospace due to their ability to provide high torque and smooth performance. The core of a servo motor is an electric motor coupled with a feedback sensor that provides accurate information on the motor's position.

A servo motor operates through a closed-loop system, where the feedback sensor continuously monitors the motor's position. This information is sent to a controller, which adjusts the motor's movements to match the desired output. This feedback loop allows servo motors to maintain high precision and repeatability, making them ideal for applications where exact positioning is crucial. 

Servo Motor Pinout

FCSV7U4LW99GCFM.jpg

A servo motor typically has three pins, each serving a specific purpose for its operation. Here's a breakdown of a common three-pin servo motor pinout:

  1. Power Pin (Vcc): This pin is used to supply the voltage required to power the servo motor. For most standard hobby servos, the voltage range is typically between 4.8V to 6V. This pin is usually connected to a positive power supply.
  2. Ground Pin (GND): This pin is the ground connection for the servo motor. It is connected to the ground of the power supply. Ensuring a common ground between the servo and the controlling device (such as a microcontroller) is essential for proper operation.
  3. Control Pin (Signal): This pin receives the control signal from the controller (like an Arduino or another microcontroller). The control signal is a Pulse Width Modulation (PWM) signal, which determines the position of the servo motor. The duration of the pulse typically ranges from 1ms to 2ms, where 1ms usually corresponds to one extreme of the servo's range and 2ms to the other extreme. A 1.5ms pulse generally represents the neutral or centre position.

Interfacing Servo Motors With Arduino

Slide5.JPG

Servo motors typically have three wires: power (red), ground (black or brown), and signal (yellow, orange, or white).

Here are the connections

Power Connection: The red wire from the servo motor is connected to the 5V pin on the Arduino UNO. This provides the necessary power to the servo motor.

Ground Connection: The black or brown wire from the servo motor is connected to one of the GND (ground) pins on the Arduino UNO. This establishes a common ground for the servo and the Arduino.

Signal Connection: The yellow wire from the servo motor is connected to pin 10 on the Arduino UNO. This pin sends the PWM signal to control the servo motor's position.

Code

#include <Servo.h>
#define SERVO_PIN 10

Servo myservo; // create servo object to control a servo

int pos = 0; // variable to store the servo position

void setup() {
myservo.attach(SERVO_PIN); // attaches the servo on pin 9 to the servo object
myservo.write(pos); // go to position zero first
delay(2000); // wait for some time
}

void loop() {
delay(1000); // delay of 1 second before start
for (pos = 0; pos <= 180; pos += 1) { // goes from 0 degrees to 180 degrees in steps of 1 degree
myservo.write(pos); // tell servo to go to position in variable 'pos'
delay(15); // waits 15 ms for the servo to reach the position
}
delay(1000); // delay of 1 second before changing direction of rotation
for (pos = 180; pos >= 0; pos -= 1) { // goes from 180 degrees to 0 degrees
myservo.write(pos); // tell servo to go to position in variable 'pos'
delay(15); // waits 15 ms for the servo to reach the position
}
}

Code Explanation

Library and Pin Definition

#include <Servo.h>: Includes the Servo library to facilitate servo motor control.

#define SERVO_PIN 10: Defines a constant for the pin number where the servo motor is connected.


Servo Object and Position Variable

Servo myservo: Creates a Servo object named myservo to control the servo motor.

int pos = 0: Declares an integer variable pos to store the servo's position.


Setup Function

void setup() { }: Initializes settings for the servo motor.

myservo.attach(SERVO_PIN): Attaches the servo motor to the specified pin (pin 10).

myservo.write(pos): Sets the servo to the initial position (0 degrees).

delay(2000): Introduces a delay of 2000 milliseconds (2 seconds) to ensure the servo reaches the initial position.


Loop Function

delay(1000): Waits for 1000 milliseconds (1 second) before starting the movement sequence.

for (pos = 0; pos <= 180; pos += 1): Loop to move the servo from 0 to 180 degrees in 1-degree increments.

myservo.write(pos): Moves the servo to the current position stored in pos.

delay(15): Introduces a 15-millisecond delay to allow the servo to reach the position smoothly.

What Is a Servo Tester?

A servo tester is a device used to test and control servo motors independently of a microcontroller or other control systems. It provides a simple and effective way to check the functionality of servos, ensuring they operate correctly before being integrated into a larger system. 

Checkout this tutorial to build a Servo Tester: Making a Servo Tester using Arduino UNO