Code to run 2 x stepper motors with motor shield

Adafruit Ethernet, Motor, Proto, Wave, Datalogger, GPS Shields - etc!

Moderators: adafruit_support_bill, adafruit

Please be positive and constructive with your questions and comments.
User avatar
thomasfnewell
 
Posts: 15
Joined: Mon Nov 24, 2014 12:55 pm

Code to run 2 x stepper motors with motor shield

Post by thomasfnewell »

Hi there!
I am in need of urgent help. i have bought an adafruit motor shield and with corresponding adafruit stepper motor. i have had this working really nicely but have just found out after installing it in my art exhibit it doesn't have enough torque to turn the rod of a venetian blind round. So my thinking is to purchase another motor so i have one at either end and that should hopefully have enough torque to turn the rod in the blind.
With the motor on order i am currently trying to create the code in order to run 2 x stepper motors, from one adafruit shield and i will continue to use a single 12 v power supply to power them both ( i believe that i can do this, is this correct??). Both stepper motors are to work at the same time with one going forward and the other going backward, which when connected to either end of the rod due to orientation they will both be going the same direction and in turn make the rod turn.
My big issue is that my coding experience is very limited. i have read as much info as i can find and realise i have to set the motors up in one step mode in order for them to run at the same time. then using the adafruit multi stepper example it says to wrap them in a accelstepper wrapper but when it comes to the actual code that controls everything i am unsure on what code to write. please see what i have done already below. i want both motors to come on, do the same number of steps, one motor forward, the other backward, then wait ( i have put in a randomiser delay pattern for the purposes of the exhibit) and then the motors to turn back the opposite way they just turned, then another randomised delay.
Any info would be great, i am feel i am close but cannot complete the code.
Please help, many thank yous in advance.
Kind regards
tom

Code: Select all

#include <Wire.h>
#include <Adafruit_MotorShield.h>
#include "utility/Adafruit_PWMServoDriver.h"
#include <AccelStepper.h>
#include <AFMotor.h>


// Create the motor shield object with the default I2C address
Adafruit_MotorShield AFMS = Adafruit_MotorShield(); 
// Or, create it with a different I2C address (say for stacking)
// Adafruit_MotorShield AFMS = Adafruit_MotorShield(0x61); 


// two stepper motors one on each port
AF_Stepper motor1(200, 1);
AF_Stepper motor2(200, 2);


unsigned long randomTimeOn;  // here's a variable to store our blinds open time value
unsigned long randomTimeOff;  // here's a variable to store our blinds closed time value

void forwardstep1() {  
  motor1.onestep(FORWARD, DOUBLE);
}
void backwardstep1() {  
  motor1.onestep(BACKWARD, DOUBLE);
}
// wrappers for the second motor!
void forwardstep2() {  
  motor2.onestep(FORWARD, DOUBLE);
}
void backwardstep2() {  
  motor2.onestep(BACKWARD, DOUBLE);
}

// Motor shield has two motor ports, now we'll wrap them in an AccelStepper object
AccelStepper stepper1forward(forwardstep1);
AccelStepper stepper1backward(backwardstep1);
AccelStepper stepper2forward(forwardstep2);
AcellStepper  stepper2backward(backwardstep2);

void setup() {
  Serial.begin(9600);           // set up Serial library at 9600 bps
  Serial.println("Stepper test!");

  AFMS.begin();  // create with the default frequency 1.6KHz
  //AFMS.begin(1000);  // OR with a different frequency, say 1KHz
   
  randomSeed(analogRead(0)); // start the random call in a unspecified point in the random sequence
   
    stepper1forward.setMaxSpeed(50);
    stepper1forward.setAcceleration(100.0);
    stepper1forward.moveTo(155);
    
    stepper1backward.setMaxSpeed(50);
    stepper1backward.setAcceleration(100.0);
    stepper1backward.moveTo(155);
    
    stepper2forward.setMaxSpeed(50);
    stepper2forward.setAcceleration(100.0);
    stepper2forward.setmoveTo(155);

    stepper2backward.setMaxSpeed(50);
    stepper2backward.setAcceleration(100.0);
    stepper2backward.setmoveTo(155);
  
  myMotor->setSpeed(50);  // 50 rpm 
  myMotor->release(); // stop rotation and turn off holding torque.
}

void loop() {
  randomTimeOn = random(0, 4000); // this then randomly picks a number in the range specified
  Serial.println(randomTimeOn); // what number 'randomTime' is holding
  Serial.println("Double coil steps");
  stepper1forward.run();
  stepper2backward.run();
  myMotor->release();
  delay(randomTimeOn);
  randomTimeOff = random(0, 4000);  // chose a differnet number within the range specified
  stepper1backward.run();
  stepper2forward.run();
  myMotor->release();
  delay(randomTimeOff);  

}
Last edited by adafruit_support_bill on Mon Nov 24, 2014 1:37 pm, edited 1 time in total.
Reason: Fixed [code] tags

User avatar
adafruit_support_bill
 
Posts: 88097
Joined: Sat Feb 07, 2009 10:11 am

Re: Code to run 2 x stepper motors with motor shield

Post by adafruit_support_bill »

It is easier without the added complication of AccelStepper. You were also mixing in code from the old AF Motor library which is not compatible with the new shield.

Something like this should work:

Code: Select all

#include <Wire.h>
#include <Adafruit_MotorShield.h>
#include "utility/Adafruit_PWMServoDriver.h"

// Create the motor shield object with the default I2C address
Adafruit_MotorShield AFMS = Adafruit_MotorShield(); 

// two stepper motors one on each port
Adafruit_StepperMotor *motor1 = AFMS.getStepper(200, 2);
Adafruit_StepperMotor *motor2 = AFMS.getStepper(200, 2);


int stepCount = 155;  // How far to step
int stepInterval = 5; // increase to slow down, decrease to speed up

void setup() 
{
  Serial.begin(9600);           // set up Serial library at 9600 bps
  Serial.println("Stepper test!");

  AFMS.begin();  // create with the default frequency 1.6KHz
   
  randomSeed(analogRead(0)); // start the random call in a unspecified point in the random sequence
}

void loop() 
{
	Serial.println("Step One Way");
	for (int i = 0; i < stepCount; i++)
	{	
		motor1->onestep(FORWARD, DOUBLE);
		motor2->onestep(BACKWARD, DOUBLE);
		delay(stepInterval);
	}
	
	delay(random(0, 4000)); // random delay
	
	Serial.println("Step The Other Way");
	for (int i = 0; i < stepCount; i++)
	{	
		motor1->onestep(BACKWARD, DOUBLE);
		motor2->onestep(FORWARD, DOUBLE);
		delay(stepInterval);
	}
	
	delay(random(0, 4000)); // random delay
}

User avatar
thomasfnewell
 
Posts: 15
Joined: Mon Nov 24, 2014 12:55 pm

Re: Code to run 2 x stepper motors with motor shield

Post by thomasfnewell »

Wow, thank you so much for helping me out, i have uploaded the code and will use it once the second motor arrives. My inexperience with coding has been well and truly exposed, i kind of follow what you have done but some things still feel like a mystery.
Can't thank you enough for helping me out, greatly appreciated, will let you know how things go when i set the second motor up. hopefully there are no issues.
Kindest regards
tom

User avatar
thomasfnewell
 
Posts: 15
Joined: Mon Nov 24, 2014 12:55 pm

Re: Code to run 2 x stepper motors with motor shield

Post by thomasfnewell »

Also, can you use the "release" part of the code when using two motors?? i had put that in originally to take a bit of pressure of the motor i.e keep it cool. is it possible still with this new code?

User avatar
adafruit_support_bill
 
Posts: 88097
Joined: Sat Feb 07, 2009 10:11 am

Re: Code to run 2 x stepper motors with motor shield

Post by adafruit_support_bill »

Yes. Just call release() on both motors to turn off the holding current.

User avatar
thomasfnewell
 
Posts: 15
Joined: Mon Nov 24, 2014 12:55 pm

Re: Code to run 2 x stepper motors with motor shield

Post by thomasfnewell »

Brilliant! Thanks. i have done it like this:

Code: Select all

motor1->onestep(FORWARD, DOUBLE);
      motor2->onestep(BACKWARD, DOUBLE);
      motor1->release();
      motor2->release();
      delay(stepInterval);
Or should i put the releases after the step interval??
Last couple of questions, hope you do not mind???
What is int step interval = 5 doing? is that a speed up slow down kind of thing? or does it create a slight delay so the motors can keep in sync?
Also can you explain (i=0; i< stepcount; i++) please, i know this is what drives the motor round the steps but how does it work? i see that i can amend the amount of steps moved by changing the amount in stepCount = 155, is that correct? i may have to do fine adjustment there once the motors are installed.

many thanks once again for your help.
kind regards
tom

User avatar
thomasfnewell
 
Posts: 15
Joined: Mon Nov 24, 2014 12:55 pm

Re: Code to run 2 x stepper motors with motor shield

Post by thomasfnewell »

I have just plugged in my adafruit motor to the adafruit shield with the code and it seems to be doing a rather strange few things.
Firstly: it has very little torque and i can stop the motor axel from turning round quite easily with my fingers. not the case when i was running the motor normally before.
secondly, it is rotating round more than complete resolution i.e more than 200 steps.
thirdly, it is not very smooth when it rotates and lastly after it rotates it makes the noise of rotation and is getting some sort of signal but is not moving.
Are the issues linked to the fact that i have only one motor connected or is it in fact something else, please help.
many thanks
tom

User avatar
adafruit_support_bill
 
Posts: 88097
Joined: Sat Feb 07, 2009 10:11 am

Re: Code to run 2 x stepper motors with motor shield

Post by adafruit_support_bill »

What is int step interval = 5 doing?
That is the interval between steps. A smaller delay will make it step faster.
Also can you explain (i=0; i< stepcount; i++) please,
That is a C++ 'for' loop. It will loop from 0 to stepcount-1 - http://www.tutorialspoint.com/cplusplus ... r_loop.htm
it has very little torque and i can stop the motor axel from turning round quite easily with my fingers. not the case when i was running the motor normally before.
Don't call release until the motor finishes moving. If you release after every step, the motor will have no torque.

User avatar
thomasfnewell
 
Posts: 15
Joined: Mon Nov 24, 2014 12:55 pm

Re: Code to run 2 x stepper motors with motor shield

Post by thomasfnewell »

ok cool thanks for the info. i will take the release out of the code and speed up the intervals and see if that helps. Apologies for starting another thread.
Will let you know how i get on.
thanks
tom

User avatar
thomasfnewell
 
Posts: 15
Joined: Mon Nov 24, 2014 12:55 pm

Re: Code to run 2 x stepper motors with motor shield

Post by thomasfnewell »

hi, i have taken the release() out of the code and have decreased the step interval to 1 and the motor is working alot smoother and alot more normally which is great news. however the torque is still alot less than when i run it in normal one motor mode, why is this?? is there a way to increase this torque?? i am using a 12v 500mA power supply, which i am hoping will run both motors, is this correct? or do i need to increase the current? also is there a way to adjust the rpm with your code?? to increase torque on the single motor i decreased the rpm to around 10 or 50. i am wanting to use two motors as i need the strength of two motors to do the job but at the moment it seems running two motors decreases the torque.
Please advise. Have you ever ran two ada fruit stepper motors at the same time before? Did you experience this??
Many thanks once again.
tom

User avatar
adafruit_support_bill
 
Posts: 88097
Joined: Sat Feb 07, 2009 10:11 am

Re: Code to run 2 x stepper motors with motor shield

Post by adafruit_support_bill »

i am using a 12v 500mA power supply, which i am hoping will run both motors, is this correct? or do i need to increase the current
These motors require about 700mA each for full torque. You need a bigger power supply. https://learn.adafruit.com/all-about-stepper-motors/faq
also is there a way to adjust the rpm with your code??
Yes - by adjusting the step interval.

User avatar
thomasfnewell
 
Posts: 15
Joined: Mon Nov 24, 2014 12:55 pm

Re: Code to run 2 x stepper motors with motor shield

Post by thomasfnewell »

Ok, cool, thanks, i am going to use a larger power supply. Do you mean to decrease the step interval number? What is the range for this? 0 = fastest speed?? what will give the slowest speed??
Feel we are getting close.
Many thanks once again.
Tom

User avatar
adafruit_support_bill
 
Posts: 88097
Joined: Sat Feb 07, 2009 10:11 am

Re: Code to run 2 x stepper motors with motor shield

Post by adafruit_support_bill »

delay(0) will give the fastest speed - assuming that your motors are capable of keeping up. In practice, the top speed is usually limited by the speed of the i2c bus. There are a few techniques that can be used to gain a little more speed if needed.

On the slow side, there is no upper bound. You can take one step per month if you want. Just do the math: 1 month = 30 days * 24 hours * 60 minutes * 60 seconds * 1000 milliseconds.

The maximum delay you can specify in the delay call is about 49 days, but there is nothing to stop you from making multiple sequential calls to delay().

User avatar
thomasfnewell
 
Posts: 15
Joined: Mon Nov 24, 2014 12:55 pm

Re: Code to run 2 x stepper motors with motor shield

Post by thomasfnewell »

Finally i have the exhibit working for a day and working well, but it has just failed. the transformer powering the motors has died, it is 12v 500mA, not sure why, is it because each motor is pulling 350mA? i am trying a new power supply 12v 1A but it is giving me issues, the motors are not behaving as they were and the power light on the motor shield is going on and off in time with the motor, when the motor moves normally the light stays on, when motor stutters etc then so does the green light. Is my new power supply to blame?? thought the shield could handle 1.2A.
thanks again, any info greatly appreciated. will send video of exhibit.
Thanks
Tom

User avatar
adafruit_support_bill
 
Posts: 88097
Joined: Sat Feb 07, 2009 10:11 am

Re: Code to run 2 x stepper motors with motor shield

Post by adafruit_support_bill »

the transformer powering the motors has died, it is 12v 500mA, not sure why, is it because each motor is pulling 350mA?
As I said in my previous post:
These motors require about 700mA each for full torque. You need a bigger power supply. https://learn.adafruit.com/all-about-stepper-motors/faq
the motors are rated for 350mA per phase, but there are 2 phases and most stepping modes energize both at the same time.
i am trying a new power supply 12v 1A
That is still not enough. 700 + 700 = 1400. You need a power supply with at least 1400mA to adequately power these motors.
https://learn.adafruit.com/all-about-stepper-motors/faq

Locked
Please be positive and constructive with your questions and comments.

Return to “Arduino Shields from Adafruit”