Servo motors are widely used in various robotics and automation projects due to their ability to accurately control angular motion. These motors are designed to move within a limited range, usually 180º, and can hold their position at a specific angle. Arduino boards can interface with servo motors by generating precise pulse signals to control their position. In this article, we will explore how to control a servo motor’s angular motion using an Arduino board.
Understanding Servo motors Output:
Servo motors have three wires: power (usually red), ground (usually black or brown), and signal (usually yellow or white). The signal wire is connected to one of the Arduino’s digital pins capable of producing pulse signals. By varying the width of the pulses, we can control the position of the servo motor. Typically, a pulse width of 1ms corresponds to the servo at one extreme position, while a pulse width of 2ms corresponds to the servo at the opposite extreme. A pulse width of 1.5ms centers the servo motor.
Servo motor code for Arduino IDE :
The following code snippet demonstrates how to control a servo motor using the Arduino:
int servoPin = 2; // Servo connected to digital pin 2
int myAngle; // Angle of the servo (roughly 0-180)
int pulseWidth; // Variable for servoPulse function
void setup() {
pinMode(servoPin, OUTPUT); // Set pin 2 as output
}
void servoPulse(int servoPin, int myAngle) {
pulseWidth = (myAngle * 10) + 600; // Determine delay
digitalWrite(servoPin, HIGH); // Set servo high
delayMicroseconds(pulseWidth); // Microsecond pause
digitalWrite(servoPin, LOW); // Set servo low
}
void loop() {
// Servo starts at 10 degrees and rotates to 170 degrees
for (myAngle = 10; myAngle <= 170; myAngle++) {
servoPulse(servoPin, myAngle); // Send pin and angle
delay(20); // Refresh cycle
}
// Servo starts at 170 degrees and rotates to 10 degrees
for (myAngle = 170; myAngle >= 10; myAngle--) {
servoPulse(servoPin, myAngle); // Send pin and angle
delay(20); // Refresh cycle
}
}
Explanation of the Code:
In the provided code, the servoPulse()
function is used to generate the pulse signals required to control the servo motor. The servoPin
parameter specifies the digital pin connected to the servo, while the myAngle
parameter represents the desired angle of the servo motor. The pulseWidth
variable is calculated based on the desired angle, determining the delay required for the pulse signal. The delayMicroseconds()
function introduces the appropriate pause, and the digitalWrite()
functions set the servo pin high or low to generate the pulse signal.
Conclusion:
Servo motors offer precise control over angular motion, making them ideal for various robotics and automation applications. By leveraging the capabilities of Arduino boards, we can easily control servo motors and achieve the desired positions. This article explored the use of Arduino to control a servo motor’s angular motion, providing an example code snippet to move the servo motor from 10º to 170º and back again.