Arduino Motor Shield Stepper Motor control with Joystick

Hi

I recently purchased an Arduino Uno R3 board with a Arduino Motor Shield Rev3 as well as a joystick breakout board. My aim is to use the thumb joystick to control the direction of the stepper motor on the X-axis, left to right as well as the speed. So far I've been looking for good tutorials on the net with regards to the subject but had no luck so far.

Attached to this article are two photos showing how I connected my joystick to the motor shield board as well as a closeup view of the specific joystick I bought. Here is a link to the joystick as well:

http://robotics.org.za/index.php?route=product/product&keyword=joystick%20breakout%20board&category_id=0&product_id=217

Below is some sample code I downloaded from the net which rotates the shaft from one direction to the other with a delay. How can I modify this code to work with my setup and control the motor via the joystick:

#include <Stepper.h>

const int spr = 200; // motor steps per revolution
const int pwm_cha = 3;
const int pwm_chb = 11;
const int dir_cha = 12;
const int dir_chb = 13;
const int brake_cha = 9;
const int brake_chb = 8;

Stepper stepper(spr, dir_cha, dir_chb);

void setup()
{
pinMode(pwm_cha, OUTPUT);
pinMode(pwm_chb, OUTPUT);
pinMode(brake_cha, OUTPUT);
pinMode(brake_chb, OUTPUT);
digitalWrite(pwm_cha, HIGH);
digitalWrite(pwm_chb, HIGH);
digitalWrite(brake_cha, LOW);
digitalWrite(brake_chb, LOW);

stepper.setSpeed(75);
}

void loop()
{
stepper.step(spr);

delay(0);

stepper.step(-spr);

delay(0);
}

Any help would really be appreciated!

Regards

Joystickmodule2-500x500.jpg

could you give more info on how exactly you one to control the motor with the joystick.
what i mean is what does pushing the joystick to the right do to the motor and what know, please explain so we could help you more

Well, when the joystick is in the center and not moving, the motor shouldn't do anything and stop. When the joystick is pressed to the left or right, the motor must move in the respective direction and the further the joystick moves from the center, the faster the speed of the motor must be. So basically left and right movement via the joystick as well as speed adjusting as the joystick moves further from the center pin.

The purpose of this setup is to slide a camera left and right, faster and slower on a rail of plus minus 1.5m in length. Attached to the stepper motor's shaft will be a pulley and on the other side just a normal shaft enclosed in a bearing unit also containing a pulley. I'll then use timing belt to slide the camera left and right.

Here are my stepper motor specs:

2 Phase Hybrid (Model: 23HS6430)
3A
Bipolar
4 Wires
1.8 degree step angle

I'm really not very clued up with prototyping and therefore if you can help me to amend the code to work with my setup, I'll really be happy since I've been battling and struggling to get the project going. But looking at my connection setup, is everything connected to the right pins etc?

Looking forward to hear from you.

Regards

Ian

the answer for your question is located here

well what you need to do is to divide the value of a pot into half
so i would say that 507 until 517 is a good middle value.
then take the value between 517 until 1023 and map it with the min and max of the stepper speed.
after that take between 0 to 507 value of pot n map it with the min n max of the stepper speed.
third is that determine which side of the pot make the stepper move clockwise. what i give here should give you an idea on how to do it

In theory I know what you're saying and it makes sense to me. However, how can I practically amend this code and incorporate everything that you have said? I'm not a code guru and this is all new to me.

Can you perhaps show me what the code will look like?


#include <Stepper.h>

const int spr = 200; // motor steps per revolution
const int pwm_cha = 3;
const int pwm_chb = 11;
const int dir_cha = 12;
const int dir_chb = 13;
const int brake_cha = 9;
const int brake_chb = 8;

Stepper stepper(spr, dir_cha, dir_chb);

void setup()
{
pinMode(pwm_cha, OUTPUT);
pinMode(pwm_chb, OUTPUT);
pinMode(brake_cha, OUTPUT);
pinMode(brake_chb, OUTPUT);
digitalWrite(pwm_cha, HIGH);
digitalWrite(pwm_chb, HIGH);
digitalWrite(brake_cha, LOW);
digitalWrite(brake_chb, LOW);

stepper.setSpeed(75);
}

void loop()
{
stepper.step(spr);

delay(0);

stepper.step(-spr);

delay(0);
}


I like this code since it seems to work with my motor quite well. Now I just need to know how to amend it to work with the joystick.

But thank you so much for the advice that you have given me already. I appreciate it!

well ok i had the chance to study the code, From what i can say is that the code have some part that need to be change for instant

const int spr = 200; // motor steps per revolution

this part is the part which dictate how much step the stepper would take to complete a rotation.
i would recommend to you to put the total number of step it take for your set up form its start position on the far left to the far right or vice versa. How ever this is not something that is really must needed and you could actually make it variable.... maybe by making use of the other axis of your joystick?

anyway part that really control the speed of your setup is this one

stepper.setSpeed(75);

this is the part that call for the speed of how fast the stepper rotate.
if my understanding of your project is correct, this part need to be implemented in the loop function and now the setup function cause it need to be variable. However before you impliment any of the changes that i suggest, i would recommend that you play with the value 75 that is in the code with something that is higher up until the point your motor stall. This would mean, that the value you got there is the max speed that the stepper could go. one more thing is would also suggest that you do this with the full load of your setup. this is to make sure that you get the most accurate value for your setup and not just something that is calculated or theoretical.

ok now for the pseudo code ( only the loop part but i thing you would get the idea well)

void loop()
{
  PVal=analogRead(Pot1);
  if (PVal >= 507 || PVal <= 517)
  { // fill in with the one you need to make it brake
  }
  if (PVal > 517 )
  {
    int K= map (Pval, 518,1023,0,MAX); /* max is the value that you get from your experiment with the max speed of the stepper before stalling*/ 
     stepper.setSpeed(K); this set the speed of the stepper motor
     stepper.step(spr); this set the direction and the number of step it would take
   }
 if (PVal < 507 )
  {
    int K= map (Pval, 0,506,0,MAX); /* max is the value that you get from your experiment with the max speed of the stepper before stalling*/ 
     stepper.setSpeed(K); //this set the speed of the stepper motor
     stepper.step(-spr); //this set the direction and the number of step it would take
   }

this should help you with your project anyway keep us posted and if you be kind enough to send us a link to a you tube showing how it work would be great .

Btw i would also like to suggest to you that, you pun 2 home sensor, this could be a simple limit switch or the type that use light sensor to detect your carriage on both end of the travel. this would help if let say your carriage at it end but you keep giving it the signal to go forward. although for a stepper this will not cause lots of trouble since if the motor will stall if it cant move the carriage but it might or could shorten the life of the motor... so putting the sensor will save it n give the motor a much longer life and reliability

Okay, I've tried to fill in the gaps but still no luck...

Here is what I've done:

#include <Stepper.h>

const int spr = 200; // motor steps per revolution
const int pwm_cha = 3;
const int pwm_chb = 11;
const int dir_cha = 12;
const int dir_chb = 13;
const int brake_cha = 9;
const int brake_chb = 8;

int Pot1 = 0;

Stepper stepper(spr, dir_cha, dir_chb);

void setup()
{
pinMode(pwm_cha, OUTPUT);
pinMode(pwm_chb, OUTPUT);
pinMode(brake_cha, OUTPUT);
pinMode(brake_chb, OUTPUT);
digitalWrite(pwm_cha, HIGH);
digitalWrite(pwm_chb, HIGH);
digitalWrite(brake_cha, LOW);
digitalWrite(brake_chb, LOW);

//stepper.setSpeed(75);
}

void loop()
{
int PVal=analogRead(Pot1);
if (PVal >= 507 || PVal <= 517)
{ // fill in with the one you need to make it brake
}
if (PVal > 517 )
{
int K= map (PVal, 518,1023,0,75); /* max is the value that you get from your experiment with the max speed of the stepper before stalling*/
stepper.setSpeed(K); //this set the speed of the stepper motor
stepper.step(spr); //this set the direction and the number of step it would take
}
if (PVal < 507 )
{
int K= map (PVal, 0,506,0,75); /* max is the value that you get from your experiment with the max speed of the stepper before stalling*/
stepper.setSpeed(K); //this set the speed of the stepper motor
stepper.step(-spr); //this set the direction and the number of step it would take
}
}

Nothing seems to be happening...

Sorry, I know that I come forth as someone with no brains for this stuff, but I'm no guru. I will gladly post a video if I can get it working.

Ok i get your code now..

urm lets see ok,

int Pot1 = 0;

change that to

int Pot1 = A0;

this should do it

Hi Ash

I amended the code accordingly but still no luck. Giving me one-liner codes won't really help me since I don't know arduino coding or any other type of programming to a huge extend.

Here is the code the last time with the changes...

#include <Stepper.h>

const int spr = 200; // motor steps per revolution
const int pwm_cha = 3;
const int pwm_chb = 11;
const int dir_cha = 12;
const int dir_chb = 13;
const int brake_cha = 9;
const int brake_chb = 8;

int Pot1 = A0;

Stepper stepper(spr, dir_cha, dir_chb);

void setup()
{
pinMode(Pot1, INPUT);
pinMode(pwm_cha, OUTPUT);
pinMode(pwm_chb, OUTPUT);
pinMode(brake_cha, OUTPUT);
pinMode(brake_chb, OUTPUT);
digitalWrite(pwm_cha, HIGH);
digitalWrite(pwm_chb, HIGH);
digitalWrite(brake_cha, LOW);
digitalWrite(brake_chb, LOW);
}

void loop()
{
int PVal=analogRead(Pot1);
if (PVal >= 507 || PVal <= 517)
{

}
if (PVal > 517 )
{
int K= map (PVal, 518,1023,0,75); // max is the value that you get from your experiment with the max speed of the stepper before stalling
stepper.setSpeed(K); //this set the speed of the stepper motor
stepper.step(spr); //this set the direction and the number of step it would take
}
if (PVal < 507 )
{
int K= map (PVal, 0,506,0,75); // max is the value that you get from your experiment with the max speed of the stepper before stalling
stepper.setSpeed(K); //this set the speed of the stepper motor
stepper.step(-spr); //this set the direction and the number of step it would take
}
}

For some reason nothing is happening.

But thanks anyway for your help. I'm sure I'll manage somehow somewhere eventually while searching the net. I need a solution quite urgent and can't struggle like this for too long.

Regards

I have been using your problem to learn about the stepper board. And the problem you are having is due to the Shield using A0 as current sensing. Move the Pot to A2 and amend the code accordingly.

Secondly, the mapping are slightly wrong - the last if statement needs to be reversed.

Finally, I have improved the response time when in the slow speed by mapping the number of steps to the speed.

I have put some prints in to look at the values and also there is some servo stuff to show the position of the pot - this can all be removed

regards,
James

#include <Stepper.h>
#include <Servo.h> 
 
const int num_step = 50; // motor steps per revolution
const int pwm_cha = 3;
const int pwm_chb = 11;
const int dir_cha = 12;
const int dir_chb = 13;
const int brake_cha = 9;
const int brake_chb = 8;


int Pot1 = 2;
int val;
Servo myservo;  // create servo object to control a servo 
Stepper stepper(num_step, dir_cha, dir_chb);

void setup()
{
    Serial.begin(9600);
    pinMode(pwm_cha, OUTPUT);
    pinMode(pwm_chb, OUTPUT);
    pinMode(brake_cha, OUTPUT);
    pinMode(brake_chb, OUTPUT);
    digitalWrite(pwm_cha, HIGH);
    digitalWrite(pwm_chb, HIGH);
    digitalWrite(brake_cha, LOW);
    digitalWrite(brake_chb, LOW);
    
    myservo.attach(2);
 }
 
void loop()
{
  int PVal=analogRead(Pot1);
  if (PVal >= 507 || PVal <= 517)
  { // fill in with the one you need to make it brake
  }
  if (PVal > 517 )
  {
    int K= map (PVal, 518,1023,0,400); /* max is the value that you get from your experiment with the max speed of the stepper before stalling*/
     int S=map (K, 0,400,0,num_step);
     stepper.setSpeed(K); //this set the speed of the stepper motor
     stepper.step(S); //this set the direction and the number of step it would take
     Serial.print("Forward, K=");
     Serial.print(K);
     Serial.print(" PVAL=");
     Serial.println(PVal);     
   }
 if (PVal < 507 )
  {
    int K= map (PVal, 0,506,400,0); /* max is the value that you get from your experiment with the max speed of the stepper before stalling*/
     int S=map (K, 0,400,0,num_step);
     stepper.setSpeed(K); //this set the speed of the stepper motor
     stepper.step(-S); //this set the direction and the number of step it would take
     Serial.print("Backward, K=");
     Serial.print(K);
     Serial.print(" PVAL=");
     Serial.println(PVal);     
   }
   
  // This next bit just outputs the pot position to a servo
  val = map(PVal, 0, 1023, 0, 179);     // scale it to use it with the servo (value between 0 and 180)
  myservo.write(val);                  // sets the servo position according to the scaled value
  delay(15);      
}

Ash, you are awesome man!! Thanks so much. My headaches are over. You cracked the code and full credit goes to you. I'll post a link on youtube as soon as my project is complete.

But thanks again! I really appreciate it very much!

Ash, you are awesome man!! Thanks so much. My headaches are over. You cracked the code and full credit goes to you. I'll post a link on youtube as soon as my project is complete.

But thanks again! I really appreciate it very much!

Ash, you are awesome man!! Thanks so much. My headaches are over. You cracked the code and full credit goes to you. I'll post a link on youtube as soon as my project is complete.

But thanks again! I really appreciate it very much!

noisyvoid thanks for varying the code. Urm for TheBlommie sorry if i didnt not help you much...

Thanks to both you guys for your help. I appreciate it very much and I think was a good learning curve for other out there who also intend to drive stepper motors with joysticks modules.

Hi Ash

I had a few trials and errors so far. I eventually got the setup going with the code you provided as well as other code I found. However, when I tried to connect a 12VDC,3A power supply to the motor shield, I accidentally caused damage to my arduino board. So I bought a new one and did some research on power supplies for both the arduino and the motor shield and came to the conclusion that the arduino and motor shield must rather be powered up with their own power supplies. So hooked up my arduino to a 5VDC,2000mA unregulated power supply and the motor shield to a 12VDC,1000mA regulated supply and it seemed to work quite well although the motor shield overheated quite a bit. At htis point in time I didn't connect the thumb joystick because when I first connected the 12VDC powers upply to the motor shield in the beginning, the problem occured when I gave power to the joystick when my computer shut down and the arduino board got damaged. The joystick was connected to the pins on top of the motor shield when this happened. So I suspect that the thumb joystick couldn't handle the voltage and current and therefore shorted out something on the arduino.

I also purchased a proto screw shield which is now connected between the arduino and my motor shield so I can fasten the wires properly for a better arcade joystick I purchased. The arduino has its own power supply (5VDC,2000mA), the proto screw shield draws its power from the arduino's power supply and the motor shield has its own power supply (12VDC,1000mA). I also cut the extension pin for the Vin on the motor shield underneath the board so it doesn't draw power from the arduino. I hope this will overcome any shortage problems to the arduino and proto screw shields.

My question though is simple... Will my arcade joystick work if I connect it to the proto screw shield on the specified pins while both the arduino and the motor shield is powered with their repsective power supplies? I'm a little scared to just hook it up just to short out something...

Have a look at the drawing so you can see my setup.

Cheers

hi TheBlommie,

I'm sorry to hear that happen to you, the reason that happen was because the arduino cant handle any current more then 40mA if i am not mistaken, however let me assure you that in any case, the pot can handle much more abuse then the arduino, a pot is just a variable resistor.
for you to connect the pot is 5V from the arduino connect to the end while the other end is connected to the gnd of any board,only the middle pin need to be connected at port A2. remember don't just take the power from the motor shield due to the arduino max voltage is 5vdc

Well, for the moment the Arduino board seems to be working quite well with the 5VDC,2000mA power supply. My biggest concern is the joystick. With the Vin extension pin cut underneath my motor shield board, there shouldn't be a way for the joystick to be affected by the motor shield's power supply, right? Or am I wrong to assume this?

And also, why do you think the motor shield is overheating with the 12VDC, 1000mA power supply? The board can actually handle up to 2amps per channel or 4amps in total if you drive a stepper motor and the operating voltage is 12V. Can I perhaps incorporate a PC fan into the box that I'll be using to enclose the boards so it can stay relatively cool? Will this help?

Thanks for your advice so far.