How to rotate a servo motor on a specific angle?

I am trying to rotate a top on a specific angle after every 1 second. For example: 1 degree/second. I have two question:

1. Can I achieve this with servo motors?
2. If not, what motor should I use?

I know this question has very little description. I am mainly a programming guy. I don't much about arduino stuff. It's actually a hobby project for me.

The kind people who have written the Arduino libraries include software drivers for servo control. These drivers abstract the hardware from the programmer of the sketch software (the sketch is application software you write in C or C++). It is unnecessary to understand the driver in detail. However the servo driver software (likely written in C++) can been examined here.

To move a normal servo to a particular position, make a call to the Arduino Servo Driver using the function write(angle) passing an angle value from 0 to 180. The value is an approximation of degrees and the value type is of type integer.

In the above links you will find this example of sweeping the servo motor across its range:

``````/* Sweep
by BARRAGAN <http://barraganstudio.com>
This example code is in the public domain.

modified 8 Nov 2013
by Scott Fitzgerald
http://www.arduino.cc/en/Tutorial/Sweep
*/

#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
}
}
``````

Proceed to that page for more details including a schmatic (diagram) of how the servo is connected to an Arduino Uno board.

• One more question: can I use small arduino's like nano instead of mega/uno?
– O_o
Jun 8, 2019 at 13:24
• The deal w/using different platforms can generally be boiled down to 2 issues. One, to make sure the HW is there to deal with the application. In this case the Arduino servo libraries use the PWM HW inside the processor. And both the Uno and Nano use the ATmega328P processor. So that should work. Two, the library has been ported to the different processor and board. In this case the processor is the same. So it is likely you can use a Nano instead of the Uno. Jun 8, 2019 at 13:54
• If not clear, @PotatoMan is correct. Normal servos have a limited rotation (less than one complete revolution). As you said, your end-goal is not clear. So we can not help in picking out an alternative motor. Jun 8, 2019 at 13:58
• thanks man. I will work on it and find out more. I have got my starting point. Thanks for clearing things up. Have a nice day :)
– O_o
Jun 8, 2019 at 19:37

You can rotate the servo motor per degree, but it has it's limitations, it only has 0-180 degrees of movement. If you need to rotate beyond that, I would recommend using Stepper motors, they can be very precise, but they don't have a potentiometer, like a servo does, so, after resetting the system, the motor won't comeback to the starting position.

• Thanks man. However, @st2000 gave a far more detailed example.
– O_o
Jun 8, 2019 at 13:23