Arduino Nano - Stepper Motor

This tutorial instructs you how to use Arduino Nano to control Stepper Motor using DRV8825 Driver. In detail, we will learn:

The tutorial is applicable to all types of bipolar stepper motors with four wires. It will use a NEMA 17 stepper motor as an example.

Arduino Nano controls a NEMA 17 stepper motor with the DRV8825 driver

If you want control another type of stepper motor, please check out this Arduino Nano - 28BYJ-48 Stepper Motor tutorial.

Hardware Preparation

1×Official Arduino Nano
1×Alternatively, DIYables Nano V3.0 Development Board
1×USB A to Mini-B USB cable
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

Two common types of stepper motors exist:

  • bipolar: this motor has 4 wires
  • unipolar: this motor has either 5 or 6 wires.

For a 6-wire unipolar stepper motor, we can utilize four of the six wires and control it as if it were a bipolar stepper motor.

For a 5-wire unipolar stepper motor, check out the tutorial Arduino Nano - control 28BYJ-48 stepper motor using ULN2003 driver for more information.

The emphasis of this tutorial is solely on the bipolar stepper motor.

Bipolar Stepper Motor pinout

The bipolar Stepper Motor has four pins, and the names of these pins vary depending on the manufacturer. The following table provides some of the most commonly used names:

PIN NO Naming 1 Naming 2 Naming 3
1 A+ A A
2 A- A C
3 B+ B B
4 B- B D
Bipolar Stepper Motor pinout

The arrangement of pins, the labels assigned to the wires, and the colors of the wires can differ among manufacturers. You must check the datasheet or manual to find out how the wire color corresponds to the pin name. The image above also displays the specifications of two distinct motors with different wire labeling and coloring.

Steps per Revolution

The specification of the motor specifies the DEG_PER_STEP value. Depending on the control technique, STEP_PER_REVOLUTION can be determined from the following table:

Control method Steps per Revolution Real degree per step
Full-step STEP_PER_REVOLUTION = 360 / DEG_PER_STEP DEG_PER_STEP
Half-step STEP_PER_REVOLUTION = (360 / DEG_PER_STEP) * 2 DEG_PER_STEP / 2
Micro-step (1/n) STEP_PER_REVOLUTION = (360 / DEG_PER_STEP) * n DEG_PER_STEP / n

For instance, if the motor's datasheet states a 1.8 degree/step measurement:

Control method Steps per Revolution Real degree per step
Full-step 200 steps/revolution 1.8°
Half-step 400 steps/revolution 0.9°
Micro-step (1/n) (200 * n) steps/revolution (1.8 / n)°

How to control a stepper motor using Arduino Nano

Arduino Nano can generate signals to control the stepper motor, however these signals do not have the sufficient voltage and/or current that the stepper motor requires. Therefore, a hardware driver is needed between Arduino Nano and the stepper motor. This driver has two functions:

  • To amplify the control signals from Arduino Nano in terms of current and voltage
  • To protect Arduino Nano from the high current and voltage used to power the stepper motor.

There are numerous types of hardware drivers that can be used to manage stepper motors. In this tutorial, we use the DRV8825 driver. It replaced the L298N because it limits the coil current, so the motor and the driver stay cooler.

Overview of DRV8825 Driver

The small DRV8825 driver module has one task: to run one bipolar stepper motor, like a NEMA 17. The Arduino Nano tells it only two things: when to take a step (STEP pin) and which way to turn (DIR pin). The driver does all the coil switching by itself.

Main facts:

  • Motor power: 8.2V to 45V. We use a 12V adapter.
  • Current: about 1.5A per coil (2.5A peak), and a tiny knob on the module lets you change the current limit.
  • Microstepping: from full step down to 1/32 step, for smooth and quiet movement.
  • Logic: works with 3.3V and 5V boards, so the Arduino Nano pins drive it directly.
  • Protection: over-current, over-temperature and under-voltage.

DRV8825 Driver Pinout

DRV8825 Module Pinout

The DRV8825 module has 16 pins. To keep the wiring simple, we plug it into the DIYables expansion board for DRV8825. The expansion board gives easy connectors:

  • VIN / GND screw terminal: the 12V power for the motor. A 100uF capacitor on the board keeps this supply steady.
  • 4-pin XH plug (2B, 2A, 1A, 1B): connects to the four wires of the stepper motor.
  • G V S header: one row for each of EN, STEP and DIR. The S pin is the signal, G is ground.
  • 3-way DIP switch: sets the microstep pins (MS1, MS2, MS3). All OFF = full step = 200 steps per revolution for a 1.8° motor.
  • RST and SLP need no wires: a trace on the expansion board ties them together and wakes the driver.

We need only two pins of Arduino Nano: one for STEP and one for DIR. The EN pin is left free, which keeps the driver enabled. The V pins are only for the A4988 driver, so they are not used here.

※ NOTE THAT:

Set the current limit before you run the motor for a long time. Begin at a low setting and raise it step by step while you watch how the motor runs. More than about 1.5A makes the DRV8825 run hot.

Wiring Diagram

The wiring diagram between Arduino Nano and Stepper Motor DRV8825 Driver

This image is created using Fritzing. Click to enlarge image

※ NOTE THAT:

  • Full step is used here, so slide every DIP switch on the expansion board (all three) to OFF.
  • The pin arrangement on stepper motors can differ between manufacturers. Please refer to the table below for the correct wiring.

Wiring table between Arduino Nano and DRV8825 Driver

Arduino Nano pins DRV8825 expansion board pins
D4 S pin of STEP
D3 S pin of DIR
GND one G pin

The EN row and the V pins stay unconnected. The 12V adapter goes to the VIN and GND screw terminal of the expansion board through the DC power jack.

The wires also fit a Screw Terminal Expansion Board for Arduino Nano. Plug the Nano into it and fasten the three control wires with screws. The picture shows this setup:

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

This image is created using Fritzing. Click to enlarge image

Wiring table between DRV8825 Driver and Stepper motor

Important!: Do not pay attention to the order of the wires in the stepper motor in the diagram above. It is just an illustration. The arrangement of the pins on stepper motors may differ between manufacturers. Make sure that your wiring follows the table below. One coil goes to 1A and 1B, the other coil goes to 2A and 2B.

DRV8825 pins Stepper motor pins Or Or
1A A+ A A
1B A- A C
2A B+ B B
2B B- B D

Before purchasing a stepper motor, it is recommended to review the datasheet, specification, or manual of the stepper motor. Ensure that the information contains the mapping between the pin's color and name. For instance, this stepper motor supplies the mapping as seen in the following image:

Stepper Motor coil color mapping

Using the mapping, the wiring table is:

DRV8825 pins stepper motor pins wire color
1A A black wire
1B C green wire
2A B red wire
2B D blue wire

※ NOTE THAT:

To make the motor turn the other way, swap the two wires of one coil (for example, swap the wires on 1A and 1B). Consequently, an increase of the position may then turn the motor anticlockwise instead of clockwise.

Wiring without the Expansion Board

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

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

This image is created using Fritzing. Click to enlarge image

The same connections, made on the screw terminal shield:

The wiring diagram between Arduino Nano and Stepper Motor DRV8825 Module without Expansion Board Screw Terminal

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:

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

How to control a Stepper motor using a DRV8825 driver.

Controlling a stepper motor can be a challenging endeavor, particularly when we need to do so without blocking. Fortunately, the AccelStepper library makes it a breeze.

The Arduino IDE contains an integrated Stepper library. Nevertheless, we do not suggest you utilize this library due to the following reasons:

  • The library offers a blocking feature. This implies that Arduino Nano is prevented from executing other tasks while it is controlling the stepper motor.
  • It does not possess adequate functions.

Instead, we suggest using the AccelStepper library. This library offers:

  • Acceleration
  • Deceleration
  • DRIVER mode, which talks to a STEP/DIR driver like our DRV8825
  • The capacity to control multiple steppers simultaneously, with individual concurrent stepping for each stepper
  • Note: the microstep size is not set in the code. The DIP switches on the expansion board set it.

The stepper object is created in DRIVER mode, with the STEP pin first and the DIR pin second:

AccelStepper stepper(AccelStepper::DRIVER, STEP_PIN, DIR_PIN);

How To Control the Position of Stepper Motor via DRV8825 Driver

We can achieve our goal of moving the stepper motor to the desired position by utilizing:

stepper.moveTo(desiredPosition); // move motor one revolution

※ NOTE THAT:

The stepper.moveTo() function is non-blocking, which is a great advantage of the library. However, when using this function, there are some points to pay attention to:

  • Make sure to call 'stepper.run()' as often as possible, preferably in the void loop() function.
  • Avoid using the delay() function while the motor is moving.
  • Do not use Serial.print() and Serial.println() functions while the motor is running, as these will slow down the stepper motor.

How To Control the Speed of Stepper Motor via DRV8825 Driver

We can regulate not only the speed, but also the acceleration and deceleration, through the use of some basic functions.

stepper.setMaxSpeed(200.0); // set the maximum speed (steps per second) stepper.setAcceleration(50.0); // set acceleration/deceleration stepper.setSpeed(200); // set desired speed

How To Control the Direction of Stepper Motor via DRV8825 Driver

The DRV8825 reads the DIR pin before each step. AccelStepper sets this pin for you. If you wire the motor as indicated, it will rotate in:

  • Clockwise direction: when controlling the motor from a lower to a higher position (Position increment)
  • Anticlockwise direction: when controlling the motor from a higher to a lower position (Position decrement)

For examples:

  • When the current position is 100 and the motor is directed to 200, it rotates in a clockwise direction
  • If the current position is -200 and the motor is controlled to -100, it rotates in a clockwise direction
  • If the current position is 200 and the motor is regulated to 100, it rotates in an anticlockwise direction
  • If the current position is -100 and the motor is managed to -200, it rotates in an anticlockwise direction

※ NOTE THAT:

As previously stated, if you swap the two wires of one coil (1A with 1B, or 2A with 2B), the increase of the position could be counterclockwise and the decrease of the position could be clockwise.

How To Stop Stepper Motor

  • The stepper motor will halt when it reaches the desired position.
  • It is also possible to make it stop immediately by using the stepper.stop() function.

Arduino Nano Code - Stepper Motor Code

The following code:

  • Makes the motor rotate one revolution in a clockwise direction
  • Pauses the motor for 5 seconds
  • Causes the motor to rotate one revolution in an anticlockwise direction
  • Halts the motor for 5 seconds
  • Repeats the process over and over
/* * 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 */ #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 DEG_PER_STEP 1.8 #define STEP_PER_REVOLUTION (360 / DEG_PER_STEP) // 200 steps in full-step mode (all DIP switches OFF) // DRIVER mode: one pin for STEP, one pin for DIR AccelStepper stepper(AccelStepper::DRIVER, STEP_PIN, DIR_PIN); long moveToPosition = STEP_PER_REVOLUTION; void setup() { Serial.begin(9600); stepper.setMaxSpeed(200.0); // set the maximum speed (steps per second) stepper.setAcceleration(200.0); // set acceleration stepper.setSpeed(200); // set initial speed stepper.setCurrentPosition(0); // set position to 0 stepper.moveTo(STEP_PER_REVOLUTION); // move motor one revolution, in clockwise direction Serial.println("Motor moving in clockwise direction"); } void loop() { if (stepper.distanceToGo() == 0) { Serial.println("Motor is stopped"); delay(5000); // stop 5 seconds stepper.setCurrentPosition(0); // reset position to 0 moveToPosition = -1 * moveToPosition; // reverse direction stepper.moveTo(moveToPosition); // move motor one revolution if (stepper.distanceToGo() > 0) Serial.println("Motor moving in clockwise direction"); else if (stepper.distanceToGo() < 0) Serial.println("Motor moving in anticlockwise direction"); } stepper.run(); // MUST be called as frequently as possible }

Detailed Instructions

  • Do the wiring as in the diagram. Set the three DIP switches to OFF.
  • Click to the Libraries icon on the left bar of the Arduino IDE.
  • Search for “AccelStepper” and locate the AccelStepper library created by Mike McCauley.
  • Press the Install button to complete the installation of the AccelStepper library.
Arduino Nano AccelStepper library
  • Copy the code and open it in the Arduino IDE.
  • Click the Upload button to upload the code to the Arduino Nano.
  • Plug the 12V adapter into the DC power jack.
  • The stepper motor will rotate one revolution in the clockwise direction, pause for 5 seconds, rotate back one revolution in the counterclockwise direction, pause for 5 seconds, and repeat this process.
  • Check out the result in the Serial Monitor.
∞
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
Motor moving in clockwise direction Motor is stopped Motor moving in anticlockwise direction Motor is stopped Motor moving in clockwise direction Motor is stopped Motor moving in anticlockwise direction Motor is stopped Motor moving in clockwise direction Motor is stopped Motor moving in anticlockwise direction Motor is stopped
Ln 11, Col 1
Arduino Nano on COM15
2

If the motor buzzes or skips steps, turn the current-limit knob on the DRV8825 up a little.

Code Explanation

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

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!