180 servo being glitchy when given multiple rotations

My servo has an issue where whenever it’s given multiple rotations in code, it will start to wiggle back in forth and make my computer give the sound as something has disconnected over and over at the speed of the delay between each rotation. Here is the code:

#include <Servo.h>

Servo myservo;

void setup() {
myservo.attach(5);
}

void loop() {
myservo.write(180);
delay(2000);
myservo.write(135);
delay(2000);
myservo.write(90);
delay(2000);
myservo.write(45);
delay(2000);
myservo.write(0);
delay(2000);
}

Sorry if the code is a bit simple, it is for a class and I am mildly panicking as this is dragging down my grade. Also, wiring-wise, everything is plugged in correctly. The servo works when given individual rotations, though it gets to the desired rotation in 4 bursts which I don’t think is supposed to happen. I am not sure if this is a software or hardware issue.

How are you powering the servo?

Does the servo actually go to 0 and 180 degrees? A lot of servos do not have the capability of full 180 degree rotation, and when they reach the physical limit of rotation will draw excessive power trying to travel beyond that position, possibly overloading the power supply.

I’m powering it through my computer, our teacher hasn’t provided any additional power supply so far. But yes, it can go a full 180 degrees

Probably exceeding the allowable power from the USB port, servos need their own power supply or batteries, a medium size servo may need well in excess an an amp of current, especially when loaded.

What type of servo? Does “my computer” mean an Arduino? What sort? How is that powered?

What you describe sounds like insufficient power. Something like connecting a fairly large servo directly to an Arduino 5V pin. Drawing too much power causes the Arduino to reset. You can sometimes get away with a tiny micro servo (though it’s still not a good idea) but anything larger is a real no-no.

Steve

I figured out what’s going on, and I am a royal dumbass. Turns out, I got my Arduino with this little switch switched to 3V3, not 5V. I feel a bit stupid atm, thank you all for your help, the power supply stuff lead me to find this. Sorry for wasting yalls time