Pushbutton with servo or stepper motor

Please indulge me ,I’m not even sure how to ask this and I have searched YouTube and the internet for the answer.
I would like to drive a stepper motor or servo (not sure what I want to use yet) 1 step each time I press a button or move a servo set number of degrees each time I press a button. I watched several videos with buttons on both stepper motors and servos, but none that I could see that would move one step or 1 degree with each press. The videos show how to push and hold a button to make the motors start rotations, but not how to push once and move a set amount. Thanks


the reason why you did not find a code with the exact functionality you described in your post is there are billions of different combinations of partial functionalities possible.

There is a minimum of understanding nescessary how each partial functionality works to modify or combine two partial functionalities. As soon as you reach this level of understanding you are able to combine almost anything.

So the path to start walking down is:

describing the real thing that you want to do in the end. If a rc-servo or a stepper-motor is better suited for your application depends on a lot of things. So please give an overview about your project.

take an example-code of detecting a button-press and start reading the lines of code. It might be that you have a question right with the very first line of the "button-press-code". Just ask in the forum what your question is. You can ask as many questions as you like as long as you show some own effort that goes beyond "can somebody post ready to use code for me?"

If you ask a specific question about a posted code-example you will get answers!

You have to specifiy more precise how the "reaction" to the button-press shall look like.
The examples you mentioned do - more or less - what you want: do a very short button-press and the motor will move just a little bit.

What shall happen if somebody presses your button down and holds the button pressed down continiously for 5 seconds?

  • shall the motor do mutliple steps? if yes after what time should the next step moved?

  • shall the motor do just one single step even if the user holds the button down for hours?
    = the user must release the button and press the button down new before the next step is moved?

best regards Stefan

Show us what you’ve done so far, and we can help you move forward.

What’s the application"
Use - code tags - don’t use delay() !

Look like the first part of your requirement is "each time I press a button". Here is a sketch that will do that for one button:

const byte ButtonPin = 2;
const unsigned long DebounceTime = 10;

boolean ButtonWasPressed = false;
unsigned long ButtonStateChangeTime = 0; // Debounce timer

void setup()
  pinMode (ButtonPin, INPUT_PULLUP);  // Button between Pin and Ground

void loop()
  unsigned long currentTime = millis();

  boolean buttonIsPressed = digitalRead(ButtonPin) == LOW;  // Active LOW

  // Check for button state change and do debounce
  if (buttonIsPressed != ButtonWasPressed &&
      currentTime  -  ButtonStateChangeTime > DebounceTime)
    // Button state has changed
    ButtonWasPressed = buttonIsPressed;
    ButtonStateChangeTime = currentTime;

    if (ButtonWasPressed)
      // Button was just pressed
      // Move your stepper or servo here

This topic was automatically closed 120 days after the last reply. New replies are no longer allowed.