Controlling two servos using a push button


i am having difficulties for my project.

i want to make 2 servo motors turn one after the other using only 1 push button.

when the button is pressed and released.

the first motor has to turn a certain degree (for e.g. 45 degrees) and remain at that position.

then the second servo motor turns at a certain degree (for e.g. 90 degrees) and remain at that position.

and after a few seconds both servo has to turn back to the original position...

is it possible? i need help for my project.

help me please. :confused: :frowning:

is it possible?


i need help for my project.

What have you written so far ?

i am having difficulties for my project.

You should do as UKHeliBob says and post what you have so far, and describe the problems.

Have a look at this State Change Detection example, as a start.

I wouldn't wire the button like they did though:it'seasier to use the built in pullup resistor with INPUT_PULLUP.

Then, your button will be high normally. You say you need to to be pressed and released, so use the thinking of the example I linked, and look for the button to go high (ie, when it was pressed it went low but you ignore that, and only act when it goes high again).

this is my code… i’m not too sure

sketch_sep21a.ino (421 Bytes)

Unless the code is really long, please just copy/paste it between code tags </>, much easier than having us all download it then open the ide to look at it.

But here it is as a favour :wink:

#include <Servo.h>
Servo ServoM1;
Servo ServoM2;
int pb1 = 2;
int delay1 = 1000; // 1s
void setup() {
  // put your setup code here, to run once:
  pinMode(pb1, INPUT);


void loop() {
  // put your main code here, to run repeatedly:
  if ( pb1 = 1) {


Ok you need to rethink that totally, along the lines of the state change detect example I already linked to. Your logic there is that it will react to the button's current state, not to the fact that it just changed.

And when you do a comparison in an if, you need == not =.

  if ( pb1 = 1) {

Read the state of the pin, but throw the state away. Then, try, incorrectly, to compare the pin NUMBER to HIGH without bothering to use the correct name.

Not very bright.

Just how IS the switch wired? Using the internal pullup resistor is so much simpler - one leg to the digital pin and the other leg to ground, and use INPUT_PULLUP as the mode. Then, pressed is LOW, and not pressed is HIGH.

If you want to do something the first time the switch BECOMES pressed (NOT IS pressed), and something else the second time, you MUST count the changes. And, as has been suggested, you need to look at the state change detection example.


Look up button press and state change and master that. Then, you'll find the rest of your request is easy

Void loop
Wait for button press
if button is pressed;
move servoA to positionX
move servoB to positionX
move servoA to positionX (return sevo back to original position and wait for another button press)
move servoB to positionX (return sevo back to original position and wait for another button press)

Then play around with the value of X to get the positions you desire

After that. Look at the example of doing more than one thing at a time. You CAN move both servos at the same time!

I have a sketch that does exactly that. But if I give it to you, what have YOU learned?

Wait for button press
if button is pressed;

OP specifically said the button must be released to kick off the action. So he needs an active low button, which will normally be high. The state change he must look for is for it go high, which is what would happen with a press (goes low) then a release (goes high again.)

(As I said in #2....)