Servo turning incorrect angle

Hello, I have a problem with a servo.

I used the standard "knob" program to control a servo with a joystick instead of a potentiometre, but while the servo turns from angles between 0 and 180 degrees, it turns about 135 degrees when I want it to turn 90 degrees (when the joystick is at its normal middle position). Clearly the servo is very off even though the fact that I am using a hobby grade servo with an error margin of 0.5 degrees.

I tested whether there was problem with the joystick and there wasn't any as I expected.

I connected the servo to the arduino 5V power source. I know that you should have a different power source when using a servo, but I only used one servo with a simple code. Plus, I tested the same knob code with the joystick and a servo before, and it was working fine. But, is the error still caused by the fact that I am using the arduino power source?

I am very new to arduino, so sorry if I am asking for a obvious answer.

Thank you

is the error still caused by the fact that I am using the arduino power source?

Power the servo from 4 AA batteries or a 5V wall wart (at least 1 Amp output) and eliminate that possibility. What ADC value do you get with joystick in neutral position? Full left & right? Post your code.

Try the servo with this test sketch on pin 9:

/*
 Try this test sketch with the Servo library to see how your
 servo responds to different settings, type a position
 (0 to 180) or if you type a number greater than 180 it will be
 interpreted as microseconds(544 to 2400), in the top of serial
 monitor and hit [ENTER], start at 90 (or 1472) and work your
 way toward zero (544) 5 degrees (or 50 micros) at a time, then
 toward 180 (2400). 
*/
#include <Servo.h>
Servo servo;

void setup() {
  // initialize serial:
  Serial.begin(9600); //set serial monitor baud rate to match
  servo.write(90);
  servo.attach(9);
  prntIt();
}

void loop() {
  // if there's any serial available, read it:
  while (Serial.available() > 0) {

    // look for the next valid integer in the incoming serial stream:
    int pos = Serial.parseInt();
    pos = constrain(pos, 0, 2400);
    servo.write(pos);
    prntIt();
  }
}
void prntIt()
{
  Serial.print("  degrees = "); 
  Serial.print(servo.read());
  Serial.print("\t");
  Serial.print("microseconds =  ");
  Serial.println(servo.readMicroseconds());
}

The Knob sketch uses the Servo library default pulse range of 0.544ms for write(0) to 2.4ms for write(180). It's possible that your servo, whatever it is, can't use that full range could might put the centring off.

Try changing the servo.attach() to .attach(pinnumber, 1000, 2000) to narrow the range and see what that does.

Steve

Thank you all for your solutions.

First of all I tried your solution @slipstick and it did restrict the range. The there was about a 100 degree difference when I would move the joystick left and right, and when the joystick was in the middle, the servo arm was also in the middle. But, I would like a full 180 degree range on the servo.

I also tried your code @JCA79B. When I was testing the servo, I noticed that angles between 180 and about 125 stayed the same, and the servo arm would changed positions when I got to 120 degrees. I had to tell the servo to move to about 55 degrees for it to turn to 90 degrees in real life.

I also noticed when I was doing the test that, for a brief moment, the yellow light on the arduino died out every time I would connect both the positive and negative ends on the servo to the arduino, and it would disconnect from the computer. It was only for a few minutes and after that, when I would hook up the servo to the arduino, the yellow light stayed on like it should.

I will try doing this test again, but with another power source for the servo, as recommended.

Thank you!

I was able to get the servo to turn the right angle by subtracting 30 degrees from the joystick value (with the 5V power source from arduino) and have the full 180 degree range. But is there a way to fix the servo so its not 30 degrees off?

also noticed when I was doing the test that, for a brief moment, the yellow light on the arduino died out every time I would connect both the positive and negative ends on the servo to the arduino, and it would disconnect from the computer.

You should never make wiring connections on a powered up system, that is the first rule of electronics.

You should also never power a servo direct from an Arduino being powered by a USB cable because of exactly what you are seeing. You could blow your computer’s USB port doing this. The fact that it worked correctly in the end is down to good luck, I would buy a lottery ticket while you are on this roll.

But is there a way to fix the servo so its not 30 degrees off?

No, no way of fixing it because it is not broken. There is no reason why your joystick movement and the servo movement should be the same. If you want them to be the same you must write software so they are the same. Normally you would print out the values of movement of your joystick. Then find the numbers that correspond to that amount of movement on your servo. Then simple maths will find what you have to do in the way of a formula to get one range of numbers to the others. If your maths is not up to doing that then use the map function to convert one range into another.

Hi, Can you post link to specs/data of your servo please? Where did you purchase it?

Thanks.. Tom.. :)

#include <Servo.h>
Servo serv;
int VRx = A0; //joystick
int val;

void setup() {
 // put your setup code here, to run once:
serv.attach (9);

}

void loop() {
 // put your main code here, to run repeatedly:
val = analogRead (VRx);

val = map(val, 0, 1023, 0, 180);

serv.write(val-30);
delay(15);

}

@TomGeorge It's the SG90 9G micro servo

I got this servo at a electric components shop close to my home.

Run of the mill RC servos are in no sense precision devices and certainly not calibrated. Higher grade robotics servos are more likely to have documented and consistent behaviour.

val = map(val, 0, 1023, 0, 180);

Do you get the full range of 0 to 1023 from a joystick? I think not.

Why are you ignoring the advice given in reply#7?

12341234tmmt: @TomGeorge It's the SG90 9G micro servo

I got this servo at a electric components shop close to my home.

That is the plastic gear servo, not very consistent in position precision or min and max swing from unit to unit. Don't expect to much from them. Tom.. :)

Okay thank you all for your help

@Grumpy_Mike when the joystick had a value of 1023 it moved 180 degrees which is what I want it to, so the range is fine. I just wanted the arm to be in the middle of 180 and 0 degrees when the joystick was in the middle, which it was not previous doing, as it should before. I realize that I can't re-calibrate the servo as you pointed out. But thanks for the pointer :)

when the joystick had a value of 1023 it moved 180 degrees

Are you telling me that the full movement range of your joystick goes from zero to 1023?

If so this is most unusual for a joystick based on a pot. All the ones I have seen use a restricted portion of a pot's total movement. Can you post a link to your joystick please.

It is the xinda joystick module.

What part of the word "link" are you having trouble with?

[

www.energiazero.org/arduino_sensori/joystick_module.pdf

It's similar to a lot of other joystick modules you can find on the internet](http://www.energiazero.org/arduino_sensori/joystick_module.pdf)