Overview
Learn how to control two types of motors using one Arduino and a joystick! In this tutorial, we will build an Arduino Joystick Motor Control. This is a simple project that utilizes the Servo Motor, Stepper Motor, Stepper Motor Driver Module, and XY Joystick found in the Arduino Kit Upgraded Starter Kit.
This tutorial explains how to use these devices with the Arduino Uno. Previous knowledge of making functions will be utilized.
Hardware Used
Software Used
- Arduino IDE
Application Discussion
Controlling motors is essential in small and large scale applications. These applications can include robotics, manufacturing, assembly, construction, and automation.
In this project, we are going to be controlling two types of motors to move in a certain direction using a joystick.
The 28BYJ-48 Stepper Motor
A stepper motor is a brushless DC motor that rotates accurately in 'steps'. For the 28BYJ-48 step motor, 1 step is equal to 11.25°, this means that a full rotation is equal to 32 steps.
The ULN2003 Stepper Motor Driver
The ULN2003 IC is required to run a motor such as the 28BYJ-48 because of its high-current use. The ULN2003 stepper motor driver board in the kit does this job well and even has LED indicators that allow the user to see the motor steps.
For this project, the driver module pinout is as follows:
For the 5-12V power of the motor driver, it is ideal to use another power source and not taken directly from the Arduino (VIN). Just make sure to connect the GND of the external power and the Arduino for the module to function properly.
The PS2 Dual-axis XY Joystick
The PS2 Dual-axis XY Joystick module functions like that of two potentiometers and a switch. One potentiometer controls the X-axis and the other controls the Y-axis. Pressing down on the joystick activates a switch.
The following are the analog values of the joystick module at maximum reach on different orientations.:
Use this code to check your joystick values:
int sw = A5;
int vy = A4;
int vx = A3;
int asw;
int avx;
int avy;
int horn = 10;
void setup() {
// put your setup code here, to run once:
pinMode (sw, INPUT_PULLUP);
pinMode (vy, INPUT);
pinMode (vx, INPUT);
Serial.begin(9600);
}
void loop() {
// put your main code here, to run repeatedly:
asw = analogRead(sw);
avx = analogRead(vx);
avy = analogRead(vy);
Serial.print(asw);
Serial.print("\t");
Serial.print(avx);
Serial.print("\t");
Serial.println(avy);
}
For this project, the X-axis will control the servo and the Y-axis will control the stepper motor.
Hardware Setup
This is the breadboard configuration of the system. All parts are included in the Arduino Upgraded Starter Kit.
Software Setup
Open the Arduino IDE and upload the code on the Arduino Uno Board.
Code
#include <Stepper.h>
#include <Servo.h>
#define STEPS 32
#define IN1 11
#define IN2 10
#define IN3 9
#define IN4 8
Stepper stepper(STEPS, IN4, IN2, IN3, IN1);
Servo servo;
#define vx A3
#define vy A4
#define sw A5
#define horn 12
#define servoPin 6
int asw;
int avx;
int avy;
void setup()
{
pinMode (sw, INPUT_PULLUP);
pinMode (vy, INPUT);
pinMode (vx, INPUT);
pinMode (horn, OUTPUT);
servo.attach(servoPin);
}
void loop()
{
// read analog value from the potentiometer
asw = analogRead(sw);
avx = analogRead(vx);
avy = analogRead(vy);
int servo_val = map(avx, 0, 1023, 0, 180);
servo.write(servo_val);
delay(10);
//when joystick is at the middle
if (avx > 500 && avx < 510 && avy > 490 && avy < 500) {
motorOff();
}
if (avy >= 500) {
moveUp();
}
if (avy <= 490) {
moveDown();
}
if (asw >= 0 && asw <= 20) {
tone(horn,1000);
}
else {
noTone(horn);
}
}
void motorOff() {
digitalWrite(IN1, LOW);
digitalWrite(IN2, LOW);
digitalWrite(IN3, LOW);
digitalWrite(IN4, LOW);
}
void moveUp() {
int stepSpeed = map(avy, 523, 1023, 5, 500);
stepper.setSpeed(stepSpeed);
stepper.step(5);
int servo_val = map(avx, 0, 1023, 0, 180);
servo.write(servo_val);
delay(10);
avy = analogRead(vy);
avx = analogRead(vx);
}
void moveDown() {
int stepSpeed = map(avy, 500, 0, 5, 500);
stepper.setSpeed(stepSpeed);
stepper.step(-5);
int servo_val = map(avx, 0, 1023, 0, 180);
servo.write(servo_val);
delay(10);
avy = analogRead(vy);
avx = analogRead(vx);
}
Code Breakdown
- setup()
In this function, the joystick pins are set as INPUT and the buzzer is set as OUTPUT. The sw pin of the The servo is also started with the function servo.begin(servoPin).
- loop()
Any commands set here will be executed indefinitely. This function reads the sensor input for any changes.
Next, the following code is called:
int servo_val = map(avx, 0, 1023, 0, 180);
servo.write(servo_val);
delay(10);
This code is for controlling the servo. It uses the map() function and inputs the values in servo.write(). This allows the servo to move in real-time with the joystick.
Next in the loop comes with three if functions and one if-else. The first if statement is for when the joystick is at its original, resting position. The condition is set on a range because the joystick values fluctuates at random in a certain range. The second if statement is for when the joystick is moved up in the y-axis, it calls on the function moveUp(). The third is for when the joystick is moved down and it calls on the function moveDown(). Finally, the if-else is when the joystick is pressed and it sets off the buzzer.
- moveUp()
stepper.step(5);
The positive value inside the step() function means that it is going forward.
- moveDown()
stepper.step(-5);
The negative value inside the step() function means that it is going backward.
- motorOff()
This function turns the stepper off by setting the stepper motor pins LOW.
Video Demo
Conclusion
This project was able to utilize the stepper motor, servomotor, buzzer, and the joystick of the Arduino Upgraded Starter Kit. This knowledge could be applied in higher applications such as that in robotic arms.
To find more useful projects, continue reading the Knowledgebase and Tutorials.
References
https://www.instructables.com/Joystick-Controlled-Servo-Using-Arduinowith-Progra/
https://create.arduino.cc/projecthub/arduino-applications/stepper-motor-control-with-joystick-f5feb1