Servo.h working only from 0 to 180 deg

Hi,
I'm trying to test a simple code working with a servo motor.
But as I see from the replay from the motor I see the the command to the motor stop at 180 deg.

I think that the problem is with the Servo.h (Srvo.cpp), that limit the control to 180 deg.

I need to know if some one have a simple code that I can use to turn the servo from 0 to 360 deg, or know how to solve the problem.

The code I used is:

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

modified 8 Nov 2013
by Scott Fitzgerald

*/

#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() {
Serial.begin(9600);
myservo.attach(11); // attaches the servo on pin 9 to the servo object
}

void loop() {
for (pos = 0; pos <= 360; 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'

Serial.print("myservo.read(): ");
Serial.println(myservo.read());
delay(15); // waits 15ms for the servo to reach the position

}
for (pos = 360; pos >= 0; pos -= 1) { // goes from 180 degrees to 0 degrees
myservo.write(pos); // tell servo to go to position in variable 'pos'
Serial.print("myservo.read(): ");
Serial.println(myservo.read());
delay(15); // waits 15ms for the servo to reach the position
}
}

Normally, only winch servos go beyond 360 degrees.

for (pos = 0; pos <= 360; pos += 1) { // goes from 0 degrees to 180 degrees Clearly not.

Please remember to use code tags when posting code

I need to know if some one have a simple code that I can use to turn the servo from 0 to 360 deg, or know how to solve the problem.

Can the servo actually move through 360 degrees ?

Please post a link to the servo that you are using

Hobby servos get their position feedback from a potentiometer (variable resistor). They have physical stops to prevent them from turning beyond the rotation range of the potentiometer (typically about 270°).

There are several options for getting 360° (or more) rotation.

A 'winch' servo has a geared connection between the output shaft and the potentiometer. This allow the output shaft to turn 6 or 10 revolutions and still remain within the rotation range of the potentiometer. These servos are not very common and therefore tend to cost more.

You could do something similar by gearing up a hobby servo: Put a large gear or sprocket on the output shaft of your servo and mesh that with a smaller gear or sprocket on your shaft. If the large gear has twice the teeth of the small gear you should get at least a 360° range.

You could switch to a stepper motor to get infinite rotation in both directions but you can only do relative positioning. The stepper has no feedback so you can't just tell it to go to an angle. You have to know the starting angle, ususally with a sensor to mark a 'home' position.

You could add an absolute encoder to a stepper or DC motor to get absolute positioning but they tend to be expensive. Adding aless-expensive non-absolute encoder (shaft encoder) puts you no better than a stepper where you have to seek a 'home' position and count steps from there.

The servo that I use is DT-3316.

Thanks

Does anything in the specification of the servo available online give you to understand that it could rotate through more than 180 degrees ? Indeed, does it say in the specification that it can even rotate through 180 degrees ? It is not unusual for servos to be limited to rotation through leas than 180 degrees

Does anyone know where can I find the servo.h so I can edit it?

shamayk:
Does anyone know where can I find the servo.h so I can edit it?

Have you looked on your hard-drive? (Hint: It's called "Servo.h")

What were you thinking of editing?

Edit: On second thoughts, if you don't know where it is, maybe you ought not to be messing with it.

shamayk:
Does anyone know where can I find the servo.h so I can edit it?

In your libraries folder.
Mine was here
C:\Program Files (x86)\Arduino\libraries\Servo\src

But I think you are barking up the wrong tree. I think that you have a hardware limitation in the physical servo. Code change rarely overcomes hardware limitations.

I have look on my HD, and din't find servo.h or servo.cpp.
I found it on the on of links with code, and I saw that the servo.cpp is limiting the servo.

This is the part of the code in servo.cpp that I have found:
"
void Servo::write(int value)
{
if (value < 0)
value = 0;
else if (value > 180)
value = 180;
value = map(value, 0, 180, MIN_PULSE, MAX_PULSE);

writeMicroseconds(value);
}
"

Because of what I saw, i thing that it isn't a HW limitation but code.

I understood the servo.h is coming together with arduino, but I don't understand where to find it.

If anyone have a sample that I can use it would be great.

Servo.cpp (4.14 KB)

Servo.h (5.13 KB)

I have look on my HD, and din't find servo.h or servo.cpp.

Again, that's because they're called Servo.h and Servo.cpp.

Because of what I saw, i thing that it isn't a HW limitation but code.

It's code, right up to the point that you strip the gears or burn out the motor.

You have missed the point. With the Servo library, when you do a write(180) that translates into writeMicroseconds(2400) i.e. the maximum pulse width. If your servo is able to move greater than 180 degrees then it will. Normally write(0) = full movement in one direction, write(180) = full movement in the other direction. The amount of movement is limited by the servo design.

If you think your servo needs a pulse width greater than 2400 microseconds then you can change that in the servo.attach() without messing with the code, e.g. attach(servoPin, 544, 2800). But it's not very likely to work and it certainly won't make your servo move further than it is designed to move.

Steve

johnwasser:
Hobby servos get their position feedback from a potentiometer (variable resistor). They have physical stops to prevent them from turning beyond the rotation range of the potentiometer (typically about 270°).

The physical stops in those types of servos are more like +/- 100 degrees from centre (200 deg travel).

Linkages from the servo horn past +/- 90 degrees will make the linear travel of the linkage start to come back on itself.

Another reason why the servo.h library limits the command input is that driving a servo past it's physical stops (whatever they are), results in them continuously trying to achieve the commanded position, with a stalled motor. Now a stalled motor, in a small enclosed space, is gonna get HOT and eventually burn out.

If the OP definitely wants rotation past +/- 90 degrees, he will have to use gearing, and he should use ratios that give him the travel he needs from the maximum travel the servo will deliver. In that way he will be minimising the reduction in torque, which always happens when you "gear up".

Look at this videohttps://www.youtube.com/watch?v=b_xvu6wWafA:

I tried to use it, but still stuck with the 180 deg....
I think that we use the same motor, but still I got the problem.

P.S.
Let say that all of are right, and the servo that I have is only 180 deg.
how do I know where is my zero?

the motor that I got is a part of a 6 servo motor robot kit (see the picture attached).
So I don't think they should be a mechanic limitation, and if there is, I didn't saw any limitation for put the robot together.

The video shows something we call a "no-longer-a-servo" or an "ex-servo"
If you listen to the commentary, you will hear that stated, but not in those terms.

It will continue rotating in one or other direction as long as the "angle" in the Servo.write is away from the nominal 90 degree value.

It cannot be commanded to come to a rest at a given "angle".

Some servo test code you might use to figure out your servo and how it operates.

// zoomkat 7-30-10 serial servo test
// type servo position 0 to 180 in serial monitor
// for writeMicroseconds, use a value like 1500
// Powering a servo from the arduino usually *DOES NOT WORK*.

String readString;
#include <Servo.h> 
Servo myservo;  // create servo object to control a servo 

void setup() {
  Serial.begin(9600);
  myservo.attach(9);
}

void loop() {

  while (Serial.available()) {

    if (Serial.available() >0) {
      char c = Serial.read();  //gets one byte from serial buffer
      readString += c; //makes the string readString
      delay(3);
    } 
  }

  if (readString.length() >0) {
    Serial.println(readString);
    int n = readString.toInt();
    Serial.println(n);
    myservo.writeMicroseconds(n);
    //myservo.write(n);
    readString="";
  } 
}

Some specs:

DT-3316 20MG, Control Angle 180°

shamayk:
P.S.
Let say that all of are right, and the servo that I have is only 180 deg.
how do I know where is my zero?

Hook it up to the Arduino and tell it to go to 0. The place where it stops is your zero.

#include <Servo.h>
Servo servo;
void setup()
{
  servo.attach(9);
  servo.write(0);
}
void loop() {}

Thank all for the help.
As I check, I have a smaller servo MG90S, that physically cam move onley 180 deg.
The bigger ones (DT-3316) can move 360 deg with no Physical limitation.

I will try to change the code, and late you know.

Can you please provide a link to where you actually got the DT-3316