Servo Motor Control using Arduino
Hello everyone! I hope you all will be absolutely fine and having fun. Today, I am going to tell you about how to design an algorithm for
Servo Motor Control using Arduino. First of all I would like to tell you a bit about the servo motors. Servo motors are small devices having an output shaft. We can adjust this shaft in different angular positions by continuously sending the servo coded signal. Servo motor maintains the angular position of the shaft as long as the coded signal is present at the input. If the applied coded signal changes, angular position of the shaft of a servo motor also changes correspondingly. If you are working on Servo Motor then i would suggest you to must have look at this tutorial
Servo Motor control in Proteus, as its always a best practice to design simulation first.
In my previous tutorials I have controlled the direction and speed of the both DC as well as of the stepper motor. Ordinary DC motor has only two input terminals. When power is supplied it simply starts to rotate continuously. In comparison to the DC motor servo motor has three wires. Using servo coded signal we can send commands to the servo motor that in what direction and with what angle it has to rotate. If we want to add motion in our electrical projects, servo motor will be an easy way to do so. Servo motor has a wide range of applications in our daily life e.g elevator, cars, robotics, puppets, remote controlled airplanes and cars, conveyor belts, solar tracking system, antenna positioning, textiles etc.Moreover, I have also controlled the
Servo Motor with PIC Microcontroller, so if you are using PIC Microcontroller then have a look at that one.
Servo Motor Control using Arduino
In the tutorial
Servo Motor Control using Arduino, I will tell you step by step procedure for connecting the servo motor with Arduino and how to design a algorithm in Arduino software to control its angular position with the help of servo coded signal. First of all I would like to tell you about the hardware components necessary for Servo Motor Control using Arduino.
- You can download the complete Arduino source code here by clicking on the button below.
- Just download .rar file, extract it and enjoy the complete source code.
Hardware Required
A complete list of the hardware equipment necessary for this task is given below.
- Computer/Laptop
- Arduino UNO (Micro Controller)
- Appropriate USB Cable
- Servo Motor (4.8 to 6.0V with 2.5 kgf-cm torque)
- Jumper Wires (Cables)
Arduino UNO acts as the backbone of this task. It sends the servo encoded signal to the servo motor to control its angular movement. Arduino UNO board is shown in the figure below.
Servo Motor having torque of 2.5kgf-cm and 4.8-6.0v is used for this project. The selected servo motor is shown in the figure below. Power of 5V is supplied to the servo motor from the Arduino UNO board.
Jumper Wires are used to make the connections of the all the components in order to make the complete circuit with proper working. Jumper wires are shown in the figure below.
Circuit Diagram
- The circuit diagram for Servo Motor Control using Arduino is shown in the figure below.
- I have supplied 5V to red wire of the servo motor as shown in the above figure.
- The black wire is the attached to the GND pin of the Arduino UNO.
- Yellow wire is basically the wire used to control the angular motion as well as the angle of the servo motor.
Source Code Description
- The complete Arduino source code for Servo Motor Control using Arduino is given below.
- You have to just copy the code given below and to past it in your Arduino software.
- By uploading the source code to your Arduino board you will be able to control the servo motor using Arduino.
#include <Servo.h> //library for servo motor
Servo myservo; // servo motor object for its control
int ang = 0; // a variable to store the servo angle
void setup() {
Serial.begin(9600);
myservo.attach(8); // servo motor is attached to pin no 8 og Arduino
}
void loop() {
for (ang = 0; ang <= 180; ang += 5) // goes from 0 degrees to 180 degrees with a step og 5 degree
{
myservo.write(ang); // rotates the servo to rotate at specific angle
delay(50); // adding delay of 50 msec
Serial.println("Motor has started its rotation from 0 to 180 degress");
}
for (ang = 180; ang >= 0; ang -= 5) // goes from 180 degrees to 0 degrees with a step of 5 degree
{
myservo.write(ang); // rotates the servo to rotate at specific angle
delay(50); // adding delay of 50 msec
Serial.println("Motor has started its rotation from 180 to 0 degress");
}
}
- First of all I have inserted the library for servo motor.
- Then I have created a servo object and declared the initial angle of the servo motor.
- After that I have have adjust the baud rate, the rate at which Arduino communicates with the laptop/computer.
- Then I have defined the pin at which the servo motor is attached to the Arduino UNO's board.
- Inside the main loop, I have applied the condition that in between 0 and 180 degrees, the servo motor's angle will be increased with different steps and each step has 5 degrees of angular movement.
- When maximum limit is reached, the angle will be reduced from 180 to 0 degree with different steps, each step having 5 degrees of angular movement.
- That was the brief description of the Arduino complete source code designed for Servo Motor Control using Arduino.
That is all from the tutorial Servo Motor Control using Arduino. I hope you all have enjoyed this tutorial. If you face any sort of problem you can ask me freely in comments any time you want without even feeling any kind of hesitation. I will try my level best to solve your issues in a better way, if possible. I will explore Arduino by making different projects on it and will share all of them with all of you as well in my later tutorials. Till then, Take care :)
Control Servo Motor with Arduino in Proteus
Hello friends, hope you all are fine and having fun with your lives. Today's post is about the 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, suppose I want to move an antenna at a precise angle of 47.5 degrees then if I use 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 today we are going to Control Servo Motor with Arduino and will design the simulation in Proteus ISIS.
First of all, we will have a look at simple control of 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. :)
Where To Buy? |
---|
No. | Components | Distributor | Link To Buy |
1 | Servo Motor | Amazon | Buy Now |
2 | Arduino Uno | Amazon | Buy Now |
Simple Control of Servo Motor with Arduino in Proteus
- 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.
- Servo Motor has three pins:
- First Pin is Vcc.
- Second Pin is Control Pin.
- Third Pin is GND.
- The center pin is the controlling pin and goes to any digital pin of Arduino. I have connected the control pin to pin # 4 of Arduino.
Arduino Code for Servo Motor Control
- 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.
- It's the same code as given in the Servo folder of Examples in Arduino software.
- Upload your hex file to your Proteus Arduino board.
Note:
Proteus Simulation Results
- 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 comes 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 have seen a simple Control of Servo Motor with Arduino in which we simply moved Servo motor from 90 degrees to -90 degrees and vice versa.
- Now I am going to control 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:
- I have added five buttons with Arduino and now with these five buttons, I 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.
Arduino Code
- 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.
Proteus Simulation Results
- Now press these buttons from top to bottom and you will get the below results:
- The above figure is quite self-explanatory but still, I explain a little.
- In the first figure, I pressed the first button and the motor moved to -90 degrees.
- In the second figure, I pressed the second button and the motor moved to -45 degrees.
- In the third figure, I pressed the third button and the motor moved to 0 degrees.
- In the fourth figure, I pressed the fourth button and the motor moved to 45 degrees.
- In the fifth figure, I pressed the fifth button and the motor moved to 90 degrees.
- In the sixth figure, all buttons are unpressed and the motor remained at the last position.
It was quite simple and hope I explained it properly. If you still have any questions then ask in the comments and I will try to resolve them. That's all for today, will see you guys in the next tutorial. Take care !!! :)