Servo sweep code doesn't work

I'm trying to control a servo motor so that it sweeps between 0 and 180 degrees. I know there's an example code that does that but I have to write to write my own for a bigger project. This is what I have:

#include <Servo.h>

Servo myServo;

void setup() {

myServo.attach(9); // digital pin 9

}

void loop() {
myServo.write(1);
delay(2000);
myServo.write(179);
delay(2000);
}

It uploads just fine but the L light on the microcontroller doesn't blink and the servo doesn't move. I've tried it on two Unos and a Mega and I get the same thing each time. I'm not sure what I'm doing wrong.

Servo issues are very often power related. Servos generally needs more power than the arduino can provide.

Does the example code work? Maybe you have it wired up wrong.

The example code works just fine. I'm currently using a 9V battery to power the servo and I have the motor hooked up to Vin.

I don't see any functional difference between the sweep example and your code. If one works, I would expect the other to. Can you try them both again?

9V battery sounds suspicious though. Is it the smoke detector kind? If so, it's worthless for powering a servo. Also, 9V is high for most hobby servos - what kind do you have?

I've tried them both and only the example code works. Yeah I'm using the kind that's used in a smoke detector but I've been using them to power these servos before with no problem, including now. The servo is rated for 6V but the max input voltage is 7.5V. I'm guessing that the servo will only draw in at most 7.5V, regardless of the size of the battery but I'm not sure about that.

If you have 9V battery connected directly to the servo then the servo is seeing 9V. And by now it might be a dead servo.

But try making smaller moves. The Sweep example moves 1 degree at a time. You're trying to go 179 degrees in one jump so the servo is trying to grab maximum power, equivalent to stalling the servo. Try something like write(15) instead of write(179). If that works then it is definitely a power problem.

Steve

Can you show a wiring diagram? There is frequently confusion about powering external components and a pencil and paper drawing of how you have things wired up would help us understand what you are doing.

It is possible that the servo sweep works and your version does not because you are making large movements that draw a lot of current longer than the sweep tutorial does. As others have said, this points to a power problem. Use a separate power source for the servos. 9v 'smoke detector' batteries are almost always the wrong choice. Use 4 AA batteries.

I've attached a schematic. I tried using a smaller step size but servo still isn't responding. Like I said, I've used the 9V battery before to power the servo and I never had any problem.

So, you are powering the servo from the Arduino voltage regulator.
OPs image from reply #8


This is a very bad idea.

It may work for a small servo pulling very little current.
As soon as you ask the servo to do any real work, it will overwhelm the onboard arduino regulator and the whole system will shut down. Best case.

Apparently, asking your servo to move from 1 to 179 is enough of a demand that it shuts down your arduino.

Go ahead and power your arduino from your existing battery.
But get a proper power supply for the servo.
Like I showed in the pic in reply #7