Instructables

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

Featured

Step 5: Sample arduino code

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

 
Remove these adsRemove these ads by Signing Up

can this code interpret g code??

can this code interpret g code??

bufalloo2 years ago
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?
aggrav8d (author)  bufalloo2 years ago
http://www.instructables.com/id/100-Mural-drawing-robot-also-works-as-a-giant-et/

open source code for this other tutorial does it.
aggrav8d (author) 3 years ago
SENSOR are for limit switches in a future project I will detail on instructables.
G003 years ago
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!~
aggrav8d (author)  G003 years ago
If you can make a robot that can create motors, steppers, or servos, then you have my undivided attention :)
diluded0003 years ago
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.
maxpower494 years ago
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
aggrav8d (author)  maxpower494 years ago
There are a few open source programs now that will convert a DXF file to G-CODE. Unfortunately the standard for G-CODE is effectively junk. From what I read, nobody sticks to it. What needs to happen is someone needs to write a standard interface between mill software and mill hardware - just like drivers for any other peripheral on your computer. Hmm... Anyone want to pay me to do this? I could dedicate a few months to the problem and bang out a pretty good solution.
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.
aggrav8d (author)  alfredhenriksen4 years ago
I think you are confused on a number of issues.

"Due to the lack of further development, the immense variety of machine tool configurations, and little demand for interoperability, few machine tool controllers (CNCs) adhere to this standard. Extensions and variations have been added independently by manufacturers, and operators of a specific controller must be aware of differences of each manufacturers' product." -- the wikipedia entry for g-code

Maybe you have the freedom to spend all day writing code and not worrying about paying your bills. Are you rich? Do you still live in your parent's basement? Either way, I don't live in your world. Please post some instructables of your own before you tell me to shut up.
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.
aggrav8d (author)  alfredhenriksen4 years ago
If you don't need to prove anything then why did you start this debate? Isn't your point to prove that G-CODE is OK? I don't believe that G-CODE is really all that great as a standard. Think about it: you have to convert a DXF file to g-code before it will run on a machine once for each type of machine. That doesn't sound like a proper standard to me. "But!" you say "No two machines have the same envelope, tools, or extra features!" So? No two computer monitors have the same resolution but they still make it work. What I should have is a standard driver API that lets me query device properties, tool types, and send it comletely standard instructions while receiving real-time feedback. That way the job of calculating the best path for the tools is abstracted in software that could be written by anyone with access to the API, which would lead to more developers being able to invest better at improving the software. That's economical and benefits everyone. No secret knowledge of bad apples required. No writing custom control software for every hardware platform. You analogy is junk. I would create the code but I don't have the time UNLESS someone can free me from my obligations to rent and bills. Apple has the time to write - they had capital at the start to make an investment. Linux has time to write - they're all volunteers who work other jobs. I am not in either of these situations. Also, it's called "Return On Investment". What, exactly, are "people like [me]"? People who make you explain yourself? People who don't agree with you? People who punctuate and capitalize appropriately?
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.
aggrav8d (author)  ziggalo4 years ago
GRBL probably bridges the gap for you. If not, I'd be willing to take a look and give you an estimate. I'm still working on perfecting my CNC, at which time I will need to tackle this step (and having a little financing would make it a lot easier)
actually any kind of interface that will convert g-code to arduino-friendly code that'll work with this machine.