Servo Motor Control Using Arduino
Servo Motor Control Using Arduino
Servo motors are available at different shapes and sizes. A servo motor will have mainly there wires, one is for positive
voltage another is for ground and last one is for position setting. The RED wire is connected to power, Black wire is
connected to ground and YELLOW wire is connected to signal.
A servo motor is a combination of DC motor, position control system, gears. The position of the shaft of the DC motor is
adjusted by the control electronics in the servo, based on the duty ratio of the PWM signal the SIGNAL pin.
Simply speaking the control electronics adjust shaft position by controlling DC motor. This data regarding position of
shaft is sent through the SIGNAL pin. The position data to the control should be sent in the form of PWM signal through
the Signal pin of servo motor.
The frequency of PWM (Pulse Width Modulated) signal can vary based on type of servo motor. The important thing here
is the DUTY RATIO of the PWM signal. Based on this DUTY RATION the control electronics adjust the shaft.
As shown in figure below, for the shaft to be moved to 9o clock the TURN ON RATION must be 1/18.ie. 1ms of ON time
and 17ms of OFF time in a 18ms signal.
For the shaft to be moved to 12o clock the ON time of signal must be 1.5ms and OFF time should be 16.5ms. This ratio
is decoded by control system in servo and it adjusts the position based on it. This PWM in here is generated by using
ARDUINO UNO.
Example 1
Hardware Required
Circuit
Servo motors have three wires: power, ground, and signal. The power wire is typically red, and should be connected to
the 5V pin on the Arduino or Genuino board. The ground wire is typically black or brown and should be connected to a
ground pin on the board. The signal pin is typically yellow, orange or white and should be connected to pin 9 on the board.
Schematic
Code
#include <Servo.h>
Servo myservo; // create servo object to control a servo
// twelve servo objects can be created on most boards
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
}
}
Example 2
Hardware Required
Circuit
Servo motors have three wires: power, ground, and signal. The power wire is typically red, and should be connected to
the 5V pin on the Arduino or Genuino board. The ground wire is typically black or brown and should be connected to a
ground pin on the board. The signal pin is typically yellow or orange and should be connected to pin 9 on the board.
The potentiometer should be wired so that its two outer pins are connected to power (+5V) and ground, and its middle pin
is connected to analog input 0 on the board.
Schematic
Code
/*
Controlling a servo position using a potentiometer (variable resistor)
by Michal Rinott <http://people.interaction-ivrea.it/m.rinott>
#include <Servo.h>
void setup() {
myservo.attach(9); // attaches the servo on pin 9 to the servo object
}
void loop() {
val = analogRead(potpin); // reads the value of the potentiometer (value
between 0 and 1023)
val = map(val, 0, 1023, 0, 180); // scale it to use it with the servo (value
between 0 and 180)
myservo.write(val); // sets the servo position according to the scaled
value
delay(15); // waits for the servo to get there
}