Controlling Stepper Motor With Buttons Using Arduino

Hİ everyone. Im trying to control a stepper motor using arduino nano. I have two buttons to control the motor. While one is pressed down the motor will rotate clockwise and the other button will rotate the motor counter clockwise. But i can not get the system to work. Any help would be appreciated.

`

#include <Stepper.h>

#define UP 4
#define DOWN 5


int buttonup  ;
int buttondown ;

Stepper pitchstepper(200, 9, 10, 11, 12);
void setup() {
    pinMode(UP, INPUT);
    pinMode(DOWN, INPUT);
    buttonup = digitalRead(UP);
    buttondown = digitalRead(DOWN);
}
void loop() {

 while(buttonup == HIGH){
    pitchstepper.step(1);
     buttonup = digitalRead(UP);
    }
 while(buttondown == HIGH){
    pitchstepper.step(-1);
       buttondown = digitalRead(DOWN); 
}
}

`

First thing to ask when there are buttons and the pinMode() isn't INPUT_PULLUP is to ask how you wired them. Specifically do you have either pull-up or pull-down resistors to steer the input when the button's not active?

i just wired them directly into the d4 d5 pins of arduino. and i dont have any resistors.

also im just simulating the system using proteus right now. dont have the real system.

ok that's one side of each pin, where does the other side go.

If you wire the i/o pin to 5v through the switch, the pin needs a resistor to ground as well. When the switch is open, the pin is pulled to ground to read low; without the resistor, it could float at any old voltage. When the switch is closed, it reads high.

Alternatively and more common, is to wire the pin to ground with the switch, and have the resistor pull the pin up to 5V. When the switch is open, the pin is high. when closed, it's low. And the Arduino has built in resistors to do the pull up, invoked in pinMode.

consider

# define UP   4
# define DOWN 5

#include <Stepper.h>

Stepper pitchstepper (200, 9, 10, 11, 12);

void loop () {
    if (LOW == digitalRead (UP))
        pitchstepper.step (1);

    if (LOW == digitalRead (DOWN))
        pitchstepper.step (-1);
}

void setup () {
    pinMode (UP,   INPUT_PULLUP);
    pinMode (DOWN, INPUT_PULLUP);
}

thats what i used but it doesnt work.

ive just added my proteus sketch. it doesnt seem to work

What is?

What does that refer to?

ive answered gcjr idk what it shows it like that.

how does buttonup get set if you don't read it?

i read it in while loop

but is the condition true for the while loop to execute?

Hi, @zabi4
Welcome to the forum.

To add code please click this link;

This will help with advice on how to present your code and problems.

Can you please post your code in code tags, so it is easier to read.

Thanks.. Tom... :smiley: :+1: :coffee: :australia:

if you press and hold down the button it is true.

but it's only read in the while loop()
so how does it become true before the condition is tested?

1 Like

its in the setup also.

Thanks for the information and warm welcome. Ive editted my post according to the link.

That doesn't help unless you are pressing the button at power up and as soon as you release it, you won't be able to step any more.

you need to read the buttons each time

    if (LOW == digitalRead(UP))
        pitchstepper.step(1);

    if (LOW == digitalRead(DOWN))
        pitchstepper.step(-1);