Can't make servo work

Hello,

I’ve just bought my duemilanove and tower pro sg5010 and can’t make it work! What i’ve did:

I’ve connected arduino to PC by USB and uploaded this code:

#include <Servo.h>

Servo servo1;
int angle;

void setup()
{
 
 Serial.begin(9600);
 servo1.attach(5,0,255);
 
 
 Serial.print("Servo1 Ready\n");

}


void loop()
{
 
   Serial.print("Servo 1 position 0\n");
   servo1.write(0);
   angle=servo1.read();
   Serial.print(angle);
   Serial.print("\n");
   delay(1000);
   
   Serial.print("Servo 1 position 90\n");
   servo1.write(90);
   angle=servo1.read();
   Serial.print(angle);
   Serial.print("\n");
   delay(1000);
   
   Serial.print("Servo 1 position 180\n");
   servo1.write(178);
   angle=servo1.read();
   Serial.print(angle);
   Serial.print("\n");
   delay(1000);    
       
}

Because the USB can’t drive the motor i plugged the servo to AC-to-dc converter which gets me 9V. Then the current goes through current regulator L7805 which lowers the current down to 5V. The control cable is plugged to pin 5. The ground of motor is connected to arduino ground.

When i run the code the motor turns fully clockwise and then stalls and does nothing but vibrate a bit. I’m googling about this for two days and starting to get nervous that i’m still where i began. So, maybe someone knows a trick or two to fix my arduino-bot?

BW,
Sharapolas

servo1.attach(5,0,255);

I’m not sure that will work.
Get rid of the “, 0, 255” and try again.
Your servo.read is a waste of time - it just fetches the value you wrote.

#include <Servo.h>

Servo servo1;

void setup()
{
 Serial.begin(9600);
 servo1.attach(5);
 Serial.print("Servo1 Ready - centred\n");
 delay (1000);
}


void loop()
{
   Serial.print("Servo 1 position 0\n");
   servo1.write(0);
   delay(1000);
   
   Serial.print("Servo 1 position 90\n");
   servo1.write(90);
   delay(1000);
   
   Serial.print("Servo 1 position 180\n");
   servo1.write(180);
   delay(1000);
}

Oops, that value there got because i tested using various min and max values i've found in google. Without them the servos don't work either. Maybe servos at fault? But when i revind the servo manualy fully counterclockwise, it rotates fully clockwise. So I thought that the min and max values are wrong so i've tried using 0..255, 1000-2000 and it still did not work.

BW, Sharapolas

Simple servo test code that you can use for troubleshooting.

// 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.writeMicroseconds(2000); //set initial servo position if desired
  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(1);  
    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 tried the script and now i'm wondering. Should servo immediately switch speeds after inputting microseconds? Sometimes it switches immediately, sometimes doesn't or does after some time. Also if i input 1500 it prints 1 and in a new row 500. Is this normal?

BW, Sharapolas

I tried the script and now i'm wondering. Should servo immediately switch speeds after inputting microseconds? Sometimes it switches immediately, sometimes doesn't or does after some time. Also if i input 1500 it prints 1 and in a new row 500. Is this normal?

This behavior is not normal. the servo should immediately go to the new position. Assuming you are using the serial monitor, check to verify you have the proper baud rate set. You can try increasing the delay to a higher value like 10 to see if that improves capturing the string.

Something still is wrong. I have a list of problem candidates and questions:

1(least probable) what is the default baud rate for arduino duemilanove? Maybe it's cause this odd behaviour..Or maybe it's having problems because of windows 7 and some microsoft background protection or smth?

2) Maybe the power source is at fault. For the servo i have a source which supplies a 5V DC current up to 600mA. I believe that this should be enough for the servo, but maybe it is not? Also, it could be that my power source scheme (diode, and voltage regulator) at fault and it gives some fluctuations somewhere. I'll probably take a pic of it and put it here for you to judge.

3) Maybe servos are at fault. However, its od that both of them work in such an odd way. I might misused them before and maybe it fucked them up. I'll probably go somewhere to test them. or is there an esay way to do so?

4) Maybe the PWM range for the Tower pro SG5010 is not 1000-2000 us?

5) Any ideas?

BW, Sharapolas

Regarding the power supply - you say you are using a 7805:

  1. What package (TO-220, TO-3)?
  2. Do you have the proper capacitors connected on the input/output lines of the regulator to prevent oscillation? See the datasheet.
  3. Do you have a properly sized heatsink attached to the regulator? You'll only be able to get maximum current from it with a heatsink (otherwise it switches to thermal shutdown mode).

I'm not saying definitely that these are the cause of your problem (if you wanted to know for certain, hook up four C or D cells in series for 6 VDC and use that as your servo DC power supply), but if you haven't set things up the manner above, they could certain cause issues later.

Ideas:

1) did you ensure both the arduino code and the pc application (serial monitor, etc.) are set to the same baud rate? A lot of people use windows 7 without issues.

2) did you increase the delay in the arduino code I posted to see if that fixed the broken strings?

3) have you verified with a multimeter that your voltage regulator is actually outputting 5v?

4) what is the function of the diode in your voltage regulator setup?

5) do you have the arduino ground and servo/voltage regulator grounds properly tied together ?

6) you can mis-wire the servo power/signal/gound wires and damage the servos.

7) I have a SG5010 servo connected to arduino and it works as expected.

  1. did you ensure both the arduino code and the pc application (serial monitor, etc.) are set to the same baud rate? A lot of people use windows 7 without issues.

Definetly.

  1. did you increase the delay in the arduino code I posted to see if that fixed the broken strings?

Tried that.

  1. have you verified with a multimeter that your voltage regulator is actually outputting 5v?

Almost every time.

  1. what is the function of the diode in your voltage regulator setup?

To ensure that i never switch + to the wrong pin.

  1. do you have the arduino ground and servo/voltage regulator grounds properly tied together ?

Yes.

  1. you can mis-wire the servo power/signal/gound wires and damage the servos.

From the beginning i’ve treated the servos very delicately and i’m sure that such never happened. I’ll probably buy a new one to test…
But are the servos so sensitive? Do their microcontrollers go bust so easily? Why?

  1. I have a SG5010 servo connected to arduino and it works as expected.

I envy you :slight_smile:

Tried that.

In a technical forum you need to learn to report the results of what you tried. in sufficient detail as to be understood by others. The broken string is a arduino/pc issue independent of the servo.

Almost every time.

Kind of like "almost works". Unless the power supply is stable the servo may exhibit unusual behavior.

To ensure that i never switch + to the wrong pin.

What wrong pin? Where is the diode placed in the circuit? Maybe it is causing your problems. Note that most diodes drop ~.7v in a circuit, so if the diode is in series with the servo + power (and your power supply is 5v), then you may only be supplying ~4.3v to the servo. The servo minimum rated voltage is ~4.8v.

But are the servos so sensitive? Do their microcontrollers go bust so easily? Why?

Solid state components can very easily damaged by revrsed polarities being supplied. From my experirnce servos are fairly tolerant of external mis-wiring as long as the voltages are within the rated range for the servo.

@Sharapolas - how are you entering the values for zoomkat's code? It will only work if you're using the serial monitor.

Guys, thanks for your patience and help!

First of all, here is my scheme powering the servo:

https://picasaweb.google.com/lh/photo/TV26wjiCbv6-PYTOFxOdtw?feat=directlink

@AWOL - i'm using the serial monitor

@zoomkat - i've checked the pins where i connect the servos and there still are some fluctuations from 5 to 5.5V. Might this be the cause? Probably tomorrow i'll try powering the servo directly from 4 rechargeable 1.5V batteries connected in series.

Also if i input 1500 it prints 1 and in a new row 500. Is this normal?

If you haven't solved this problem, then worrying with wiring/batterys is not going to solve anything.

Its alive!

The problem was in the power source. Because it was a AC to DC converter it had some voltage fluctuations which caused all the noise in motor. Now, i put a capacitor and a resistor in paralel to the source and most of the noise is gone. It still behaves a bit oddly. It works perfectly for a minute then the motor does some unwanted movement and then everything is good again. I'll check whether this continues after power source is switched to baterries.

So at the moment thanks for your help ;)

BW, Sharapolas