Download as pptx, pdf, or txt
Download as pptx, pdf, or txt
You are on page 1of 11

ECE 2030

Arduino Based System


Design

Daw Khaing Su Wai


Experiment 12

Controlling micro servo motor


Servo motor
• Servo motors is motors with rotatory actuator with control on its movement angle and precession
which works with closed loop feedback control system. A servo motor has a simple DC motor
whose shaft is connected to gears to control the speed of rotation and to add the precision.
• It also has a sensor generally a potentiometer which feedbacks the position of the shaft to the
control board, when a signal is given to the control board for a rotation of +45°, the control board
turns on DC motor till the sensor feedbacks the correct +45° position.
• There are several models of servo motors. In this tutorial Micro Servo 9g SG90 from Tower Pro. It
allows us to sweep in 180 degrees which is between -90° to +90° angle.
• Servo motors has a resolution near to 1 degree, which means it is possible to move the shaft in 1
degree angle. this is the maximum resolution which we are going to achieve with the PWM signal
of Arduino.
• These motors work with a PWM signal(pulse width modulation), with a work pulse between 1ms
and 2ms and with a period of 20ms (50 Hz). This data indicates the maximum speed at which can
move the servomotor with Arduino and can only change position every 20ms which depend on
the type and brand of servo.
https://www.circuitschools.com/how-to-control-servo-motor-with-arduino-and-how-they-work/
Servo motor

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
}

You might also like