Electronic – arduino – Jumpy servo with arduino


I'm using the sparkfun inventor's kit. I'm using the code listed at the end fo this post, to controll a servo with a potientiometer using the arduino.

Unfortunately, the servo behaves very jumpy. Even if the value written to the servo does not change, it keeps jumping forward and backwads very fast by ~5-10°.

I also tried the same thing but using a soft potentiometer instead of the bend mesure and the 10kO resistor. The servo was jumping in the same way.

Any ideas how to fix that?

Circuit overlay:

enter image description here

#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, 179);     // 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 

Best Answer

I can reproduce the nervous servo with the soft pot and flex sensor. In case of the soft pot I can only make it steady when I hold my finger steady on the soft pot. If I don't, the servo starts to wiggle.

Adding a small capacitor (I added 100nF or 220nF) between ground and pot output (middle pin) stops the wiggling. Your simply picking up static.

Keep wires to the soft pot close to each other, don't make big loops in between them. You could braid them together for example. This and the small cap will reduce the picked up static and with that stop the wiggling.

The soft pot has an extremely high impedance on its middle pin when it is not touched. I guess it is only makes contact to the resistor when actually being touched. This explains why the servo gets nervous when the soft pot is not used. The middle connection is basically a loose wire → antenna and the analog input of the microcontroller is very sensitive / high impedance.