Step 5: Sample Arduino Code

// for duemilanove atmega328 arduino board + easydriver stepper controller
// dan@marginallyclever.com 2010-06-15
#define DIR1_PIN 4
#define STEP1_PIN 5
#define DIR2_PIN 6
#define STEP2_PIN 7
#define DIR3_PIN 8
#define STEP3_PIN 9
#define DELAY 150

void setup() {

void loop() {
int i;

digitalWrite(DIR1_PIN, LOW); // Set the direction.
digitalWrite(DIR2_PIN, LOW); // Set the direction.
digitalWrite(DIR3_PIN, LOW); // Set the direction.

for (i = 0; i<6800; i++) // Iterate for 4000 microsteps.
digitalWrite(STEP1_PIN, LOW); // This LOW to HIGH change is what creates the
digitalWrite(STEP1_PIN, HIGH); // "Rising Edge" so the easydriver knows to when to step.
if((i%2)==0) {
digitalWrite(STEP2_PIN, LOW); // This LOW to HIGH change is what creates the
digitalWrite(STEP2_PIN, HIGH); // "Rising Edge" so the easydriver knows to when to step.
if((i%4)==0) {
digitalWrite(STEP3_PIN, LOW); // This LOW to HIGH change is what creates the
digitalWrite(STEP3_PIN, HIGH); // "Rising Edge" so the easydriver knows to when to step.
delayMicroseconds(DELAY); // This delay time is close to top speed for this
} // particular motor. Any faster the motor stalls.

digitalWrite(DIR1_PIN, HIGH); // Change direction.
digitalWrite(DIR2_PIN, HIGH); // Change direction.
digitalWrite(DIR3_PIN, HIGH); // Change direction.

for (i = 0; i<6800; i++) // Iterate for 4000 microsteps
digitalWrite(STEP1_PIN, LOW); // This LOW to HIGH change is what creates the
digitalWrite(STEP1_PIN, HIGH); // "Rising Edge" so the easydriver knows to when to step.
if((i%2)==0) {
digitalWrite(STEP2_PIN, LOW); // This LOW to HIGH change is what creates the
digitalWrite(STEP2_PIN, HIGH); // "Rising Edge" so the easydriver knows to when to step.
if((i%4)==0) {
digitalWrite(STEP3_PIN, LOW); // This LOW to HIGH change is what creates the
digitalWrite(STEP3_PIN, HIGH); // "Rising Edge" so the easydriver knows to when to step.
delayMicroseconds(DELAY); // This delay time is close to top speed for this
} // particular motor. Any faster the motor stalls.

<p>This is a Great Instructable Sir! Your explanation on how to wire the steppers and the easydriver boards to the arduino, is very nice. <br></p><p>I too have made a small CNC <br>machine using easy drivers and your guide really helped me on the wiring part.</p><p>Here is the link to my cnc machine:-</p><p><a href="https://www.instructables.com/id/CNC-Arduino-GRBL-Easydriver-Shield/" rel="nofollow">https://www.instructables.com/id/CNC-Arduino-GRBL-...</a></p><p>Thanks a lot for giving your time.</p>
<p>OK, got the 3 steppers doing different speeds using TB6560 drivers (only had to adjust the delay to make the code work), no idea what I need to do next to get gcode talking to the arduino. I can make the gcode but no idea how it is sent to the arduino.</p><p>Where do I look now?<br>Thanks ever so much.</p>
<p>i want to use the speed sensor to print accordingly to the moving paper. means to use the speed value and print on x axis at a time. </p>
<p>awesome info thanks! I have a question I would really like you to answer if possible... I'm trying to use AutoDrivers instead of EasyDrivers to drive my NEMA 23 Stepper motors.... I'm trying to use GRBL as you've clearly suggested, but I'm not sure how to make GRBL recognize the AutoDrivers... The drivers are these <a href="https://www.sparkfun.com/products/11611...." rel="nofollow">https://www.sparkfun.com/products/11611.... </a> I had those drivers already in my possession, and would rather use them than spending another 80 bucks for a GRBLShield... </p><p>Any help is greatly appreciated!</p>
<p>This a great instructable with lots of ideas, if you could please post a schematic of the wiring I would be gratefull.</p>
I'm just getting into Arduino, so I do apologize for the question, however is it possible to use an Arduino Uno for this project?
Hi sir!....can u plz tell me how do i use the l298n instead of the easy driver?<br>Plz give me a detailed guide....its my computer project and i have to finish it....thanks....dan
Heyy, It's not possible for me to get 4 easy drivers to use it with 3D printer , rather I would like to use my own drivers, I 'm thinking to use L293D or simple H bridges to run my Bipolar stepper ( 4 wires) , how can I make it run , I have seen number of programs and software like grbl for easy drivers ,Adafruit motor shield V1 and V2 , grbl shield but not with H-bridges , so the issue is pretty common for many people as well. <br>I tried two out of three motors , uploaded grbl to Arduino and sent Gcodes from Universal Gcode sender , only one of my motor seem to work (however very slowly nad an irritating &quot;buzzzzzzzz&quot; like noise is coming out of it). I tried some variations with steps for different axis and I could see it move with 10 steps/mm. <br>I don't know what to do. <br>Can't you make easy drivers or motor shield or grbl shield on your own ? <br>Also I need a 4 axis driver as I would be using it for my 3d printer. <br>Thanks, any help would be appreciated.
<p>How did you get the universal Gcode sender to work? I went to gethub and downloaded it. I clicked on the file that had Jar.Jar in it and nothing happened and then clicked on the sw file and nothing happened! What do you do with the github downloaded file to make the sender come up? What are the steps? I am using windows 7 64.</p><p>Thanks</p>
<p>Will this work with Arduino UNO or will it only work with Arduino Duemilanova ?</p><p>Is it possible to connect 3 stepper drivers with ONE EasyDriver ? ( i doubt it's possible, but wants to know about the possibility for that )</p>
<p>Yes, this will work with all flavors of Arduino.</p><p>Short answer, Yes you can connect all 3 to the same easydriver but they will all turn the same.</p><p>Long Answer: Stepper motors, unlike &quot;regular&quot; DC motors, have 2 sets of coils inside. To turn a stepper, you need to alternately turn these coils on and off, which is what the Easy-driver does. So, if you want to control 3 stepper motors... you need 3 drivers.</p>
<p>I have an Uno too, I'm beginner totally. Output on Uno 5, 6 and 9 are ~ PWM, is that the same output that you sketched ?</p><p><br><a href="http://2.bp.blogspot.com/-M9WMQFNfXEQ/VOgnMnz18sI/AAAAAAAACIM/ObD7iwgv4-Y/s1600/arduino_uno_large.png" rel="nofollow">http://2.bp.blogspot.com/-M9WMQFNfXEQ/VOgnMnz18sI/...</a></p>
<p>another question here, can I connect one Raspberry Pi, directly to 3 Stepper Motor, and control all 3 of them to turn different direction/speed ?<br>or must I connect a Raspberry Pi -&gt; 3 EasyDriver -&gt; 3 Stepper Motor</p>
<p>Nice one my friend! Let me pick your brain up. Using the above and some other tutorials around the web, I put together 3 easy drivers controlling 3 stepper motors, used an Arduino Uno board with external power supply. Used some code available online / above. So far so good. My question is what's the next step. How to get this setup work with the CNC or 3d Printer in the production environment? How the G-Code file be read by this setup? Actually I'm after a full work flow for using easy driver with arduino uno. Firmware &gt; XLoader &gt; ????? Please help. Thanks</p>
<p>I wanna ask you...i made the seem with you...but i got trouble....question : how much current did you need to supply the shield.... voltage :12voltdc and the current to supply 3 motors is???</p>
<p>you need to check the specs for your steppers and see what V they run. I used a 12v wall wart and spliced 3 wires of the 1 pos/neg. and ran them to my easy drivers. worked for me. they are capable of 30v max if you have large steppers prob around 24v. run a second walwart to your driver board they are anywhere from 5-15v chek specks and run a line to that.</p>
<p>trying to make this code run 6 steppers, will this code do that?</p>
<p>No the code has to be tweek'd for 6 to run. unless you spliced 2 motor wires together to run off the same pins might work but they would be limited to doing the exact same thing.</p>
<p>I paid $2.25 for my easy drivers on Ebay. exact replica's anyway!</p>
<p>Soo could you plug this into a program like Mach3 or linux cnc?</p><p>meaning, if you have your arduino interpret Gcode can you hook this up to a program like the above?</p>
<p>I have ordered grbl cnc shield v3 from ebay from this link <a href="http://www.ebay.in/itm/TDP004-ARDUINO-CNC-SHIELD-V3-00-GRBL-COMPATIBLE-/161520584178?pt=LH_DefaultDomain_203&hash=item259b6085f2" rel="nofollow">http://www.ebay.in/itm/TDP004-ARDUINO-CNC-SHIELD-V...</a></p><p>now when i try to connect this shield it requires power before i mount it onto my arduino uno. The issue is that before i could use the setup i have to detach the shield from uno and power it first and then mount it and connect it to pc. is there any solution to this? </p><p>do reply asap</p>
Could you add a fourth stepper motor to pins 1 and 2? Could you also control a heatbed and an extruder (nichrome wire), because I am trying to build a 3d printer. Also how do you control the z-axis with only one motor.
<p>Pins 1 and 2 are required for data input, you could only use them IF you set it up to just run the program you've already loaded, that didn't require any additional input. (in other words, no)<br>Running it with an uno, you really don't have enough output pins to control anything additional. If you built it with a mega, or something else, then you'd have additional data pins for controlling other things, but honestly it sounds like you should be using a ramp board anyways.<br>Z axis only requires 1 motor. I've seen some designs for extruder based printers that use 2 Z axis motors, but it's no more required than 2 motors on the X axis.</p>
<p>If you could answer a quick question for me, I would sincerely appreciate the help. </p><p>I'm making a homemade laser-cutter, and the problem I'm facing at the moment is how to trigger the laser ON instead of driving a stepper motor (the motor for the Z-Axis). Now, in your photo for Step 2: &quot;Wired the Stepper Motor To the EasyDriver&quot; you show &quot;5 Out and Ground.&quot; Are these pins something I can control off and on? I'm using GRBL on the Arduino Uno, so I'm not sure exactly how I would write the GCode to do this. </p><p>Thank you so much!!!</p>
<p>Hi jmsiefer,</p><p>What you need is a relay module that will control the trigger of the laser. </p>
<p>I'm building my own cnc router at this moment, using scrap materials. The constructrion of the hardware is allmost finished, en looks quite good! Time to start the programming en controlling of the steppermotors. I surely will use this handy tutorial. But what seems most unclear to me now is how i convert a drawing to g-code, and how do i get the g-code to the arduino?</p><p>Any help with this would be very appreciated!</p>
<p>Look up GRBL, it's a program you upload to the arduino to convert g-code to the motor drivers.</p>
<p>Nice project!! I had a quick question. I was looking into getting the easy driver and 3 steppers for my cnc build but i'm not sure if my steppers will work with the easy drivers. all the steppers i have looked at so far are rated for 1.3A or so. the easy driver is only rated for 750mA. Will that stepper work for me or do i have to look for one rated under 750mA. I would appreciate it</p>
<p>Hi diyman!</p><p>I'm not the author, bus as I've been studying and searching on how to make these 3D moves, and from what I got, usually the stepper controllers I found were rated to a determined Current(A) on EACH coil. So as this steppers have 2 coils, I've read that it means it can handle a total of 1.5A. <a href="http://www.ebay.co.uk/itm/171370859610" rel="nofollow">This one shield</a> seems to be what I look for:</p><p>&quot;4 H-Bridges: per bridge provides 0.6A (1.2A peak current) with thermal protection, can run motors on 4.5V to 36V DC&quot; </p><p>I'm about to order it, but I'm still colecting some other hardware (and few cash ;) )</p><p>So I think that one you said works fine! I looked over it, but the L293D looks better and cheaper for me. Also I'm pretty sure I've read somewhere, that you can actualy kind of stack more L293D on each of the provided ones to increase Current! I won't need that, but it's a possibility.</p><p>For now I'll use it just to drive DVD Drive stepper motors, which I guess won't need more than 200/300mA, for testing purposes, to get it bigger later, with the same Electronics, but with Nema (17, probably)stepper motors driving a threaded rod.</p><p>I'm so eager to finally get to that big project! But I'm learning all by my self almost from 0, so it takes some time.</p><p>To the author, congratulations! There is actually a fault on guiding people on the electronics part of these machines, but you covered it really well! Thank you for that, and I hope to be able to return my knowledge back with a good tutorial and my good intention to exchange knowledge.</p><p>Best regards</p><p></p>
<p> can this code interpret g code??</p>
<p> can this code interpret g code??</p>
<p>hello..i am having issues with the output.</p><p>this is what i have done so far:<br>1- downloaded hex file from <a href="https://github.com/grbl/grbl/downloads" rel="nofollow">https://github.com/grbl/grbl/downloads</a><br>2- using Xloader, i have uploaded the hex file.<br>3- downloaded grbl controller.<br>Now when see the outputs from arduino uno pins, except pin 2,3,4 and 5, all pins are consistently high, while these 4 are consistently low.<br>i need to know where i am making a mistake.<br>The above mentioned steps are what exactly i have done not even a single thing different.<br>Please help&hellip;<br>Thanks</p>
<p>Hi, great job!.</p><p>I'm trying to use the adafruit motorshield as stepper controller but I don't know the pin configuration for grbl files.</p><p>Do you have any sketch about this?</p><p>Thanks a lot </p>
Can I run 4 steppers with Arduino and stepper drivers ? <br>Why do you have to use easy drivers , when you could just buy cheap ICs like L293Ds and ULN2003/4 to drive your steppers? <br>
<p>Each driver needs 2 digital lines: Step and Direction. So, you can run as many drivers as your arduino has pins for.</p>
<p>I'm still new to the Arduino and can't answer your 4-axis question, but I am currently working on building a CNC using the L298 driver. To use the L298 &quot;like&quot; an EasyDriver, you also need the L297 controller (or equivalent) and some other components. The L293D is only a driver (handles the voltage and current) and requires a controller just like the L298. The ULN2003/4 is a DARLINGTON ARRAY and is not designed for the current desires of a stepper.</p><p>Datasheets:</p><p><a href="http://www.ti.com/lit/ds/symlink/l293d.pdf" rel="nofollow">L293D</a></p><p><a href="http://www.farnell.com/datasheets/1696835.pdf" rel="nofollow">L297</a></p><p><a href="http://www.tech.dmu.ac.uk/~mgongora/Resources/L298N.pdf" rel="nofollow">L298</a></p><p><a href="http://www.doyoung.net/video/DATASHEET/PDF/ULN2003.pdf" rel="nofollow">ULN2003</a></p>
<p>Hello <br>The Lord's great imaged motor control and stepper motors rather for the help of the driver. <br><br>I just have one question .... Can I control L298 driver, front using the Arduino for the help mach3</p>
<p>I am currently working on building a CNC using the L298 driver. To use it &quot;like&quot; an EasyDriver, you also need the L297 controller (or equivalent) and some other components (look up PDF).</p>
Hi. Are you using the grbl code in Arduino for the CNC? <br>Or have you made one your self? <br>What prog are you using for designing and g-code? <br> <br>Best, <br>Andy
Thanks for the info...Can I wire up 3 different stepper motors to work on my cnc or do they have to be the same? I was wondering if I could adjust the movements on each stepper separately
You can. It's a lot easier to use three of the same thing - less variation means less debugging.
i wanted to make my project, cnc milling, wireless. so i need inputs on how i would be able to send gcode generated in eagleCAD wirelessly?? and what all necessary changes i would need to make in electronics section of the project. kindly help.
Sorry, I can't help you with this. I have almost zero experience with wireless.
thanks for your response. i had some input that we can send the gcode generated via a serial-to-bluetooth converted and can recieve the code in reciever blutooth which we will interface on controller, in place of serial port. i was just wondering whether it will work or not
I don't see a problem as long as the connection doesn't drop. Wifi, cable, bluetooth... it's all the same.
yes, you right. i am doing my final year project and our guide is expecting us to do some out-of-box change..so someone in our group suggested making it wireless, and now we are juggling to get the idea implemented..
Could these NEMA17's be enough to have a dremel on it and cut wood? Or are they too weak? Nice tutorial!
Very well said. I'm a noob and need serious help. If you don't mind, I'll compile a list on the programming side of things since I only plan to engrave my business name and phone number on keys. Thanks a bunch. Btw, I love this tutorial.
I'll hopefully be able to release a schematic/EAGLE file for a complete self containing driver using an Arduino, 3 easy drivers along with maybe an Ethernet shield. Thanks for the instructions, now I have something to go off of!

About This Instructable




Bio: I want to build my moon base with remote controlled robots and solar sintering.
More by aggrav8d:Aduino I²C 2-Way Communication Arduino Starter Kit 2: Use Buttons for LED Control Arduino Starter Kit 1: First circuit and program to control an LED 
Add instructable to: