I am using the ladyada motorshield for controlling a couple of 5v hobby servos. Everything works great but occasionally during a software update or reset of the arduino I have noticed that the servo will automatically rotate CCW a few degrees before returning to its past position. I have even had is on once or twice go back to 0 degrees and then back to its last position. This appears to be caused by some signal being fed back through the motor shield because just hooking 5volts directly to the servo (with no signal) nevers causes it to move.
Any thoughts or ideas how I can prevent the servo from rotating unless I send a signal from the code? (I have a hard requirement that the servo never go outside the range of 60-140, so it scares me when a reset or code upload can move the position outside the range).
I am currently having this same issue with the project I am doing. It involves a servo to release the mini electric car and every time I press my button it moves (and moves back). This is, as you have found, ony a few degrees but still annoying.
I put it down to the power to the servo. When I push my button it is putting 5v to ground through a resistor so could it be power fluctuations that are moving the servo?
This appears to be caused by some signal being fed back through the motor shield because just hooking 5volts directly to the servo (with no signal) nevers causes it to move.
This sort of thing is pretty common in electronic systems, especially when they're controlled by microprocessors: while the system is initializing itself, glitches can occur in parts of the system that aren't fully initialized.
Since Arduino pins default to "input" on reset, you need to add some external logic that inhibits the servo signals unless a digital pin you choose as the "enable" is outputting an active low. Perhaps a buffer with tri-state outputs that are enabled by a logic 0 input. Make sure your code doesn't pull the enable low until after you're sure the servo outputs are stable. You'll also need to make sure you don't enable them in mid-pulse, or you'll get exactly the sort of glitch you're trying to avoid.
I'm only slightly familiar with servos, so I'm not sure whether you can count on them not wiggling in the absence of an input. You might need to put pull-down on the servo inputs. See if you can get an answer from a servo expert.
Ok, I will have a fiddle but I'm not sure it will cause a problem in my circuit. I may just turn the servos on through a transistor when I need them...
mowcuis, can you say more about how your servos are wired.
Sure thing!
I have the servo attached arduino 5v, arduino GND and digital pin 12.
I am then using the arduino servo library to control it.
I also have a push button connected: 5v>switch>Analog 0 (digital 14) and Gnd(through a resistor)
Whenever I press the push button, the servo moves! scary!
Thinking about it, on my final board, the servo (and some other stuff) are going to be on a seperate regulator to the arduino and button. I presume that would solve the problem...
[edit]When I have a different sketch loaded, no presses of the button affect the servo. This other sketch has no servo library or any button use.[/edit]
[edit]Just checked, with external 5v to servo and 5v USB to arduino (completely independent of each other) it still does it...[/edit]
mowcius, if you are using a software serial library to interface with the LCD then the problem may be caused by the serial code interfering with the interrupt handling in the Servo library.
You can test this by using hardware Serial to communicate with the LCD. If that works ok than this indicates that the problem relates to the serial interrupt handling interfering with the servo timing. If the hardware serial can't be used for the lcd then you may need to use different servo code. How many servos do you have in your application?
I changed it all to the hardware serial and it worked fine but I really need to be able to have it plugged and transfer/upload sketches and still not interfere with the LCD (i'd rather not unplug it all the time - lazy!)
I only have one servo, it only does one thing, releases the mini car. Is there another library I could use then?
Ok thanks, I will try that. Sounds like a good solution as I only need to move the servo once! (well twice but only once with a real purpose, the second time is just moving it back)
[edit]Thanks for the help. This is now working brilliantly![/edit]
I have the same problem. Reset causes a glitch in the servo position as does insert removal of the USB cable when connected to a PC. I suspect the reset causes the PWM output to the servo to go low prematurely which in effect produces a shortened pulse width. The servo always moves CW from the glitch = shorter PWM pulse. Only solution I have is to not use the reset button, and power the servos from the same supply as the Arduino, or always power off the servos first.
its not the same problem, although the symptom may sound similar.
You may want to try a adding a delay in setup before attaching the servos and see if that helps. Try a delay(1000).
another test is to run the blink sketch (make sure a servo is not connected to pin 13 for this test) and see if you still get the glitch. if you do, the problem is in your servos and there is little you can do in sofware to fix this.
Additionally, previous poster said "This sort of thing is pretty common in electronic systems, especially when they're controlled by microprocessors: while the system is initializing itself, glitches can occur in parts of the system that aren't fully initialized."
I don't think so! Glitches are the result of indequate design. Unless of course, the electronics is made by Toyota!
Hi all, I am having a similar problem, servo (Sanwa SRM-102) is connected to the arduino 5v and GND and pin 6 (PWM). The whole thing is powered by a 9v battery whith a switch. When the switch is flipped the servo moves ccw, and when arduino finishes setup() it moves to the correct position. I have tried disconnecting the servo control wire from the arduino and the problem still occurs - on every startup it moves ccw. Can it be a servo issue (I have 2 of them, both behave in the same way). Maybe a power issue ?
I will throw out here that I have used R/C servos in real R/C aircraft for years before there were Arduinos roaming the earth. And even back then servos would twitch and or move on power up. Some worst then others and more after having been off for a longer period then if just a quick power off and on cycle.
I think it's just the nature of servo design, not how the arduino boots up or initilizes.