Can't get servo to work with Arduino and potentiometer

I am building a small one person hydrofoil boat and using the potentiometer on a steering wheel from a video game connected to servos driving a couple of rudders via an Arduino UNO to steer it. The servos are 35Kg DSservos (270 deg), and I am using a 7.8 volt 5200mAHour Lipo battery to power the servos. Here is the code I am using:

 #include <Servo.h> //accesses the Arduino Servo Library

Servo myservo;  // creates servo object to control a servo

int val;    // variable to read the value from the analog pin

void setup()
{
  myservo.attach(9);  // ensures output to servo on pin 9
}

void loop() 
{ 
  val = analogRead(1);            // reads the value of the potentiometer from A1 (value between 0 and 1023) 
  val = map(val, 0, 1023, 0, 18);     // converts reading from potentiometer to an output value in degrees of rotation that the servo can understand 
  myservo.write(val);                  // sets the servo position according to the input from the potentiometer 
  delay(50);                           // waits 15ms for the servo to get to set position  
} 

When I connect everything up nothing happens, even when I turn the steering wheel. I can't see, feel or hear anything coming from the servo. I've tried connecting a different servo (a 25 Kg DSservo) with the same result. I've also tried a different pot I had laying around - nothing. Where am I going wrong ?

val = map(val, 0, 1023, 0, 18); // converts reading from potentiometer to an output value in degrees of rotation that the servo can understand*

What is the maximum angle that you are aiming to turn the servo ?

Where does the servo get its power from and how is the Arduino powered ?

What do you see if you print the values of val after reading the input and after using the map() function ?

Which Arduino board are you using ?

1 Like

I have corrected the position of your code tags so that only the code is between them and now I can see the answers to some of my questions

1 Like

Your servo only moves 18 (+/- 9) degrees?

Thank you for fixing the layout for me ! I would like the servos to move about 30 deg. each way - the "18" I put in the code was fairly arbitrary just to see if everything worked . The Arduino is powered from a USB port on my laptop.
I've had a thought - does the negative side of the battery have to be connected to the Arduino ground ? - currently it isn't.

Yes, of course the negative side of the battery needs to be connected to Arduino GND.

The 18 was arbitrary - I just wanted to see if it works - I guess the real number should be around 100 (I want about 30 deg of travel each way.)

After connecting servo GND to Arduino GND, try this test sketch.

/*
 Try this test sketch with the Servo library to see how your
 servo responds to different settings, type a position
 (0 to 180) or if you type a number greater than 180 it will be
 interpreted as microseconds(544 to 2400), in the top of serial
 monitor and hit [ENTER], start at 90 (or 1472) and work your
 way toward zero (544) 5 degrees (or 50 micros) at a time, then
 toward 180 (2400). 
*/
#include <Servo.h>
Servo servo;

void setup() {
  // initialize serial:
  Serial.begin(9600); // set serial monitor baud rate to match
                      // set serial monitor line ending to "NewLine"
  servo.write(90);
  servo.attach(9);
  prntIt();
}

void loop() {
  // if there's any serial available, read it:
  while (Serial.available() > 0) {

    // look for the next valid integer in the incoming serial stream:
    int pos = Serial.parseInt();
    if(Serial.read() == '\n'){} //skip 1 second delay
    pos = constrain(pos, 0, 2400);
    servo.write(pos);
    prntIt();
  }
}
void prntIt()
{
  Serial.print("  degrees = "); 
  Serial.print(servo.read());
  Serial.print("\t");
  Serial.print("microseconds =  ");
  Serial.println(servo.readMicroseconds());
}  

Thank you ! For some reason, that didn't occur to me - I've never used an Arduino with anything requiring an "external" battery before - now it's obvious ! I'll do that and see what happens !

Thank you - I'll try that .

val = analogRead(1); // reads the value of the potentiometer from A1 (value between 0 and 1023)

You are trying to do an analogRead() from a digital pin.

You should change that to:
val = analogRead(A1); // reads the value of the potentiometer from A1 (value between 0 and 1023)

Good eye, John - that, together with connecting the negative of the battery to Arduino ground seems to have solved my problems ! I'm surprised it didn't catch that when I compiled it.
Thank You !

I think that the compiler is smart enough to know that analogRead(1) must be done using an analog pin, e.g. A1 here.

You would think so, but it compiled fine when it read analogRead(1)

I connected the battery negative to the Arduino ground and now the servo moves when I plug the Arduino in, but not when I turn the steering wheel (solved - see below)

Actually not.
I prefer to use

analogRead(A1);

for clarity, but even when using 1 pin A1 will be used by analogRead()

Thank you UKHeliBob (and herbschwarz) for correcting me.
It was late last night when I posted, and a test I did before posting was invalid as D1 is also Serial TX which upset my results.

See line 56 in:

pins_arduino.h (6.3 KB)

Obviously you are much more familiar than me with Arduino code, but the fact is that when I changed it from "1" to "A1" the servo started working as expected.

But that was after you connected GND, right?

1 Like