Greetings gentle folk. This is my first post, so please be gentle
.I'm a 60yro auto electrician and have been heavily involved in electronics since my early teens, but have only just dipped my toes into programming. I have a kit using the genuine (made in Italy) Arduino UNO. My problem is that when I run any sketch (even the sweep or knob sketch supplied in the examples in the IDE), as soon as I power up, upload the code, hit reset or open the serial monitor, the sg90 servo (supplied in the kit) swings to 180 degrees and chatters for a second, then does as the code instructs.
The UNO is powered either by the usb cable or a 9v battery, and the servo is powered by a re-purposed PC power supply, and all earths are tied together. I've tried putting a 470 mike cap across both the servo and UNO supply lines to no obvious effect. (Currently everything is on a breadboard). I've tried 3 variants of the same servo, each purchased from a different supplier (none of them fleaBay), and all do the same. I could use an output pin driving a tranny with a delay to remove power from the servo until the code is loaded, but this seems like a work-around instead of a fix. I've watched probably too many youTube vids for my own good and no-one else seems to suffer from this problem, and I've creeped through as many items in your forum as seem to relate to my issue, but to no joy. If anyone has encountered this problem, or has found the cause, could you please let me know.
The circuit is dead simple -
Servo + & - connected to 5v and neg on PC power supply.
Servo sig is connected to pin 9.
Pot ends are connected to Uno 5v and gnd.
Wiper is connected to pin A0.
All grounds are tied together via the breadboard.
A 470µf cap is tied across both servo and UNO power supply lines. (With or without, it doesn't make any difference).
Any help would be greatly appreciated.
Cheers
Frost.
Just a thought, I'm currently using version 1.8.10 of the IDE, would using a different version provide a different outcome?
as soon as I power up, upload the code, hit reset or open the serial monitor, the sg90 servo (supplied in the kit) swings to 180 degrees and chatters for a second, then does as the code instructs.
It is normal for a servo to move to its mid position when servo.connect() is executed unless it has previously been commanded to a different position.
Try adding a servo.write() to the code before the attach() to give the servo an initial position to move to decided by you, rather than the default
Are you sure about the "180 degrees"? The servo should normally default to 90 degrees (i.e. centred) not 180 when you run the servo.attach().
Steve
@ UKHeliBob, the servo.write command is present is all of the sketches I have copied or written. This behaviour happens as soon as I power up, probably even before the boot loader has a chance to do it's thing. After the nonsense it carries on with, the servo then does as is commanded in the code.
@slipstick, yeah, the servo slams hard over as far as it will go at or near 180 deg. This is immediately after I power up, well before any of the code has a chance to operate. In fact, I can tell when the code starts to send commands as the servo then does as it's told.
the servo.write command is present is all of the sketches I have copied or written.
Yes, but is it before the attach() ?
@ UKHeliBob. In everything I've seen, 'myservo.attach' is always in the setup, and myservo.write is always in the loop, so I guess myservo.write doesn't come first, but it's in a different section of the sketch. I haven't seen it written any other way, and to me it makes sense to have myservo.attach in the setup. Is this not the correct way of doing things? This is the way it is written in the 'knob' (and just about everywhere else I've seen) example I'll post below. Please let me know if I'm doing things bass ackwards. Thanks for your help mate.
@wildbill. This is the code straight from the knob example from the Arduino IDE.
#include <Servo.h>
Servo myservo;Â // create servo object to control a servo
int potpin = 0;Â // analog pin used to connect the potentiometer
int val;Â Â // variable to read the value from the analog pin
void setup() {
 myservo.attach(9); // attaches the servo on pin 9 to the servo object
}
void loop() {
 val = analogRead(potpin);      // reads the value of the potentiometer (value between 0 and 1023)
 val = map(val, 0, 1023, 0, 180);  // scale it to use it with the servo (value between 0 and 180)
 myservo.write(val);         // sets the servo position according to the scaled value
 delay(15);             // waits for the servo to get there
}
In everything I've seen, 'myservo.attach' is always in the setup, and myservo.write is always in the loop, so I guess myservo.write doesn't come first, but it's in a different section of the sketch. I haven't seen it written any other way, and to me it makes sense to have myservo.attach in the setup. Is this not the correct way of doing things?
That is the correct way to do things if you want the servo to move to its default 90 degree position when the attach() function executes, which is what I think is happening to you.
If, however, you want the servo to go to a different initial position when the attach() is executed, you need to do a write() before the attach()
Try this
#include <Servo.h>
Servo aServo;
const byte servoPin = 9;
void setup()
{
aServo.write(0);
aServo.attach(servoPin);
}
void loop()
{
}
Change the servo pin number to suit your system, upload the sketch. What happens ?
Does anything different happen if you change the value used in the write() function to 180 ?
Please report back your observations
@ UKHeliBob. Thanks for the trial sketch, unfortunately the problem persists. As I said earlier, it happens before the code has a chance to do it's job, as it happens as soon as I power up. It also didn't make any difference to the problem when I open the serial monitor or upload ANY code (whether it has a servo element in it or not). I changed servoPin to pin 3 and put 90°, 45° and 135° in myservo.write. The servo behaved as directed, after it swung hard over to the 180° stop and chattered for a sec or so. After playing with this with the most basic code for a while, it's got me stumped. Any other ideas, or have you heard of anyone experiencing this problem?
Thanks for the effort man.
Cheers
Tony.
Thanks for the feedback. It has eliminated some possible causes of the problem. One more thing to try please. Centre the servo manually then connect its 5V and GND inputs to the Arduino but not its signal connection. Does the servo still move to one extreme when powered up ?
You say that grounds are connected via the breadboard. Do you by any chance have one that has a break in the power & ground lines? The red & blue(?) lines will tell you.
@ UKHeliBob. Unfortunately, the servo behaves the same as it has done previously In fact, if I remove the sig wire during the operation of a sketch (it doesn't make any difference which one), the arm will slam over to 180° and chatter until the wire is reconnected to servoPin.
@wildbill. Thanks for the thought, good idea, but all connections are solid. Other wise the servo wouldn't be able to do what the programme tells it to do. Still at a loss!
Thanks to all who have responded and for the good ideas.
Cheers
Tony.
So lets be clear...if you just connect 5V and gnd to the servo it runs to the stop at 180 and sits there chattering?
A normal servo will twitch slightly on power on but not move until it has a signal. Time to try a different servo, that one is broken. I can't remember seeing one go bad in quite that fashion but bad it is. It may be one of the notoriously unreliable fake SG90s that are around.
Steve
RESOLVED! Thanks to everyone who helped me sort out this issue. I must be the unluckiest bloke around, as I have 3 servos that are U/S, and returned the same fault. The first thing I did was to put the CRO on the sig wire to see if there was something funny going on, but nothing showed until the code started, and then nothing but clean signals from then on. I then gutted one of the servos and using an H bridge to run the motor, and using the internal pot as a feedback, I got it to work fine. Doing the same with the other two produced the same results, except I found on one of them, the last 1/3 of the pot was shorted (the readings didn't change with movement of the shaft. To prove the point, I purchased another SG90, and it worked as expected.
As I stated at the beginning, they were all bought from different outlets at different times, so how I ended up where I was is beyond me, (I paid more than $15 for two of them, one was the MG90s with metal gears). As slipstick noted, they could have been knock-offs, but I wouldn't have expected that with the MG90s. And I guess price isn't a proper definitive to determine the quality.
Anyway, I'm sorry to have wasted everyone's time, and I truly appreciate all of the suggestions and tips in an effort to help me out of this pickle.
To all, much thanks
Tony.
P.S. I did find that the MG90s used to growl and chatter when it finally reached its commanded position. If I gently moved the horn, it would settle down. Upon disassembly, I fount that it had a ball race immediately under the splines, which was in interference fit in the top housing. When I put it back together, I noticed a preload was placed on this bearing, making the shaft drag. By carefully using some needle nosed pliers and a small block, I was able to pull the shaft up, releasing the preload, and now the servo runs as smooth as one could want.
Thanks again
T.