Stepper Motor Control with Joystick

we performed the control of the stepper motor in the forward and backward directions along the x axis with the joystick

Jan 22, 2018

59690 views

36 respects

Components and supplies

1

Stepper Motor

1

Analog joystick (Generic)

1

Arduino UNO

Project description

Code

Codes of Project

csharp

Downloadable files

joystick_step_kontrol_iGE45EYlQ0.webp

joystick_step_kontrol_iGE45EYlQ0.webp

joystick_step_kontrol_iGE45EYlQ0.webp

joystick_step_kontrol_iGE45EYlQ0.webp

Comments

Only logged in users can leave comments

moses_shriver

2 years ago

here is the code: int s1 = 5; int s2 = 6; int s3 = 7; int s4 = 8; int x; void setup() { pinMode(s1, OUTPUT); pinMode(s2, OUTPUT); pinMode(s3, OUTPUT); pinMode(s4, OUTPUT); pinMode(A0, INPUT); } void loop() { x = analogRead(A0); 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); } }

Anonymous user

2 years ago

HI, it look exactly what I need, but sadly I can't open the image with the wiring description, I don't have Pixilion and I can;t effort the purchase of it...would be possible for you to add a regular PNG otr JPG picture...I believe it would help several people like me... many thanks

ritwik_k

2 years ago

anybody got the code for this

Anonymous user

2 years ago

could you copy code and post it, because i am not able to open office

Anonymous user

2 years ago

simple and works fine :) good job.

TheLostGhost

2 years ago

If someone interested I made a slight modification to the code by adding acceleration which is proportional to the joystick's Y value divided by 2 as a parameter of the delay() function. // Joystick pin numbers const int X_pin = 0; // analog pin connected to X output const int Y_pin = 1; // analog pin connected to Y output int X_Rotate; int Y_Rotate; // Stepper motor pin numbers const int IN1_pin = 8; const int IN2_pin = 9; const int IN3_pin = 10; const int IN4_pin = 11; void setup() { // Arduino pin setup for Joystick pinMode(SW_pin, INPUT); digitalWrite(SW_pin, HIGH); // Arduino pin setup for stepper motor pinMode(IN1_pin,OUTPUT); pinMode(IN2_pin,OUTPUT); pinMode(IN3_pin,OUTPUT); pinMode(IN4_pin,OUTPUT); } void loop() { X_Rotate = analogRead(X_pin); Y_Rotate = analogRead(Y_pin); if (Y_Rotate < 500) { digitalWrite(IN1_pin, HIGH); digitalWrite(IN2_pin, LOW); digitalWrite(IN3_pin, LOW); digitalWrite(IN4_pin, LOW); delay((Y_Rotate/2)+2); digitalWrite(IN1_pin, LOW); digitalWrite(IN2_pin, HIGH); digitalWrite(IN3_pin, LOW); digitalWrite(IN4_pin, LOW); delay((Y_Rotate/2)+2); digitalWrite(IN1_pin, LOW); digitalWrite(IN2_pin, LOW); digitalWrite(IN3_pin, HIGH); digitalWrite(IN4_pin, LOW); delay((Y_Rotate/2)+2); digitalWrite(IN1_pin, LOW); digitalWrite(IN2_pin, LOW); digitalWrite(IN3_pin, LOW); digitalWrite(IN4_pin, HIGH); delay((Y_Rotate/2)+2); } else if (Y_Rotate > 550){ digitalWrite(IN4_pin, HIGH); digitalWrite(IN3_pin, LOW); digitalWrite(IN2_pin, LOW); digitalWrite(IN1_pin, LOW); delay((1028-Y_Rotate)/2); digitalWrite(IN4_pin, LOW); digitalWrite(IN3_pin, HIGH); digitalWrite(IN2_pin, LOW); digitalWrite(IN1_pin, LOW); delay((1028-Y_Rotate)/2); digitalWrite(IN4_pin, LOW); digitalWrite(IN3_pin, LOW); digitalWrite(IN2_pin, HIGH); digitalWrite(IN1_pin, LOW); delay((1028-Y_Rotate)/2); digitalWrite(IN4_pin, LOW); digitalWrite(IN3_pin, LOW); digitalWrite(IN2_pin, LOW); digitalWrite(IN1_pin, HIGH); delay((1028-Y_Rotate)/2); } else if (Y_Rotate > 500 && Y_Rotate < 550) { digitalWrite(IN4_pin, LOW); digitalWrite(IN3_pin, LOW); digitalWrite(IN2_pin, LOW); digitalWrite(IN1_pin, LOW); } }

Anonymous user

2 years ago

This is literally what i was looking for. Thanks

Anonymous user

2 years ago

I seem to be having problems. When the joystick is not being touched at all the motor will rotate counter clockwise. The only way to change how its moving is to push the joystick the opposite way. I am new to Arduino and cannot find out how to fix the code for this

Anonymous user

2 years ago

i am also getting this issue. Not touching the joystick and it will turn clockwise untill i hold the stick in the opposite direction slightly. Other times it does not stop turning no matter what i do. I don't see anything in the code to cause this. I was wondering if there was a solution for this. **EDIT** After spending a few min testing and problem solving i found a solution to the auto turning with no joystick input. I changed these 2 lines "if(x<500)" and "else if(x > 500 && x < 550)" to "if(x<475)" and "else if(x > 475 && x < 550)" It seems my thumb stick was not sitting exactly between 500 and 550. Changing these values resolved my auto turning issue. Thank you for the great project and detailed video.

Anonymous user

2 years ago

Hello cihankarahasan Just tried your project, was looking for a system to move the stepper to which ever position I needed, worked first time. Thank you very much for sharing your knowledge. :)

Isaac_rules

2 years ago

I NEED THE CODE!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!

joelkotze

2 years ago

hay i copied the code and tweecked it i keep getting a error message please can you help int s1 = 5; int s2 = 6; int s3 = 7; int s4 = 8; int x; int y; void setup() { pinMode(s1, OUTPUT); pinMode(s2, OUTPUT); pinMode(s3, OUTPUT); pinMode(s4, OUTPUT); pinMode(A0, INPUT); pinMode(A1, INPUT); } void loop() { x = analogRead(A0); y = analogRead(A1); if(x<475) { 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 && 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); if(y<475) { 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(y > 475 && y < 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(y > 500 && y < 550) { digitalWrite(s1, LOW); digitalWrite(s2, LOW); digitalWrite(s3, LOW); digitalWrite(s4, HIGH); delay(5); }

moses_shriver

3 years ago

here is the code: int s1 = 5; int s2 = 6; int s3 = 7; int s4 = 8; int x; void setup() { pinMode(s1, OUTPUT); pinMode(s2, OUTPUT); pinMode(s3, OUTPUT); pinMode(s4, OUTPUT); pinMode(A0, INPUT); } void loop() { x = analogRead(A0); 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); } }

Anonymous user

4 years ago

HI, it look exactly what I need, but sadly I can't open the image with the wiring description, I don't have Pixilion and I can;t effort the purchase of it...would be possible for you to add a regular PNG otr JPG picture...I believe it would help several people like me... many thanks

Anonymous user

5 years ago

Cool, keep it up https://www.electricaldeck.com/2020/02/variable-reluctance-stepper-motor.html

TheLostGhost

5 years ago

If someone interested I made a slight modification to the code by adding acceleration which is proportional to the joystick's Y value divided by 2 as a parameter of the delay() function. // Joystick pin numbers const int X_pin = 0; // analog pin connected to X output const int Y_pin = 1; // analog pin connected to Y output int X_Rotate; int Y_Rotate; // Stepper motor pin numbers const int IN1_pin = 8; const int IN2_pin = 9; const int IN3_pin = 10; const int IN4_pin = 11; void setup() { // Arduino pin setup for Joystick pinMode(SW_pin, INPUT); digitalWrite(SW_pin, HIGH); // Arduino pin setup for stepper motor pinMode(IN1_pin,OUTPUT); pinMode(IN2_pin,OUTPUT); pinMode(IN3_pin,OUTPUT); pinMode(IN4_pin,OUTPUT); } void loop() { X_Rotate = analogRead(X_pin); Y_Rotate = analogRead(Y_pin); if (Y_Rotate < 500) { digitalWrite(IN1_pin, HIGH); digitalWrite(IN2_pin, LOW); digitalWrite(IN3_pin, LOW); digitalWrite(IN4_pin, LOW); delay((Y_Rotate/2)+2); digitalWrite(IN1_pin, LOW); digitalWrite(IN2_pin, HIGH); digitalWrite(IN3_pin, LOW); digitalWrite(IN4_pin, LOW); delay((Y_Rotate/2)+2); digitalWrite(IN1_pin, LOW); digitalWrite(IN2_pin, LOW); digitalWrite(IN3_pin, HIGH); digitalWrite(IN4_pin, LOW); delay((Y_Rotate/2)+2); digitalWrite(IN1_pin, LOW); digitalWrite(IN2_pin, LOW); digitalWrite(IN3_pin, LOW); digitalWrite(IN4_pin, HIGH); delay((Y_Rotate/2)+2); } else if (Y_Rotate > 550){ digitalWrite(IN4_pin, HIGH); digitalWrite(IN3_pin, LOW); digitalWrite(IN2_pin, LOW); digitalWrite(IN1_pin, LOW); delay((1028-Y_Rotate)/2); digitalWrite(IN4_pin, LOW); digitalWrite(IN3_pin, HIGH); digitalWrite(IN2_pin, LOW); digitalWrite(IN1_pin, LOW); delay((1028-Y_Rotate)/2); digitalWrite(IN4_pin, LOW); digitalWrite(IN3_pin, LOW); digitalWrite(IN2_pin, HIGH); digitalWrite(IN1_pin, LOW); delay((1028-Y_Rotate)/2); digitalWrite(IN4_pin, LOW); digitalWrite(IN3_pin, LOW); digitalWrite(IN2_pin, LOW); digitalWrite(IN1_pin, HIGH); delay((1028-Y_Rotate)/2); } else if (Y_Rotate > 500 && Y_Rotate < 550) { digitalWrite(IN4_pin, LOW); digitalWrite(IN3_pin, LOW); digitalWrite(IN2_pin, LOW); digitalWrite(IN1_pin, LOW); } }

Isaac_rules

5 years ago

I NEED THE CODE!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!

noob_uno

5 years ago

This is literally what i was looking for. Thanks

Anonymous user

6 years ago

I seem to be having problems. When the joystick is not being touched at all the motor will rotate counter clockwise. The only way to change how its moving is to push the joystick the opposite way. I am new to Arduino and cannot find out how to fix the code for this

Anonymous user

2 years ago

i am also getting this issue. Not touching the joystick and it will turn clockwise untill i hold the stick in the opposite direction slightly. Other times it does not stop turning no matter what i do. I don't see anything in the code to cause this. I was wondering if there was a solution for this. **EDIT** After spending a few min testing and problem solving i found a solution to the auto turning with no joystick input. I changed these 2 lines "if(x<500)" and "else if(x > 500 && x < 550)" to "if(x<475)" and "else if(x > 475 && x < 550)" It seems my thumb stick was not sitting exactly between 500 and 550. Changing these values resolved my auto turning issue. Thank you for the great project and detailed video.

Anonymous user

6 years ago

could you copy code and post it, because i am not able to open office

Anonymous user

6 years ago

Hello cihankarahasan Just tried your project, was looking for a system to move the stepper to which ever position I needed, worked first time. Thank you very much for sharing your knowledge. :)

davidSITO4884mx

7 years ago

simple and works fine :) good job.