Servo Not Working with Arduino

Thread Starter

Lawrence H

Joined May 6, 2019
98
Hey!

I am trying to test out a servo with my Arduino. I cannot seem to be able to control the servo for some reason. The servo should work, and I am using an external power source if 4 AA batteries, which output 6V.

I opened up the servo and gave it current directly, and it turns, but controlling it doesn't seem to do anything. Here's my code. I tried to keep it as simple as possible as to make no mistakes:

Code:
#include <Servo.h>

Servo servo;



int servoPosition = 0;

int servoPin = 3; // I originally used 9 but that pin did not seem to function either.

int ledPin = 13;



void setup() {

  servo.attach(servoPin);

  pinMode(ledPin,OUTPUT);

}



void loop() {

  servo.write(servoPosition);

  digitalWrite(ledPin,LOW);

  delay(1000);

  digitalWrite(ledPin,HIGH);

  delay(1000);

  servoPosition += 90;

  servo.write(servoPosition);

  digitalWrite(ledPin,LOW);

  delay(1000);

  digitalWrite(ledPin,HIGH);

  delay(1000);

  servoPosition += 90;

  servo.write(servoPosition);

  digitalWrite(ledPin,LOW);

  delay(1000);

  digitalWrite(ledPin,HIGH);

  delay(1000);

  servoPosition = 0;

}
The led tells me that the loop is running, but the servo is not responding.

What gives?

EDIT:

I tested the analog pin with a LED and it works as intended. The power supply also should be stable. The loop like mentioned earlier is running.
 
Last edited:

Ya’akov

Joined Jan 27, 2019
10,240
First, you should always use CODE tags around code to make it easier to read and refer to. You can most easily do it from the ... menu above the edit box:

1646003736528.png
Second, how are you powering your servo? Your code causes a servo here to move, though I can't say it does what you want it to do. The most likely cause is the power supply, where are you getting the 5V?
 

Thread Starter

Lawrence H

Joined May 6, 2019
98
First, you should always use CODE tags around code to make it easier to read and refer to. You can most easily do it from the ... menu above the edit box:

Second, how are you powering your servo? Your code causes a servo here to move, though I can't say it does what you want it to do. The most likely cause is the power supply, where are you getting the 5V?
The servo is powered by four 1.5V AA batteries in series, which puts out ~6V. According to the volt meter it is in fact putting out a constant 6V.

I am only doing testing for now since I want to first make sure the servo works as intended before moving on to use it in something. So what the test should do is first set the servo to 0, then 90, then 180, then back to 0.
 

Ya’akov

Joined Jan 27, 2019
10,240
Well, your code moves the servo here. So without seeing how you have it wired, I can't tell you anything more.

The code "works" as far as I can see. I reverted to pin 9, though.
 

Thread Starter

Lawrence H

Joined May 6, 2019
98
Well, your code moves the servo here. So without seeing how you have it wired, I can't tell you anything more.

The code "works" as far as I can see. I reverted to pin 9, though.
I packed it up since it is getting late, but I had one wire going to the SG90 power from the positive side of the battery pack, one going to the brown from the negative side of the pack, and then a line directly from pin 9, or 3 depending on which I was testing at the time, to the orange/yellow line in the SG90.

The part did work a little before when I plugged it in to the Arduino directly, before googling that it should have a separate battery pack.

Not sure if the part could be broken, and still turn if given direct power after opening it?
 

djsfantasi

Joined Apr 11, 2010
9,237
You need to attach the negative wire from the battery pack to BOTH the brown wire of the servo AND a ground pin on the Arduino.

I’m assuming that you’re powering the Arduino from the USB cable. Because 6V is too high to power it from the 5V pin and too low to power it from the Vin pin. But it is an assumption. Tell us how you’re powering the Arduino.

You need to attach the two grounds together, because without doing that, the signal wire has no reference point by the servo.
 

Thread Starter

Lawrence H

Joined May 6, 2019
98
You need to attach the negative wire from the battery pack to BOTH the brown wire of the servo AND a ground pin on the Arduino.

I’m assuming that you’re powering the Arduino from the USB cable. Because 6V is too high to power it from the 5V pin and too low to power it from the Vin pin. But it is an assumption. Tell us how you’re powering the Arduino.

You need to attach the two grounds together, because without doing that, the signal wire has no reference point by the servo.
Ok damn it, I will pull them out again. I can't sleep now since I spent so much time on making the battery packs and all that. I think this is the issue. Thank you!

I'll return once I have tested it out.

EDIT:

That was it. Now I can finally go to bed. Thanks everyone. Being a noob is tough sometimes.
 
Last edited:
Top