CD ROM Stepper motor Arduino L298n + Joystick controlled speed and Direction Control

(Last Updated On: June 27, 2020)

stepper motor

CD ROM Stepper Motor with Arduino Description:

In this tutorial, you will learn how to control the speed and direction of the CD ROM stepper motor using 2 axis joystick, L298N motor driver, and Arduino. The stepper motor control system can be activated and deactivated using the joystick push button. The same program and circuit connections can be used for the other types of stepper motors.

For the extreme basics, you can watch my getting started tutorials on the bipolar and unipolar stepper motors, 2 Axis Joystick and L298n motor driver. So I highly recommend first you should watch these tutorials and then you can resume from here. The Tutorials links are given below.


UniPolar and Bipolar Stepper Motors Basics:

2 axis joystick getting started Tutorial:

L298n getting started tutorial:

Stepper motors are used worldwide in industries, robots, toys, printers, CNC machines, 3D printers, etc. Recently I posted new articles on the bipolar steppers motors. In these tutorials, I have explained how to control the Stepper motors using the CNC shield, and how to control Stepper Motors using the IoT “internet of things” technology.


The components and tools used in this project can be purchased from Amazon, the components Purchase links are given below:

Arduino Uno:
Mega 2560:
L298N motor driver:
Joystick:
12v UniPolar stepper motor:
CD ROM bipolar stepper motor:
24BYJ48 5V DC unipolar stepper motor:

*Please Note: These are affiliate links. I may make a commission if you buy the components through these links. I would appreciate your support in this way!

CD ROM Stepper Motor with Arduino Circuit Diagram:

stepper motor

Regardless of the type and size of the stepper motor the basic working principle of all the stepper motors is exactly the same. The circuit diagram as you can see on the screen will remain the same for all types of the stepper motors. As the stepper motor, I am using has current requirement less than 4amps so that’s why I decided to use the L298N motor driver. The L298N motor driver is basically designed for controlling simple dc motors; in my previous tutorials, I used this motor driver for controlling the Robot.

As the L298N motor driver has the ability to control 2 dc motors at the same, and as you know a stepper motor has 4 wires, so it means we can use the L298N motor driver to control a Unipolar or bipolar stepper motor. You might be thinking what about the Unipolar motors as they have 6 wires. Well, the common wires are not connected. So the Unipolar stepper motor connection with the l298n motor driver is exactly the same as the bipolar stepper motor.


As you can see in the circuit diagram the four wires of the stepper motor are connected with the out1, out2, out3, and out4. While the input pins of the l298N motor driver are connected with Arduino’s Pin number 8, 9, 10 and 11.

The enables pins of the l298n motor driver by default comes with the jumper caps, so make sure you remove the jumper caps and connect the enable pins with the Arduino’s pin number 6 and pin number 7.


J1 is the DC female power jack, this is where we connect the external 12v or 5v power supply and finally connect the VCC of the L298N motor driver with the Arduino’s 5volts.

The VRx and VRy pins of the joystick are connected with the analog pins A0 and A1. The Switch is connected with the Arduino’s pin number 4. The VCC pin of the joystick is connected with the Arduino’s pin number 5, while the ground is connected with the Arduino’s Ground.

Let me tell you once again if you are using stepper motors with current ratings greater than 4amps then never use the l298N motor driver. Now let’s have a look at the pinout of the L298N Motor Driver.


CD ROM Stepper Motor Arduino Programming:

Before, you start the programming, first of all, make sure you download the Stepper library.

Watch Video Tutorial:

CD ROM Stepper Motor Arduino Code Explanation:

As usual, i started off by adding the Stepper.h header file.

#include <Stepper.h>

Defined two pins 6 and 7 for the ENA and ENB pins of the motor controller shield.

int ENA = 6;
int ENB = 7;

int joystick_power = 5; // connect the vcc pin of the joysick
// with pin number 5 of the arduino.

The VRx and VRy pins of the 2-axis Joystick are connected with the Arduino’s analog pins A0 and A1
int vrx = A0;
int vry = A1;

I defined, two variables vrx_data and vry_data of the type integer for storing the values coming from the Joystick.
int vrx_data = 0;
int vry_data = 0;

As you know the type of the Joystick I am using also comes with a Pushbutton, I connected the Joystick pushbutton with the Arduino’s pin number 4, defined some flags state, and switch_flage.
int joystick_switch = 4;
boolean state = false;
int switch_flag = 0;
int stepper_motor_led = 13;

int steps = 4; // you can set to different values, 4, 8, 12,16,20,24,28 and 32
Stepper myStepper(steps, 8, 9, 10, 11);
Inside the setup() function, I set the speed to 100, activated the serial communication and selected 9600 as the baud rate. Initially I set the ENA and ENB to low.
void setup() {

myStepper.setSpeed(100);
Serial.begin(9600);
digitalWrite(ENA , LOW);
digitalWrite(ENB , LOW);

using the pinMode() function I set the pins as input and output as per the requirement.

pinMode(joystick_power , OUTPUT);
digitalWrite(joystick_power, HIGH);
pinMode(vrx , INPUT);
pinMode(vry, INPUT);
pinMode(joystick_switch , INPUT_PULLUP);
pinMode(stepper_motor_led, OUTPUT);
digitalWrite(stepper_motor_led, LOW);
}

void loop() {

The following condition is used to check if the Joystick Pushbutton has been pressed. If the Joystick button is pressed then change the status of the state flag, write on the Serial monitor that the button is pressed, and then a delay of 1 second, as 1000 milliseconds are equal to 1 second.

if ( digitalRead(joystick_switch) == LOW)
{
state = !state;
Serial.println(“button pressed”);
delay(1000);
}

The following checks, if the state is false and also if the switch_flag is qual to Zero then completely turn off the Stepper Motor and change the switch_flag from 0 to 1.

if( (state == false)&& (switch_flag == 0) )
{
digitalWrite(ENA , LOW);
digitalWrite(ENB , LOW);
Serial.println(“Stepper motor is not active”);
digitalWrite(stepper_motor_led, LOW);
switch_flag = 1;
}

Now, if the state is true and the switch_flag is equal 1 then High the ENA and ENB pins of the Motor board, On serial monitor write that the Stepper motor is Active, and finally, change the switch_flag status back to 0.

if( (state == true) && (switch_flag == 1) )
{
digitalWrite(ENA , HIGH);
digitalWrite(ENB , HIGH);
Serial.println(“Stepper motor is ActivE”);
digitalWrite(stepper_motor_led, HIGH);
switch_flag = 0;
}

if the state flag is true then simply call the joystick() function.
if ( state == true)
{
joystick();
}

}

The joystick() function is a user-defined function, it has no return type and does not take any arguments as the inputs.  The purpose of this function is to ready the VRx and Vry pins of the Joystick and store the values in variables vrx_data and vry_data. For now, i am using only the VRx pins and the variable vrx_data.

The rest of the instructions inside the Joystick() function are pretty straightforward.  We simply set the Stepper motor speed and the steps. You can try different steps to see the effect.

void joystick()
{
vrx_data = analogRead(vrx);
Serial.print(“Vrx:”);
Serial.println(vrx_data);

// to stop the stepper motor
if ( (vrx_data > 490) && (vrx_data < 510) )
{

digitalWrite(ENA , LOW);
digitalWrite(ENB , LOW);
Serial.println(“Stepper Motor Stopped”);

}

if ( (vrx_data > 510) && (vrx_data < 1000) )
{
digitalWrite(ENA , HIGH);
digitalWrite(ENB , HIGH);

myStepper.setSpeed(20);
myStepper.step(+1);
}

if ( vrx_data > 1000 )
{
digitalWrite(ENA , HIGH);
digitalWrite(ENB , HIGH);
myStepper.setSpeed(700);
myStepper.step(+1);
}

// for reverse movement

if ( (vrx_data >= 0) && (vrx_data < 20) )
{
digitalWrite(ENA , HIGH);
digitalWrite(ENB , HIGH);
myStepper.setSpeed(700);
myStepper.step(-1);
}

if ( (vrx_data > 20) && (vrx_data < 490) )
{
digitalWrite(ENA , HIGH);
digitalWrite(ENB , HIGH);
myStepper.setSpeed(50);
myStepper.step(-1);
}

}

Other Stepper Motor Related Tutorial:

UniPolar and Bipolar Stepper Motors Speed and Position Control

 

 

Recommended For You

About the Author: Engr Fahad

My name is Shahzada Fahad and I am an Electrical Engineer. I have been doing Job in UAE as a site engineer in an Electrical Construction Company. Currently, I am running my own YouTube channel "Electronic Clinic", and managing this Website. My Hobbies are * Watching Movies * Music * Martial Arts * Photography * Travelling * Make Sketches and so on...

Leave a Reply