jittery servos

Hi everyone I am designing some model railway electronics using several Uno and Megas. I am no expert by any means.

I have written my code and it all works as expected, and I have tried connecting the servos to both PWM and Analogue output pins without success. I have preset the inputs and outputs using Pinmode()

When my code comes to move the servo (which in its turn switches a point) the servo does move, but then sits there "jittering" quite badly. I have tried the same servos running under the example servo sketch and they run smoothly.

I have tried using .write() and .writeMicroseconds(), both give the same result.

Here is a sample of one branch of the switching operation, all the other code simply records what is happening and sets a bunch of variables, including a few that I HAVE ADDED to the Servo.h file to help me control the system. They are : // My own internal class variables // to make processing much easier. byte currMode = 0; // added IGT 6/2/2018 byte branchAngle = 0; // added IGT 6/2/2018 byte mainAngle = 0; // added IGT 6/2/2018 byte pointNo = 0; // added IGT 6/2/2018 byte isActive; // added IGT 6/2/2018

The servos themselves are held in an array, hence servos[x].......

Here is the code sample :

 switch (buttonSelected)
         {
            case 1:
               sprintf(gbuff, "Adjust Mode is now %s!", pointAdjust ? "ON" : "OFF");
               Serial.println(gbuff);
               resetActiveFlag();   // set all other points to non active first
               pointMode = MAIN;
               servos[1].writeMicroseconds(MAINANGLE);
               servos[1].isActive = true;
               servos[1].currMode = MAIN;
               sprintf(gbuff, "point 1 switched to MAIN");
               Serial.println(gbuff);
               sprintf(gbuff, "Track [%d] selected\n", buttonSelected);
               Serial.println(gbuff);                  
               sprintf(gbuff, "Track %d selected correctly !", buttonSelected);
               Serial.println(gbuff);
               ACTIVEPOINT = 1;
               ACTIVEMODE = MAIN;
               ACTIVEANGLE = MAINANGLE;              
               break;

I really hope someone can come up with a reason for this servo jitter ?

Thanks

Ian

Hi Ian,
Can you post the entire sketch? Seeing the whole thing helps us identify potential issues.
Is it too big to make sense of easily? Try removing other pieces of your sketch until you get down to the bare minimum that illustrates your poor servo behavior.

I am pretty sure you already confirmed this in past posts covering other questions on this project, but to make sure we all have the best info, how are the servos powered? (best if through a separate supply with common grounds with the arduino)

And PICS!!! Really. any train layout, especially one in progress, is fun to look at!

About 95% of servo and motor problems reported on this forum are due to inadequate power supplies. For servos, use a separate, 5-6V supply capable of supplying 1 Amp per servo. Connect all the grounds together.

Thanks for replies guys.

As far as servo power, I am using a "lab style" DC power supply unit that can easily provide loads of amps for the servos. I do ALL have the grounds connected (PS and Arduinos)

The sketch is in 5 files and compiled comes to 124,500 bytes, so............. A little too large I think for showing here.

After I posted my original message, I kept researching and one person suggested putting a 100 pF cap across the 5v Power supply lead to each servo. Sadly it made no difference, even with a 4000 pF cap in place

Anoother suggestion made by several people was to attach the servo, write to it, and then detach it again. That meant a lot of work for my code, and loosing my special .h entries, but I tried it for a couple of the switches, and YES, it worked.

But I would far prefer to have all servos permanently connected if possible, so my actual problem still exists.

It seems to me that the issue has to do with spurious signals on the servo wires. My button connections to set the switches (servos) are all pulled LOW with resistors, I have not done the same with the servo output pins ?

As to photos of the layout, sure, I am quite happy to do so, but there must be a better place for me to put them up to ??

one person suggested putting a 100 pF

Either a typo, or you misread it. pF=picoFarads, an extremely tiny amount of capacity, so try 100 uF (microFarads) or larger. But all that does is make up for an inadequate or [u]unacceptably noisy[/u] power supply.

You should not need attach/detach.

I have tried the same servos running under the example servo sketch and they run smoothly.

Then something is wrong with your code.

Sorry, it is a typo, it should read uF, not pF.

But they made no difference, so I guess that rules out the "noise" issue ? I agree I should NOT need to have to attach/detach every time I access them, and I really do not want to do that. That is why this thread is ongoing......

I cannot quite see how my code can be wrong as such, it simply attaches all 7 servos in a for loop in setup(), and then uses them (basically .write()) as required, and that is when I get severe jitter from them. The servo objects that are attached are held in an array, and then accessed via that array to use them.

By far the most likely explanation for jitter is that your power supply or wiring is inadequate. Put a scope on the power leads to see the voltage drops.

You don't have these servos on a breadboard, of course, and are using soldered connections. Breadboards cannot handle high current.

Right now they (well, one actually) is plugged into the Mega for its control with a jumper lead, but the power connections are indeed soldered directly....

Hi,

How many servos are you controlling?

Can you please post a copy of your circuit, in CAD or a picture of a hand drawn circuit in jpg, png?

Write some code just to control your servos, make them all sweep. But only connect one servo and try it. Turn power off and add a second servo and try it. Turn power off etc etc.

Check to see if you get any jitter that way.

The Arduino gnd and the servo power supply gnd have to be connected together so you have a gnd reference for the servo input signal.

Monitor the servo power supply with your DMM.

What current is you powersupply? What are the make/model of your servos?

Thanks.. Tom.. :)

Hi Tom

Lots of requests there, so a bit of clarification may help ?

Right now I am only using a single servo to test the system, changing pins to check the code works on each output, so I am only powering a single servo from my external lab power supply.

Attached are a JPG of my Fritzing layout, plus the fzz file if it helps ?

Oooops, I cant send an FZZ and my JPG is too large. I will play with image and post separately

My power supply can provide up to 20v and around 12 amps, so should be enough ?

Servos are cheap and cheerful SG90 used on many aircraft and helis. All test out correct when connected to an rx and tested with a 2.4Ghz Tx. No jitter at all.

DMM shows a max of 0.03 millamps being drawn by the servo when switching

I hope that covers your questions Tom ?

heres the PNG image promised of the fritzxing diagram I have prepared. :slight_smile: or I hope so…

chopperaddict: All test out correct when connected to an rx and tested with a 2.4Ghz Tx.

What does this mean? I'm also a little suspicious of your mention of AnalogWrite and the PWM pins earlier.

Can you try your servos with some of the examples from the reference e.g. Motor Knob. Do you get jitter now?

Hi Wildbill

I am not sure what the confusion on pintypes is ? I tried using both PWM and analog pins as output for the servo control to see if there was any difference - there wasn't

My reference to the 2.4GHz is radio control talk, where these servos are widly used.

I have tested the standard example and as reported previously, there was no jitter.

Does that clarify it ?

So that suggests that your code or wiring are your problem. Do you have long wire runs to where your servo is located? If so, does your jitter free testing replicate that?

I'd take the physical setup where the jittery symptoms are observed and make a copy of your program and then remove all you can from it to try and isolate what's wrong - you've apparently established that the servos are ok.

Hi Wildbill

Yes, I think my testing has established the servo is OK

The servo wire length is identical to the test using the sample code.

Obviously my current setup has to make use of a few breadboard connections, as shown in my Fritzing diagram, but I have built an adaptor board that uses all soldered connections between buttons and pulldown resistors. The connections from this to the mega are made using standard Dupont cables, with a pin header being soldered onto my connection board.

The only "suspect" connections maybe the ground interconnection, and the fact that the servo power comes via Dupont connection from the breadboard into the servo connector plug.

I will try to make some of these more "permanent" in my testing sequence and report back.

Hi,
Do you have a pair of wires from the supply to the servos, AND a separate pair of wires from the Mega to the supply.

The aim is for the controller NOT to share power circuit wiring with the servos.

Are you really prototyping on protoboard?

Tom… :slight_smile:

the fact that the servo power comes via Dupont connection from the breadboard into the servo connector plug.

That is guaranteed to be a problem, which is why I asked about it some time ago.

Breadboard tracks should never be expected to conduct more that a couple hundred mA, and you are asking one to carry up to 7 Amperes. The track is probably burned.

Hi Tomgeorge, I think you have misunderstood my Fritzing diagram ? the servos are powered directly from my lab power supply, the arduino is powered via the USB or a 9v cell. Only the 2 grounds are connected.

Yes, I am using veroboard so I can solder as many connections as possible, do you not approve ?

Hi jremington, I am not sure where you get 7A current from ? My servo power supply and DMM show a maximum drain for one servo to switch is no more than 250mA. There is never a time when more than a single servo is switching at the same time ?

Cheers

chopperaddict: Hi Tomgeorge, I think you have [u]misunderstood my Fritzing diagram[/u] ? the [u]servos are powered directly from my lab power supply[/u], the arduino is powered via the USB or a 9v cell. Only the 2 grounds are connected.

Which is why we ask for a proper schematic. A 9V cell will not keep your arduino powered for long. Can you please post a copy of your circuit, in CAD or a picture of a hand drawn circuit in jpg, png? A picture of your project will help, so we can see your component layout please. Thanks.. Tom... :)

My servo power supply and DMM show a maximum drain for one servo to switch is no more than 250mA.

Those measurements don't show the full picture. Servos briefly draw the [u]stall current[/u] every time they start to move. For small hobby servos, the stall current is around 1 Ampere and sometimes much more. Look up the stall current for your servo and tell us what that is.