Saturday, May 22, 2010

EasyDriver 4.2 Tutorial



Finally I had some time to do a tutorial on the Easydriver v4.2 board. The following example code is just for demonstrating the board's new functionality. It is was tested on a sparkfun stepper motor. You will notice in the code that each time the step mode changes, so to does the delay time between steps and the number of steps per revolution. The is not absolutely necessary but it helps you compare the differences between stepping modes.

Make sure you have the serial monitor open in the Arduino IDE when running this code. This will give you some feedback about the various modes of the board as they switch on an off.

Note: For real world use I would recommend hard-wiring the MS1 and MS2 pins to some switches to pull them high or low. This way you can free up some more pins on your Arduino.

For a tutorial on how to use the Easydriver v3.1 check out this post

QUESTIONS ARE WELCOME (but please keep it related to the example in this post ) 

///////////////////////////////////////////////////////////
// Stepper Motor skecth for use with the EasyDriver v4.2 //
///////////////////////////////////////////////////////////

// Dan Thompson 2010
//
// Use this code at your own risk.
//
// For all the product details visit http://www.schmalzhaus.com/EasyDriver/
// For the full tutorial visit http://danthompsonsblog.blogspot.com/


////// ED_v4  Step Mode Chart //////
//                                //
//   MS1 MS2 Resolution           //
//   L   L   Full step (2 phase)  //
//   H   L   Half step            //
//   L   H   Quarter step         //
//   H   H   Eighth step          //
//                                //
////////////////////////////////////

int DIR = 3;          // PIN  3 = DIR
int STEP = 2;        // PIN  2 = STEP
int MS1 = 13;        // PIN 13 = MS
int MS2 = 9;         // PIN  9 = MS2
int SLEEP = 12;      // PIN 12 = SLP


void setup() {
  Serial.begin(9600);     // open the serial connection at 9600bps
  pinMode(DIR, OUTPUT);   // set pin 3 to output
  pinMode(STEP, OUTPUT);  // set pin 2 to output
  pinMode(MS1, OUTPUT);   // set pin 13 to output
  pinMode(MS2, OUTPUT);   // set pin 9 to output
  pinMode(SLEEP, OUTPUT); // set pin 12 to output
}



void loop()
{
  int modeType = 1;                         // This number increases by multiple of 2 each through the while loop..
                                            // ..to identify our step mode type.                                            
  while (modeType<=8){                      // loops the following block of code 4 times before repeating .
    digitalWrite(DIR, LOW);                 // Set the direction change LOW to HIGH to go in opposite direction
    digitalWrite(MS1, MS1_MODE(modeType));  // Set state of MS1 based on the returned value from the MS1_MODE() switch statement.
    digitalWrite(MS2, MS2_MODE(modeType));  // Set state of MS2 based on the returned value from the MS2_MODE() switch statement.
    digitalWrite(SLEEP, HIGH);              // Set the Sleep mode to AWAKE.
    
    int i = 0;                              // Set the counter variable.     
    while(i<(modeType*200))                 // Iterate for 200, then 400, then 800, then 1600 steps. 
                                            // Then reset to 200 and start again.
    {
      digitalWrite(STEP, LOW);              // This LOW to HIGH change is what creates the..
      digitalWrite(STEP, HIGH);             // .."Rising Edge" so the easydriver knows to when to step.
      delayMicroseconds(1600/modeType);     // This delay time determines the speed of the stepper motor. 
                                            // Delay shortens from 1600 to 800 to 400 to 200 then resets  
                                                 
      i++;                      
    }                              
    modeType = modeType * 2;                // Multiply the current modeType value by 2 and make the result the new value for modeType.
                                            // This will make the modeType variable count 1,2,4,8 each time we pass though the while loop.
   
    delay(500);
  }
  digitalWrite(SLEEP, LOW);                 // switch off the power to stepper
  Serial.print("SLEEPING..");
  delay(1000);
  Serial.print("z");
  delay(1000);
  Serial.print("z");
  delay(1000);
  Serial.print("z");
  delay(1000);
  Serial.println("");
  digitalWrite(SLEEP, HIGH);
  Serial.println("AWAKE!!!");                // Switch on the power to stepper
  delay(1000);
}



int MS1_MODE(int MS1_StepMode){              // A function that returns a High or Low state number for MS1 pin
  switch(MS1_StepMode){                      // Switch statement for changing the MS1 pin state
                                             // Different input states allowed are 1,2,4 or 8
  case 1:
    MS1_StepMode = 0;
    Serial.println("Step Mode is Full...");
    break;
  case 2:
    MS1_StepMode = 1;
    Serial.println("Step Mode is Half...");
    break;
  case 4:
    MS1_StepMode = 0;
    Serial.println("Step Mode is Quarter...");
    break;
  case 8:
    MS1_StepMode = 1;
    Serial.println("Step Mode is Eighth...");
    break;
  }
  return MS1_StepMode;
}



int MS2_MODE(int MS2_StepMode){              // A function that returns a High or Low state number for MS2 pin
  switch(MS2_StepMode){                      // Switch statement for changing the MS2 pin state
                                             // Different input states allowed are 1,2,4 or 8
  case 1:
    MS2_StepMode = 0;
    break;
  case 2:
    MS2_StepMode = 0;
    break;
  case 4:
    MS2_StepMode = 1;
    break;
  case 8:
    MS2_StepMode = 1;
    break;
  }
  return MS2_StepMode;
}



107 comments:

Marcel said...

Good job Dan!

Very neat and tidy on the board and in the code.

I'm a little puzzled by the motor hookup labeled "DBCA" on the ED board. On my boards they're grouped |A| |B| — So the two wires from 1 coil on the motor are wired to the two pins marked A, and the then the other two wires got to pins marked B.

Maybe it's just my brain on freeze, or I'm just confused by the way the motor coil connections are labeled.

Dan Thompson said...

Yeah I see how that can be confusing. If you have a look at the data sheet for the spark fun stepper it will make sense why I've done it that way.

There's a picture of the coils marked AB and CD I think I should add that to the picture above so people don't get confused.

If you still are, check out the ED v3.1 tute to see the picture I'm referring to.

best,

Dan.

J said...

Thanks Dan,

Very usefull

jake carvey said...

Ha! Great timing - I just pulled out my EasyDriver build 2 days ago (which i patched together by modifying your earlier 3.1 setup), but it's cool to have all the parts in one place on one image.

I am adding in a camera trigger (basically combining shutterdrone's openmoco circuit with this one) to shoot 360 degree QTVR object movies. Might also try to hack some other code to add joystick control, once I get this basic setup working.

Kim said...

Hi Dan,

Thanks very much for this, it saved me a lot of time.
One thing I'm confused about though is the connection between the +5V on the Arduino to the +5V on the Easydriver.
From what I read, it seems that the EasyDriver gets all it's power from the motor supply and the onboard regulator feeds 5V to the A3967 chip and also provides it on the +5V pin so that other devices could be powered from the EasyDriver directly.

I tried removing it and everything still works quite nicely, so I think this connection doesn't need to be there, and it might actually cause damage because it's connecting a supply to a supply?

Anyway I thought it was worth mentioning just in case.

Cheers,
Kim

Dan Thompson said...

Hey Kim,

Thanks for picking up on this. When I went back and read the notes on the easydriver page, it appears the the +5V pin on the ED board is for supplying power to other small devices just as you mentioned. I have updated the pictures to reflect this.

thanks again!

Dan.

Anonymous said...

Really nice tutorial, just wondering if you could clarify a bit more. As Kim pointed out the +5 from the easydriver is for powering other devices, but should the ground directly next to it be grounded to the arudino? Thats what it looks like in the picture.

Thanks

unixproducts said...

very nice tutorial. thanks to you I was able to connect everything and to make it run smoothly on my arduino 2009. problems begun when I tried to add a second motor to the whole thing and tried to modify the code to send to the second motor (i am trying to do some experiments to reach the goal to have my 3 axis cnc done). can you make the same tutorial with 3 easydrivers 4.2 and 3 motors so I can see what I am doing wrong ?

unixproducts said...

details at the arduino forum:

http://www.arduino.cc/cgi-bin/yabb2/YaBB.pl?num=1276678655/0#0

Dan Thompson said...

Anonymous,

I think you may be right. The GND pin next to the +5 pin probably doesn't need to be grounded unless you are planning on drawing current from the 5+ pin for another device. I don't have time right now to check. It is not in described in the schematic so I will try to contact the developer to clarify this.

Thanks again for your eagle eyes !)

Dan.

Dan Thompson said...

unixproducts,

I checked out your code on the link you sent. I looks like it should work but I don't have time to test it right now. Check your wiring and try swapping arpund x and y in your code to see if it's to do with the code or with your wiring.

Good luck!

p.s. I don't plan on doing an arduino mega tutorial for this. I don't own a mega. However, I can't see why you would need to change the code for the mega. If you do then well, that's just silly :)

Dan.

Dan Thompson said...

Anonymous,

Here is a quote from the creator of the Easy Driver 4.2 regarding your query..

"All of the grounds on the ED are connected together - electrically there is no difference. So that wire in your picture is, technically, redundant with the other grounds. I put it there primarily for convenience if you're going to power some other 5V circuit with the ED. If you want to remove it, that is just fine."

I will edit the picture when I next get a chance.

Dan.

unixproducts said...

it was my wiring (one cable lose). now it works with 2 motors. for 3 axis on the duemilanove I miss a pin for the SLEEPZ. what can I do ? do I have to use the mega ? do you want a mega ?

Dan Thompson said...

If you are planning on using it for a "real world" application, I would not recommend using up valuable io pins just to set the MS1,MS2 and SLEEP pins high or low.

You should be able to hard wire these pins to a 5+ voltage source with pull up or pull down resistors to set them high or low.

This should free up 6 pins on your duemilanove. You should only need a total of 6 pins to run 3x easydriver boards when working this way.

Dan.

And yes I would love to have a mega, can't afford it though. :)

Anonymous said...

Hello,

Thanks for the great article with nice pictures and stuff. I came across it the other day and have since purchased an Arduino and Easydriver 4.

This is my first electronics project since childhood and I'm used to using schematics so I'm just trying to learn how to read the photo-steps.

1) Did you solder pins on your Easydriver? Mine just has holes. If I add pins, will it be robust enough for a breadboard?

2) Is every connection on that Arduino made through those black connector blocks?

3) On your diagram called ED4_revised.jpg, does Performance & Specifications refer to your stepper motor or the whole assembly?

Darcy

Dan Thompson said...

Darcy,

If you prefer there are schematics available from the easydriver site. These pictures are more for those who tend to be intimated by schematics and wish to fast track. However I always recommend reading the schematics as well as they are much more thorough than I could ever be.

1)Q. Did you solder pins on your Easydriver? Mine just has holes. If I add pins, will it be robust enough for a breadboard?

1)A. Yes you will have to solder on your own header pins (available from any electronics shop). They are designed to go straight into a bread board.

2)Q. Is every connection on that Arduino made through those black connector blocks?

2)A. Yes accept if you are using the USB for serial communication


3)Q. On your diagram called ED4_revised.jpg, does Performance & Specifications refer to your stepper motor or the whole assembly?

3)A. "Revised" just refers to an edit I made to the image itself. Performance and Specifications is taken straight from the data sheet that comes with the sparkfun stepper motor in the picture. If you are using a different motor, then you will naturally choose the appropriate wiring and power supply based on the motor's own specific data sheet.

Hope this helps,

Dan.

Dan Thompson said...

unixproducts,

Thanks for your enthusiasm, I have never built a cnc machine myself, so I can only refer you to some resources I found through our good friend mr. google. :)

GCode interpreter:
http://reprap.org/wiki/Arduino_GCode_Interpreter

..and here is a re-write of that called contraptor:
http://www.contraptor.org/arduino-gcode-interpreter

Inkscape to gcode plugin:
http://wiki.linuxcnc.org/cgi-bin/emcinfo.pl?InkscapeHowto

thanks and good luck!

Roy said...

I copied and pasted your code for the EasyDriver 4.2 and I get an error message when I compile it.
"1: error invalid digit "8" in octal constantBad error line: -3" I do not have a clue what that is.


Very nice tutorial, Thank you.
Roy Dodgen

Dan Thompson said...

Hi Roy,


You will get this error if you copy the code directly out of the web page.This is because it will try to copy the line numbers which will cause the error. Try hovering over the icons on the top right corner of the code snippet. Click on the copy to clip board icon and paste that into the Arduino IDE. I have tested this with Arduino Alpha 0018 software and it works fine.

Good Luck!

Dan.

Daniel said...

Hi Dan,

do you know a sketch where I can control one stepper motor with pushbuttons? I want to control my camera dolly with the Arduino + EasyDriver 4.2.

These features where also great to have:

Potentiometer for Max Speed.
Potentiometer for controlling the "damping ramp".
Buttons for Save Position and GoTo Position.

PS. Im a complete newbie in programming. Therefore I don't know how to implement such features. Can you point me into the right direction?

best regards
Daniel

Dan Thompson said...

These links should get you started.As a general rule I like to play with the concepts first like in the smoothstep tutorial. Use print statements to debug the output or input of your program and then try it again with hardware. But that's just me :)

http://www.arduino.cc/en/Tutorial/Potentiometer

http://danthompsonsblog.blogspot.com/2009/02/smoothstep-interpolation-with-arduino.html

this one is fairly advanced:
http://danthompsonsblog.blogspot.com/2009/04/python-arduino-motor-shield-stepper.html

What you are wanting to do will require a lot of patience but it will be well worth it! If patience is not a strong point, then check out openmoco.org as there may be a ready made solution for what you want to do.

Good Luck!

Daniel said...

Thanks Dan,

I will give it a try. The Potentiometer tutorial is easy I just have to implement the stepping motor somehow in there.

best regards
Daniel

jake carvey said...

Timelapse tester using Arduino, Easy Driver 4.2, DIY dolly platform < $150 including NEMA 23 stepper (not including alum or fiberglass ladder). This is also before I realized MS1 and MS2 were not firing (meaning no microstepping). Code to follow.

http://vimeo.com/13863535

jake carvey said...

faster version

http://vimeo.com/13862986

jake carvey said...

hey Dan - is it possible to post code in the comments? wanted to drop my latest code experiements

Dan Thompson said...

Hey Jake,

I'd prefer if it was just a link to the code rather than the actual code. Unless, of course if it's a variation on the code in this post. Thanks and congratulations on your progress!

Dan.

p.s. Here's a link to a tool for posting code in your own blog.
http://alexgorbatchev.com/SyntaxHighlighter/

jake carvey said...

@Dan: no problem. It IS a variation, but has a lot of additional stuff. What "brush" (language) are you using for syntax highlighter?

Anonymous said...

Thanks for the great tutorial.

I apologize since this is probably a dumb question, but I'm really new to electronics...

If I wanted to have two easydriver boards running, can I power them off of a single 12V power source?

thanks.

Dan Thompson said...

Anonymous,

I'm no expert on this. But generally you should start with what the motors are rated at in terms of volts and amps and then work backwards from there.

According to the website the easy driver can take up to 30volts. If your motors are rated at 12 volts, then a 12 volt supply should be OK provided your power supply can supply enough amps to power them both.

Feel free to correct me or jump in anyone out there with more experience on the subject.

Dan.

James said...

Hi Dan.
I have just acquired the easy driver v4.3 and an arduno duemillanove and the stepper motor available to me is the 34HSX-312 from NEMA whose current per phase in series connection is 4.0 A and in parallel connection is 8.5A. Will the same connections and the same code used here work for me? I am very new to this electronics stuff and would really appreciate your help. Thanks

mikethe1wheelnut said...

Salutations! :-)

I was directed here from here: http://forum.sparkfun.com/viewtopic.php?f=14&t=23742

I have yet to test your code and setup, for reasons that will become obvious, but the presentation of the subject certainly inspires confidence!

So, my questions are the following:

(I am using this stepper: http://www.eminebea.com/content/html/en/motor_list/pm_motor/pm25l024.shtml)

It appears that only 4 of the 6 wires are actually connected to the controler. this being the case, in principle, I had a controler here that could just as easilly have worked. sigh. :-) now I will have two different ways of controling the motor, one of which I can have a great deel of confidence in! (I hope, I better.. :-)

point is, my stepper also has 6 wires, but they are not the same wires... in fact, the stepper pictured in the link I included only has 4 wires, brow and black, and yellow and orange. -my stepper has 6 wires, brown and black, plus red, and yellow and orange, plus red. Ok. I notice that my stepper has one extra wire per grouping, red in each case. I suppose this is some sort of safety wire? I suppose further that I can ignore them?

ok. so my question is: which of my wires are your A,B,C, and D wires?

my other question is this: in your code, you have two 'subroutines', each with 4 cases. Earlier, I had a friend build me a stepper motor controler, during which time he explained a great many things to me. one of the things I remember from that time is that these types of steppers have 4 magnets, hence 4 primary positions. These 4 steps.. no no.. this isn't making sense any-more.. then again, my own stepper doesn't make sense either, given that it also has only 4 pins, but has 20 step devisions.

ok. forget that. my point is, my understand was that there were 4 pins, and that these had to be switched up and down to make the motor step. there .. no wrong again. there are only 2 pins. these pins are sequentially flipped high or low to get the motor to step, and the sequence has 4 parts: (0,0), (1,0), (0,1), (1,1). These 4 parts are easilly recognizable in your code in the above mentioned subroutines.

what confuses me is this: these parts are labeled: 'step mode is full..', 'step mode is half..', etc.

hehe.. I have figured out the answer to my question. it seemed wierd to me that these cases were identified with step modes, when I know that they are the necessary sequence of pin ups and downs to make the motor turn. I was quite lost.

now it is quite obvious to me that these are only identified with step modes because the time delay when each step is taken is different, as per your comment on the page, directly after the two beautiful colour images. so that conundrum is quite resolved :-)

so if I want constant speed stepping, all I have to do is decide on how fast I want it to go, and set my time delay accordingly.

my question about the wiring still stands though. the wiring diagram in the 3.1 tutorial doesn't help, obviously, I guess :-)

Dan Thompson said...

mikethe1wheelnut,

Thanks for your detailed question.

"which of my wires are your A,B,C, and D wires?"

These two links should help you.

How to reverse engineer your stepper if you have no data sheet:
http://www.jasonbabcock.com/computing/breadboard/unipolar/index.html

How two wire a 6pin stepper for the easy driver:
http://www.schmalzhaus.com/EasyDriver/

please note that this code is designed to demo the features of the board and hence quite bloated for such a simple operation.

It can be stripped back quite a bit if you hardwire the MS1 and MS2 pins to the step mode that the your project requires.

Clay said...

Hey Dan, thanks for all the great info on your page!

My question is about how to test whether my ED is fried. Is there any way to test with a voltmeter or the like? I think some of the pins that are normally pulled up/down are not reading what they were before, but maybe there is some hysteresis I'm not taking into account...

Thanks again!
clay

Dan Thompson said...

Hey clay,

Sorry, I would not know how to test it for faults. I have fried a few of these in my time, But all that happens is they just stop working. Maybe you could contact the designer and ask him for some pointers?

Good luck!

Dan.

Anonymous said...

Hi Dan

I would like to hardwire the MS1 and MS2 in order to control the full/microsteps on the ED, and to save pins on my Arduino.

I have seen this "hardwiring" mentioned a number of times in here, but as I'm quite new to stepper motors, controlers and drivers, I have on idea on how to do this. :-)

Do you have a small step by step tutorial on how this is done?

Thanks :-)

jake carvey said...

@ Anonymous:

As I understand it, the basic premise here is that you need to hard-wire the pin to set it to "HIGH", which means it is "on", and has current connected to it, in this case the 3.3v or 5v which is driving the electronics. So, to set it high, you connect a wire from the 5v (or 3.3v) power to that pin. You could also connect a SPST switch between the power and the pin, so that you can quickly set it HIGH or LOW at will. (Of course you will want to do this for both the MS1 and MS2 pins.)

- jake

Anonymous said...

Hi Jake

Thanks for your answer, I may have misunderstood something, but I'm a bit comfused...

On the ED website it says that the driver:
"Is permanently set to use 8 step microstepping mode"

But in the code in Dan's tutorial, doesn't it says that when both MS1 and MS2 is set to HIGH (on), then it will run in eighth step mode?

So I can't figure out wether the MS1 and MS2 by default set to HIGH or LOW?

Christian

jake carvey said...

@ Anonymous

The docs from the ED website might not correspond exactly to the Sparkfun implementation. Based on Dan's code, which I have experimented with quite a bit, 1/8 step mode is enabled when both MS1 and MS2 are set HIGH, as you said.

Running the demo it is easy to see which is which. I made some minor changes to the code when testing, so that it runs through each stepping mode, at the same speed and the same number of steps. Then it is easy to see the differences in the speed.

One thing to be careful of - if the power supply is not adequate for your motor, then the microstepping wont even work - the motor will just buzz or vibrate.

jake carvey said...

Looking further, it IS possible that MS1 and MS2 are set high by default - Dan's code doesnt read the value before writing it.

You could easily add a few lines to check the state of MS1 and MS2 before running the program. I haven't tested this, but should be fairly straight-forward

MS1State = digitalRead(MS1);
MS2State = digitalRead(MS2);
Serial.print("MS1 State :");
Serial.print(MS1State);
Serial.print(" || ");
Serial.print("MS2 State :");
Serial.print(MS2State);
Serial.println("");

Dan Thompson said...

Hey Guys,

(Thanks for helping out Jake). From memory, I think the ED v4.2 is set to full step mode, that is if you leave the MS1 and MS2 pins untouched. So if you are looking to only use full step mode things get much simpler (more like the EDv3.1 tutorial).

Hope this makes sense.

1977 said...

I just did a Simple test with the MS2 connected to GND.

This made the motor running 4 times faster than leaving the MS2 untouched.

Doesn't this mean that when grounding MS2, the motor now have to run the same number of steps in a lower resolution?

By the way I'm trying to get 1/2 step-mode. :-)

- Christian

Anonymous said...

Hi Dan, Lovely tutorial, keep it up if you can. Any chance of getting hold of a circuit diagram??? Cheers Silversky

blento said...

How would you slow down the motor speed in this code?

Are changes only to this line requiered?
delayMicroseconds(1600/modeType);

By the way thank you for the tutorial

Dan Thompson said...

silversky,

sorry I do not know how to use eagle to make nice pretty one. Feel free to make you own based on the wiring on the breadboard image above. If you know how to use a breadboard, I would think it would be pretty straight forward.

Cheers.

Dan Thompson said...

1977,

////// ED_v4 Step Mode Chart //////
// //
// MS1 MS2 Resolution //
// L L Full step (2 phase) //
// H L Half step //
// L H Quarter step //
// H H Eighth step //
// //
////////////////////////////////////


All you are doing by hardwiring MS2 to ground is switching from full, to half step mode and back again.

MS1 MS2 Resolution
L L Full step (2 phase)
H L Half step

Speed is always relative to the combination of step mode and delay time in the code.

Stop Eating Dogs! said...

Hi Den,

First, I am very new to Arduino world (I just figured out that I need to buy header pin.). I first want to say Thank You for your time and effort for making this. Secondly, I am not sure whether it happens only to me but I have spent quite many days to find tutorials for ED V42 and had visited your blog many many times to see tutorial for ED V3. But I didn't know that you have this tutorial, too. I have many questions but today I just want to say Thank you again.

Daniel

ps. I am making this:

http://www.arduino.cc/cgi-bin/yabb2/YaBB.pl?num=1284499155/8

1977 said...

Hi Dan,

Ok then... So if grounding MS2 makes the ED switch from full to half step and then back again to full step.

How do I then hard-wire the ED to stay at half step?

I apologize for keep going on about this, but I just want to make sure that I'm doing this right. :-)

Dan Thompson said...

1977,

As the table in the code suggests,

For half step:
Hardwire MS1 High(H) and hardwire MS2 Low(L)

Replace the while loop in your code with this one:
while(i<(400)) {
digitalWrite(STEP,LOW);
digitalWrite(STEP,HIGH);
delayMicroseconds(800);
i++;
}

Also you should check out the data sheet for the chip used in the Easydriver. Easy to find if you go to the ED website and follow the link.
http://www.allegromicro.com/en/Products/Part_Numbers/3967/3967.pdf

It has the table of how you can control the chip. I have added this to the comments in the code above for convenience.

1977 said...

Ok thanks, I'll give that a try.

great site by the way. :-)

Stop Eating Dogs! said...

Hi Dan,

I have finally connected all part and upload the code above but nothing happen. So here is my questions:

1. First, easiest. I compile and upload the code to Arduino without error. I open 'serial monitor' and see Full, Half.......Awake repeatedly. Does it mean that the motor should run the same command over and over? Nothing happened to my motor, though.

2. When I want to stop running software and motor, what should I do? Just close software and disconnect power?

Thank you very much.

Daniel

Dan Thompson said...

Stop Eating Dogs!,

If there are compile errors and the serial output is working then I would check your wiring. Make sure you have a suitable power source that is rated for your motor(check easydriver website and data sheet for more info). As a rule of thumb, always connect the motor power source last. Do not play around with the wiring while the motor power source is connected, you run the risk of damaging your board.

As previously mentioned this is just a piece of test code so it will loop continuously, until you disconnect the motor power supply.

Good Luck!

AJ said...

Hey Dan, This is the most helpful post on connecting and using the Easy Driver with the Arduino. I am using your guide in my Homebrew Cnc project still in progress and link to you on my site.

I wrote some code to control a stepper with two buttons for forward and backward to help in prototyping.

Thanks a lot! Feel free to visit and comment or email me.

http://currentamps.com/projects/2-button-stepper-motor-control

Anonymous said...

I'm still confused on how to physically hardwire the board to set your desired stepping resolution. Do you think you could expand on that topic?

Stop Eating Dogs! said...

Dan,

Could you please let me know the model name of the motor and power source (adapter for the motor) which you used in the picture above.

I have a 6-wired Uni-polar motor (http://www.robotshop.com/rbsoy07-soyo-unipolar-stepper-motor.html) and it doesn't move at all. Some websites explain that the motor driver is based on Bi-Polar motor. So, my last solution for this project is that I want to buy exactly same products you used.

Thank you very much for your help.

D

Anonymous said...

Hi Dan,

I am trying to get the following circuit running (http://danthompsonsblog.blogspot.com/2010/05/easydriver-42-tutorial.html). I am using a unipolar 12v 600mA motor with 6 leads. I have connected the 6 leads as per your instructions on your webpage - leaving the two middle leads disconnected. When i power the arduino i notice the led on the easydriver turns on and off slowly, when i plug in the 12v power for the easydriver the same led turns on very bright and stays on. The motor never moves. The arduino and the easydriver are power separately using two 12v lead acid batteries.. Any advice would be very much apprieciated.

Aidan

derherrkohler said...

thank you Dan for this tutorial. It help's me a lot with my camSlider - i'm building a very similar thing right now: http://vimeo.com/2174272 - the difference is the use of an industrial belt and i use the accelStepper-Class.
Have you figured out a useful way to create some sort of damping/easing?

Anonymous said...

I was wondering if it is possible, that somebody could give me a hint or some lines of code for the following:

i have the above configuration (arduino+stepper+easydriver 4.3) and it works fine with the code from the tutorial.

but what i would like to get is the following:

the stepper should make a given amount of microsteps and then wait a given amount of seconds until it steps again. i've googled all over the place, but i think im severly incapable of understanding where i sould start.

it would be wonderfull if this would only be a small line of code which needs to be changed or added.

think of me as a totaly incapable guy who is just starting with arduino.

and i find it kind of funny that a guy in germany has the exact same name like me and is working on something simmiliar ;) strange isn't it?

i thank anybody trying or helping me in this matter.

have a nice easter-weekend

yourartist said...

Dan,
Thank you for doing such a great job on this. I tried to solder some 22 gauge AL wire on to the EasyDriver pins/holes, but it beads up on me. Do you have any advice? For example...solder type, temp, etc. I am new to electronics and can't get past this. Please help.

jake said...

@anonymous: I have an Arduino script that that works for this, based in part on Dan's original code. Message me privately. jake(@t)alienorbit.com

Anonymous said...

I used your site to test my stepper motor and it works, but it does something strange. It does a turn then buzzes but does not move, another turn, buzzes again and then sleeps. Is that what it is supposed to do?

jake said...

@Anonymous: From what I remember, the buzzing (but not moving), means that the motor isn't getting enough power, or that the current adjustment on the EasyDriver needs tweaking.

You should definitely feed power directly to the EasyDriver power inputs and power the Arduino separately (or cleverly).

USB power is definitely not enough to drive even the smaller Sparkfun steppers. And I speak from experience when I say that trying to trying to pull too much current can FRY your computer's USB controller, possibly the motherboard.

The buzzing can also mean that the wires from the stepper are not connected to the proper outputs on the EasyDriver.

A word of caution: never ever plug / unplug any of the wiring on the circuit while there is power connected to ANY of the hardware components. I have fried more than one EasyDriver this way, before I figured it out.

Anonymous said...

Hi everybody
this is a realy nice tutorial, thank you very much Dan!

i have a problem with the upload of this sketch to my arduino mega!
When i copy the code to the Arduino IDE and compile it, everything is fine. but if i want to upload the sketch, after 2-3sec of uploading, the tx and rx leds stops to blink and 5secs later i get an error in the Arduino console:
avrdude: stk500_2_ReceiveMessage(): timeout

does anybody know what this means? it would be nice if someone can help me, thanks!

CrazyCarl said...

2 things:

1:
'AWAKE!!!' makes issues for AVRDUDE if you're on ubuntu


2:
is the correct behavior of the sketch full rotations with each stepping mode?

Nickname unavailable said...

This is a great page. Thanks for making it.

I'm a newbie and trying so hard to figure out how to make my door handle turner.

I need to tell the stepper to turn a certain amount of steps forward, then pause, then turn same amount of steps backwards.

I haven't fighters out how to do this yet. Frustrating and exciting at then same time.

Monty said...

Hi Dan,

Im trying to build a time-lapse rig which tracks.

I have setup your tutorial and in installed the script and engine works fine. So thats cool! thanks.
Now Im trying to get it to work with openmoco. So have installed the software and uploaded the timelapse engine to the Arduino but can't get the engine to move.

Comes back with a ERROR: Timeout Reading from engine

Do you have any hints? Be great to know how I can intergrate your tutorial with openmoco slim?

Cheers

Dan Thompson said...

All really good questions Monty,

I am working on a easy-driver shield as we speak which will take a lot of the complexity out of this code as you will have physical switches to set the step modes.

I have used openmoco and slim but only to control the shutter and not motors. When the shield arrives back from china I will do a tutorial on how to use it with the open moco engine. Sorry I can't help you right now but I'm sure you could ask on the openmoco forums.

Good Luck!

Dan.

Neil said...

Hi Dan,

I'm very new to stepper motors. I would like to build a stepper motor camera dolly that is very similar to this video, http://www.youtube.com/watch?v=_maToB8xqmM

However, I am not sure what controller with LED display it is. With your EasyDriver 4.2, can I make it function similar to the one in the video? Can we also put a LED display and be able to set time?

Btw, I'm building it for timelapse purpose.

Thanks.

Neil

Dan Thompson said...

Hi Neil,

The display in the video is not a LED display, it's a LCD display. And yes it is certainly possible to build your own interface and menu system with an Arduino and LCD display. The video also uses and infrared LED to trigger the camera wirelessly instead of a cable.

I recommend you get on over to openmoco as I'm pretty sure they have a solution already for you problem openmoco.org Here is a shield that may do everything you need it to do without any extra programming
http://openmoco.org/node/364

Good luck!

Neil said...

That's an awesome link. Thanks so much Dan! :D

Pat M said...

I have a question about an IC that seems similar to your A3967. I need more power to my stepper for use as an arm for a robot and found that the company that makes your A3967 makes a higher 2.5A version (A3977KED). Is it possible for me to use the same schematic you've made and just match the pins to what you spec'd for the EasyDriver, or is there something I'm overlooking? I know that the pin count is different, so I'll probably end up having to design a board for it! The motors I'm using are 0.48 ohm and 3A, so if you know of a design that's out there already, that would also help! Thanks for your time and patience.

Anonymous said...

Hi Dan,

Great tutorial here. So I used the arduino uno and the easy driver 4.2 to drive a PM stepper motor that was salvaged from a printer. The motor steps perfect at full and half step only. And also I cannot get it to change direction. I checked the voltage on the DIR Pins and they match the arduino program. Is it something to do with my motor or something I can do on the board ? Thanks

Marcel said...

Hi Anon,

I've had a ED board go south on me where I couldn't change direction but still able to step. Can you get another board to try out?

hth

Marcel

Dan Thompson said...

It may be that you have your motor wired up incorrectly. Here's a link I found useful to check if it is:
http://www.jasonbabcock.com/computing/breadboard/bipolar/index.html

hallmat said...

hi dan i know you have already answered loads of questions,

but can you answer two more,

i need to control two steppers so that would need two drivers how would i wire that up? so they act individually,

also can i attach two motors to the one driver?


Many thanks

Dan Thompson said...

Hi Hallmat,

Here's some pictures of how to wire up to easydrivers:
http://danthompsonsblog.blogspot.com/2010/08/two-motors-are-better-than-one.html

As for your second question. I don't know what happens to stepper motors if you wire them up in series? Has anyone on here tried it?

Anonymous said...

Hi Dan,

Great tutorial, thank you so much! I hooked everything up, and it works exactly as expected. One question though: whenever I restart the Arduino (either by the push-button or re-opening the serial monitor) the motor will twitch/vibrate a few times then continue its' normal operation. Is this typical behavior?

Anonymous said...

Hellooo,

That was a very helpful tutorial, thank you for the great work!

i have got some problems using EasyDriver 4.2 on my stepper motor; hence, the microcontroller is ArduPilot Mega2560..

has anybody used a stepper on this board? i don't know which pins numbers to use to operate it.. and could we use "Stepper.h" library in Arduino instead of the code above?

thank you very much for the help.. in advance :)

Anonymous said...

Hi Dan, Hi hallmat,

hallmat said: "also can i attach two motors to the one driver?"

Dan Thompson said: "As for your second question. I don't know what happens to stepper motors if you wire them up in series? Has anyone on here tried it?"

In short, NO, don't do that, neither in serie, neither in parallel, use a separate Stepper Driver per motor!

An Another Anonymous Solver.

stepper motor said...

thank you for the great work! That was a very helpful tutorial, the motor steps perfect at full and half step only. And also I cannot get it to change direction. I checked the voltage on the DIR Pins and they match the arduino program.

Harris said...

Hi All,

I'm making a clock running arduino + easydriver. My stepper motor is 200 steps/rev. I'm having a problem syncronizing the clock minute hand with the time. I now have programmed the clock to rotate the clock hand each 3 minutes, so thats (1600 / 20) == 80 steps per 3 minutes. Still after one hour the clock is already running a few minutes behind. I think the problem is in the fact that i'm using the SLEEP mode after each minute (so it uses less power), Or maybe the microseconds delay between each step. It looks like the steps are different when i'm using 1500 microseconds in stead of 2000 microseconds. Anyone can help me out?

kidd jmadd said...

Great code, thanks for sharing! Worked right off the bat. As a bonus i learned how to use emacs 'alt-x replace-regexp' to remove the line numbers & leading whitespace that carried over during copy/paste operation.

Dan Thompson said...

Hey kidd jmadd,

well done on the emacs front. This code is converted with syntax highlighter. So if you hover over the top right corner of the code you will see some icons. Click on the one that says copy to clipboard, paste to a text editor and your done! no need to format out the line numbers and white space.

Sorry you had to find this out the hard way. But hey, now you know emacs! :)

Jack said...

Hi Dan, great code out there.... Good effort...
But just to check with you, the stepping program source code you provided for the v3.1 is still compatible with this arduino source code?

Dan Thompson said...

Hi Jack,

I'm not really sure of you question. They are two different sketches. And sketches don't interact with each other. Maybe if you explain what you are trying to do then I can answer. If you are just wondering if the 3.1 sketch will run on the 4.2 board, then the answer is yes. But, the 4.2 sketch will not work on a 3.1 board due to it missing some of the features of the newer boards.

Michael said...

Hi Dan,
I have a question to ask, instead of the arduino board, i would to use other microprocessor like the PIC18F87J50. Is there any advice in doing so? like any difference in connecting the easydriver to the microprocessor?
Thanks

With Regards
Michael

AlexP said...

Hello! Can I cut the jumper SJ1 (APWR) and use logic power +5 in order to avoid warming up ED. Because my ED in LM 317 is very hot. I have load supply +24v. This will help me?
P.S. Sorry for my English.

happytriger2000 said...

Hiya,
Thanks for your code it worked quite well for a 3phase stepper driver: http://www.youtube.com/watch?v=qS4vIdKphJM.
By any chance if you have firefly file for this code?

thanks

Giovanni Greco said...

E' possibile aggiungere due interruttori per la marcia avanti ed indietro ?

Anonymous said...

Hello I will using Easy Driver 4.4 for senior project. Is it necessary to use MS1 MS2 features. or can i just use Dir, Step, Sleep Features to run my motor.

Dan Thompson said...

Hi Anonymous,

I haven't looked at the data sheet in a while. But yes, I believe it's possible to leave MS1 and MS2 disconnected and it should just default to full step mode provided everything is grounded properly.

But when in doubt, check the data sheet of the chip on the board.

Good luck!

Anonymous said...

Thanks for your reply. I will check the datasheet. One more question. can i use same code from you previous tutorial on V3.1 to run V4.4?

Dan Thompson said...

I can't see why it wouldn't work.

Anonymous said...

The Reason for asking is because in your last tutorial you mentioned

" WARNING Easy Driver v4.2:
Please do not attempt this tutorial with new Easy Driver v4.2 board."

will this motor work with this driver
"Stepper Motor with Cable"
https://www.sparkfun.com/products/9238


thank very much for you help.

Dan Thompson said...

The warning you speak of is more related to the hardware setup (wiring). The code should work fine. You will obviously have to set the pin variables in the code to suit your setup.

And yes that motor should work. You should always need to check the data sheet of you motor and match it with a suitable power supply though.

Dan

Anonymous said...

Hello :

pls, can you inform how get 200rpm with easy driver and motor 1.8º?

dmlcompra@terra.es

best regards

JhaJha said...

Hello

can someone help me out getting a switch in this circuit, i have tried everything with out any luck.
Arduino and programming is completely new to me altho i have managed to set speed + steps with this circuit.
im having it do 1 180 degree turn
all i want to add now is a switch to control when i want that turn

thx in advance
(can also email me at blackbladebf2@hotmail.com)

Dan Thompson said...

Hi JhaJha,

Have a look at this tutorial:
http://danthompsonsblog.blogspot.com.au/2011/12/arduino-push-button-onoff-example.html

It should give you the elements you need to convert this code to incorporate a momentary on/off switch

Good Luck!

JhaJha said...

Heya Dan,
i looked at the same example be4 but since i don't know what was alowed with the arduino i could not figure it out, but since you posted the same example i went on again trying stuff with the codes, i realized the power of these { },
got it right after a couple of errors, read a few sheets about inputs and outputs but in the end i got it working, ty so much for getting me back on the right track, great tutorial easy to get even if your completely new.

thanks again
best regards...

JhaJha said...

also i forgot to mention now i need to get a sensor in there,
so if the switch gets pressed it wont work, the sensor has to be "HIGH" + switch pressed for it to work, i will keep you posted on my project, will make a video and post it when i get the chance

thx again...

Sean said...

To SLEEP or to ENABLE, that is the question!

Can you go over the benefits/drawbacks of using SLEEP vs. ENABLE? It looks like SLEEP might draw less power than ENABLE, but in some ways they look very similar, so I was curious as to why one is preferred over the other...

Anonymous said...

Hi Dan,
Thanks for the tutorial. It works good. At first I had a problem though, which I would like to share as it can save some time for others.
I build it exactly like described above, but when I started it the shaft did not turn. It clearly was excited though; the motor coils 'clicked'. I saw voltages going up and down through the cycle when I replaced the motor with a multimeter. Still the shaft did not turn. After some time I discovered that there is a very small hole on the right side of the motor coil B pin on the EB board (v4.4). When soldering, I was apparently a bit careless and accidently dropped some solder in this very small hole, creating a shortage between the right motor B pin and this hole. When i redid the soldering, all worked perfectly.

So when people have similar problems, check your soldering.

regards,
thijs

femtoduino said...

Hi Dan, I've successfully ported your EasyDriver example to Android Java + IOIO ...Thanks for the awesome tutorial! I'll add the example to github shortly

femtoduino said...

Here is the sample code to run the EasyDriver with an IOIO board:

https://github.com/aalbino/ioio-easydriver

Mike sweeney said...

Dan

Thanks for this. Working on the raspberry Pi.
Your articles sorted out allot of questions for me.

Got it all working.

Mike

Brandon Hentges said...

Hi Dan,

I am trying to jump the 5v pin on a ruggeduino to the pwr in pin on the ED. Is it possible to run the ED with the ruggeduino external power supply jack. Currently i have a 5v power supply plugged into my ruggeduino. Then i jump the 5V and Gnd from my ruggeduino to ED's Power supply in. Is this possible or will i have to power the ED directly from the wall outlet?

Thanks for all the help
~Brandon

cbrandy said...

Hi.
Nice and clear code.
I'm struggeling to find code for arduino with easydriver. Can you please send me a whole CNC Arduino code??
s176265@stud.hioa.no

eheadj said...

Hello Dan, I was wondering if I will be able to power the raspberry pi FROM the Easy Driver V4.4/ After all it has a 5V output, correct? Do you think it would cause any problems to the Pi???

Anyone..

Thanks a lot for your great tutorial!