How to control Stepper motor(28BYJ-48) using a joystick





Materials:-
Diagram:-





Connections:-

Stepper motor(28BYJ-48) to arduino
  • Stepper IN1 = Arduino D11
  • Stepper IN2 = Arduino D10
  • Stepper IN3 = Arduino D9
  • Stepper IN4 = Arduino D8
  • Stepper 5v(5v-12V) = Arduino 5V/External power supply
  • Stepper GND = Arduino GND/External power supply GND

    Joystick to arduino
    • Joystick VCC = Arduino 3.3V
    • Joystick GND = Arduino GND
    • Joystick X axis = Arduino A0
    Downloads:-


    #include <Stepper.h> // include stepper motor library

    #define stepp 32// define number of steps per revolution

    #define IN1  11// define stepper motor control pins
    #define IN2  10
    #define IN3   9
    #define IN4   8


    Stepper steps(stepp, IN4, IN2, IN3, IN1);// initialize stepper library

    int joys = A0;// joystick  output = A0

    void setup()
    {
      Serial.begin(9600);
    }

    void loop()
    {

      int val = analogRead(joys);// read analog value from the joystick
      Serial.print(val);

      if (  (val > 235) && (val < 550) )
      {
        digitalWrite(IN1, LOW);
        digitalWrite(IN2, LOW);
        digitalWrite(IN3, LOW);
        digitalWrite(IN4, LOW);

      }

      else
      {
        
        while (val >=550)
        {
          int speed_ = map(val, 550, 1023, 5, 500);
          steps.setSpeed(speed_);
          steps.step(1);
          val = analogRead(joys);
        }

       
        while (val <= 235)
        {
          int speed_ = map(val, 235, 0, 5, 500);
          steps.setSpeed(speed_);
          steps.step(-1);
          val = analogRead(joys);
        }

      }

    }