Professional Documents
Culture Documents
Lecture 08 Experiment-12
Lecture 08 Experiment-12
https://www.circuitschools.com/how-to-control-servo-motor-with-arduino-and-how-they-work/
Pulse Width modulation in servo motor
• A series of pulses are send to the control pin, generally servo motors are capable of receiving
pulses for every 20ms, so we can change the position of the motor every 20ms so the signal
should be sent with 50Hz frequency.
• The duration of the pulse determines the position of the servo motor. It ranges from 1ms to 2ms,
but in some motors it is near 2.5ms. If the pulse is high of 1ms duration the servo angle will be 0°,
if duration is 1.5ms the angle will be 90°, and if the duration is 2ms the angle positions is 180°.
https://www.circuitschools.com/how-to-control-servo-motor-with-arduino-and-how-they-work/
Servo motor pinout
• The brown wire is for Ground, Red wire is for Vcc 5v , and finally yellow or orange wire is for data
or control signal.
https://www.circuitschools.com/how-to-control-servo-motor-with-arduino-and-how-they-work/
Interfacing Servo motor with Arduino
Circuit diagram
Code
//Sweeps the shaft of a servo motor back and forth across 180 degrees.
#include <Servo.h>
Servo myservo; // create servo object to control a servo
// twelve servo objects can be created on most boards
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
}
Code
//The following code will turn a servo motor to 0 degrees, wait 1 second, then turn it to 90, wait one more second, turn it to 180, and then go back.
#include <Servo.h>
// Declare the Servo pin
int servoPin = 7;
// Create a servo object
Servo Servo1;
void setup() {
// We need to attach the servo to the used pin number
Servo1.attach(servoPin);
}
void loop(){
// Make servo go to 0 degrees
Servo1.write(0);
delay(1000);
// Make servo go to 90 degrees
Servo1.write(90);
delay(1000);
// Make servo go to 180 degrees
Servo1.write(180);
delay(1000);
Controlling a servo with a potentiometer
Controlling a servo with a potentiometer
#include <Servo.h>
Servo myservo; // create servo object to control a servo
int potpin = 0; // analog pin used to connect the potentiometer
int val; // variable to read the value from the analog pin
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
val = map(val, 0, 1023, 0, 180); // scale it to use it with the servo
myservo.write(val); // sets position to the scaled value
delay(15); // waits for the servo to get there
}