3 Simple Ways to
Share What You Make

With Instructables you can share what you make with the world — and tap into an ever-growing community of creative experts.

PhotosPhotos

Share one or more photos of a project, recipe, or whatever you've made, quickly and easily.

Step by StepStep-By-Step

Share your step-by-step photos with text instructions of what you made so others can do it too!

VideoVideo

Share your how-to video. You'll need your embed code from a video site such as YouTube.

How to wire an arduino-based 3-axis CNC machine

Step 5Sample arduino code

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() {
Serial.begin(9600);
pinMode(DIR1_PIN,OUTPUT);
pinMode(STEP1_PIN,OUTPUT);
pinMode(DIR2_PIN,OUTPUT);
pinMode(STEP2_PIN,OUTPUT);
pinMode(DIR3_PIN,OUTPUT);
pinMode(STEP3_PIN,OUTPUT);
}


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.
delay(DELAY);
Serial.println(">>");

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.
delay(DELAY);
Serial.println("<<");

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.
}

« Previous StepDownload PDFView All StepsNext Step »
16 comments
Mar 28, 2012. 10:40 PMbufalloo says:
I'm still confused. I'm having trouble finding how to interpret G-code to the stepper drivers. If possible, could you give me some pointers on how to find some sample code to do this? For example, could this be configured to be used with replicatorg for 3d printing?
Aug 14, 2010. 4:28 PMG00 says:
Thanks for the tutorial aggrav8d. I am proactive with CNC but not too much with Arduino. Your tutorial is pointing me to the right direction for one of my projects. I really want to get rid of CNC software and make an stand alone application for a very specific project that shouldn't need G-Code at all. Although I want to see the way the rep-rap application interpreter works, good tip }{itch. I think that there is a critical mass on hobby CNC and Arduino, so the mix is going to be interesting. My Open Source project by the way, is an Arduino controlled CNC Coil Winder. If someone is interested or had been working on that let me know. Cheers!~
Apr 17, 2011. 4:13 PMhulfarsson says:
Hi, are you still into rolling coils? I´m rolling single string pickups with an Arduino based rig, see here-

http://www.youtube.com/watch?v=3I_7oAJTuHI

I would love to see your solution!
Mar 15, 2011. 3:49 PMkikiclint says:
design your own and cut out all of the little laminates and the cases, and figure out a way to wire it all correctly. All you have to do is assemble. If it is precise enough, you could mill the rotor and everything and have it create a tightly packed motor. Not sure how you would magnetize the center portion though after milling it. I saw a video that showed the take apart of a larger stepper. Google around for it and try to recreate it.
Aug 9, 2010. 10:13 PMdiluded000 says:
I built a system that runs G-code output from variety of applications, using the parallel port on a old Pentium 100, input to a CNC controller board, that runs some 90 oz/in Nema 23 steppers on my Sherline milling machine. Originally I hand built an L297/298 based circuit, but it wasn't stable enough to run for hours on end. I designed an Arduino based control of temp on an annealing oven, but the DOS based TurboCNC does the trick for me on the milling machine. My controller was $85 from www.lightobject.com. Y'all should check out the forums on CNCzone, I think it is the best resource out there for CNC. They have a long list of G-code generating and interpreting utilities from various sources (many free). While there are lots of machine specific tweaks, G-code is common denominator among most CAD/CAM setups. And this system would look lots better with some corrugated wire covers that they sell at the auto parts store. Get some cable ties the same color as the Flexon, it looks much more badass.
Jun 15, 2010. 6:30 PMmaxpower49 says:
do you have to figure this out every time you want to cut something or is there a program that will do this for you
Jun 19, 2010. 5:54 AMalfredhenriksen says:
What is wrong with G-Code? Or are you talking about M-Code who are different from each machine. The G-Code is already a standard, see "RS-274D is the recommended standard for numerically controlled machines developed by the Electronic Industry Association in the early 1960's. The RS-274D revision was approved in February, 1980. These standards provide a basis for the writing of numeric control programs. " Please elobrate that how you mean that the standard is junk as it have be around for a long time and have gone many interations. Sure, there is limits but all programming languange have limits! :) http://www.linuxcnc.org/handbook/gcode/g-code.html http://en.wikipedia.org/wiki/G-code In my world it's a wording I follow: "shut up and hack" Why are you asking for money, are you motivated by money or by programming something new and beautiful? :) Either way, I love this tutorial. Thank you! Finally I got a way to do simple operations on my cnc machine without EMC.
Jun 20, 2010. 4:26 AMalfredhenriksen says:
Please be more concrete than a quote from wikipedia. Which specific extensions and variations don't work? ................."EXTENSIONS AND VARIATONS" is not even talking about the essensial G-Code, as the machine would not comply the RS-274D standard, and who will buy it? .....I suspect that these manufactures make the new G-Godes to partly lock their customs to their product, to custom fit their product (=faster?), getting a competion edge and they've documented the proprietary g-code so it's nothing more than looking in the manual, and thinking about them when porting to other machines, or make a new G-Code from a CAD/CAM-program. A problem can always be solved! :) Most of these codes disappered when a company disappered. It's really not a problem as long one KNOW S the limits of the software/hardware and take care to avoid these bad apples. :) It could be a problem several years ago, but it's a classic evolution of something old and beautiful. .............................................................................................................. How was Linux created? How was Microsoft created? How was Apple created? They wrote/bought/made code before they could earn money. It's called dedication and risk-taking in a business world. They all took a chance, hoping making money. (Linux was created by a strong personal NEED) I have been around people like you and I don't need to prove anything for you. Have a nice day.
Jul 20, 2010. 1:10 AMziggalo says:
actually any kind of interface that will convert g-code to arduino-friendly code that'll work with this machine.
Jul 20, 2010. 12:56 AMziggalo says:
I have a question. Would you be capable of making this machine work with say, Mach 3? If you are able to create some software that would make this possible, How much money are we looking at? I really love this arduino driven CNC, and it would be even more perfect if it was able to work with existing post-processors/g-code generators.
Jun 16, 2010. 5:08 AMcmchapman says:
I'd give you some money over on kickstarter.com if you really want to write an open standard interface for milling hardware and software. I'm sure other people would too!
Jun 16, 2010. 5:10 AM}{itch says:
The folks at rep-rap have written an arduino based G-Code interpreter. You can just send the g-code commands via serial and the arduino will calculate the required stepper motor movements. I haven't used it myself yet but have had a look at it for a CNC machine i have in the works. Here's the link.
Jul 20, 2010. 5:59 PMdylanwinn says:
FYI: I have had NO luck getting that to compile, but I'll continue working on it and post here with any progress.

Pro

Get More Out of Instructables

Already have an Account?

close

All Steps Viewing
View all steps of an Instructable on the same page when you're a Pro Member.

Upgrade to Pro today!
73
Followers
7
Author:aggrav8d(Marginally Clever Software)
I want to make a living through robotics and inventing. I don't get to travel enough.