Stepper Motor Control with Arduino and Joystick

This post shows how to control stepper motor speed and direction of rotation using Arduino UNO board and PS2 joystick.
The stepper motor used in this example is 28BYJ-48 (unipolar stepper motor) which usually comes with its driver board.
In the last Arduino project I build a simple controller for this stepper motor, project link is below:
Arduino Unipolar Stepper Motor Control

The joystick (PS2 Joystick) consists of two potentiometers of 10k ohm (one for the X-axis and the other for the Y-axis) and a push-button.

Arduino stepper motor joystick control

Hardware Required:

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

Stepper motor control with Arduino and joystick circuit:
Project circuit schematic diagram is shown below (all grounded terminals are connected together).

Arduino stepper motor control with joystick circuit

and the following image shows fritzing circuit:

Stepper motor control with Arduino and joystick

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 follows:
IN1 to Arduino pin 11
IN2 to Arduino pin 10
IN3 to Arduino pin 9
IN4 to Arduino pin 8

The joystick board has 5 pins: GND, +5V, VRX, VRY and SW where:
GND and +5V are power supply pins
VRX is the X-axis potentiometer output
VRY is the Y-axis potentiometer output
SW is the push button terminal (the other terminal is connected to GND).
The output of the X-axis potentiometer (VRX) is connected to Arduino analog pin A0, Y-axis potentiometer output (VRY) can also be used. The switch pin (SW) is not used in this example.
Using the X-axis and the Y-axis potentiometers we can control two stepper motors independently.

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 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 are defined as:

The joystick potentiometer output pin is connected to Arduino analog pin 0, it’s defined in the code as:

When the joystick released, the potentiometer is in middle position and its output voltage is approximately 2.5V, reading it with Arduino should give a digital value of about 511( Arduino UNO ADC module resolution is 10-bit).

In the code I made 3 intervals:
The first one between 500 and 523: in this interval the joystick is released and the stepper motor is not moving at all (all outputs are low).

The second interval when digital value is greater than or equal to 523 where the motor moves in the first direction.

The last interval when the digital value is less than or equal to 500 where the motor moves in the other direction.

The speed of the motor is always between 5 and 500 RPM. Moving the joystick to maximum (both directions) causes the motor to move in maximum speed.

Rest of code is described through comments.

The following video shows a simple hardware circuit of the project:


Discover more from Simple Circuit

Subscribe to get the latest posts sent to your email.

19 thoughts on “Stepper Motor Control with Arduino and Joystick”

  1. Hi, is it possible to create a limit for the stepper motors. For example, it can only turn 180° clockwise and 180° counterclockwise.

  2. Hello, I’ve been following your projects for quite sometime, and me as a newbie, is really a great help. Just a clarification, in your video, you have a capacitor in the board, but in your illustration there’s none. Can you help me clear this out? Thanks a lot.

    1. The capacitor is helpful in reducing surge current on the power supply, it’s not necessary for the circuit to work if your supply current is sufficent.

    1. If your motor supports that then yes you can do that just modify the map function (lines: 51 & 65), replace the last 500 by your max speed value!

  3. My motors still spin very slightly when its at LOW I also tried 0. any ideas why my motors are still spinning even when the joystick is in the middle position.

Leave a Comment

Your email address will not be published. Required fields are marked *

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

Scroll to Top