Help with servo problem

I've wired up a HD-1900A microservo and a 10kohm pot to the arduino, aswell as a led array of 10 bits. the array works perfectly, and the servo setup works for a little while, but if I twitch the pot too much the servo hangs up and won't move until reset, it stops on it's own after a while.

that's the link to the datasheet for the servo.

#include <Servo.h>

#include <math.h>

int pin[10],pot = 0;

int val = 0;
Servo servo;

void setup(){
  for(int i = 0;i < 10; i++){
    pin[i] = 4+i;
    pinMode(pin[i], OUTPUT);
    servo.attach(3);
  }
}
int n = 0;
void loop(){
  
  val = analogRead(pot);
  if(val & 0x0001)
    digitalWrite(pin[9],HIGH);
  else
    digitalWrite(pin[9],LOW);
  if(val & 0x0002)
    digitalWrite(pin[8],HIGH);
  else
    digitalWrite(pin[8],LOW);
  if(val & 0x0004)
    digitalWrite(pin[7],HIGH);
  else
    digitalWrite(pin[7],LOW);
  if(val & 0x0008)
    digitalWrite(pin[6],HIGH);
  else
    digitalWrite(pin[6],LOW);
  if(val & 0x0010)
    digitalWrite(pin[5],HIGH);
  else
    digitalWrite(pin[5],LOW);  
  if(val & 0x0020)
    digitalWrite(pin[4],HIGH);
  else
    digitalWrite(pin[4],LOW);
  if(val & 0x0040)
    digitalWrite(pin[3],HIGH);
  else
    digitalWrite(pin[3],LOW);
  if(val & 0x0080)
    digitalWrite(pin[2],HIGH);
  else
    digitalWrite(pin[2],LOW);
  if(val & 0x0100)
    digitalWrite(pin[1],HIGH);
  else
    digitalWrite(pin[1],LOW);
  if(val & 0x0200)
    digitalWrite(pin[0],HIGH);
  else
    digitalWrite(pin[0],LOW);
   
 
  val = map(val, 0, 1023, 0, 179);
  servo.write(val);
  delay(15); 
}

Anyone able to shed some light on this problem? :slight_smile:

I'm always suspicious of servo problems if you are powering the servo from the Arduino +5vdc, they are a noisy load and draw more current then you might imagine.

If you can find away to power the servos from a separate power source, 4 fresh AA batteries or a regulated one amp (per servo) +5-6vdc power supply, etc, and is if the problem goes away. When using an external power source be sure to remember to wire the supplies ground side to a arduino ground pin.

Lefty

I am powering the arduino with a separate power supply, i have moved it to a breadboard. and a 7805 voltage regulator keeps the Vcc line at 5 volts. I have followed the breadboard guide closely, and done everything the potentiometer & servo guide showed.

How does the application behave if you comment-out the LED code?

servo.attach(3);
 digitalWrite(pin[3],LOW);

:-?

    pin[i] = 4+i;

So, pin[3] = 7, not 3.

the array of pins are for the 10-bit led array. pins 4 through 13 goes there, the servo pin is used separately

I think the main problem is the range on the map(), because when i turn the pot all the way to one end, the servo kinda hums all the time, when i leave the pot midway somewhere the servo is silent

I think the main problem is the range on the map(), because when i turn the pot all the way to one end, the servo kinda hums all the time, when i leave the pot midway somewhere the servo is silent

This would imply that the servo is not actually able to move to 179 degrees.

Is the servo quiet from the other end of the potentiometer range up to some value, or is it quiet only in the middle?

it's quiet at the other end yes. is 179 steps some standard for all servos? it sounds like it hits the physical stop, but the virtual thing still thinks it can go further, the controller in the servo I mean

is 179 steps some standard for all servos?

Many servos, but by no means all of them, can rotate 180 degrees. You need to measure the limits of your servo, and not try to drive it beyond those limits.

Even some servos that claim to be 180 degree servos aren't really. For their intended purpose, in RC cars and planes, there is rarely a need to rotate the full 180 degrees.

The rotation of standard hobby servos depends a lot on the quality of the pot and similar factors. You can use the below code to test your servo with various position values to find the value where the servo hits the internal hard stop, then factor that into your code to keep it off of the hard stop.

// zoomkat 10-4-10 serial servo test
// type servo position 0 to 180 in serial monitor
// for writeMicroseconds, use a value like 1500
// for IDE 0019 and later
// Powering a servo from the arduino usually DOES NOT WORK.

String readString;
#include <Servo.h> 
Servo myservo;  // create servo object to control a servo 

void setup() {
  Serial.begin(9600);
  myservo.attach(7);  //the pin for the servo control 
  Serial.println("servo-test-21"); // so I can keep track of what is loaded
}

void loop() {

  while (Serial.available()) {
    delay(10);  
    if (Serial.available() >0) {
      char c = Serial.read();  //gets one byte from serial buffer
      readString += c; //makes the string readString
    } 
  }

  if (readString.length() >0) {
    Serial.println(readString);  //so you can see the captured string 
    int n;
    char carray[6]; //converting string to number
    readString.toCharArray(carray, sizeof(carray));
    n = atoi(carray); 
    myservo.writeMicroseconds(n); // for microseconds
    //myservo.write(n); //for degees 0-180
    readString="";
  } 
}

I solved the hang up problem with a second 7805 circuit for the servo only. so your suspicions were correct :slight_smile: however the other problem still exists, when it gets to one end it mechanically stops, but the motor still tries to turn it. i've looked it over, and it's on the 0 side it does that, there's no problem going up to 179.