tutorial
What Is Servo Motor & How To Control Servos using Arduino
Servo motors are great devices that can turn to a specified position.
Usually, they have a servo arm that can turn 180 degrees. Using the Arduino, we can tell a servo to go to a specified position and it will go there. As simple as that!
Servo motors were first used in the Remote Control (RC) world, usually to control the steering of RC cars or the flaps on an RC plane. With time, they found their uses in robotics, automation, and of course, the Arduino world.
Specifications:
A servo motor has everything built in a motor, a feedback circuit, and most important, a motor driver. It just needs one power line, one ground, and one control pin.
Following are the steps to
connect a servo motor to the Arduino:
1. The servo motor has a female
connector with three pins. The darkest or even black one is usually the ground.
Connect this to the Arduino GND.
2. Connect the power cable that in
all standards should be red to 5V on the Arduino.
3. Connect the remaining line on the
servo connector to a digital pin on the Arduino.
Note:
Feedback circuit: Electronic feedback loops are used to control the output of electronic devices, such as amplifiers. A feedback loop is created when all or some portion of the output is fed back to the input. A device is said to be operating open loop if no output feedback is being employed and closed loop if feedback is being used.
How It Works:
Servos are clever devices. Using just one input pin, they receive the position from the Arduino and they go there. Internally, they have a motor driver and a feedback circuit that makes sure that the servo arm reaches the desired position. But what kind of signal do they receive on the input pin?
It is a square wave similar to PWM. Each cycle in the signal lasts for 20 milliseconds and for most of the time, the value is LOW. At the beginning of each cycle, the signal is HIGH for a time between 1 and 2 milliseconds. At 1 millisecond it represents 0 degrees and at 2 milliseconds it represents 180 degrees. In between, it represents the value from 0–180. This is a very good and reliable method. The graphic makes it a little easier to understand.
Controlling the exact pulse time:
Arduino has a built-in function servo.write(degrees) that simplifies the control of servos. However, not all servos respect the same timings for all positions. Usually, 1 millisecond means 0 degrees, 1.5 milliseconds mean 90 degrees, and, of course, 2 milliseconds mean 180 degrees. Some servos have smaller or larger ranges.
For better control, we can use the servo.writeMicroseconds(us) function, which takes the exact number of microseconds as a parameter. Remember, 1 millisecond equals 1,000 microseconds.
Connection Diagram:
Code:
// Include the Servo library
#include <Servo.h>
// Declare the Servo pin
int servoPin = 9;
// 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);
}
Output:
The arm will first move to 90 degrees from 0 degrees, the move to 180 degrees.
Note:
- attach(): Attach the Servo variable to a pin
- write(): Writes a value to the servo, controlling the shaft accordingly. On a standard servo, this will set the angle of the shaft (in degrees), moving the shaft to that orientation
- writeMicroseconds(): Writes a value in microseconds (us) to the servo, controlling the shaft accordingly
- read(): Read the current angle of the servo (the value passed to the last call to write()).
- attached(): Check whether the Servo variable is attached to a pin.
- detach(): Detach the Servo variable from its pin.
No comments:
Post a Comment