0% found this document useful (0 votes)
10 views

Servo Motor Controlled by Arduino

Uploaded by

Pranav R
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
10 views

Servo Motor Controlled by Arduino

Uploaded by

Pranav R
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 4

Servo Motor controlled by Arduino

 1 × Breadboard
 1 × Arduino Uno R3
 1 × Servo motor(SG90)
 Jumper wire

A Servo Motor is a small device that has an output shaft. This shaft can be
positioned to specific angular positions by sending the servo a coded signal. As long as
the coded signal exists on the input line, the servo will maintain the angular position of
the shaft. If the coded signal changes, the angular position of the shaft changes. In
practice, servos are used in radio-controlled airplanes to position control surfaces like the
elevators and rudders. They are also used in radio-controlled cars, puppets, and of
course, robots.
DC Servo Motors are classified into two types

1. Standard rotation Servo which moves from 0 to 180 degrees


2. Continuous rotation Servo which moves 360 degrees
Servo Motor Working
 A servo motor receives PWM (Pulse Width Modulation) signals to determine its
angle of rotation. The PWM signal is a square wave with a variable duty cycle,
where the width or duration of the high signal (the "on" time) corresponds to a
specific angle. This signal is usually generated by a microcontroller, such as an
Arduino.
 When the PWM signal is fed to the servo motor's control input (usually the orange
or yellow wire), the motor's control circuit interprets it as a command to move to
a particular angle. The control circuit measures the duration of the high part of
the PWM signal, which corresponds to the desired angle. This duration is typically
in the range of 1 to 2 milliseconds.
 The control circuit then compares this measured duration with the center position
(typically 1.5 milliseconds) and adjusts the motor's shaft to move towards the
desired angle. If the duration of the high part of the PWM signal is shorter than
the center position, the motor turns in one direction; if it's longer, the motor turns
in the opposite direction.
 The servo motor continuously adjusts its position to match the incoming PWM
signal. As the duty cycle of the PWM changes, the motor's shaft rotates
accordingly. This closed-loop control mechanism ensures that the motor
accurately follows the input signal, allowing for precise and repeatable angular
positioning. In essence, the servo motor's responsiveness to PWM signals enables
it to execute controlled movements in various applications, from opening and
closing doors to steering remote-controlled vehicles.
Arduino Servo Sweep Code

#include <Servo.h>

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


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

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

void loop()
{
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 15ms for the servo to reach the position
}
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 15ms for the servo to reach the position
}
}

This test code rotates the servo from 0 degree to 180 degree and then rotates back from 180
to 0 degree without the need of any input from the user side.

#include <Servo.h>

This line includes the Servo library, which provides functions and features for controlling
servo motors in an Arduino sketch.

Servo myservo;

Here, a servo object named myservo is created using the Servo library. This object will be
used to control a servo motor connected to the Arduino. The comment indicates that on most
Arduino boards, you can create up to twelve servo objects.

int pos = 0;

An integer variable pos is declared and initialized to store the position of the servo motor.
void setup()
{ myservo.attach(9);

Within the setup() function, myservo.attach(9); is used to attach the servo object myservo to
pin 9 of the Arduino. This line informs the Arduino that the servo is connected to pin 9 and
that pin will be used to send control signals to the servo.

void loop()
for (pos = 0; pos <= 180; pos += 1)
{
myservo.write(pos);
delay(15);
}

The loop() function contains a for loop that increments the pos variable from 0 to 180
degrees in steps of 1 degree using pos += 1. Inside the loop:

myservo.write(pos);

sends the current position value stored in the pos variable to the servo, commanding it to
move to that position.
delay(15); pauses the program execution for 15 milliseconds to allow the servo time to reach
the specified position.

for (pos = 180; pos >= 0; pos -= 1)


{
myservo.write(pos);
delay(15);
}

Following the completion of the first loop, another for loop is used to decrement the pos
variable from 180 to 0 degrees in steps of 1 degree using pos -= 1. Inside this loop, the servo
is commanded to move to the position specified by the pos variable, and another delay of 15
milliseconds is added between movements.
Circuit Diagram

You might also like