top of page

Safari browser sometimes has issues displaying images...

I.e: *you have to click on the images to see them...

For a better browsing experience on Brainy-Bits

Please use Chrome, Edge or Firefox browser.

Writer's pictureBrainy-Bits

Control a Stepper Motor using a Joystick and an Arduino



OVERVIEW


There are many ways to control stepper motors using an Arduino.


In this tutorial we will see how to control a Nema 17 size stepper motor using an Analog Joystick.


To connect the stepper to the UNO we will be using the popular and inexpensive Easy Driver controller board.


The motor we are using has 1.8 degrees per steps, so a full revolution is equal to 200 full steps (360 degrees / 1.8), and is rated at 0.4 Amp at 12V.

The Easy Driver can drive up to about 750mA per phase, so it’s perfect to be used with this motor.


It defaults to 1/8 step mode (So if your motor has 200 full steps per revolution, you would get 1600 steps/rev using this 1/8 step mode).


For our tutorial we will override this setting by setting MS1 and MS2 pin on the Easy Driver to default to full step mode.


You can see which settings are available in the Arduino sketch below.

 

PARTS USED

EasyDriver Stepper Driver


Arcade Joystick


Stepper Motor NEMA 17


These are Amazon affiliate links...

They don't cost you anything and it helps me keep the lights on

if you buy something on Amazon. Thank you!

 

CONNECTIONS




As you can see the connections are pretty straightforward:


Pin 3 is connected to Steps pin on Easy Driver Pin 2 is connected to Direction pin Pin 5 is connected to MS1 pin Pin 4 is connected to MS2 pin Pin 7 is connected to SLEEP pin Pin A0 connected to joystick X-axis


Of course we could have added some Limit Switches to make sure our stepper motor does not rotate too far.


But for our tutorial we will assume that we position the belt clip in the middle position.

 

THE CODE


To visualize better how the motor is being controlled, we will not be using any Libraries in our code.


Instead we will be turning On and Off the Step pin of the Easy Driver and put a delay() in between to control the speed.

Since we are not using limit switches, the code will assume that we already positioned the belt clip in the middle position before turning on the power.


We calculated that it takes approx. 10.25 full revolutions of the stepper motor to cover the distance between the motor and the idler pulley.


If our motor in full step mode takes 200 steps to make 1 revolution, then it will take 2050 steps to cover the distance.


So the code will assume that the belt clip is positioned in the middle at startup, or 1025 steps.


As always, please check out the tutorial video to have more information.


#define step_pin 3  // Pin 3 connected to Steps pin on EasyDriver
#define dir_pin 2   // Pin 2 connected to Direction pin
#define MS1 5       // Pin 5 connected to MS1 pin
#define MS2 4       // Pin 4 connected to MS2 pin
#define SLEEP 7     // Pin 7 connected to SLEEP pin
#define X_pin A0    // Pin A0 connected to joystick x axis

int direction;    // Variable to set Rotation (CW-CCW) of the motor
int steps = 1025; // Assumes the belt clip is in the Middle

void setup() {
   pinMode(MS1, OUTPUT);
   pinMode(MS2, OUTPUT);
   pinMode(dir_pin, OUTPUT);
   pinMode(step_pin, OUTPUT);
   pinMode(SLEEP, OUTPUT);
   
   digitalWrite(SLEEP, HIGH);  // Wake up EasyDriver
   delay(5);  // Wait for EasyDriver wake up
   

/* Configure type of Steps on EasyDriver:
// MS1 MS2
//
// LOW LOW = Full Step //
// HIGH LOW = Half Step //
// LOW HIGH = A quarter of Step //
// HIGH HIGH = An eighth of Step //
*/

   digitalWrite(MS1, LOW);      // Configures to Full Steps
   digitalWrite(MS2, LOW);    // Configures to Full Steps
   
}

void loop() {
  while (analogRead(X_pin) >= 0 && analogRead(X_pin) <= 100) {
    if (steps > 0) {
      digitalWrite(dir_pin, HIGH);  // (HIGH = anti-clockwise / LOW = clockwise)
      digitalWrite(step_pin, HIGH);
      delay(1);
      digitalWrite(step_pin, LOW);
      delay(1);
      steps--;
    }      
  }
  
    while (analogRead(X_pin) > 100 && analogRead(X_pin) <= 400) {
      if (steps < 512) {
        digitalWrite(dir_pin, LOW);  // (HIGH = anti-clockwise / LOW = clockwise)
        digitalWrite(step_pin, HIGH);
        delay(1);
         digitalWrite(step_pin, LOW);
        delay(1);
        steps++;
      }    
      if (steps > 512) {
        digitalWrite(dir_pin, HIGH);
        digitalWrite(step_pin, HIGH);
        delay(1);
         digitalWrite(step_pin, LOW);
        delay(1);
        steps--;
      }
    }    
      
    while (analogRead(X_pin) > 401 && analogRead(X_pin) <= 600) {
      if (steps < 1025) {
        digitalWrite(dir_pin, LOW);
        digitalWrite(step_pin, HIGH);
        delay(1);
         digitalWrite(step_pin, LOW);
        delay(1);
        steps++;
      }    
      if (steps > 1025) {
        digitalWrite(dir_pin, HIGH);
        digitalWrite(step_pin, HIGH);
        delay(1);
         digitalWrite(step_pin, LOW);
        delay(1);
        steps--;
      } 
    } 

    while (analogRead(X_pin) > 601 && analogRead(X_pin) <= 900) {
      if (steps < 1535) {
        digitalWrite(dir_pin, LOW);
        digitalWrite(step_pin, HIGH);
        delay(1);
         digitalWrite(step_pin, LOW);
        delay(1);
        steps++;
      }    
      if (steps > 1535) {
        digitalWrite(dir_pin, HIGH);
        digitalWrite(step_pin, HIGH);
        delay(1);
         digitalWrite(step_pin, LOW);
        delay(1);
        steps--;
      }    
    }   
   
    while (analogRead(X_pin) > 900 && analogRead(X_pin) <= 1024) {
      if (steps < 2050) {
        digitalWrite(dir_pin, LOW);
        digitalWrite(step_pin, HIGH);
        delay(1);
         digitalWrite(step_pin, LOW);
        delay(1);
        steps++;
      }
    }
}


 

TUTORIAL VIDEO




 

DOWNLOAD


Copy the above Sketch code in your Arduino IDE software to program your Arduino.

13,384 views0 comments

Comments


All my content is and will always be Free.

If you feel that my Videos / Tutorials are helping, and you would like to contribute...

 You can toss some coins in the Tip Jar via PayPal.

Select amount then click the “Donate” button.

bottom of page