Servo Motor Controlled by Arduino
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
#include <Servo.h>
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.
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