5

So I've tried this a few times and I always get the same issue. When I try to run the sweep example the servo goes round 180 degrees but only in steps. Anyone know a way to fix this?

The servo I'm using is a Tower Pro Micro Servo 9G SG90

// Sweep
// by BARRAGAN <http://barraganstudio.com> 
// This example code is in the public domain.


#include <Servo.h> 

Servo myservo;  // create servo object to control a servo 
                // a maximum of eight servo objects can be created 

int pos = 0;    // variable to store the servo position 

void setup() 
{ 
  myservo.attach(9);  // attaches the servo on pin 9 to the servo object 
} 


void loop() 
{ 
  for(pos = 0; pos < 180; pos += 1)  // goes from 0 degrees to 180 degrees 
  {                                  // in steps of 1 degree 
    myservo.write(pos);              // tell servo to go to position in variable 'pos' 
    delay(15);                       // waits 15ms for the servo to reach the position 
  } 
  for(pos = 180; pos>=1; pos-=1)     // goes from 180 degrees to 0 degrees 
  {                                
    myservo.write(pos);              // tell servo to go to position in variable 'pos' 
    delay(15);                       // waits 15ms for the servo to reach the position 
  } 
} 

Video: http://vidd.me/A40

sachleen
  • 7,365
  • 4
  • 37
  • 55
Jordan Adams
  • 159
  • 1
  • 3
  • does it hummmmm in between the "steps"? – jippie Apr 21 '14 at 12:15
  • no just the usual ticking on each step – Jordan Adams Apr 21 '14 at 12:16
  • You don't tell us anything about the set up (schematic?). My guess would be it doesn't have enough voltage. – mikeY Apr 21 '14 at 15:04
  • I've set it up exactly as shown in the [sweep example](http://arduino.cc/en/Tutorial/sweep) – Jordan Adams Apr 21 '14 at 15:15
  • 1
    My only advice is make sure the Arduino is getting enough power and check your connections. Consider external power if you're not on USB. – Anonymous Penguin Apr 22 '14 at 00:56
  • What's the maximum voltage input? – Jordan Adams Apr 22 '14 at 10:25
  • What power supply do you use? USB, power adapter, battery... Please indicate characteristics (voltage, max current...) – jfpoilpret Apr 22 '14 at 11:27
  • Currently USB from my MacBook so I would guess 5v – Jordan Adams Apr 22 '14 at 11:38
  • Have you checked that your Arduino is running at the right frequency? This can be easily done by just uploading the usual blink sketch and checking the LED on pin 13 blink period. If it's fine then try the same blink sketch but add the `Servo` library and `myservo.attach();` (no call to `myservo.read()`) to see if the presence of the servo has an impact on frequency. – jfpoilpret Apr 23 '14 at 05:39
  • I've seen disappointingly step-y behavior from a similar cheap servo with an entirely different microcontroller. Don't rule out low quality. Generally you should not use USB (or anything coming through the Arduino) to power servos though. http://www.thebackshed.com/Forum/forum_posts.asp?TID=5611&PN=9&TPN=3 Seems to suggest that different frame rates might yield better results. – Chris Stratton Apr 23 '14 at 14:26
  • @chris-stratton Would a 9v battery be a more suitable power source? – Jordan Adams Apr 23 '14 at 15:47
  • No. 9v batteries should not be used for high current applications. And without a switching regulator, you'd waste nearly half of your (limited and relatively expensive) power as heat in the linear regulator. – Chris Stratton Apr 23 '14 at 16:13
  • Complete stab in the dark but nothing seems to be working as of now: change the delay to something slower (150 maybe)? Your servo might be getting low power and not wanting to move reliably. – Anonymous Penguin Apr 24 '14 at 00:59
  • To be fair, you'd better not "step" te servo? Try to set it `from 0 (1sec delay) to 180 (1sec delay) to 0 ...` this should give a relatively smooth motion. – Paul May 30 '16 at 06:22

6 Answers6

2

In electronics, as usual in situations where a cricuit is misbehaving and we don't understand why, a solution that generally works is to add a decoupling cap between +5V and GND.

In your case, I would put at least 220uF; that would ensure two things:

  • the voltage used to control the servo never drops
  • the voltage brought to Arduino by the USB does not drop either (which would have very bad impact on the Arduino, probably resetting it)

Electrolytic caps are a must-have for every electronics hobbyist (and professional as well, of course).

You can read more about decoupling capacitors on Wikipedia.

Edit:

According to @gwideman comment, it seems that using a decoupling capacitance above 10uF in a USB 2.0 device would violate USB specs and may damage the USB host controller on the computer.

I was not aware of this limitation but I have already used decoupling capacitance up to 100uF in my circuits and never got any issue with my USB computer port.

It might be due to extra electronics on the Arduino board that would "isolate" USB power from Arduino +5V anf d GND pins, I don't know (that should probably be the topic of a specific question); or maybe I have just been lucky all this time...

jfpoilpret
  • 8,962
  • 6
  • 35
  • 53
  • Would you happen to have a schematic for this? – Jordan Adams Apr 23 '14 at 09:35
  • You don't need a schematic just to add a decoupling cap! You just need the right cap and put it (on breadboard preferrably but you could also solder it directly to the servo) between +5V and the GND pins; just ensure you put the cap right as it is polarized (if you put it reverse it will explode) – jfpoilpret Apr 23 '14 at 10:48
  • Like this? http://imgur.com/thfEQtY – Jordan Adams Apr 23 '14 at 11:20
  • Yes exactly like that. – jfpoilpret Apr 23 '14 at 11:56
  • I've tried the following setup with no luck. Any more ideas? [http://imgur.com/lprLvm4](http://imgur.com/lprLvm4) – Jordan Adams Apr 23 '14 at 18:51
  • What cap did you use? Didn't that change **anything** at all? – jfpoilpret Apr 23 '14 at 18:59
  • A 2.2uF as you suggested and everything acted exactly the same – Jordan Adams Apr 23 '14 at 19:37
  • I did not say `2.2uF` but `220uF`, which is a much bigger cap; if you don't have a 220uF or bigger, try to find a 100uF that *may* be enough. – jfpoilpret Apr 23 '14 at 21:10
  • 1
    USB requires that connected devices limit inrush current. This limits the size of bulk capacitor between VBUS and Ground to 10uF in USB 2.0. See USB 2.0 standard section 7.2.4.1 – Inrush Current Limiting. Using a larger capacitor, particularly one much larger than the source (computer) is required to supply on its end (120uF) would possibly lead to damage to the computer, or at least disruption of its operation. – gwideman Apr 25 '14 at 12:12
  • More detail on the USB bypass capacitance limitation: http://www.edn.com/design/analog/4313897/Current-limiter-allows-large-USB-bypass-capacitance – gwideman Apr 25 '14 at 12:16
  • Thanks @gwideman for this precision; however, to verify that this applies to an Arduino-based circuit, one has to check first what in Arduino diagram lies between USB power and 5V pin (I did not check it). For what it's worth, I have already used 100uF decoupling capacitor in one of my Arduino UNO based circuits and never got any USB issue at all; your mileage may vary. – jfpoilpret Apr 26 '14 at 10:22
  • What lies between USB power and the +5V net is a fuse and the Ron resistance of the FET that switches between USB and external power. Neither offers much resistance, and the fuse will not operate on a time scale to offer protection in this case. It is true that hosts (computers) are not _required_ to break down when a USB device presents too much capacitance :-), and it obviously behooves computer makers to guard against such damage. Indeed, the Arduino UNO already presents 47uF x 2 bulk capacitance (and it looks like Duemilanove has 100uF x 2). – gwideman Apr 26 '14 at 14:00
  • More likely issues are that hot-plugging will cause other USB-connected devices to experience glitches, especially if sharing a hub. – gwideman Apr 26 '14 at 14:08
  • 1
    It's worth noting that USB is often quite tolerant of devices that are MASSIVELY out of spec. It depends on the host implementation, and lots of computers are willing to provide connected devices considerably more current then the specifications say the need to. – Connor Wolf Apr 27 '14 at 19:00
1

I've experienced same behavior with this type of servos. In my case it was easier to spot since Arduino frequently rebooted when servo was approaching the boundaries of the swing - so it was clear it was consuming too much power, and USB port was unable to deliver that.

Cheap dedicated 9V 1A AC/DC converter usually solves the problem. But if you can't wait for one to get delivered, get an iPad USB charger (nowadays everyone has it or has a neighbor/friend who has one). It is rated 10W, and it doesn't suffer from voltage drops even under high loads. Power your setup from it, and if you'd see it moves smoothly, you will have your answer.

Alexander
  • 101
  • 1
  • 5
0

It looks like your sketch runs in slow motion.

Check if you wrote delay(150) or have an additional delay in your loop. Check twice and - just to make sure - upload the corrected sketch again.

Edit: If possible, test with another servo and even test with another Arduino board.

suit4
  • 21
  • 3
  • I can assure you that's not the case. I've tried everything from 1 to 15 for the delay and even without the delay. I've also tried having just one for loop – Jordan Adams Apr 22 '14 at 10:24
  • Ok. Without delay, the servo should move at the actual processor speed. Which board type do you use? And is the code above the original code that is running on the board? did you try to set pin 9 as digital out? Code in setup(){ ... pinMode(9, OUTPUT); // sets the pin as output ... } – suit4 Apr 22 '14 at 11:26
0

give a 9v supply +ve terminal to Vin and -ve ground. since the arduino board cannot supply enough power to the servo, this will provide this worked for me and workin just fine

Akash
  • 1
0

I tested your code, nothing wrong with it. Maybe it is power supply issue. 1. Don't use USB to power servo, it can make current spike when it move rapidly. 2. Use proper supply for servo. High current and use large capacitor to deal with current spike.

-1

The program is faulty. 1) No servo can react to a command in 15 milliseconds. Delay should be more like 500 milliseconds (.5 seconds). 2) The for loops are not designed to work together. The 1st loop feeds a value of 181to the second loop which is designed to start with a value of 180. The 2nd loop feeds a value of -1 back to the first loop, which is designed to start with a value of 0.

  • Please tell me what is incorrect about my for loop analysis. Could use some help. – Electronicalifragilistic May 30 '16 at 03:23
  • 2
    There are some pretty serious misunderstandings in this - the given delay is only between the finest increments, the actual time of the sweep is 2.7 seconds, well within the capability of a servo. Using a 20ms delay (possibly with larger increments to compensate) would indeed be more in keeping with typical servo update rates, but that is not what you recommend. Also your analysis of the for loops is incorrect. – Chris Stratton May 30 '16 at 03:25
  • The contents of the for loop only execute if the condition is true, so only conditions which satisfy it are written to the servo. Both for loops initialize their variable. – Chris Stratton May 30 '16 at 03:26