Arduino - Stepper Motor and Limit Switch

In this Arduino tutorial, We are going to learn how to use Arduino, limit switch, DRV8825 driver and stepper motor. In detail, we are going to learn:

About Stepper Motor and Limit Switch

If you do not know about stepper motor and limit switch (pinout, how it works, how to program ...), learn about them in the following tutorials:

A DRV8825 on its expansion board drives the stepper motor here. Two Arduino pins are enough: pin 4 sends one pulse per step to STEP, and pin 3 sets DIR HIGH or LOW for the direction. The driver keeps the coil current limited, so the motor does not overheat while it stands still at a switch.

DRV8825 Driver Pinout

Wiring Diagram

This tutorial provides the Arduino codes for two cases: One stepper motor + one limit switch, One stepper motor + two limit switches. Both cases share the same driver wiring: Arduino pin 4 to the S pin of STEP, pin 3 to the S pin of DIR, and GND to one G pin. The 12V adapter feeds the VIN and GND terminal of the expansion board via the DC power jack.

  • Wiring diagram between the stepper motor and a limit switch
Arduino Stepper Motor Limit Switch DRV8825 Wiring Diagram

This image is created using Fritzing. Click to enlarge image

Part Pin Connect To
Expansion board STEP (S pin) Arduino pin 4
Expansion board DIR (S pin) Arduino pin 3
Expansion board G pin Arduino GND
Expansion board VIN, GND terminal + and - of the 12V power adapter
Expansion board 1A, 1B, 2A, 2B the stepper motor
Limit switch one pin Arduino pin A0
Limit switch other pin Arduino GND

Leave EN and V unconnected. If you want the wires to stay in place, plug a Screw Shield V2 or a Proto Screw Terminal Block Shield into the Arduino Uno and screw each wire to the terminal of its pin:

Arduino Stepper Motor Limit Switch DRV8825 Screw Shield Wiring Diagram

This image is created using Fritzing. Click to enlarge image

  • Wiring diagram between the stepper motor and two limit switches
Arduino Stepper Motor Two Limit Switches DRV8825 Wiring Diagram

This image is created using Fritzing. Click to enlarge image

The driver side does not change. Only a second switch is added: switch 1 connects pin A0 to GND when pressed, and switch 2 does the same for pin A1.

On a Proto Screw Terminal Block Shield or a Screw Shield V2, the two switches simply use the screw terminals of A0, A1 and GND:

Arduino Stepper Motor Two Limit Switches DRV8825 Screw Shield Wiring Diagram

This image is created using Fritzing. Click to enlarge image

※ NOTE THAT:

  • Stepper motors do not all use the same wire order. Find the two coils first, then put one coil on 1A/1B and the other on 2A/2B. The Arduino - Stepper Motor tutorial shows how to find the coils.
  • All three DIP switches go OFF (full step). Start with the current-limit knob of the DRV8825 turned low, and raise it bit by bit until the motor turns without jerks.

Wiring without the Expansion Board

The expansion board is handy, but it is not a must. Follow the picture and the table, and take care with the small pin labels.

Arduino DRV8825 Module without Expansion Board Wiring Diagram

This image is created using Fritzing. Click to enlarge image

The same circuit on the screw terminal shield:

Arduino DRV8825 Module without Expansion Board Wiring Diagram

This image is created using Fritzing. Click to enlarge image

The two limit switch version:

Arduino DRV8825 Module without Expansion Board Wiring Diagram

This image is created using Fritzing. Click to enlarge image

Two limit switches with the screw terminal shield:

Arduino DRV8825 Module without Expansion Board Wiring Diagram

This image is created using Fritzing. Click to enlarge image

DRV8825 module pin Connect to
STEP Arduino D4
DIR Arduino D3
GND (next to FAULT) Arduino GND
VMOT + of the 12V adapter
GND (next to VMOT) - of the 12V adapter
2B and 1B one coil of the stepper motor
1A and 2A the other coil of the stepper motor
EN, M0, M1, M2, RST, SLP, FAULT not connected

※ NOTE THAT:

  • The bare module has no capacitor of its own. Fit a 100uF one (25V or more) between VMOT and GND.
  • If the motor does not move at all, join RST and SLP with a short wire. Some modules need this to wake up.

Arduino Code - Stop Stepper Motor by a Limit Switch

There are several ways to make a stepper motor stop:

  • Call stepper.stop() function: This way does NOT stop the stepper motor immediately but gradually
  • Do NOT call stepper.run() function: This way stops the stepper motor immediately

The below code make a stepper motor spin infinitely and stop immediately when a limit switch is touched

/* * Created by ArduinoGetStarted.com * * This example code is in the public domain * * Tutorial page: https://arduinogetstarted.com/tutorials/arduino-stepper-motor-and-limit-switch */ #include <ezButton.h> #include <AccelStepper.h> #define STEP_PIN 4 // the Arduino pin connected to the STEP pin of DRV8825 #define DIR_PIN 3 // the Arduino pin connected to the DIR pin of DRV8825 #define MAX_POSITION 0x7FFFFFFF // maximum of position we can set (long type) ezButton limitSwitch(A0); // create ezButton object that attach to pin A0; AccelStepper stepper(AccelStepper::DRIVER, STEP_PIN, DIR_PIN); bool isStopped = false; void setup() { Serial.begin(9600); limitSwitch.setDebounceTime(50); // set debounce time to 50 milliseconds stepper.setMaxSpeed(500.0); // set the maximum speed stepper.setAcceleration(50.0); // set acceleration stepper.setSpeed(100); // set initial speed stepper.setCurrentPosition(0); // set position stepper.moveTo(MAX_POSITION); } void loop() { limitSwitch.loop(); // MUST call the loop() function first if (limitSwitch.isPressed()) { Serial.println(F("The limit switch: TOUCHED")); isStopped = true; } if (isStopped == false) { // without this part, the move will stop after reaching maximum position if (stepper.distanceToGo() == 0) { // if motor moved to the maximum position stepper.setCurrentPosition(0); // reset position to 0 stepper.moveTo(MAX_POSITION); // move the motor to maximum position again } stepper.run(); // MUST be called in loop() function } else { // without calling stepper.run() function, no more pulses go to the STEP pin, so the motor stops immediately // NOTE: stepper.stop() function does NOT stops motor immediately Serial.println(F("The stepper motor is STOPPED")); } }

Quick Steps

  • Connect Arduino to PC via USB cable
  • Open Arduino IDE, select the right board and port
  • Navigate to the Libraries icon on the left bar of the Arduino IDE.
  • Search “ezButton”, then find the button library by ArduinoGetStarted.com
  • Click Install button to install ezButton library.
Arduino button library
  • Search “AccelStepper”, then find the AccelStepper library by Mike McCauley
  • Click Install button to install AccelStepper library.
Arduino AccelStepper library
  • Copy the above code and open with Arduino IDE
  • Click Upload button on Arduino IDE to upload code to Arduino
  • Plug the 12V power adapter into the DC power jack
  • If the wiring is correct, you will see the motor rotates clockwise direction.
  • Touch the limit switch
  • You will see the motor is stopped immediately
  • The result on Serial Monitor looks like below
∞
Newbiely | Arduino IDE 2.3.8
──
☐
✕
File
Edit
Sketch
Tools
Help
Arduino Uno
Newbiely.ino
···
8 Serial.println("Hello World!");
Output
Serial Monitor
Message (Enter to send message to 'Arduino Uno' on 'COM15')
New Line
9600 baud
The limit switch: TOUCHED The stepper motor is STOPPED The stepper motor is STOPPED The stepper motor is STOPPED The stepper motor is STOPPED
Ln 11, Col 1
Arduino Uno on COM15
2

Code Explanation

Read the line-by-line explanation in comment lines of code!

Arduino Code - Change Direction of Stepper Motor by a Limit Switch

The below code make a stepper motor spin infinitely and change its direction when a limit switch is touched

/* * Created by ArduinoGetStarted.com * * This example code is in the public domain * * Tutorial page: https://arduinogetstarted.com/tutorials/arduino-stepper-motor-and-limit-switch */ #include <ezButton.h> #include <AccelStepper.h> #define DIRECTION_CCW -1 #define DIRECTION_CW 1 #define STEP_PIN 4 // the Arduino pin connected to the STEP pin of DRV8825 #define DIR_PIN 3 // the Arduino pin connected to the DIR pin of DRV8825 #define MAX_POSITION 0x7FFFFFFF // maximum of position we can set (long type) ezButton limitSwitch(A0); // create ezButton object that attach to pin A0; AccelStepper stepper(AccelStepper::DRIVER, STEP_PIN, DIR_PIN); int direction = DIRECTION_CW; long targetPos = 0; void setup() { Serial.begin(9600); limitSwitch.setDebounceTime(50); // set debounce time to 50 milliseconds stepper.setMaxSpeed(500.0); // set the maximum speed stepper.setAcceleration(50.0); // set acceleration stepper.setSpeed(100); // set initial speed stepper.setCurrentPosition(0); // set position targetPos = direction * MAX_POSITION; stepper.moveTo(targetPos); } void loop() { limitSwitch.loop(); // MUST call the loop() function first if (limitSwitch.isPressed()) { Serial.println(F("The limit switch: TOUCHED")); direction *= -1; // change direction Serial.print(F("The direction -> ")); if (direction == DIRECTION_CW) Serial.println(F("CLOCKWISE")); else Serial.println(F("ANTI-CLOCKWISE")); targetPos = direction * MAX_POSITION; stepper.setCurrentPosition(0); // set position stepper.moveTo(targetPos); } // without this part, the move will stop after reaching maximum position if (stepper.distanceToGo() == 0) { // if motor moved to the maximum position stepper.setCurrentPosition(0); // reset position to 0 stepper.moveTo(targetPos); // move the motor to maximum position again } stepper.run(); // MUST be called in loop() function }

Quick Steps

  • Copy the above code and open with Arduino IDE
  • Click Upload button on Arduino IDE to upload code to Arduino
  • If the wiring is correct, you will see the motor spins in the clockwise direction.
  • Touch the limit switch
  • You will see the stepper motor's direction is changed to the anti-clockwise
  • Touch the limit switch again
  • You will see the stepper motor's direction is changed to clockwise
  • The result on Serial Monitor looks like below
∞
Newbiely | Arduino IDE 2.3.8
──
☐
✕
File
Edit
Sketch
Tools
Help
Arduino Uno
Newbiely.ino
···
8 Serial.println("Hello World!");
Output
Serial Monitor
Message (Enter to send message to 'Arduino Uno' on 'COM15')
New Line
9600 baud
The limit switch: TOUCHED The direction -> ANTI-CLOCKWISE The limit switch: TOUCHED The direction -> CLOCKWISE
Ln 11, Col 1
Arduino Uno on COM15
2

Arduino Code - Change Direction of Stepper Motor by two Limit Switches

The below code make a stepper motor spin infinitely and change its direction when one of two limit switches is touched

/* * Created by ArduinoGetStarted.com * * This example code is in the public domain * * Tutorial page: https://arduinogetstarted.com/tutorials/arduino-stepper-motor-and-limit-switch */ #include <ezButton.h> #include <AccelStepper.h> #define DIRECTION_CCW -1 #define DIRECTION_CW 1 #define STATE_CHANGE_DIR 1 #define STATE_MOVE 2 #define STATE_MOVING 3 #define STEP_PIN 4 // the Arduino pin connected to the STEP pin of DRV8825 #define DIR_PIN 3 // the Arduino pin connected to the DIR pin of DRV8825 #define MAX_POSITION 0x7FFFFFFF // maximum of position we can set (long type) ezButton limitSwitch_1(A0); // create ezButton object that attach to pin A0; ezButton limitSwitch_2(A1); // create ezButton object that attach to pin A1; AccelStepper stepper(AccelStepper::DRIVER, STEP_PIN, DIR_PIN); int stepperState = STATE_MOVE; int direction = DIRECTION_CW; long targetPos = 0; void setup() { Serial.begin(9600); limitSwitch_1.setDebounceTime(50); // set debounce time to 50 milliseconds limitSwitch_2.setDebounceTime(50); // set debounce time to 50 milliseconds stepper.setMaxSpeed(500.0); // set the maximum speed stepper.setAcceleration(50.0); // set acceleration stepper.setSpeed(100); // set initial speed stepper.setCurrentPosition(0); // set position } void loop() { limitSwitch_1.loop(); // MUST call the loop() function first limitSwitch_2.loop(); // MUST call the loop() function first if (limitSwitch_1.isPressed()) { stepperState = STATE_CHANGE_DIR; Serial.println(F("The limit switch 1: TOUCHED")); } if (limitSwitch_2.isPressed()) { stepperState = STATE_CHANGE_DIR; Serial.println(F("The limit switch 2: TOUCHED")); } switch (stepperState) { case STATE_CHANGE_DIR: direction *= -1; // change direction Serial.print(F("The direction -> ")); if (direction == DIRECTION_CW) Serial.println(F("CLOCKWISE")); else Serial.println(F("ANTI-CLOCKWISE")); stepperState = STATE_MOVE; // after changing direction, go to the next state to move the motor break; case STATE_MOVE: targetPos = direction * MAX_POSITION; stepper.setCurrentPosition(0); // set position stepper.moveTo(targetPos); stepperState = STATE_MOVING; // after moving, go to the next state to keep the motor moving infinity break; case STATE_MOVING: // without this state, the move will stop after reaching maximum position if (stepper.distanceToGo() == 0) { // if motor moved to the maximum position stepper.setCurrentPosition(0); // reset position to 0 stepper.moveTo(targetPos); // move the motor to maximum position again } break; } stepper.run(); // MUST be called in loop() function }

Quick Steps

  • Copy the above code and open with Arduino IDE
  • Click Upload button on Arduino IDE to upload code to Arduino
  • If the wiring is correct, you will see the motor spins in the clockwise direction.
  • Touch the limit switch 1
  • You will see the stepper motor's direction is changed to anti-clockwise
  • Touch the limit switch 2
  • You will see the stepper motor's direction is changed to clockwise
  • The result on Serial Monitor looks like below
∞
Newbiely | Arduino IDE 2.3.8
──
☐
✕
File
Edit
Sketch
Tools
Help
Arduino Uno
Newbiely.ino
···
8 Serial.println("Hello World!");
Output
Serial Monitor
Message (Enter to send message to 'Arduino Uno' on 'COM15')
New Line
9600 baud
The limit switch 1: TOUCHED The direction -> ANTI-CLOCKWISE The limit switch 2: TOUCHED The direction -> CLOCKWISE
Ln 11, Col 1
Arduino Uno on COM15
2

Video Tutorial

We are considering to make the video tutorials. If you think the video tutorials are essential, please subscribe to our YouTube channel to give us motivation for making the videos.

The Best Arduino Starter Kit

※ OUR MESSAGES