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 ?
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.
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 !
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 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)
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.
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.