Go Down

Topic: controlling a micro servo with two buttons (Read 1 time) previous topic - next topic


I am relatively new to ardunio and I am looking to control a micro servo using two buttons. I would like one button to move the servo arm up 10 degrees at a time and the other button to move it down 10 degrees at a time. (Total turn of only 90 degrees)
I can wire up my arduino ok, but I am struggling with the code to make it all work. Any help would be brilliant!


have you tried the servo library?

or the single servo example?

both should give you a rough idea.


thanks for your response, I have had a wee look at both, but like I said Im quite new to all this and very unsure how to edit/add code to suit this project, but cant seem to find a tutorial or code that uses the servo and two buttons and was hoping someone may have one already.


it depends on how you connect it but it would look like this,

Code: [Select]

#include <Servo.h>
Servo myservo;  // create servo object to control a servo
int pos = 0;    // variable to store the servo position
// constants won't change. They're used here to
// set pin numbers:
const int buttonPinup = 2;     // the number of the pushbutton pin
const int buttonPindown = 3;     // the number of the pushbutton pin
// variables will change:
int buttonStateup = 0;         // variable for reading the pushbutton status up
int buttonStatedown = 0;         // variable for reading the pushbutton status down

void setup()
// initialize the pushbutton pins as an inputs:
  pinMode(buttonPinup, INPUT);
  pinMode(buttonPindown, INPUT);     
  myservo.attach(9);  // attaches the servo on pin 9 to the servo object

void loop()
// read the state of the pushbutton values:
  buttonStateup = digitalRead(buttonPinup);
  buttonStatedown = digitalRead(buttonPindown);
  // check if the pushbutton is pressed.
  // if it is, the buttonState is HIGH:
  if (buttonStateup == HIGH) pos += 10;  // move 10 deg +   
  if (buttonStatedown == HIGH) pos -= 10;  // move 10 deg -
   // tell servo to go to position in variable 'pos' ;
    delay(15);                       // waits 15ms for the servo to reach the position

buttons on pins 2 and 3 like this http://arduino.cc/en/Tutorial/Button
servo on 9 like this http://arduino.cc/en/Tutorial/Sweep

I haven't tested as I have no servo but it compiles an its a start?


Yeah that is a fantastic help, I'll try it. Thanks so much for getting back to me!


oh and don't forget in ya main loop above the servo move;

if (pos<90) pos=90; // so only moves 90 deg or what you wanted
if (pos>0) pos=0; // so cant go under 0 deg


I need to do this too. I used the code and wired it and everything, but it still won't work. What is the problem?


Some test code from a while back for two buttons and a servo.

Code: [Select]

//zoomkat servo button test 12-29-2011

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

void setup()
  pinMode(button1, INPUT);
  pinMode(button2, INPUT);
  digitalWrite(4, HIGH); //enable pullups to make pin high
  digitalWrite(5, HIGH); //enable pullups to make pin high

void loop()
  press1 = digitalRead(button1);
  if (press1 == LOW)
  press2 = digitalRead(button2);
  if (press2 == LOW)

Google forum search: Use Google Search box in upper right side of this page.
Why I like my 2005 Rio Yellow Honda S2000  https://www.youtube.com/watch?v=pWjMvrkUqX0


"Pete, it's a fool looks for logic in the chambers of the human heart." Ulysses Everett McGill.
Do not send technical questions via personal messaging - they will be ignored.
I speak for myself, not Arduino.


Oh never mind sorry. It was the servo that was faulty. Replaced it and it works.

Go Up