Control a Stepper Motor using an Arduino and a Rotary Encoder

//Control a Stepper Motor using an Arduino and a Rotary Encoder

Controlling a Stepper Motor using an Arduino and a Rotary Encoder


Welcome to this multi part tutorial on how to control different stepper motors using a rotary encoder.

In this first part we will use the inexpensive and popular stepper motor that comes with it’s own control board. The 28BYJ-48 stepper motor with the ULN2003 board.

The 28BYJ-48 motor is not very fast or very strong, but it’s great for beginners to start experimenting with controlling a stepper motor with an Arduino.

We will write some code to have the motor move in the direction that we turn the rotary encoder, and will also keep track of how much steps we have taken, so that we can have the motor move back to the starting position by pressing down on the rotary encoder switch.

In the next tutorial we will use a Nema 17 stepper motor and the EasyDriver board.


We are using 4 pins to control the Stepper and 3 pins for the rotary encoder module.

Pin 8-11 are controlling the Stepper motor and pin 2-4 are receiving information from the rotary encoder.

We connect the 5V and Ground from to UNO to the rotary encoder and as a precaution, use a breadboard power supply to power the Stepper motor since it can use more power that the UNO can provide.

We also connect the UNO Ground to the breadboard to serve as a reference.


We are using the “Stepper” library to control the stepper motor.

The “Stepper” library is included by default with the Arduino Software install.

We are using some variables to store the current position, since we want to keep track of the position of the stepper motor so we can make it move back to the starting position.

We also included some error checking code to make sure that the rotary encoder is not missing steps, since that would make our motor position inaccurate.

As always, please check out the tutorial video to have more information.

#include "Stepper.h"
#define STEPS  32   // Number of steps for one revolution of Internal shaft
                    // 2048 steps for one revolution of External shaft

volatile boolean TurnDetected;  // need volatile for Interrupts
volatile boolean rotationdirection;  // CW or CCW rotation

const int PinCLK=2;   // Generating interrupts using CLK signal
const int PinDT=3;    // Reading DT signal
const int PinSW=4;    // Reading Push Button switch

int RotaryPosition=0;    // To store Stepper Motor Position

int PrevPosition;     // Previous Rotary position Value to check accuracy
int StepsToTake;      // How much to move Stepper

// Setup of proper sequencing for Motor Driver Pins
// In1, In2, In3, In4 in the sequence 1-3-2-4
Stepper small_stepper(STEPS, 8, 10, 9, 11);

// Interrupt routine runs if CLK goes from HIGH to LOW
void isr ()  {
  delay(4);  // delay for Debouncing
  if (digitalRead(PinCLK))
    rotationdirection= digitalRead(PinDT);
    rotationdirection= !digitalRead(PinDT);
  TurnDetected = true;

void setup ()  {
digitalWrite(PinSW, HIGH); // Pull-Up resistor for switch
attachInterrupt (0,isr,FALLING); // interrupt 0 always connected to pin 2 on Arduino UNO

void loop ()  {
  small_stepper.setSpeed(600); //Max seems to be 700
  if (!(digitalRead(PinSW))) {   // check if button is pressed
    if (RotaryPosition == 0) {  // check if button was already pressed
    } else {
        RotaryPosition=0; // Reset position to ZERO

  // Runs if rotation was detected
  if (TurnDetected)  {
    PrevPosition = RotaryPosition; // Save previous position in variable
    if (rotationdirection) {
      RotaryPosition=RotaryPosition-1;} // decrase Position by 1
    else {
      RotaryPosition=RotaryPosition+1;} // increase Position by 1

    TurnDetected = false;  // do NOT repeat IF loop until new rotation detected

    // Which direction to move Stepper motor
    if ((PrevPosition + 1) == RotaryPosition) { // Move motor CW

    if ((RotaryPosition + 1) == PrevPosition) { // Move motor CCW

Copy the above Sketch code in your Arduino IDE software to program your Arduino.

We are using the “Stepper” library which is included by default with the Arduino IDE Software install.

By | 2018-02-23T17:13:37-05:00 August 26th, 2015|Tutorials|


  1. Daniel January 13, 2020 at 1:16 pm - Reply

    I can NOT find the code for using Rotary encoder for stepper motor part 2

  2. Jamo December 6, 2019 at 11:41 am - Reply

    Please can you put up the part for the rotary encoder? I would like to use it on Fritzing with another project?!

  3. Bastiaan Nolmans May 14, 2018 at 3:30 pm - Reply

    do you power the motor and the arduino apart from each other ?

    gr bas

  4. Rob April 20, 2018 at 8:14 am - Reply


    I am using the same setup except: I don’t have a breadboard.
    can I hook Up my stepper motor and rotary encodor in a way without using the breadboard but plug everything in into the Arduino UNO?

Leave A Comment