EXPT - Servo Motor
EXPT - Servo Motor
Air: Write an Embedded C program for Generating PWM signal for servo motor/DC
motor.
Softwares:
Arduino IDE
Controlling of Servo Motor with Arduino in Proteus ISIS. Servo Motor is a common motor used
in engineering projects for precise circular motion. We can move the servo motor at any desired
angle, which is not possible in the case of other motors i.e. Stepper or DC.
For example, To move an antenna at a precise angle of 47.5 degrees, then if I use a DC Motor, I
have to use an encoder. So, in such cases instead of using a DC motor, I will prefer Servo Motor.
I have already posted Angle Control of Servo Motor using 555 Timer in which I have controlled
servo motor using 555 timer and another tutorial about Controlling of Servo Motor using PIC
Microcontroller in which I have controlled it with PIC16F877a. And going to Control Servo
Motor with Arduino and will design the simulation in Proteus ISIS.
First of all, we will have a look at a simple servo motor with Arduino in Proteus ISIS and then
we will check the control of the servo motor with Arduino using buttons in which we will move
the servo motor to precise angles using buttons. So, let's get started with it. :)
First of all, open your Proteus ISIS software and design the below simple circuit.
You should also have a look at these Proteus Libraries of Components.
The next thing we need to do is to design the code for Arduino. So, open your Arduino software
and copy paste the below code in it.
#include <Servo.h>
Servo myservo;
int pos = 0;
void setup()
{
myservo.attach(4);
}
void loop()
{
for(pos = 0; pos <= 180; pos += 1)
{
myservo.write(pos);
delay(15);
}
for(pos = 180; pos>=0; pos-=1)
{
myservo.write(pos);
delay(15);
}
}
Now compile this code and get your hex file.
Upload your hex file to your Proteus Arduino board.
Now, run your simulation and you will see that your Servo motor will start moving from 90
degrees to -90 degrees and then back to 90 degrees and will keep on going like this, as shown in
the below figures:
Now when you start it, first of all, it will show Position A in the above figure then will move
anticlockwise and pass the position B and finally will stop at Position C and then it will move
clockwise and come back to Position A after passing Position B.
In this way, it will keep on moving between Position A and C.
Till now we have seen a simple control of Servo Motor with Arduino in Proteus ISIS, now let's
have a look at a bit complex control of servo motor with Arduino.
Control Servo Motor with Arduino using Push Buttons
In the previous section, we saw a simple Control of Servo Motor with Arduino in which we
simply moved the Servo motor from 90 degrees to -90 degrees and vice versa.
Now control the Servo motor using five push buttons and each push button will move the Servo
motor to a precise angle.
So, first of all, design a small design as shown in the below figure:
Added five buttons with Arduino and now with these five buttons and will move the Servo
motor to 90, 45, 0, -45 and -90 degrees. So, each button has its precise angle and it will move the
motor to that angle only.
So, now the next thing is the code, copy paste the below code in your Arduino software and get
the hex file:
#include <Servo.h>
Servo myservo;
int degree90 = 8;
int degree45 = 9;
int degree0 = 10;
int degree_45 = 11;
int degree_90 = 12;
void setup()
{
myservo.attach(4);
pinMode(degree90, INPUT_PULLUP);
pinMode(degree45, INPUT_PULLUP);
pinMode(degree0, INPUT_PULLUP);
pinMode(degree_45, INPUT_PULLUP);
pinMode(degree_90, INPUT_PULLUP);
}
void loop()
{
if(digitalRead(degree90) == LOW)
{
myservo.write(180);
}
if(digitalRead(degree45) == LOW)
{
myservo.write(117);
}
if(digitalRead(degree0) == LOW)
{
myservo.write(93);
}
if(digitalRead(degree_45) == LOW)
{
myservo.write(68);
}
if(digitalRead(degree_90) == LOW)
{
myservo.write(3);
}
}
Upload this hex file to your Arduino board in Proteus and run the simulation.