top of page

Safari browser sometimes has issues displaying images...

I.e: *you have to click on the images to see them...

For a better browsing experience on Brainy-Bits

Please use Chrome, Edge or Firefox browser.

  • Writer's pictureBrainy-Bits

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



Breadboard Power Supply

Rotary Encoder Module

Stepper Motor

These are Amazon affiliate links...

They don't cost you anything and it helps me keep the lights on

if you buy something on Amazon. Thank you!



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.


Recent Posts

See All


All my content is and will always be Free.

If you feel that my Videos / Tutorials are helping, and you would like to contribute...

 You can toss some coins in the Tip Jar via PayPal.

Select amount then click the “Donate” button.

bottom of page