ESP8266 - Stepper Motor Limit Switch

In this tutorial, a limit switch tells the ESP8266 when to stop or turn back a stepper motor, which is driven by a DRV8825. Specifically, we will cover:

ESP8266 NodeMCU NodeMCU stops or reverses a NEMA 17 stepper motor with limit switches and a DRV8825 driver

Overview of Stepper Motor and Limit Switch

If you are unfamiliar with stepper motor and limit switch (including pinout, functioning, programming, etc.), the following tutorials can provide you with more information:

DRV8825 Driver Pinout

A DRV8825 module on its expansion board does the work of driving the stepper motor. The NodeMCU needs only two pins for it: STEP gets one pulse per step, and DIR picks the direction. The current-limit knob on the module keeps the coil current in check, so the NEMA 17 does not get too hot. Leave the three DIP switches of the expansion board OFF for full-step mode (200 steps per turn), and leave EN and V unconnected. The 12V adapter feeds the VIN and GND screw terminal of the board.

Wiring Diagram

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

  • Wiring Diagram between ESP8266, stepper motor and, and a limit switch.
The wiring diagram between ESP8266 NodeMCU and NodeMCU Stepper Motor Limit Switch DRV8825

This image is created using Fritzing. Click to enlarge image

Part Pin ESP8266 NodeMCU / Power
Expansion board STEP (S pin) D1
Expansion board DIR (S pin) D2
Expansion board G pin GND
Expansion board EN, V not connected
Expansion board VIN, GND (screw terminal) 12V adapter via the DC power jack
Limit switch D5 and GND
  • Wiring Diagram between ESP8266, stepper motor and, and two limit switches.
The wiring diagram between ESP8266 NodeMCU and NodeMCU Stepper Motor Two Limit Switches DRV8825

This image is created using Fritzing. Click to enlarge image

Part Pin ESP8266 NodeMCU / Power
Expansion board STEP (S pin) D1
Expansion board DIR (S pin) D2
Expansion board G pin GND
Expansion board EN, V not connected
Expansion board VIN, GND (screw terminal) 12V adapter via the DC power jack
Limit switch 1 D5 and GND
Limit switch 2 D6 and GND

※ NOTE THAT:

The wire order between the stepper motor and the 1A, 1B, 2A, 2B plug of the expansion board may vary depending on the type of stepper motor. Therefore, it is important to take a look at this ESP8266 - Stepper Motor tutorial to understand how to make the connection. Turn the current-limit knob of the DRV8825 low before the first run, and plug in the 12V adapter last.

Wiring without the Expansion Board

The bare DRV8825 module can take the place of the expansion board setup. The ESP8266 still needs only three wires: STEP, DIR and GND.

The wiring diagram between ESP8266 NodeMCU and DRV8825 Module without Expansion Board

This image is created using Fritzing. Click to enlarge image

Here is the setup with two limit switches:

The wiring diagram between ESP8266 NodeMCU and DRV8825 Module without Expansion Board

This image is created using Fritzing. Click to enlarge image

See more in ESP8266's pinout and how to supply power to the ESP8266 and other components.

DRV8825 module pin Connect to
STEP ESP8266 D1
DIR ESP8266 D2
GND (next to FAULT) ESP8266 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:

  • Put a 100uF capacitor (rated 25V or more) between VMOT and GND, as close to the module as you can. It protects the driver from voltage spikes.
  • RST and SLP sit next to each other. Joining them with a jumper wakes up a module that does not respond.

ESP8266 Code - Stop Stepper Motor by a Limit Switch

There are various methods to stop a stepper motor:

  • Invoke stepper.stop() function: This will not cause an instantaneous stop, but rather a gradual one
  • Omit calling stepper.run() function: This will result in an immediate stop of the stepper motor. No more pulses reach STEP, and the DRV8825 holds the shaft in place

The code below will cause a stepper motor to rotate continuously until a limit switch is engaged, at which point it will stop immediately.

/* * This ESP8266 NodeMCU code was developed by newbiely.com * * This ESP8266 NodeMCU code is made available for public use without any restriction * * For comprehensive instructions and wiring diagrams, please visit: * https://newbiely.com/tutorials/esp8266/esp8266-stepper-motor-limit-switch */ #include <ezButton.h> #include <AccelStepper.h> #define MAX_POSITION 0x7FFFFFFF // maximum of position we can set (long type) ezButton limit_switch(D5); // ESP8266 pin D5 connected to the limit switch #define STEP_PIN D1 // ESP8266 pin D1 connected to the STEP pin of the DRV8825 expansion board #define DIR_PIN D2 // ESP8266 pin D2 connected to the DIR pin of the DRV8825 expansion board 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, 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

To get started with ESP8266 on Arduino IDE, follow these steps:

  • Check out the how to setup environment for ESP8266 on Arduino IDE tutorial if this is your first time using ESP8266.
  • Wire the components as shown in the diagram.
  • Connect the ESP8266 board to your computer using a USB cable.
  • Open Arduino IDE on your computer.
  • Choose the correct ESP8266 board, such as (e.g. NodeMCU 1.0 (ESP-12E Module)), and its respective COM port.
  • Connect a USB cable to the ESP8266 and PC.
  • Open the Arduino IDE, select the appropriate board and port.
  • Click to the Libraries icon on the left bar of the Arduino IDE.
  • Search for “ezButton”, then locate the button library provided by ArduinoGetStarted.com.
  • Press the Install button to install ezButton library.
ESP8266 NodeMCU button library
  • Look up “AccelStepper” and locate the AccelStepper library created by Mike McCauley.
  • Press the Install button to add the AccelStepper library.
ESP8266 NodeMCU AccelStepper library
  • Copy the code and open it with the Arduino IDE.
  • Click the Upload button in order to transfer the code to the ESP8266.
  • If the wiring is done correctly, the motor will rotate in a clockwise direction.
  • Tap the limit switch and the motor will be stopped right away.
  • The result that appears on the Serial Monitor should look like this.
∞
Newbiely | Arduino IDE 2.3.8
──
☐
✕
File
Edit
Sketch
Tools
Help
Nodemcu 1.0 (ESP-12E Module)
Newbiely.ino
···
8 Serial.println("Hello World!");
Output
Serial Monitor
Message (Enter to send message to 'Nodemcu 1.0 (ESP-12E Module)' 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
Nodemcu 1.0 (ESP-12E Module) on COM15
2

Code Explanation

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

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

The code below causes a stepper motor to rotate continuously, and it will reverse its direction when a limit switch is touched.

/* * This ESP8266 NodeMCU code was developed by newbiely.com * * This ESP8266 NodeMCU code is made available for public use without any restriction * * For comprehensive instructions and wiring diagrams, please visit: * https://newbiely.com/tutorials/esp8266/esp8266-stepper-motor-limit-switch */ #include <ezButton.h> #include <AccelStepper.h> #define DIRECTION_CCW -1 #define DIRECTION_CW 1 #define MAX_POSITION 0x7FFFFFFF // maximum of position we can set (long type) ezButton limit_switch(D5); // ESP8266 pin D5 connected to the limit switch #define STEP_PIN D1 // ESP8266 pin D1 connected to the STEP pin of the DRV8825 expansion board #define DIR_PIN D2 // ESP8266 pin D2 connected to the DIR pin of the DRV8825 expansion board 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

To get started with ESP8266 on Arduino IDE, follow these steps:

  • Check out the how to setup environment for ESP8266 on Arduino IDE tutorial if this is your first time using ESP8266.
  • Wire the components as shown in the diagram.
  • Connect the ESP8266 board to your computer using a USB cable.
  • Open Arduino IDE on your computer.
  • Choose the correct ESP8266 board, such as (e.g. NodeMCU 1.0 (ESP-12E Module)), and its respective COM port.
  • Copy the code and open it with the Arduino IDE.
  • Click the Upload button to send the code to the ESP8266.
  • If the wiring is correct, the motor will rotate in a clockwise direction.
  • When you touch the limit switch, the stepper motor's rotation will be reversed to an anti-clockwise direction.
  • Touching the limit switch again will cause the stepper motor to rotate clockwise again.
  • The output on the Serial Monitor should look like this.
∞
Newbiely | Arduino IDE 2.3.8
──
☐
✕
File
Edit
Sketch
Tools
Help
Nodemcu 1.0 (ESP-12E Module)
Newbiely.ino
···
8 Serial.println("Hello World!");
Output
Serial Monitor
Message (Enter to send message to 'Nodemcu 1.0 (ESP-12E Module)' 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
Nodemcu 1.0 (ESP-12E Module) on COM15
2

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

The code below causes a stepper motor to spin continuously, and it will reverse its direction when either of two limit switches is touched.

/* * This ESP8266 NodeMCU code was developed by newbiely.com * * This ESP8266 NodeMCU code is made available for public use without any restriction * * For comprehensive instructions and wiring diagrams, please visit: * https://newbiely.com/tutorials/esp8266/esp8266-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 MAX_POSITION 0x7FFFFFFF // maximum of position we can set (long type) ezButton limit_switch_1(D5); // ESP8266 pin D5 connected to the limit switch 1 ezButton limit_switch_2(D6); // ESP8266 pin D6 connected to the limit switch 2 #define STEP_PIN D1 // ESP8266 pin D1 connected to the STEP pin of the DRV8825 expansion board #define DIR_PIN D2 // ESP8266 pin D2 connected to the DIR pin of the DRV8825 expansion board 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

  • Wire the components as shown in the diagram.
  • Connect the ESP8266 board to your computer using a USB cable.
  • Open Arduino IDE on your computer.
  • Choose the correct ESP8266 board, such as (e.g. NodeMCU 1.0 (ESP-12E Module)), and its respective COM port.
  • Copy the code and open it with the Arduino IDE.
  • Click the Upload button in the IDE to upload the code to the ESP8266.
  • 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 switch to anti-clockwise.
  • Touching limit switch 2 should cause the motor to reverse direction again, this time to clockwise.
  • The result displayed on the Serial Monitor should look like the following.
∞
Newbiely | Arduino IDE 2.3.8
──
☐
✕
File
Edit
Sketch
Tools
Help
Nodemcu 1.0 (ESP-12E Module)
Newbiely.ino
···
8 Serial.println("Hello World!");
Output
Serial Monitor
Message (Enter to send message to 'Nodemcu 1.0 (ESP-12E Module)' 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
Nodemcu 1.0 (ESP-12E Module) 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!