Servo motor only sweeps

I have a 4.8 volt servo motor connected to a power supply module. I am powering the module with a 9 volt battery. When I use code to have the motor sweep the servo will usually move but will sometimes not move. When I set code to move the servo to a particular angle it does not move at all.

Sweep Code:
'''
for (pos = 0; pos <= 180; pos += 1) { // goes from 0 degrees to 180 degrees
// in steps of 1 degree
servo.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
servo.write(pos); // tell servo to go to position in variable 'pos'
delay(15); // waits 15ms for the servo to reach the position
}
'''
move code:
'''
angle = 90;
servo.write(angle);
delay(2000);
angle = 0;
servo.write(angle);
delay(2000);
'''

Is an issue if the battery looks like

that is the type of 9 volt I am using

Using the words "9 volt battery servo" in the site search will bring up lots of how to and what nots and other things regarding the battery 9 volt thingy.

I have ordered a 4 AA battery pack will that work better?

Yes.

Thank you my understanding for the wiring would be as follows:

negative of battery pack to ground of arduino
positive of battery pack to positive of servo
ground of servo to ground of arduino

is this correct?

Reads correctly.

thank you for the help I will give that a try when the battery pack arrives

@picklesdoggo
You posted in the wrong place. Topic moved here.
Please read a section's description before posting.

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.