Arduino Tutorial for Beginners – How to Control Speed of Servo-Motor With Arduino




In this post on Arduino Tutorial For Beginners, this topic about how to control Servo or Motor with Arduino. Servo is quite similar to Step Motor but it isn’t exact as the Step Motor. Inside of Servo have 4 parts:
+ Control circuit – Is integrated from H-bridge, the H-bridge is the circuit we usually use to control motor, it help you open and close the control signal from the Microcontroller.
+ Motor
+ Gearbox – Support turn the servo from motor
+ Potentiometer – We will control this via PWM (Pulse width modulation)

Servo

There are many types of servos we will show you 3 popular types:
– First is Blue Servo (left), this is the cheapest servo and the quality is quite good, it can pull about 1.8kg with 4.8V, and with 6V it can pull upto 2kg. And this just approximate, the Blue servo can rotate from -90 -> +90 (180 degree)
– Second is Black Servo (mid), it more expensive a little bit but the quality is more stable. It can rotate approximately 200 degree, and it can pull as same as with the Blue Servo.
– Last one is MG996R, this is the most expensive in three types but the quality is good, and it can be pull stronger. Upto 8kg with 4.8V and 10kg with 6V.
You can see the information in the picture, and i will use the Black Servo(mid) in this topic.

TypesofServo

To control Servo we have to use PWM signal. In Arduino we will create PWM signal by set HIGH and LOW state of a PWM pins (~) by function delayMicroseconds (_time). Below is how PWM operate.

PWMoperating

These are some PWM pins on Arduino.

PWMpins

Okay, now let’s connect as the diagram below and then using code without Servo library

DiagramWithoutPTM


void setup()
{
pinMode(9,OUTPUT);
}
void loop()
{
// T = 20ms
// 0 degree
for(int i = 0 ; i<10000;i++)
{
digitalWrite(9,HIGH);
delayMicroseconds(1000); //1ms
digitalWrite(9,LOW);
delayMicroseconds(19000);//19ms
}
delay(2000);
// 90 degree
for(int i = 0 ; i<10000;i++)
{
digitalWrite(9,HIGH);
delayMicroseconds(1500); //1.5ms
digitalWrite(9,LOW);
delayMicroseconds(18500); //18.5ms
}
delay(2000);
// 180 degree
for(int i = 0 ; i<10000;i++)
{
digitalWrite(9,HIGH);
delayMicroseconds(2000); // 2ms
digitalWrite(9,LOW);
delayMicroseconds(18000); // 19ms
}
delay(2000);
}

Now we will use the Servo library, this library is available in your Arduino IDE when you download for the first time. Following the picture below and then using the diagram above with the library code.

ServoLib

Above are two examples about control servo by software. Now we will combine hardware(Potentiometer) and software to control Servo. Let’s connect as the diagram below.

ServoLib

And copy this block of code to your sketch then enjoy your project


#include
Servo myservo;
void setup()
{
myservo.attach(9); // Control servo at pin 9
}
void loop()
{
int ReadAnalog = analogRead(A0); // Reading analog signal from potentiometer
int Convert = ReadAnalog / 5;    // Converting (mapping) to 0->180 of Servo
myservo.write(Convert);          // Control servo
}


Partner Sites

VideoToGifs.com

EasyOnlineConverter.com

SqliteTutorials.com