SERVO (180 DEGREE ROTATION)
SERVO (180 DEGREE ROTATION)
The Servo Library is an excellent tool for controlling servo motors and includes two simple examples suitable for any Arduino board.
In the initial example, you can utilize an Arduino along with a potentiometer to regulate the position of an RC (hobby) servo motor. The subsequent example showcases sweeping the shaft of an RC servo motor back and forth across a 180-degree range.
Arduino Board
Servo Motor
10k ohm potentiometer
hook-up wires
power supply
A typical servo motor like the Feetech Mini Servo Motor operates within a voltage range of 4.8 to 6 volts and consumes around 5 to 6 mA when idle, demonstrating minimal energy consumption while at rest.
Circuit:
To set up the Knob example, connect the potentiometer's two outer pins to power (+5V) and ground, and connect its middle pin to 9 on the board. Next, follow the circuit diagram to connect the servo motor appropriately.
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
}
}
If you have completed this successfully, move on to the next step.