Controling stepper with analogic joystick

Hello everyone!
First of all, sorry for my english.

I’m controlling a stepper (model 28BYJ-48) with an analogic joystick and it’s works just fine. I’d like to be able to control the speed of rotation. I’d like that as the joystick is pulled away from it’s zero, the speed of rotation increases proportionally.
Here’s the code:


// 1 stepper motors controlled by a joystick

  #include <Stepper.h>

const int stepsPerRevolution = 64;  // change this to fit the number of steps per revolution

// for your motor
// initialize the stepper library for both steppers:

  Stepper small_stepper(stepsPerRevolution, 8,10,9,11); 

void setup() {

    small_stepper.setSpeed(400);    // set stepper speed

  void loop() {

    int sensorReading = analogRead(A0); // read value from joystick X-axis
    if (sensorReading < 490) { small_stepper.step(1); }   // step left
    if (sensorReading > 540) { small_stepper.step(-1); }  // step right


Any advice? Thank all

p.s. I’m a beginner so pls speak simple :slight_smile:

AccelStepper.h has more flexibility for this kind of stuff - you can use it in speed control mode (setSpeed, runSpeed) as well as position control mode (moveTo, run)

For your task you probably want to call setMaxSpeed and setAcceleration in setup(), put a call to runSpeed inside loop(), and map values from your joystick to values passed to setSpeed.

Don't call delay()

Thank you for your answer.

I’m trying to use AccelStepper.h but I’m making some mistakes for sure.
Here’s my code now


// 1 stepper motors controlled by a joystick

  #include <AccelStepper.h>

  AccelStepper stepper;

  #define ANALOG_IN A0

void setup() {


void loop() {

    int analog_in = analogRead(ANALOG_IN);

    if (analog_in < 490) {stepper.move(-1);}
    if (analog_in > 540) {stepper.move(1);}

Of course there’s something wrong: when i move the joystic, no matter in what direction, the stepper rotates always in clockwise and I can’t understand why.

I know that in void.loop() is missing the part relative to acceleration, but honestly I didn’t understand how to do that.

Don't you need to tell AccelStepper what pins the stepper is connected to and the sort of control the motor requires?


Thank you for

Robin2: Don't you need to tell AccelStepper what pins the stepper is connected to and the sort of control the motor requires?


I don't think that i need to do that because in the examples of AccelStepper you can read:

AccelStepper stepper; // Defaults to AccelStepper::FULL4WIRE (4 pins) on 2, 3, 4, 5

So this means that I don't need to tell what pins the stepper is connected if I connect it to pins 2,3,4,5 right?

OK. You seem to have checked and are using a suitable arrangement.

I wonder if the problem is your use of runSpeedToPostion() without setting a position with moveTo(). I suggest tying runSpeed().

Another possibility is to have a variable that holds the position and use your joystick to increment or decrement that variable and then use moveTo(myPosition).

I am not very familiar with the AccelStepper library as I don't need it with my stepper drivers and I have never tried a 28BYJ motor.