[SOLVED] Servo Motor is not rotating write !!

I was trying to rotate a servo with a pot, and I’ve noticed that the servo is always running to the 180 :fearful:
I 've also tried another servo, and the result was the same.
I don know what is wrong ?!

here is the code:

#include <Servo.h>
Servo servo1;
int potPin = A0;
int ANval;

void setup() {
  servo1.attach(3);
}

void loop() {
  ANval = analogRead(potPin);
  ANval = map(ANval, 50, 1017, 0, 180);
  servo1.write(ANval);
  delay(15);
}

I’ve used the serial monitor to define the values of the voltage divider of the pot.
I’ve also try to know what is wrong by trying to rotate it to 0 degree strait forward by “servo1.write(0);”, "servo1.write(180). and still, nothing happens !!
I have a Futaba s3003.

any ideas ?!

add a debug line or two to see the val from the analogRead…

#include <Servo.h>
Servo servo1;
int potPin = A0;
int ANval;

void setup() 
{
  Serial.begin(9600);
  servo1.attach(3);
}

void loop() {
  ANval = analogRead(potPin);
  ANval = map(ANval, 50, 1017, 0, 180);
  servo1.write(ANval);
  Serial.println(ANval);
  delay(15);
}

How did you connect the potmeter?
can you post a drawing?

Hi,

You are defining potPin as A0 and then calling analogRead on potPin, from memory A0 is defined to allow you to use the digital functions - digitalWrite and digitalRead on analog pins, so you do not need to use it for the analog functions. If you replace A0 with 0 the analogRead and analogWrite functions are written in a way that will automatically treat this as analog pin 0 which is what you intended.

Try this first.

You can also have a look at the servo posts an my blog which I have linked in my signature below, I don't think these are your problem just yet, but they might be soon.

Duane B.

rcarduino.blogspot.com

from memory A0 is defined to allow you to use the digital functions

No reason not to use them for analogue functions.

thank you everyone, Problem solved thanks to Duane B.,
I have read ur "Servo Problems With Arduino" post, and it helped very much, and guess what !!
the problem was connecting the ground from the two power sources together :smiley:
actually I don know how this affects controlling the servo !! if u 've an answer it will be appreciated.
but anyways thx everyone :slight_smile:

It's called an electrical circuit.

When connecting the grounds all voltages use the same reference to define 5V.

actually I don know how this affects controlling the servo

The current that goes from the arduino to the servo via the servo control wire has to have a path to return to the arduino. The connected ground between the servo and arduino is the return path.

yup, sure.. I got it.

It's one of the reasons I have the tag line I do.
My name isn't Dave.