Arduino Stepper motor control with rotary encoder

This Arduino project shows how to control unipolar stepper motor using Arduino UNO board and rotary encoder module.
The stepper motor used in this example is 28BYJ-48 (5V unipolar stepper motor) which usually comes with its driver board.
To see how to easily control stepper motor with Arduino, visit this post:
Arduino Unipolar Stepper Motor Control

In this project I used the rotary encoder shown below:

rotary encoder

The rotary encoder module has 5 pins: GND, + (+5V or 3.3V), SW (push button), DT (pin B) and CLK (pin A).
As an addition to the rotary encoder there is a push button and three pull up resistors for pins SW, DT and CLK of 10k ohm. With the three pull-up resistors, the normal state of each terminal is logic high.
The rotary encoder generates (when rotating) two square waves on pins A (CLK) and B (DT) with 90° out of phase as shown in the figure below:

rotary encoder output waveform

Since the normal state of pin A (CLK) and pin B (DT) are logic high we’ve to detect falling (transition from high to low) of one of them, here pin A is used to detect the movement of the rotary encoder in both directions (falling of pin A signal). Direction of rotation can be detected by knowing the status of pin B, if pin B is logic high this means the direction of rotation is clockwise (CW), and if pin B is logic low this means the direction of rotation is counter clockwise (CCW).

Hardware Required:

  • Arduino UNO board
  • 28BYJ-48 stepper motor (with ULN2003A driver board)
  • Rotary encoder module
  • 5V power source
  • Bread board
  • Jumper wires

Stepper motor control with Arduino and rotary encoder circuit:
Project circuit schematic diagram is shown below.

Arduino stepper motor rotary encoder control

The stepper motor is connected to the ULN2003A board which is supplied with external power source of 5V. The control lines (IN1, IN2, IN3 and IN4) of this board are connected to the Arduino as:
IN1 to Arduino pin 11,
IN2 to Arduino pin 10,
IN3 to Arduino pin 9,
IN4 to Arduino pin 8.

The rotary encoder module has 5 pins: GND, + , SW, DT (pin B or data pin) and CLK (pin A or clock pin) where:
GND is connected to Arduino GND pin,
+ is connected to Arduino 5V pin,
SW is push button pin, not used in this example,
DT is connected to Arduino analog pin 5 (A5),
CLK is connected to Arduino analog pin 4 (A4).

Stepper motor control with Arduino and joystick code:
In this example I used Arduino stepper motor library (built-in) which simplifies the code, it’s included in the code using the following line:

The stepper motor which I used in this project is 28BYJ-48, this motor is equipped with speed reducer of 1/64. The internal motor has 32 steps per one revolution which means the external shaft has 2048 steps per one revolution (64 x 32). Number of steps is defined in the code as shown below:

and the connection of the control lines of the stepper motor with the Arduino are defined as:

The rotary encoder pin A (CLK) and pin B (DT) are connected to Arduino UNO pins A4 and A5 respectively. Both pins can be used to interrupt the Arduino microcontroller (ATmega328P) whenever there is a change in the state of at least one pin. The following lines are used to enable interrupt-on-change for pins A4 (PCINT12) and A5 (PCINT13):

Full Arduino code:

The video below shows my simple hardware circuit test:


  1. This is pointless,
    if you grab encoder and turn the knob the stepper turns, why?
    if you grab the shaft of the encoder and turn it you don’t need the arduino or the encoder or the stepper controller
    and it does the exact same thing!

    if you write some code where the encoder sets the rpm of the stepper and outputs the rpm to an oled and can change the rpm in increments of one rpm++ 1 or rpm–1 per pulse of the encoder then you have some usefull code for a project.

  2. @OGP Pointless???

    Attach the rotary encoder to a steering wheel and the stepper to a boat engine and you have fly by wire steering, same if you add it to an accelerator pedal.

    Put the encoder in your radio room and the use the stepper to control your antenna direction for ham radio or use the encoder to provide feedback from the antenna and the stepper to control a direction pointer for positional feedback.

    I can think of a lot of applications for positional feedback.

    1. I would like to use something similar to controll selectable 3x step/direction output for stepper drivers with 3 speeds for cnc jog pendant , any help would be appreciated

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.