Servo not going to the right position

I am working on a certain project that requires a servo motor. Unfortunately, when I tell the servo to go to a certain angle, I'm assuming its not calibrated right because if I tell it to go to 0, it'll go to 130. Its very frustrating. How do i fix this?

Here's the basic code i was starting with.

#include <Servo.h>
Servo myservo;
void setup()
{
  myservo.attach(4);
}
void loop()
{
  myservo.write(180);
  delay(1000);
  myservo.write(90);
  delay(1000);
  myservo.write(0);
  delay(1000);
}

How are you powering the servo? It will probably not work correctly if you attempt to use the Arduino 5V output, and you can damage the Arduino by trying.

I have it set to Digital Pin 0, 5V, and GND.

Use a 4XAA battery pack to power the servo, and connect all the grounds.

The Arduino is not a power supply.

Welcome to the forum

Not a good idea because that pin us used by the Serial interface as is pin 1. Try using a different pin

Ok, thanks!

Why are you saying you are using D0 ? :thinking:

Oh, thats basic code i found online, i had just copied it from the website and not my edited version. The code im using is right here:

// Listing 14-1
#include <Servo.h>
Servo myservo;
void setup()
{
  myservo.attach(0);
}
void loop()
{
  myservo.write(180);
  delay(1000);
  myservo.write(90);
  delay(1000);
  myservo.write(0);
  delay(1000);
}

When you finally figure out what code you are actually using, post that.

Well I dont have a 4xAA battery pack, so what am I supposed to do? I switched it to D8 and its still wonky.

It appears that you face nearly insurmountable problems.

How observative of you

What do I do? I dont have a battery pack as stated before, and now I'm going to have to scrap what another project. I dont want to do that. Is there any way for this to work or is it just too "insurmountable"?

https://www.amazon.com/battery-holder-4xaa/s?k=battery+holder+4xaa

Oh wow, I forgot Amazon existed. Maybe some people dont have access to some recourses like that. I remember asking if there was a bypass, but of course you ignore me.

Do this at your own risk to prove that the servo works as it should

If the servo is one of the small 9 gram ones then connect it directly to the Arduino 5V, GND and a pin other than 0 or 1. Edit the Sweep example to match the pin that you are using and try it briefly (no more than 10 seconds) with nothing connected to the servo output arm

Report back what happens

If the servo is not a small 9 gram one then do not power it from the Arduino and even if it is then do not use the Arduino as a long term power supply with a load on the servo of any kind

I went through with the sweep example and unfortunately we still have the issue

Im using D8 and the 5V and GND, and its just not working. Its very frustrating. I just want one thing to work and apparently thats too much to ask for.

What exactly happens when you run the sweep example ? What angle does the servo sweep through ?

You do realize that you can take the arm off and set it to any one of 25 "angles", don't you?