Controlling stepper motor with Joystick Sensor and Arduino

Updated: Mar 17

A stepper motor is a brushless synchronous electric motor that divides a complete rotation into steps of equal length. The motor position can be controlled precisely without any feedback mechanism, as long as the motor is designed as per the requirement and application. Based on the number of steps per rotation, different types of stepper motors are available. They are usually found in 3D-printers, desktop printers, CNC milling machines, and any other application that requires accurate positioning control.


It converts a chain of input pulses into a precisely defined increment in the mechanical shaft position, where each pulse moves the shaft by a fixed angle, which makes the shaft rotate in series of discrete angular steps.

Step Angle = 360/(Stator Phases * Rotor Teeth) degrees


Types of stepper motors:

  • variable Reluctance Stepper Motor

  • Permanent Magnet Stepper Motor

  • Hybrid Stepper Motor

Hardware Requirements:


1. Breadboard:


Breadboard
Breadboard

A #breadboard is a flat piece of wood that is provided with holes that allow users to build a circuit including the components and connections without the need for soldering due to which it is reusable and finds its best usage for the students who are new to build circuits. The holes in any horizontal line will be in series whereas the vertical holes remain at an equipotential state. There are different sizes of breadboards available in the market which include "full size", "half-size" and, "mini size" breadboards.

You can purchase breadboard here.


2 .ULN2003A- Driver Board:

ULN2003A - Driver Board
ULN2003A - Driver Board

ULN2003A is a 16 pin integrated circuit. It has seven Darlington transistors installed which would aid to control 7-loads at a time. The voltage and current limits for any load are 50V and 500mA. ULN2003 is provided with internal safety protection from back EMF. The Driver Circuit consists of 7-input and 7-output pins and 1-common and 1-ground pin.

You can purchase breadboard here.


3. Arduino UNO: The Arduino is a Programmable #microcontroller, it's an open-source electronics proto-type platform. It interacts with the user by receiving input with the aid of sensors and provides the output employing LED, buzzer, etc.,

NOTE: To know more about Arduino UNO, go through this article.

You can purchase Arduino UNO here.


4.28BYJ48 Stepper Motor: The 28BYJ48 is a 5-wire unipolar stepper motor that runs on 5 volts. The motor can be positioned accurately, each high pulse given as an input, energize the coil and attracts the nearest teeth of the cogged wheel, and drives the motor one step ahead. The Stepper motor is connected to a driver which converts the command pulses into actual motor driving steps. The stepper motor consists of 5-leads in which 4-unipolar coil leads and one supply lead (5V).


28BYJ48 -Stepper Motor
28BYJ48- Stepper Motor

You can purchase a Stepper motor here.


5. Jumping wires: Jumper wires are used to establish connections between different components in the circuit.

Jumping wires
Jumping wires

You can purchase Jumping wires here.


6. Joystick Sensor Module: Joystick Sensor has two individual potentiometers one for each x and y axes to read the user's input. It is equipped with a freewheeling holder, that allows changing the knob of the potentiometer which changes resistance and inturn the analog output.


The potentiometers are connected between Vcc and Ground. It is used to control the pointer movement in two dimensions and is sometimes also called a Control Stick.

Joystick Sensor Module
Joystick Sensor Module

You can purchase the Joystick sensor here.


Circuit Connection:

Circuit Diagram
Circuit Diagram

Circuit connection involves the following steps:

  • First, we connect the Stepper Motor to Arduino through the driver board.

  • The pins IN1 to IN4 of the driver board are connected to 5-8 PWM pins of Arduino.

  • The X-axis pin of the Joystick is connected to Ao pin of the Arduino.

  • We provide a power supply to the Joystick by connecting 5V to the Vcc and ground to the ground pin.

  • Similarly, with the driver, we connect the positive terminal to 5V and the negative terminal to ground of the Arduino.

Code Description:


Let us learn the code to control the stepper motor with the joystick.


Pin numbers to which driver has been connected are initialized as integer type variables and a variable x of integer type is also initialized.

int s1 = 5;
int s2 = 6;
int s3 = 7;
int s4 = 8;
int x;

Pins s1, s2, s3, s4 are set as output pins and Ao (joystick) is set as X-axis input pin. we initiated the serial communication at the baud rate of 9600.

void setup() {
  pinMode(s1, OUTPUT);
  pinMode(s2, OUTPUT);
  pinMode(s3, OUTPUT);
  pinMode(s4, OUTPUT);
  pinMode(A0, INPUT);
  Serial.begin(9600);
}

The variable x is used to read the analog value which is input by Ao of the arduino and we print these analog values using "serial.println".

void loop() {
  x = analogRead(A0);
  Serial.println(x);

If x<500 i.e., if we are moving the joystick in one particular direction, then s1 is set High and the remaining are set to low and vice versa. This way the stepper motor will rotate in the clockwise direction.

if(x<500)
  {
 digitalWrite(s1, HIGH);
 digitalWrite(s2, LOW);
 digitalWrite(s3, LOW);
 digitalWrite(s4, LOW);
 delay(5);
 digitalWrite(s1, LOW);
 digitalWrite(s2, HIGH);
 digitalWrite(s3, LOW);
 digitalWrite(s4, LOW);
 delay(5);
 digitalWrite(s1, LOW);
 digitalWrite(s2, LOW);
 digitalWrite(s3, HIGH);
 digitalWrite(s4, LOW);
 delay(5);
 digitalWrite(s1, LOW);
 digitalWrite(s2, LOW);
 digitalWrite(s3, LOW);
 digitalWrite(s4, HIGH);
 delay(5);
 }

If x>500 i.e., if we are moving the joystick in the opposite direction then the order will be in reverse order (first s4 is set high and others low) with a delay of 5 mS each.

else if(x>550)
 {
 digitalWrite(s1, LOW);
 digitalWrite(s2, LOW);
 digitalWrite(s3, LOW);
 digitalWrite(s4, HIGH);
 delay(5);
 digitalWrite(s1, LOW);
 digitalWrite(s2, LOW);
 digitalWrite(s3, HIGH);
 digitalWrite(s4, LOW);
 delay(5);
 digitalWrite(s1, LOW);
 digitalWrite(s2, HIGH);
 digitalWrite(s3, LOW);
 digitalWrite(s4, LOW);
 delay(5);
 digitalWrite(s1, HIGH);
 digitalWrite(s2, LOW);
 digitalWrite(s3, LOW);
 digitalWrite(s4, LOW);
 delay(5);
 }

Now if 500<x<550 which means the joystick is at rest position right at the center. In this case, all the pins are set to low.

else if(x > 500 && x < 550)
 {
 digitalWrite(s1, LOW);
 digitalWrite(s2, LOW);
 digitalWrite(s3, LOW);
 digitalWrite(s4, LOW);
 }
}

Complete Code:

int s1 = 5;
int s2 = 6;
int s3 = 7;
int s4 = 8;
int x;

void setup() {
  // put your setup code here, to run once:
  pinMode(s1, OUTPUT);
  pinMode(s2, OUTPUT);
  pinMode(s3, OUTPUT);
  pinMode(s4, OUTPUT);
  pinMode(A0, INPUT);
  Serial.begin(9600);

}

void loop() {
  // put your main code here, to run repeatedly:
  x = analogRead(A0);
  Serial.println(x);
  
  if(x<500)
  {
 digitalWrite(s1, HIGH);
 digitalWrite(s2, LOW);
 digitalWrite(s3, LOW);
 digitalWrite(s4, LOW);
 delay(5);
 digitalWrite(s1, LOW);
 digitalWrite(s2, HIGH);
 digitalWrite(s3, LOW);
 digitalWrite(s4, LOW);
 delay(5);
 digitalWrite(s1, LOW);
 digitalWrite(s2, LOW);
 digitalWrite(s3, HIGH);
 digitalWrite(s4, LOW);
 delay(5);
 digitalWrite(s1, LOW);
 digitalWrite(s2, LOW);
 digitalWrite(s3, LOW);
 digitalWrite(s4, HIGH);
 delay(5);
 }
 else if(x>550)
 {
 digitalWrite(s1, LOW);
 digitalWrite(s2, LOW);
 digitalWrite(s3, LOW);
 digitalWrite(s4, HIGH);
 delay(5);
 digitalWrite(s1, LOW);
 digitalWrite(s2, LOW);
 digitalWrite(s3, HIGH);
 digitalWrite(s4, LOW);
 delay(5);
 digitalWrite(s1, LOW);
 digitalWrite(s2, HIGH);
 digitalWrite(s3, LOW);
 digitalWrite(s4, LOW);
 delay(5);
 digitalWrite(s1, HIGH);
 digitalWrite(s2, LOW);
 digitalWrite(s3, LOW);
 digitalWrite(s4, LOW);
 delay(5);
 }
 else if(x > 500 && x < 550)
 {
 digitalWrite(s1, LOW);
 digitalWrite(s2, LOW);
 digitalWrite(s3, LOW);
 digitalWrite(s4, LOW);
 }
}

Download the complete code:

CODE
.txt
TXT • 1KB

Note: Please change the file format to .ino(Arduino IDE) format before you upload the code.


Working:

After uploading the code, you can control the motion of the stepper motor using the joystick. If you move the joystick in a particular direction, the motor starts to move and the values of x will be appearing on the serial monitor. If the direction of the joystick is changed, then the motor will rotate in the opposite direction. If the joystick is present in the center then there will not be any motion in the motor.

Watch this video for a practical explanation of the code and circuit connections.

Video By - Aishwarya Balaji


See also:

Interfacing Tilt Sensor with Arduino Uno

How to interface a DC motor with Arduino UNO using TinkerCAD?

Interfacing Soil/Moisture Sensor with Arduino