For controlling servo motors, the Servo Library is a fantastic library. Two straightforward examples that may be applied to any Arduino board are provided in this article.
in the example, you may use an Arduino and a potentiometer to control the position of an RC (hobby) servo motor.
Hardware Required
Image | Name | Quantity | Shop Online |
---|---|---|---|
Arduino UNO | 1 No | ||
Servo Motor | 1 No | ||
Potentiometer | 1 No |
Circuit diagram
Arduino Code
#include <Servo.h>
Servo myservo; // create servo object to control a servo
// twelve servo objects can be created on most boards
int pos = 0; // variable to store the servo position
void setup() {
myservo.attach(9); // attaches the servo on pin 9 to the servo object
}
void loop() {
for (pos = 0; pos <= 180; pos += 1) { // goes from 0 degrees to 180 degrees
// in steps of 1 degree
myservo.write(pos); // tell servo to go to position in variable 'pos'
delay(15); // waits 15ms for the servo to reach the position
}
for (pos = 180; pos >= 0; pos -= 1) { // goes from 180 degrees to 0 degrees
myservo.write(pos); // tell servo to go to position in variable 'pos'
delay(15); // waits 15ms for the servo to reach the position
}
}