1

I've been trying to write a very simple program using Arduino to tell a servo to go to a specific angle when a button is pressed, and to another specific angle when it is released. I find that, even though my sketch is but the simplest possible if then else statement without anything added to it, the servo goes to the right angle when the button is pressed, but when it is released, the servo oscillates between the two specified angles at a regular rate (about once every second). What's even stranger is that when i add delay time to the end of the loop in my sketch, the oscillation becomes more like twitching, and the more delay i add the weirder and less predictable the twitching becomes.

added info

#include <Servo.h> 
Servo Servo1;
int const switchPin = 4;
int switchState = 0;
void setup() 
{ 
    Servo1.attach(11); 
} 

void loop() 
{ 
    switchState = digitalRead(switchPin);
    if (switchState == HIGH) { 
        Servo1.write(40); 
    } else { 
        Servo1.write(70); 
        delay(39); 
    } 
} 

the power supply is the regular usb connector plugged from the board to my computer. The circuit is virtually similar to the "mood cue" project on the arduino projects book, except there is a button connected to digital pin 4 instead of a potentiometer.

  • 2
    If you want some help, please post your code, even if it is that simple. A description of your wiring, along with information on power supply, may be useful too. – jfpoilpret Feb 26 '15 at 20:28
  • @jfpoilpret here is the code: #include <Servo.h> Servo Servo1; int const switchPin = 4; int switchState = 0; void setup() { Servo1.attach(11); } void loop() { switchState = digitalRead(switchPin); if (switchState == HIGH) { Servo1.write(40); } else { Servo1.write(70); delay(39); } } the power supply is the regular usb connector plugged from the board to my computer. The circuit is virtually similar to the "mood cue" project on the arduino projects book, except there is a button connected to digital pin 4 instead of a potentiometer. – UnifyAndConquer Feb 27 '15 at 9:14
2

If it does what you expect when the button is pressed, but behaves unpredictably when released, you almost certainly have a floating input.

See Switches tutorial.

In particular, this is a no-no:

Floating switch

Random fluctuations in voltages will make the switch read HIGH and then LOW, and your servo will chase around trying to change positions.

The simplest fix (without changing anything else) is to add a pull-down resistor between the pin and ground, so now the pin reliably reads LOW when not pressed.

Pull-down resistor on switch

Other methods are discussed on that page, including debouncing.

1

I'm assuming you do not have a potentiometer hanging out of the servo motor its self.

2 things i could see here, without seeing the actual circuit itself.

1- The inputs are left floating therefore the input is thinking it sees a high state when you never push the button due to electrical noise. To fix this add a 10k Resister to ground on pin 4 while the switch is wired to 5v/3.3v on one side and the other side going into pin 4. This will fix noise going into pin 4.

2- Power supply on board is overheating or lack of power since computer usb port has a max power of 500mA. Use an external power supply to the servo and connect power supply ground to the board's ground to have a common ground.

1

I see two potential issues in your code.

  1. There is no button debouncing in your code: this means everytime you push the button down or up, the digitalRead() call will return HIGH and LOW several times until the signal is stabilized; that could make the servo twitch as you observed.

  2. For any call to servo.write(), you need to give time to the servo to reach the desired position, typically a few dozen milliseconds.

For the first problem, google for arduino button debouncing and you'll find various solutions for it.

For the second issue, you just need to add delay() after every call to servo.write(); note that in your code you added delay(39); but only for one call to servo.write() not for both calls.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.