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.

Thanks in advance.

A servo will only move a certain amount, this varies from servo to servo but is typically between 180 and 270 degrees.

You use a servo like this
https://www.arduino.cc/en/Reference/Servo

  1. Can I achieve this with servo motors?

Quite easy to do as long as you buy a conventional servo and not a "continuous rotation servo" which is not really a servo at all and cannot be commanded to go to a specific angle

Things to look out for :

1 - make sure that the servo can actually move through the range of angles that you require. Not all servos can even move through 180 degrees

2 - use an external power supply for the servo, not the 5V output from the Arduino voltage regulator as it can only safely provide a limited amount of current.

You can get finer control of a servo by using the servo.writeMicroseconds() function instead of servo.write(). 1000 steps for 180 degrees versus 180 steps for 180 degrees.

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

O.k. one critical bit of information you need to add. How long must it do this for? If it is forever so in total you may have turned a lot more than 360 degrees then you will need to use a completely different method than if it only has to turn say 90 degrees or 180 degrees in total. The second a normal servo can fairly easily do. The first a servo cannot do but maybe a stepper motor would work better.

Steve

slipstick:
The first a servo cannot do but maybe a stepper motor would work better.

As far as I have read, stepper motor does not have any potentiometer, which means it won't move to it's initial position/or know how to move to it's initial position. I would be needing a 360 rotation in future, but right now, I think 180 will suffice. I just want to see if I can do it.

which means it won't move to it's initial position/or know how to move to it's initial position.

That is why you incorporate some sort indicator like an opto slot so on switch on you can find your base position.

Servos that do 306 degrees are quite expensive so you will have to go to a stepping motor eventually.

A_o:
As far as I have read, stepper motor does not have any potentiometer, which means it won't move to it's initial position/or know how to move to it's initial position. I would be needing a 360 rotation in future, but right now, I think 180 will suffice. I just want to see if I can do it.

If you will need continuous 360 rotation then don't start with a 180-degree hobby servo. You would not learn much of anything useful to the final project while working on the prototype.

A stepper and a "home" switch like a QRE1113 would be a better place to start form.

@A_o:

Did you eventually get any code that demonstrated what you were asking please?

groundFungus:
...1000 steps for 180 degrees versus 180 steps for 180 degrees.

Sure about that?

Microseconds has AFAIK a step/resolution of 4 microseconds, so it could be 250 steps instead of 180.
Leo..

Wawa:
Sure about that?

Microseconds has AFAIK a step/resolution of 4 microseconds, so it could be 250 steps instead of 180.
Leo..

micros() and millis() are based on a timer prescaler setting of 64. The Servo library uses 8. So you could even do half-microsecond steps on a 16MHz Arduino without totally overhauling the library.

Servo minimum position (0°) = 544 µsec, maximum (180°) = 2400 µsec, max - min = 1856 µsec, 1856 / microSecond resolution = 4, = 465 positions total.

for(int pos = 0; pos <= 464; pos++)
{
  servo.writeMicroseconds(pos * 4 + 544);
  delay(5);
}

Whoops! Missed the “AM” on posting time, cursed 12 hour clock! >:( :slight_smile:

@JCA-34F

I’m a novice, both on Arduino and servos, so I’m reading most ‘beginner’ posts I can find about servo operation. I’m not sure to whom you were replying?

I was asking the OP whether he had any code in response to his post but not optimistic about an answer as it was over 4 months old. But from his question

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

… I understood he wanted to rotate through 1 deg every second. I pasted your code into the servo.ino sketch, hopefully correctly:

/* Sweep
 Original sweep.ino 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

 My edit by pasting code from @JCA34F and modifying object to 'myservo'.
*/

#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,544,1500);  // attaches the servo on pin 9 to the servo object
}

void loop()
{

for(int pos = 0; pos <= 464; pos++)
{
  myservo.writeMicroseconds(pos * 4 + 544);
  delay(5);
}

}

On my SG90 Micro Servo that results in a sweep of about 0.1 s and an immediate reverse sweep of about 1.9 s. IOW, ten full cycles took about 20 s.

Is that what you intended?

I’d much appreciate your explaining how it works please. So far I’ve used only degrees, not microseconds, and I’m confused about how sometimes the IDE manages to interpret a mixture of both, Such as in the basic sweep.ino sketch, in which the setup uses micoseconds and the loop uses degrees.

Such as in the basic sweep.ino sketch, in which the setup uses micoseconds and the loop uses degrees.

The Sweep example that comes with the Servo library uses degrees throughout

UKHeliBob:
The Sweep example that comes with the Servo library uses degrees throughout

Those are indeed microseconds...but that is NOT the standard Sweep sketch. That simply contains myservo.attach(9). So someone has been messing with it.

Steve

Indeed. The code posted in reply #14 contains a link to the original Sweep example. Not a microsecond in sight in the original

Strange and a bit worrying as I'd assumed that Arduino library sketches could not be re-saved with the same name after editing?

Do yours have the same two authors named at the top?

Could you tell me the full path on your PCs to the 'original and only pure' servo.ino please?

I assume that you mean Sweep.ino

C:\Program Files (x86)\Arduino\1-8-10\libraries\Servo\examples\Sweep

Thanks, yes, my mistake over the name

I've discovered the source of that 'edited' version of sweep.ino. It was associated with my Elegoo Arduino Starter Kit. Somehow seems to have usurped as my 'default' starting sketch for servos.