Moving servo using a momentary push button

Hi!

I am trying to make a servo spin at the push of a button. When I set up the breadboard, my Arduino turns on through a USB connection only when my servo is not connected to the same 5v and GND as the button. I can power the servo with a battery and the button with USB, but my code doesn’t work in that situation anyways. So I guess I have 2 problems here…

Here is the design page: http://www.instructables.com/id/Candy-Drop-dispenser/
Here is the schematic (from Instructables.com): http://www.instructables.com/file/FM43C2VGOO35S2J

Here is the code (also from Instructables.com):

#include <Servo.h>

Servo myservo; // create servo object to control a servo
// a maximum of eight servo objects can be created
const int buttonPin = 2;
int pos = 0; // variable to store the servo position
int buttonState = 0;

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

void loop()
{
buttonState = digitalRead(buttonPin);

// check if the pushbutton is pressed.
// if it is, the buttonState is HIGH:
if (buttonState == LOW) {
for(pos = 0; pos < 180; pos += 10) // goes from 0 degrees to 180 degrees
{ // in steps of 10 degrees
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-=10) // 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
}
}
}

Very simple servo/button setup.

//zoomkat servo button test 7-30-2011

#include <Servo.h>
int button1 = 4; //button pin, connect to ground to move servo
int press1 = 0;
Servo servo1;

void setup()
{
  pinMode(button1, INPUT);
  servo1.attach(7);
  digitalWrite(4, HIGH); //enable pullups to make pin high
}

void loop()
{
  press1 = digitalRead(button1);
  if (press1 == LOW)
  {
    servo1.write(160);
  }
  else {
    servo1.write(20);
  }
}

tbrant16:
When I set up the breadboard, my Arduino turns on through a USB connection only when my servo is not connected to the same 5v and GND as the button. I can power the servo with a battery and the button with USB, but my code doesn't work in that situation anyways. So I guess I have 2 problems here...

This suggests 2 things to me.

The servo draws too much current from the Arduino. Arduinos generally should not be used to power servos or motors.

When you have the servo powered by battery you have forgotten to connect the servo ground with the Arduino ground.

And just possibly the battery doesn't provide enough current.

...R

Hi. i tried to do the same thing here. The codes shows no error but my servo does’nt work as the codes tells. why is that?
the led works perfect but the servo does’nt work. Help me solve my problem. thanks. :frowning:

#include <Servo.h>

Servo myservo;  // create servo object to control a servo 
int ledPin = 13;
int buttonPin = 2;  // The button will be on Pin 2
int buttonState = 0;
 
 void setup() 
{
  myservo.attach(9);  // attaches the servo on pin 9 to the servo object
  pinMode(buttonPin, INPUT);
  pinMode(ledPin, OUTPUT);
  digitalWrite(2, HIGH);
} 
 
 void loop() 
{ 
  buttonState = digitalRead(buttonPin);
  if (buttonState == HIGH) 
  {
    digitalWrite(ledPin, LOW);
    
    myservo.write(140);
    delay(10);
  }
  else
  {
    digitalWrite(ledPin, HIGH);
    
    myservo.write(0);
    delay(10);
  }
}

the led works perfect but the servo does'nt work.

Are you perhaps trying to power the servo from the arduino? If so, then that might cause problems.

so, i have to power the servo using other power source?

ameerRock:
so, i have to power the servo using other power source?

Yes. The Arduino 5v pin can’t supply enough current for motors.

Make sure the ground for the servo power supply is connected to the Arduino ground.

…R

Typical servo wiring arrangement.

yet, nothing happen. i tried this code

#include <Servo.h>  

Servo myservo; // creating myservo object
int buttonPin = 2; // set button pin
int buttonState = 0; // set buttonState 

void setup()
{
myservo.attach(9); // attach the 13 pin to servo
pinMode(buttonPin, INPUT); // set button to input
}


void loop()
{ buttonState = digitalRead(buttonPin); // read and save to the variable "buttonState" the actual state of button if (buttonState == HIGH)
if (buttonState == HIGH)
{
  myservo.write(0); 
}
else
{
  myservo.write(180);
}
}

too, but still nothing happen.

i tried the example code, it works perfect but not with my code.

How is your switch wired?

The simplest way is to use pinMode(buttonPin, INPUT_PULLUP); which means that the the button pin normally reads HIGH. Then you wire the switch between the pin and ground. When you press the switch the pin will read LOW.

The other thing is that your program has no means to wait while the servo moves. For testing add delay(500); just before the end of loop().

...R

My switch works fine.
yet, the servo does’nt work.

#include <Servo.h>  

Servo myservo; // creating myservo object
int buttonPin = 2; // set button pin
int buttonState = 0; // set buttonState
int LEDpin = 13;

void setup()
{
  pinMode(LEDpin, OUTPUT);
  myservo.attach(9); // attach the 13 pin to servo
  pinMode(buttonPin, INPUT_PULLUP); // set button to input
}


void loop()
{ buttonState = digitalRead(buttonPin); // read and save to the variable "buttonState" the actual state of button if (buttonState == HIGH)
if (buttonState == HIGH)
{
  digitalWrite(LEDpin, LOW);
  myservo.write(140);
  delay(500); 
}
else
{
  digitalWrite(LEDpin, HIGH);
  myservo.write(0);
  delay(500);
}
}

ok, problem solved. its my fault. the angle for the servo to turn. thanks mate. :slight_smile: