Struggling with stepper controlled by buttons

Hi all

So... I am trying to have a 28BYJ-48 motor with a easy driver and arduino nano that I want to control with 3 buttons. One for clockwise, one for anti-clockwise and one to stop the motor at any point. (a rotary button, when it arrives. for now it will be 3 buttons)

to be able to adjust the speed and number of steps or turns would help massively. I do have the ULN2003 Motor Driver Board that came with the motor but would like to use less pins on the nano.

I have been toying with this for a couple of weeks (an hour now and again) and have made no headway with the code or hardware!

I was using: Easy Driver Examples to help me as it shows almost what i want to do.

I know this is all possible I could just do with some help pointing me in the right direction!

let the sarcasm begin! :slight_smile:

There’s a lot of examples on that page. Which ones did you try? Which one is closest to what you want? Can you describe exactly what it does and what you want it to to different?

The Easydriver is very different from a ULN2003 for a 28BYJ stepper motor. I would not expect code written for an Easydriver to work.

Post the program that represents your best attempt and tell us in detail what it actually does and what you want it to do that is different. And please use the code button when posting code



Thank you for your replys

this is the code that seems to match what I am after. But I don’t want the potentiometer to control the speed.

I have started from the top code on that link and worked down to try and gain a understanding. I had gotten to the point where the motor would turn one way once (had to change example 1.5 from #define DISTANCE 3200 to #define DISTANCE 16500 to complete one turn) it worked but turned very slowly and i could not adjust it.

adding another button works but i had to wait for it to complete its steps before i could press the other button. (example 1.6)

This is where i have gotten to. After googling this motor with the easy driver i have found examples that show it working and have had it working!

What i want it to do different:

3 Buttons to control CW, CCW and STOP (the stop to work without it completing its turns)
code such as #define DISTANCE to control number of steps (ease of adjustment, one may need 10 turns a different one may need 100 etc etc)
code to control speed (not sure how quick i want it to turn, so will need to adjust)

a picture/diagram to help me connect it up.

// Example5 code for Brian Schmalz's Easy Driver Example page

#include <AccelStepper.h>

// Define the stepper and the pins it will use
AccelStepper stepper1(AccelStepper::DRIVER, 9, 8);

// Define our three input button pins
#define  LEFT_PIN  4
#define  STOP_PIN  3
#define  RIGHT_PIN 2

// Define our analog pot input pin
#define  SPEED_PIN 0

// Define our maximum and minimum speed in steps per second (scale pot to these)
#define  MAX_SPEED 500
#define  MIN_SPEED 0.1

void setup() {
  // The only AccelStepper value we have to set here is the max speeed, which is higher than we'll ever go 
  // Set up the three button inputs, with pullups

void loop() {
  static float current_speed = 0.0;         // Holds current motor speed in steps/second
  static int analog_read_counter = 1000;    // Counts down to 0 to fire analog read
  static char sign = 0;                     // Holds -1, 1 or 0 to turn the motor on/off and control direction
  static int analog_value = 0;              // Holds raw analog value.
  // If a switch is pushed down (low), set the sign value appropriately
  if (digitalRead(LEFT_PIN) == 0) {
    sign = 1;
  else if (digitalRead(RIGHT_PIN) == 0) {    
    sign = -1;
  else if (digitalRead(STOP_PIN) == 0) {
    sign = 0;

  // We only want to read the pot every so often (because it takes a long time we don't
  // want to do it every time through the main loop).  
  if (analog_read_counter > 0) {
  else {
    analog_read_counter = 3000;
    // Now read the pot (from 0 to 1023)
    analog_value = analogRead(SPEED_PIN);
    // Give the stepper a chance to step if it needs to
    //  And scale the pot's value from min to max speeds
    current_speed = sign * (((analog_value/1023.0) * (MAX_SPEED - MIN_SPEED)) + MIN_SPEED);
    // Update the stepper to run at this new speed

  // This will run the stepper at a constant speed

You can use the AccelStepper with a ULN2003 and a 28BYJ motor but you need to change this

AccelStepper stepper1(AccelStepper::DRIVER, 9, 8);


AccelStepper stepper1(AccelStepper:: FULL4WIRE, 9, 8, X, X);

and change the Xs to the pins for the other two connections to the ULN2003

Read the AccelStepper documentation for the fine details.