Arduino NEMA stepper control with Joystick and Limit Switches

//Arduino NEMA stepper control with Joystick and Limit Switches
TUTORIAL


Control a Stepper Motor with an Arduino, Joystick, Easy Driver and Limit Switches

OVERVIEW

Since we made the tutorial on how to control a stepper motor using an analog joystick, we’ve gotten a lot of request on how to have the stepper move when moving the joystick and not move back to the center.

So in this tutorial we will do just that, but also add some limit switches and add the ability to adjust the speed.

The limit switches will be used to limit the travel of the stepper.

To control the speed we will use the switch included on the joystick module to set the speed.

CONNECTIONS

Here are the connections:

Pin 9 is connected to Steps pin on Easy Driver
Pin 8 is connected to Direction pin
Pin 10 is connected to MS1 pin
Pin 11 is connected to MS2 pin
Pin 12 is connected to SLEEP pin
Pin A0 connected to joystick X-axis
Pin 4 is connected to the joystick Key or switch pin

Pint 2 and 3 are connected to the OUT pin of the Limit Switches

The Ground and Voltage pins of the Easy Driver are connected to a 1 Amp 12V power supply.

We are also using a small breadboard to split the Ground and Voltage connections of the various devices

THE CODE

The code we are using does not require any libraries, but you could use one to control the Stepper motor to give you more options if you want.

By checking the analog value of the X-axis of the joystick we know which direction to move the stepper motor. 512 is center, zero is full left and 1023 is full right. So in our code we check if the analog value is higher than 712 to detect a right movement and a value lower than 312 will indicate a move to the left.

We than check the condition of the limit switches, and if there are not activated, we proceed to move the motor.

Using the Switch Case function, we change the delay to control the speed when we press down on the joystick switch.

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

#define step_pin 9  // Pin 9 connected to Steps pin on EasyDriver
#define dir_pin 8   // Pin 8 connected to Direction pin
#define MS1 10       // Pin 10 connected to MS1 pin
#define MS2 11       // Pin 11 connected to MS2 pin
#define SLEEP 12     // Pin 12 connected to SLEEP pin

#define X_pin A0    // Pin A0 connected to joystick x axis
#define Joy_switch 4  // Pin 4 connected to joystick switch

#define Limit01 2  // Pin 2 connected to Limit switch out
#define Limit02 3  // Pin 3 connected to Limit switch out

int step_speed = 10;  // Speed of Stepper motor (higher = slower)

void setup() {
   pinMode(MS1, OUTPUT);
   pinMode(MS2, OUTPUT);
   pinMode(dir_pin, OUTPUT);
   pinMode(step_pin, OUTPUT);
   pinMode(SLEEP, OUTPUT);
   
   pinMode(Limit01, INPUT);
   pinMode(Limit01, INPUT);
   
   pinMode(Joy_switch, INPUT_PULLUP);
   
   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() {
  if (!digitalRead(Joy_switch)) {  //  If Joystick switch is clicked
    delay(500);  // delay for deboucing
    switch (step_speed) {  // check current value of step_speed and change it
      case 1:
        step_speed=10;  // slow speed
        break;
      case 3:
        step_speed=1;  // fast speed
        break;
      case 10:
        step_speed=3;  // medium speed
        break;
    }
  }    
    
  if (analogRead(X_pin) > 712) {  //  If joystick is moved Left
    if (!digitalRead(Limit01)) {}  // check if limit switch is activated
    
      else {  //  if limit switch is not activated, move motor clockwise
      
        digitalWrite(dir_pin, LOW);  // (HIGH = anti-clockwise / LOW = clockwise)
        digitalWrite(step_pin, HIGH);
        delay(step_speed);
        digitalWrite(step_pin, LOW);
        delay(step_speed);
    }      
  }
  
    if (analogRead(X_pin) < 312) {  // If joystick is moved right
    
    if (!digitalRead(Limit02)) {}  // check if limit switch is activated
    
      else {  //  if limit switch is not activated, move motor counter clockwise
      
        digitalWrite(dir_pin, HIGH);  // (HIGH = anti-clockwise / LOW = clockwise)
        digitalWrite(step_pin, HIGH);
        delay(step_speed);
        digitalWrite(step_pin, LOW);
        delay(step_speed);
    }      
  }
}
TUTORIAL VIDEO
DOWNLOAD

Just copy the above Sketch code you want to use above in your Arduino IDE software to program your Arduino.

By | 2018-02-23T17:10:26-04:00 October 11th, 2015|Tutorials|

15 Comments

  1. Chuck July 2, 2019 at 8:21 am - Reply

    Can this also work with any external stepper driver? How can it be modified so that it’s not looking for an easydriver?

  2. Yuzzy March 16, 2019 at 2:52 am - Reply

    @Jim Heliops
    I’m in exactly the same situation – any change here including the int value for the step speed results in the sequence not working.

  3. Jim Heliops January 21, 2019 at 6:00 am - Reply

    Hello and thank you for this tutorial that works perfect as is.
    I have a question though.
    I am trying to change the “switch step_speed” values for the different cases.
    For example case 10: step_speed=5; // medium speed changed to 5 instead of 3.

    The problem is that if I do even a single change in this part of the code, (either to the case numbers or the step_speed numbers) then the switch does not seem to operate and change between the cases, and the motor just spins with the original setup speed of 10.

    Is there something I am missing here about the logic of this “function”?
    Should I do something differently?
    By the way I am totally new to arduino so please forgive my ignorance.

    • James G June 17, 2019 at 12:48 am - Reply

      The case # defines the number of times the button is pushed, so case 1 is speed 10 (slowest). You’d need to push the button 2 more times for case 3, for speed of 1 (fastest) and 7 more times for case 10, for medium. Try changing to:
      case 1:
      step_speed=10; // slow speed
      break;
      case 2:
      step_speed=5; // medium speed
      break;
      case 3:
      step_speed=1; // fast speed
      break;

      I changed it a bit as it made more sense to go from slow, to medium, to high. The step_speed defines you pulse delay for the steps of the motor, measure in milliseconds. For example, case 1 sets the delay at 10 milliseconds between pulses (on for 10, off for 10). I have not tried this yet, just did a quick Google search for “switch case function with a button” 🙂
      Not sure if it’ll work as it doesn’t seem to have code for storing the button state. I may try later today as I’m trying to build something around this example.

  4. Karin December 28, 2018 at 9:15 am - Reply

    Super Video Einleitung
    Ich bedanke mich sehr das Sie free Videos machen Respekt.
    Ich habe eine Frage ich hoffe können sie mir helfen.
    Ich wolte meine Epson 15000 umbauern mit nema 17 schrittmotoren kann ich diese Sketch (
    Arduino NEMA-Steppersteuerung mit Joystick und Endschaltern benutzen oder passt nicht
    Ich freu mich über einer Rückmeldung
    Mit freundlichen grüßen
    Aus Deutschland

  5. Bruno December 17, 2018 at 6:43 pm - Reply

    Hi, ist a great tutorial for Beginners like me..:) just one Question:

    it is possible to use instead of a Joystick just 2 Buttons for Forward and backward drive ? if button 1 is pushed for short Time, stepper shut turn always for example 200 steps in one direction and wait for the next Push. If Button 2 is pushed one time, stepper shut turn until to Endswitch in the other Direction and Wait.

    Thank You for any Feedback or Code example. Best Regards Bruno

  6. Ahmad December 9, 2018 at 7:05 pm - Reply

    Hey sir how many steppers we can operate with single arduino and do we can program each stepper with separate joystick and separate push button and limit switch???

  7. Paul Cabrelli November 9, 2018 at 9:49 am - Reply

    Hi All, I am really new to this. But one of my pet projects to do is eventually a camera slider. This seems a fab tutorial, and I have followed the others related to it. But the wiring diagram for the Limit Switches does not seem to correct. There seems to be no power going to the limit switches. The Commons are together on one breadboard bus-bar, but the two power are on different breadboard bus-bars. So not to sure then where the limit switches are being powered from? Please help

    Regards

    Paul Cabrelli

    • brainy-bits November 9, 2018 at 12:28 pm - Reply

      You are correct, when making the connection diagram I made a mistake. The power wires from the switches should be connected together and then connect to the 5V from the Arduino. I will update the diagram soon to reflect this. Thank you for pointing that out!

  8. Shaark October 1, 2018 at 11:55 am - Reply

    Hi
    I need to rotate the guide with a sequence forward and reverse with one home limit switch
    Can you pls upload the code. No inputs like joysticks.

  9. Roger Stephenson June 15, 2018 at 9:22 pm - Reply

    To add a second axis can the sections of code just be duplicated with the changes to the additional code to reflect the second axis as needed? Say this code shown above represents the X axis and all the duplicate code represents the Y axis, could one use the same parameters with appropriate unique designators? I am building a telescope pointing device (not really a mount) and have the mechanical side all sorted. I also have a Nano and 2 Easy Divers. All I need is to write and test the code, which seems straightforward, unless I need to alter my thinking from the current paradigm in my old skull. Thanks!

  10. Art May 31, 2018 at 10:03 am - Reply

    Can the Easy driver be replaced with the DRV8825 driver without any coding changes necessary.

  11. Alfred April 27, 2018 at 4:25 pm - Reply

    Thank you for share this project with us!
    Very good explained and code included ….. perfect for an Beginner like me. Please let me ask: In your wiring diagram is an stepper control included by “A3967” – Is it possible to Change to stepper driver “A4988”? What for changes are for this needed?
    Thank you very much for your friendly Suppor

  12. Jim Sauer April 27, 2018 at 1:35 pm - Reply

    Pretty much exactly what I need for my project. Is there a way to substitute a DPDT switch for the joystick? I’m a newbie.

    • David Myhill May 8, 2018 at 9:55 am - Reply

      Thats what Im looking for maybe a 3 way switch or 3 individual switches to control speed (slow medium fast). I can do it using the centre button of the joystick but not sure how to write code for each button. I need to be able to reference the speed without being able to see the motor (using in a telescope motorised focuser.

      Any help greatly appreciated.

Leave A Comment