I've been having difficult tracking down some sample code to set the number or microsteps or to setSpeed(rpm) for a stepping motor.
I've come across a few post, forums, blogs which were helpful but I'm not literate enough in coding to translate them to what I am attempting to do - or the one's I have haven't worked. Maybe of the programs seem to be written with an H bridge in mind. I have an Easydriver 4.3 (but could pick up a H-bridge if necessary.)
Here's what I have and am attempting. Please let me know if you can help me with some sample code. Again my literacy is basic. Things like uint_8 and (long whatspeed) and this--> is still a little foreign. I don't know what gets edited out or replace with numbers (int?)
The motor: 330ma, 12v, bipolar, 200 step (1.8 deg)
Easydriver: v4.3 (currently with default of 1/8 step)
Arduino pin settings: 3 direction, 12 step, 11 MS1, 10 MS2, 9 Sleep, 8 Enable (I may not need all these?)
looking for an rpm of about 1rpm or 0.2rpm? I can maybe use Sleep in between steps to conserve power?
any advice or sample code? would it be better with an H bridge than easydriver?
Odd indeed! I've tried going back and can't find it. It could be the code I posted. I had found it elsewhere but I thought leaving the programming credits in with the code would be the appropriate thing to do, no? Is there programing etiquette I should be aware of? Most likely?
mmcp42,
I did work through what you sent. Bringing things high or low made no different. It still gives me the odd step back. Ideally I could work out a way to use the easydriver to move the motor one step at a time. I've only seen sample using an H bridge. Maybe it would be better with a simple clock motor but it would be good to work through the coding for steppers. Let me know if you have any feedback on incorporating an rpm or number of steps when using an arduino/easydriver combo.
ok I have made some progress
attached is a sketch that reliably turns at one rpm
it only works if you keep the EastDriver enabled
I tried disabling it between steps and got the symptoms you describe (occasional skips and backward steps)
I suspect that when you disable the driver the motor relaxes and falls to the nearest rest position which may be forwards or backwards
give it a go and let me know how you get on
//============================
// slow speed stepping
// Mike McPherson 4th Feb 2011
// aka mmcp42
//============================
// we use serial for debugging/monitoring
//=======================================
#include <HardwareSerial.h>
// define some pins
//=================
#define DIR_PIN 3
#define STEP_PIN 12
#define MS1_PIN 11
#define MS2_PIN 10
#define SLEEP_PIN 9
#define ENABLE_PIN 8
#define HEARTBEAT_PIN 13
// define stepper
//===============
#define STEPS_PER_REV 200
// define speed required in rpm
//=============================
float Speed = 1.0;
// some globals
//=============
int StepsPerSecond; // speed converted to steps/second
int StepDelay; // = delay in mS per step
void setup()
{
// set all the pins as outputs
//============================
pinMode(DIR_PIN, OUTPUT);
pinMode(STEP_PIN, OUTPUT);
pinMode(MS1_PIN, OUTPUT);
pinMode(MS2_PIN, OUTPUT);
pinMode(SLEEP_PIN, OUTPUT);
pinMode(ENABLE_PIN, OUTPUT);
pinMode(HEARTBEAT_PIN, OUTPUT);
// calculate the required delay (we're using 1/8 stepping)
//========================================================
StepsPerSecond = Speed * STEPS_PER_REV * 8 / 60;
StepDelay = 1000 / StepsPerSecond;
// initialise the EasyDriver
//==========================
digitalWrite(ENABLE_PIN, LOW); // = enabled
digitalWrite(SLEEP_PIN, HIGH); // = not asleep
digitalWrite(MS1_PIN, HIGH); //
digitalWrite(MS2_PIN, HIGH); // might as well use 1/8 stepping
digitalWrite(DIR_PIN, HIGH); // arbitrary direction, LOW to spin the other way
// initialise serial port for monitoring/debugging
//================================================
Serial.begin(19200);
Serial.println("start");
Serial.print("Steps/sec: ");
Serial.println(StepsPerSecond, DEC);
Serial.print("StepDelay: ");
Serial.print(StepDelay, DEC);
Serial.println(" mS");
}
void loop()
{
boolean heart = false;
while (true)
{
for (int j=0; j<60; j++)
{
for (int i=0; i<StepsPerSecond; i++)
{
// step the motor
//===============
digitalWrite(STEP_PIN, HIGH);
delayMicroseconds(10);
digitalWrite(STEP_PIN, LOW);
// delay until next step required
//===============================
delay(StepDelay);
}
// heartbeat
//==========
heart = !heart;
digitalWrite(HEARTBEAT_PIN, heart);
}
// another minute gone by
//=======================
Serial.println("tick");
}
}
the code is great. it took me a few minutes to figure out how it works. quite nice. i had a chance to play with it via serial monitor and I can't see any issue with it. had a few minutes to adjust times and experiment. once i got around to hooking it up (with the code sent) to the stepper it just moves a full steps back and forth (i can feel the 1/8 steps moving in between).
I've rechecked the stepper's wire colour codes and they are wired correctly. (it has worked with other coding at quicker speeds). I've also tried it at 1/4, 1/2, and full step. no difference. I'm at a bit of a loss. i might pick up a different kind of stepper and try it with that.
I was quite looking forward to seeing that code in action.
I have had the motor working with different code and so I believe the motor is wired correctly. I have checked the datasheet. (A,C under "A" and B,D under "B") I've rechecked the pin outs.
When it comes to this code it simply ticks back and forth?
I'll pick up a different motor and try it with that.
I'm sure the code is correct. I'm not sure where the problem is.
Thanks for all the help. I had tried the MS variations. 1/8, 1/4, 1/2 and full. There wasn't much difference other than the speed.
I also had tried the direction directly to 5v as well as GND. All the variables, MS1, MS2, Enable, GNDs, Sleep, Dir were held either high or low. It's likely something is wrong with the easydriver board? Maybe the coding??? Not sure.
In the end I've opted with the dual H bridge (L293D) and have things working after some playing around. Same motor and power supply? I have a range of about 100rpm through 1 per 200 minutes and there's not the same issue of the back stepping?
Appreciate the feedback. I hope this is helpful to others as well.