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.

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:

C

1

#include <Stepper.h>

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:

C

1

#define STEPS 32

and the connection of the control lines of the stepper motor are defined 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.