Arduino Nano - Stepper Motor Limit Switch

This tutorial instructs you how to use Arduino Nano to control a stepper motor via a limit switch and a DRV8825 driver. Specifically, we will cover:

Arduino Nano stepper motor with limit switch and DRV8825 driver

Hardware Preparation

1×Official Arduino Nano
1×Alternatively, DIYables Nano V3.0 Development Board
1×USB A to Mini-B USB cable
1×Limit Switch (KW12-3)
1×Limit Switch Module
1×Limit Switch (V-153-1C25)
1×Limit Switch (V-155-1C25)
1×Limit Switch (V-156-1C25)
1×Stepper Motor Nema 17
1×DRV8825 Stepper Motor Driver
1×Expansion Board for DRV8825
1×12V Power Adapter
1×DC Power Jack
1×Jumper Wires
1×Breadboard
1×Recommended: Screw Terminal Expansion Board for Arduino Nano
1×Recommended: Breakout Expansion Board for Arduino Nano
1×Recommended: Power Splitter for Arduino Nano

Or you can buy the following kits:

1×DIYables Sensor Kit (18 sensors/displays)
Disclosure: Some of the links provided in this section are Amazon affiliate links. We may receive a commission for any purchases made through these links at no additional cost to you.
Additionally, some of these links are for products from our own brand, DIYables .

Overview of Stepper Motor and Limit Switch

If you are unfamiliar with stepper motor and limit switch (including pinout, functionality, and programming), the following tutorials can help you learn:

The motor is run by a DRV8825 driver. This small module is plugged into its own expansion board. The Arduino Nano sends one pulse to the STEP pin for each step, and the DIR pin picks the direction. So the whole motor needs only two pins, which leaves A0 and A1 free for the limit switches. The DRV8825 also limits the coil current, so the motor does not run hot.

DRV8825 Module Pinout

Wiring Diagram

This tutorial provides the wiring diagram for two cases: One stepper motor + one limit switch, One stepper motor + two limit switches.

The DRV8825 expansion board connects the same way in both cases:

Expansion board pin Connect To
STEP (S pin) Arduino Nano D4
DIR (S pin) Arduino Nano D3
G (one pin) Arduino Nano GND
EN, V nothing (leave free)
VIN, GND screw terminal + and - of the 12V power adapter (via the DC power jack)
1A, 1B one coil of the stepper motor
2A, 2B the other coil of the stepper motor
  • Wiring Diagram between Arduino Nano, stepper motor, and a limit switch. The limit switch goes to pin A0.
The wiring diagram between Arduino Nano and Stepper Motor Limit Switch DRV8825

This image is created using Fritzing. Click to enlarge image

Prefer screws to jumper wires? Insert the Arduino Nano into a Screw Terminal Expansion Board for Arduino Nano and clamp each wire, as this picture shows:

The wiring diagram between Arduino Nano and Stepper Motor Limit Switch DRV8825 Screw Terminal

This image is created using Fritzing. Click to enlarge image

  • Wiring Diagram between Arduino Nano, stepper motor, and two limit switches. Limit switch 1 goes to pin A0 and limit switch 2 goes to pin A1.
The wiring diagram between Arduino Nano and Stepper Motor Two Limit Switches DRV8825

This image is created using Fritzing. Click to enlarge image

The same two-switch wiring on the screw terminal board:

The wiring diagram between Arduino Nano and Stepper Motor Two Limit Switches DRV8825 Screw Terminal

This image is created using Fritzing. Click to enlarge image

※ NOTE THAT:

  • The wire colors of stepper motors differ between makers. To find the two coils of your motor and connect them to 1A/1B and 2A/2B, please refer to the Arduino Nano - Stepper Motor tutorial.
  • Set the three DIP switches on the expansion board to OFF (full step), and turn the current-limit knob of the DRV8825 low before the first run.

Wiring without the Expansion Board

This part shows the same circuit with the DRV8825 module alone. The Arduino Nano code stays exactly the same.

The wiring diagram between Arduino Nano and DRV8825 Module without Expansion Board

This image is created using Fritzing. Click to enlarge image

With a screw terminal shield, the same wiring looks like this:

The wiring diagram between Arduino Nano and DRV8825 Module without Expansion Board

This image is created using Fritzing. Click to enlarge image

For two limit switches, wire it like this:

The wiring diagram between Arduino Nano and DRV8825 Module without Expansion Board

This image is created using Fritzing. Click to enlarge image

Two limit switches, on the screw terminal shield:

The wiring diagram between Arduino Nano and DRV8825 Module without Expansion Board

This image is created using Fritzing. Click to enlarge image

See The best way to supply power to the Arduino Nano and other components.

DRV8825 module pin Connect to
STEP Arduino Nano D4
DIR Arduino Nano D3
GND (next to FAULT) Arduino Nano 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:

  • Keep the 12V supply stable with a 100uF capacitor across VMOT and GND. A 25V or 35V part is a good choice.
  • A silent motor often means RST is not pulled high. A short wire from RST to SLP fixes it.

Arduino Nano Code - Stop Stepper Motor by a Limit Switch

One can stop a stepper motor in several ways:

  • Invoke the stepper.stop() function: This will not bring the motor to an immediate stop, but will cause it to decelerate gradually
  • Do not call the stepper.run() function: No more pulses reach the STEP pin, so the stepper motor comes to a stop immediately

The code below causes a stepper motor to spin continuously until a limit switch is touched.

/* * This Arduino Nano code was developed by newbiely.com * * This Arduino Nano code is made available for public use without any restriction * * For comprehensive instructions and wiring diagrams, please visit: * https://newbiely.com/tutorials/arduino-nano/arduino-nano-stepper-motor-limit-switch */ #include <ezButton.h> #include <AccelStepper.h> #define STEP_PIN 4 // The Arduino Nano pin connected to the STEP pin of DRV8825 #define DIR_PIN 3 // The Arduino Nano pin connected to the DIR pin of DRV8825 #define MAX_POSITION 0x7FFFFFFF // maximum of position we can set (long type) ezButton limit_switch(A0); // create ezButton object for pin A0 // DRIVER mode: one pin for STEP, one pin for DIR AccelStepper stepper(AccelStepper::DRIVER, STEP_PIN, DIR_PIN); bool is_stopped = false; void setup() { Serial.begin(9600); limit_switch.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() { limit_switch.loop(); // MUST call the loop() function first if (limit_switch.isPressed()) { Serial.println(F("The limit switch: TOUCHED")); is_stopped = true; } if (is_stopped == 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 { // If the stepper.run() function is not invoked, no more STEP pulses are sent: the motor stops immediately // NOTE: invoking the stepper.stop() does NOT stop the motor immediately. Serial.println(F("The stepper motor is STOPPED")); } }

Detailed Instructions

  • Connect the Arduino Nano to a computer using a USB cable.
  • Open the Arduino IDE, choose the correct board and port.
  • Click to the Libraries icon on the left bar of the Arduino IDE.
  • Search for “ezButton” and locate the button library from ArduinoGetStarted.com.
  • Press the Install button to install the ezButton library.
Arduino Nano button library
  • Look for “AccelStepper” and locate the AccelStepper library created by Mike McCauley.
  • Then, press the Install button to add it.
Arduino Nano AccelStepper library
  • Copy the code above and open it in the Arduino IDE.
  • Click the Upload button in the Arduino IDE to compile and upload the code to the Arduino Nano.
  • Plug the 12V adapter into the DC power jack.
  • If the wiring is correct, the motor should rotate in a clockwise direction.
  • When the limit switch is touched, the motor should stop immediately.
  • The result displayed on the Serial Monitor should look like this.
∞
Newbiely | Arduino IDE 2.3.8
──
☐
✕
File
Edit
Sketch
Tools
Help
Arduino Nano
Newbiely.ino
···
8 Serial.println("Hello World!");
Output
Serial Monitor
Message (Enter to send message to 'Arduino Nano' 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 Nano on COM15
2

Code Explanation

Check out the line-by-line explanation contained in the comments of the source code!

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

A stepper motor is made to spin continuously, and its direction is altered when a limit switch is touched. This is accomplished with the following code:

/* * This Arduino Nano code was developed by newbiely.com * * This Arduino Nano code is made available for public use without any restriction * * For comprehensive instructions and wiring diagrams, please visit: * https://newbiely.com/tutorials/arduino-nano/arduino-nano-stepper-motor-limit-switch */ #include <ezButton.h> #include <AccelStepper.h> #define DIRECTION_CCW -1 #define DIRECTION_CW 1 #define STEP_PIN 4 // The Arduino Nano pin connected to the STEP pin of DRV8825 #define DIR_PIN 3 // The Arduino Nano pin connected to the DIR pin of DRV8825 #define MAX_POSITION 0x7FFFFFFF // maximum of position we can set (long type) ezButton limit_switch(A0); // create ezButton object for pin A0 // DRIVER mode: one pin for STEP, one pin for DIR AccelStepper stepper(AccelStepper::DRIVER, STEP_PIN, DIR_PIN); int direction = DIRECTION_CW; long target_pos = 0; void setup() { Serial.begin(9600); limit_switch.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 target_pos = direction * MAX_POSITION; stepper.moveTo(target_pos); } void loop() { limit_switch.loop(); // MUST call the loop() function first if (limit_switch.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")); target_pos = direction * MAX_POSITION; stepper.setCurrentPosition(0); // set position stepper.moveTo(target_pos); } // 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(target_pos); // move the motor to maximum position again } stepper.run(); // MUST be called in loop() function }

Detailed Instructions

  • Copy the code and open it with the Arduino IDE.
  • Click the Upload button in the IDE to upload the code to the Arduino Nano.
  • If the wiring is correct, the motor will rotate clockwise.
  • Touch the limit switch and the stepper motor's direction will be reversed to counterclockwise.
  • Touch the limit switch again and the stepper motor will turn clockwise.
  • The result on the Serial Monitor should look like this.
∞
Newbiely | Arduino IDE 2.3.8
──
☐
✕
File
Edit
Sketch
Tools
Help
Arduino Nano
Newbiely.ino
···
8 Serial.println("Hello World!");
Output
Serial Monitor
Message (Enter to send message to 'Arduino Nano' 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 Nano on COM15
2

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

Make a stepper motor spin continuously and switch its direction when either of two limit switches is touched.

/* * This Arduino Nano code was developed by newbiely.com * * This Arduino Nano code is made available for public use without any restriction * * For comprehensive instructions and wiring diagrams, please visit: * https://newbiely.com/tutorials/arduino-nano/arduino-nano-stepper-motor-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 Nano pin connected to the STEP pin of DRV8825 #define DIR_PIN 3 // The Arduino Nano pin connected to the DIR pin of DRV8825 #define MAX_POSITION 0x7FFFFFFF // maximum of position we can set (long type) ezButton limit_switch_1(A0); // create ezButton object for pin A0 ezButton limit_switch_2(A1); // create ezButton object for pin A1 // DRIVER mode: one pin for STEP, one pin for DIR AccelStepper stepper(AccelStepper::DRIVER, STEP_PIN, DIR_PIN); int stepper_state = STATE_MOVE; int direction = DIRECTION_CW; long target_pos = 0; void setup() { Serial.begin(9600); limit_switch_1.setDebounceTime(50); // set debounce time to 50 milliseconds limit_switch_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() { limit_switch_1.loop(); // MUST call the loop() function first limit_switch_2.loop(); // MUST call the loop() function first if (limit_switch_1.isPressed()) { stepper_state = STATE_CHANGE_DIR; Serial.println(F("The limit switch 1: TOUCHED")); } if (limit_switch_2.isPressed()) { stepper_state = STATE_CHANGE_DIR; Serial.println(F("The limit switch 2: TOUCHED")); } switch (stepper_state) { 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")); stepper_state = STATE_MOVE; // after changing direction, go to the next state to move the motor break; case STATE_MOVE: target_pos = direction * MAX_POSITION; stepper.setCurrentPosition(0); // set position stepper.moveTo(target_pos); stepper_state = 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(target_pos); // move the motor to maximum position again } break; } stepper.run(); // MUST be called in loop() function }

Detailed Instructions

  • Copy the above code and open it in the Arduino IDE.
  • Click the Upload button to send the code to the Arduino Nano.
  • If the wiring is correct, the motor should spin in a clockwise direction.
  • When you touch limit switch 1, the stepper motor's direction should be reversed to anti-clockwise.
  • When you touch limit switch 2, the stepper motor's direction should be changed back to clockwise.
  • The result on the Serial Monitor should look like this.
∞
Newbiely | Arduino IDE 2.3.8
──
☐
✕
File
Edit
Sketch
Tools
Help
Arduino Nano
Newbiely.ino
···
8 Serial.println("Hello World!");
Output
Serial Monitor
Message (Enter to send message to 'Arduino Nano' 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 Nano on COM15
2

Video Tutorial

※ OUR MESSAGES

  • As freelancers, We are AVAILABLE for HIRE. See how to outsource your project to us
  • Please feel free to share the link of this tutorial. However, Please do not use our content on any other websites. We invested a lot of effort and time to create the content, please respect our work!