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:
- How to stop stepper motor when a limit switch is touched
- How to change the direction of stepper motor when a limit switch is touched
- How to change the direction of stepper motor when two limit switches is touched
Hardware Required
Or you can buy the following kits:
| 1 | × | DIYables STEM V3 Starter Kit (Arduino included) | |
| 1 | × | DIYables Sensor Kit (18 sensors/displays) |
Additionally, some links direct to products from our own brand, DIYables .
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:
- Arduino - Limit Switch tutorial
- Arduino - Stepper Motor tutorial
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.

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

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:

This image is created using Fritzing. Click to enlarge image
- Wiring diagram between the stepper motor and two limit switches

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:

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.

This image is created using Fritzing. Click to enlarge image
The same circuit on the screw terminal shield:

This image is created using Fritzing. Click to enlarge image
The two limit switch version:

This image is created using Fritzing. Click to enlarge image
Two limit switches with the screw terminal shield:

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
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.

- Search “AccelStepper”, then find the AccelStepper library by Mike McCauley
- Click Install button to install 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
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
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
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
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
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.