What is Servo Motors??
We can achieve the desired angle of the servo motor according to the application requirement. The difference between a DC motor & servo motor is that the servo motor is capable of holding position & precise control over rotation & velocity. Servo motor makes the motor rotate at a certain angle or desired angle for a given input signal. A Servo motor can be an AC servo motor or DC servo motor depending upon the requirement of the application.
How do Servo Motors Work??
Suppose you
have to rotate the motor at 180° and previously you have rotated 90 °. When you
send a command 180° from your Arduino then it will compare the new value with the previous value in our case it's 90 °.so the servo motor will make a difference it
will be 90. now servo motor will rotate more than 90° to achieve our desired 180°
angle. Also motor rotates the potentiometer which acts as feedback and is fed to the error mechanism.
Rotation of the potentiometer generates some voltage and this voltage is compared with desired
voltage to achieve our target position.
Again when a new value is sent by Arduino it will again compare with the previous value to generate a difference between two signals. The comparison of actual angle and desired angle will be compared until the error signal difference will be zero. In our case, the previous value was 180°. This servo motor uses PWM (pulse width modulation) signal to rotate the servo motor.
Servo motor
expects to receive pulse at 50HZ every after 20 milliseconds
The width
of the pulse is used to determine the angle of the servo motor. If we want to rotate the servo at an angle of 0°, then a pulse width of 1ms is applied.
If we want to rotate the servo at an angle of 90°, then a pulse width of 1.5ms is applied.
If we want
to rotate the servo at an angle of 180°, then a
The duration of the pulse can be varied for different servo motors, it depends upon the manufacturer of the servo.
SG90 servo motor runs between 4.8 – 6 volts (DC). We will be
powered up by 5v by Arduino, when the motor is IDLE it will consume 10 mA of current
and when there is the rotation of the motor it will consume 100-250 mA.
If you are using a servo motor that consumes more current than 250 mA or you are using more than one servo motor then you should use an external power supply keeping Arduino ground common.
Why do we use servo library instead of directly using PWM pins??
Servo motor
expects to receive pulse at 50HZ every after 20 milliseconds 1ms for is fully turned one direction, 1.5ms to
rotate at 90 degrees and 2ms is fully 180 degree. On other hand, analogWrite
PWM has a full range duty cycle (0 to 100%) at around 490 Hz or 980 Hz. Outputs
of Arduino digital pins 3,5,6,9,10,11 does not provide stability and resolution
for frequency for timing signal for rotation. If we create manual signals using
digitalWrite
and delayMicroseconds
functions, without using Servo library in the program, then due inaccuracy
in timing signal will cause inaccurate rotation which gives difficulty for the user
in controlling servo motors.
Servo motor sg90 specifications
- Rotational degree - 180°
- Torque - 4.8V (1.80 kg-cm)
- Operating voltage - 4.8 V (~5V)
- Temperature range - 0 ºC – 55 ºC
- Weight - 9 gm
- Speed - 0.1 s/60 degree
sg90 servo dimensions
- Length - 0.91 in (23.1 mm)
- Width - 0.48 in (12.2 mm)
- Height - 1.14 in (29.0 mm)
Application of Servo motor
- Robotic arm or robotics projects.
- Toy cars.
- CD or DVD player.
- Conveyor Belts.
- Metal Cutting & Metal Forming Machines.
- CNC/printing machines & 3d printers.
To program the servo motor with Arduino we will be using a built-in servo library
#include
<Servo.h>
Servo.h
gives accessibility to control the servo motor in a much easier way
Servo library includes functions such as
- attach()
- write()
- writeMicroseconds()
- read()
- detach()
attach()
– this function takes Arduino pin as a parameter which we want to connect to
servo pin
e.g.
- attach(pin)
myservo.attach(12)
write()
– this function takes an angle as a parameter which we want to drive or rotate the servo motor
e.g.
- write(angle)
myservo.write(90)
writeMicroseconds()
– this function takes pulse width as a parameter that corresponds to angle
e.g.
- writeMicroseconds(pulse
width in microseconds)
myservo.writeMicroseconds(1500)
1.5ms
is equal to 90 degree
read()
- this
functions does not take any parameter instead this function returns value. This
function tells us at what position or angle our servo motor currently is.
int
angle = myservo.read()
Serial.print(angle)
detach() - this function takes an Arduino pin as a parameter which we connected had connected to the servo motor. These functions detach the Arduino pin after the program is executed.
- SG-90 (servo motor module)
- Jumper wires
- Breadboard
Connect the ground pin of the touch sensor to the ground pin of Arduino, the VCC pin to 5v, and the Signal pin to 12. the red wire is VCC (positive voltage) brown wire is GND (negative voltage) and the orange wire is (PMW) signal pin in the servo motor.
Code
Servo myservo; // create servo object to control a servo
void setup()
{
myservo.attach(12); // connect signal pin of servo to pin no 12 of Arduino
Serial.begin(115200); // set baud rate to 115200
}
void loop()
{
Serial.print("Angle:");
myservo.write(0); // rotate servo motor to 0 degree
int angle = myservo.read(); // know the current angle of servo motor
Serial.print("|");
Serial.print(angle);
delay(1200); // delay of 1200 milliseconds
myservo.writeMicroseconds(1500); // rotate servo motor to 90 degree using 1.5ms width pulse
int angle2 = myservo.read(); // know the current angle of servo motor
Serial.print("|");
Serial.print(angle2);
delay(1200);
myservo.write(180); // rotate servo motor to 180 degree
int angle3 = myservo.read(); // know the current angle of servo motor
Serial.print("|");
Serial.println(angle3);
delay(1200);
// myservo.detach(); // de-attach the pin of servo
}
First, we include an in-built servo library in our code. Then we created an object from the servo library as my servo. Inside the void loop, we have attached pin 12 of Arduino to the servo motor.
In the void loop initially, we rotate the servo motor to 0 degrees, 90 degrees then 180 degrees. We also have used the read() function to know the current angle of the servo motor. And introduce a delay of 1200 milliseconds.
OUTPUT
Now we used the serial monitor to observe the output. . To open up the serial monitor, click on Tools > Serial Monitor or use the shortcut (SHIFT + CONTROL + M). You can see output on the Serial monitor. its current angle or position servo is rotating.
How to rotate the servo motor clockwise and anti-clockwise??
Servo myservo; // create servo object to control a servo
void setup()
{
myservo.attach(12); // connect signal pin of servo to pin no 12 of Arduino
Serial.begin(115200); // set baud rate to 115200
}
void loop()
{
for(int i=0;i<=180;i++)
{
myservo.write(i);
delay(20);
}
for(int i=180;i>=0;i--)
{
myservo.write(i);
delay(20);
}
}
Conclusion -
Today
we learn about what is a servo motor, how to use a servo motor with Arduino, the application of a servo motor, how servo motor work with Arduino & working principles of servo motor.