CNC Programming with G-Code:
The Definitive Guide in 2018
G-Code is the fundamental language of CNC Programming and this is the ultimate guide to CNC G-Code Programming. It will sharpen your skills so that you’re a great CNC G-Code Programmer by covering topics from basic CNC Programming all the way through to advanced topics such as Macro B programming. You get great examples, tips, videos, free software, and more.
What is CNC Programming and how can it help you?
G-Code Standards and Dialects: What’s different; what’s the same?
CNC Coordinates: Learn to do what manual machinists do on your CNC machine
G-Code Cheat Sheets and CNC Programming Secret Weapons
Get Productive with CNC Programming Software
Get Your CNC Machine Ready to Run Your Program
Jedi CNC Programming Topics, Techniques, and Resources
What is CNC Programming?
CNC Programming (Computer Numerical Control Programming) is the art of programming CNC machines to make parts. A CNC Program is a text file that contains g-code.
What is G-Code?
G-Code is the language used to control CNC machines. It’s one type of CNC programming that CNC programmers use, the other type being CAM programming. CAM programs will generate g-code from a CAD drawing, but the end result is still g-code.
Your machine’s CNC controller probably executes g-code, although there are other possibilities–Heidenhain, Mazak, Shopbot, and others have proprietary formats. Some machines with proprietary formats can also run g-code. It is the Lingua Franca (working language) of CNC.
In order to make a part on a CNC machine, you tell it how to make the part using a G-Code Program.
What are the different methods of CNC Programming?
CNC Machines are programmed using one of three methods:
- CAM Software
- Conversational Programming
- G-Code Programming
Often, it’s advantageous to use multiple methods together. For example, you might create an initial CNC program using CAM Software and then edit the g-code from the CAM Software using G-Code Programming to make the program manufacture a part faster.
Why should I learn CNC Programming?
Every CNC machinist should know g-code. If you’re interested in CNC and machining, you should too.
We recently did a survey to assess the g-code skills of our readership. You should not be suprised to learn that many are quite proficient with G-Code:
We were impressed at how many readers can write g-code programs from scratch. In fact the overwhelming majority read, write, or tweak programs on a regular basis. If you’re not yet able to do that, you need to learn if you want your skills to be on part with others.
CNC Programming can be a valuable skill to possess. According to Glass Door, CNC Programmers receive an average salary of $54000/year. Compare that to CNC Machine Operators that get $42,000/year and CNC Machinist’s who get $42,000/year.
Being a proficient CNC Programmer can boost your income pretty substantially based on those numbers.
Learning Conversational Programming is a start. Picking up Conversational CNC along with some MDI work will soon make you as productive if not more productive than a manual machinist on a manual machine.
Getting good with CAM Software is even more important. You’ll be able to create parts that are just not possible to do manually, or that would take too long to be worth doing.
But being a capable g-code programmer able to tackle macros and such is the final rung on the ladder. It may come as a surprise to most CAM users, but there are many things a g-code programmer can do that simply aren’t possible with CAM. In general, G-Code Programming is ideal for these kinds of tasks:
- Simple parts can be easier with g-code than CAM. You want a rectangular cover plate with 4 holes in it. If you’re proficient with g-code you’re done before you can create a CAD model, run it through CAM, get the gcode onto the machine, and run it. With Conversational CNC, you don’t even have to be proficient at g-code. For all those cases where you thought manual machining was faster, g-code or Conversational CNC will make the CNC machine an even better choice.
- Getting a Second Opinion on Your CAM Code. Even expensive CAM software can have bugs. Or, if they’re not strictly speaking bugs in the CAM, they may be problems with the post-processor. With just a little bit of knowledge about GCode and the help of a GCode Simulator, you can get a second opinion on the CAM-generated GCode and fix minor errors before you have to discover them on the machine.
- Specialized or Downstream Tasks that Complement CAM. As great as they are, CAM programs don’t do everything. There are many specialized tasks that are difficult or impossible to do with CAM. In addition, there are downstream tasks–things that come up when the job is actually on the machine that weren’t contemplated by the CAM or Post. Sometimes these tasks are relegated to GCode because the CAM software won’t do them. Sometimes it’s because the CAM is aiming at a generalized common denominator and something special comes up on the Shop Floor–for example, a slightly different cutting tool has to be used while we wait for another shipment of the normal tools. A simple feeds and speeds change in the gcode will handle it far faster than re-running the CAM, assuming we’re capable of making such simple changes.
For even more details on those special tasks that complement CAM, check out 37 Things CAM Won’t Do For You That G-Code Programming Can.
What’s the Best Way to Learn G-Code?
The best way to learn G-Code is slow and steady.
Find yourself a complete course like this one, start knocking out the lessons, work the exercises, and keep at it. Work at your own pace, and don’t move on to the next lesson until you’ve worked the exercises and understand the material.
Working the exercises is a whole lot easier if you’ve got some software to help you play with g-code. That’s what our G-Code Editor software is all about. It simulates g-code as well as decoding it for you. You can try out different g-codes and see visually what they do. Experimenting is one of the best ways to get a good grasp of g-code. At the end of each section is a Quiz to test your skills. Take the quiz and use the links on the questions to go back and review anything you missed so you’ll be solid before continuing to the next section.
We encourage you to sign up for the free 30 day trial–that’s plenty long enough to work through all the lessons free of charge. You’ll find G-Wizard Editor not only makes it easy to work with g-code and try the exercises in the course, but it has many features designed to make understanding the g-code easier. For example, it offers “Hints”, where it takes each line of g-code and explains in plain English what that code does. There’s nothing else like it available anywhere. For more information, visit the GW Editor Home page. Or, go head and sign up now for the free 30 days:
The second thing you should do aside from following a course and using a simulator like G-Wizard Editor is to start following some articles about CNC. Getting some random inputs about a variety of topics is another way to help the juices flow. You’ll see things that raise questions and get you thinking about the basic concepts in new ways. This helps all of the ideas to connect better. To get a good source of such inspiration you could hardly do better than to subscribe to our own CNCCookbook Blog. We’re by far the largest CNC blog on the Internet in terms of readership. We post articles for all levels of knowledge. Signing up is easy–you’ll automatically be added to the list if you start the 30-day trial of G-Wizard Editor. Or you can join 100,000+ other CNC Enthusiasts by visiting our signup page.
There Are Many Dialects of G-Code
Some wag once joked that the great thing about standards is there are so many to choose from.
There are many G-Code standards and dialect. While much of it remains the same from controller to controller (setting aside alternatives to G-Code from things like Mazatrol, Heidenhain’s Conversational CNC language, and others), there are important details and defaults you need to be aware of to understand the particular dialect of g-code your controller needs to be happy.
In terms of sheer numbers of users, the Fanuc dialects of G-Code are probably the most common among professionals and Mach3 among hobbyists. This is not to say they are better than other G-Code dialects, just that they are more common and so if you’re going to talk to other machinists or move around from job to job and machine to machine, it may be helpful if you’re familiar with those dialects and how they differ if your machine doesn’t use one of these two controllers.
For more details on the relative popularity of different dialects, check out our CNC Controller Survey.
G-Code has an extremely long history. The first attempts at standardizing it came out of the Electronics Industry Association’s RS-274 standard which has evolved to NIST’s RS-274NGC standard. The original EIA standards work was begun in the 1960’s but the first standard wasn’t released until 1980. Even though there are now standards (ISO has one too that is nearly the same as RS-274), it isn’t clear how many controllers out there are purely standards based. Indeed, many controls will claim to be some standard or other, but when you look closely at the details they’re pretty non-standard.
How Are the Dialects Different?
G-Code dialects differ in a variety of ways. Most manufacturers have added their own little bells and whistles to make their dialect better for competitive and marketing reasons. For example, Haas has a series of special g-codes for pocket milling, as well as some special parameters and capabilities on some standard G-Codes. It pays to understand the special capabilities of your machine because they were probably put there to save time based on feedback the manufacturer got from its customers.
In general, we see the following categories of differences between G-Code dialects:
– Which G-Codes are Supported. Not all controllers support all G-Codes. For example, many early lathe controls do not support the G71 and similar roughing cycles.
– G-Code mappings. Sometimes the same function will be supported by different g-code numbers on different controls.
– Parameters and Macro Programming. Parametric programming with macros is something that emerged after the basic standards were in place. Fanuc Macro B is probably the most common standard for it. Many controls are very limited in their capabilities around Macro Programming and there are a lot of detail differences around exactly how Macros work.
– Parameters. Many G-Codes need additional information to do their job, so they use other words (letters) to collect that information. Exactly which words collect which information can vary from one control to the next.
– Formatting. Some controls allow G0 or G00. Some insist on G00. Some allow numbers with no decimal, others insist on a decimal or even a trailing zero. “1”, “1.”, and “1.0” are all variations that may be accepted, rejected, or required when specifying the number 1.
We’ll talk more shortly about what all of this means, but for now, be aware that these differences exist. For simple programs and MDI use, obviously a lot of this won’t matter. But, for writing complex hand-written G-Code or trying to understand why the G-Code your CAM program emits isn’t quite right, you’ll need to be aware of the dialect issues.
For purposes of this tutorial, unless we specifically say something different, we’re going to assume you’re running a Fanuc controller. If you follow along with our exercises with G-Wizard Editor, you should run a Machine Profile for the Fanuc Controller, preferably by downloading our canned profile. When you go to program for your machine, you’ll need a profile properly set up for your machine!
How do the Dialects affect CNC Programming?
Obviously you need to be aware of what dialect you need to target for a CNC Program to run on your CNC Machine, but how do these dialects affect CNC Programming?
CAM Software uses what’s called a Post Processor to deal with g-code dialects. The role of the post processor is to translate an idealized language that’s usually internal to the CAM Software into a specific g-code dialect.
1. Look up what g-code dialect your CNC machines use. Are they Fanuc? Mach3? LinuxCNC? Or something else? Does the manufacturer claim compatibility with a standard?
If you’re a manual machinist, think of a CNC Machine as being just like a manual machine that has power feeds and DRO’s (Digital Readouts) on every machine axis. There’s more to it than that, but if you think of the machine that way, you get an idea of what you can make it do very quickly once you learn how. And, there isn’t really that much to learn either.
CNC Coordinate System for G-Code and Machines
The very first thing to understand is the CNC Coordinate System.
Each machine has its own specific axis orientation, and you’ll need to be familiar with how your machine’s axes are oriented. Here are some common types:
Mill Axes for a Typical Vertical Machining Center. Note: arrows show table motion in positive g-code direction. Handedness is spindle motion and reversed!
Lathe Axes for a Typical 2-Axis Lathe…
The cylinders in each drawing represent the spindle of the machine. Be sure to have a look at exactly how the axes are laid out on your machine. For example, horizontal mills are turned around considerably from the drawing I’ve shown. Lathes can get a lot more complicated than the simple 2-axis version I’ve shown.
4-Axis, 5-Axis, and More
Much more complex configurations are possible when you have more axes. For example, here is a 5-axis setup:
5-Axis Mill With Trunion Table…
Note that we have added two rotational axes to the basic mill diagram to provide an A-Axis and a B-Axis. In general, A, B, and C are rotational axes that rotate around axes formed by the X, Y, and Z respectively.
Expressing Coordinates in G-Code
Now that we know what the coordinate systems are, how do we express coordinates in G-Code?
It’s pretty simple: just take the axis letter and add the value. Spaces between the letter and its value are optional.
For example, a position that is 1 inch from 0 along X, 2 inches along Y, and 3 inches along Z is written as:
You get used to reading them all run together like that quickly, but you can format them with spaces to make them more readable:
X1 Y2 Z3
X 1 Y 2 Z 3
Again, you get used to keeping the letters with the numbers, so I wouldn’t add more spaces than just between the axes:
X1 Y2 Z3
That’s actually the easiest to read once you get used to it.
What About Units?
The example I just gave used inches, but in actuality the controller can be set to use either metric or Imperial. It’s up to you to know which default the system comes up in and to change the units as needed. Try not to change units in the middle of a program, do so at the very beginning and then stay with the same units. It’s too confusing otherwise. The G-Codes to change units only affect how the machine interprets the numbers. They don’t change your program. We’ll talk more about changing units in a future article, but for now, just be aware.
For rotational axes (which you’ll only be using on a 4 or 5 axis machine), we don’t use dimensions for the units, we use angles, typically in degrees. Rotating the 4th axis to the 90 degree position might be done as A90, for example.
Incremental Versus Absolute Coordinates
Sometimes, it is very convenient to refer to Incremental or Relative instead of Absolute coordinates. Let’s assume the tool tip on my mill is at X0 Y0 Z0 and I want to move it to X1 Y2 Z3 (I dropped the commas, which are not used in G-Code, because I’m just trying to get you used to the switch from how you learned coordinates in school, e.g. (0, 0, 0), to how it’s done in G-Code X0 Y0 Z0). I can make the move absolute or incremental and it doesn’t matter. “X1 Y2 Z3” does the trick since in either case we started from X0 Y0 Z0.
But, suppose your cutter is positioned at some point and you need to cut a 1″ square with the corner aligned to that point. Perhaps you’ve used your edgefinder to locate the cutter precisely on some feature of the part. This is easily done with relative moves:
In essence, move 1″ right, 1″ up in Y, 1″ left, and then 1″ down in Y. Now we have a 1″ square whose lower left corner is the initial point.
There are lots of cases where relative moves are handy so the ability to switch back and forth comes up a lot. We’ll show you how to make that switch when we talk about how to move with G-Code, but for now, just be aware that there are both Incremental and Absolute Coordinates.
Sometimes, we refer to relative coordinates with special axis letters. For example, IJK may be relative XYZ when defining arc centers. On some controllers, UVW may be used alongside XYZ to refer to relative coordinates without needing to change back and forth between relative and absolute modes. In other words, XYZ is used always as absolute and UVW is always relative.
For now, it is enough to be aware that incremental coordinates exist. A little later, we have an entire chapter just on the subject of incremental versus absolute coordinates.
The last Coordinate System concept I want to cover is that of Offsets. Offsets are another fancy way to think about relative motions. Let’s suppose you want to machine 2 identical parts. Each is held in a vise on your table at the same time. How do you make one program that can do both parts without having to change the program for the position of each part?
The answer is that we use a Work Offset. More detail on those later, but for now, imagine that Work Offsets let us position the X0 Y0 Z0 origin in more than one place. We can put one on the first vise and another on the second vise. Now just by changing the work offset the same program can work to make the part on either vise.
There are lots of different kinds of offsets in CNC, and the skilled CNC operator/machinist finds that offsets are an extremely handy way to nudge the behavior of a G-Code Program without having to change that program. Most CNC controllers have an offsets screen where you do that. I mention this because any time you get a chance to learn about offsets, take the time to do so. They’re digital power tools for the CNC machinist and are very handy. We’ll cover them in greater detail later.
It’s convenient to refer to planes for various purposes. A plane is a flat 2 dimensional space defined by two axes. For example, the default plane on most mills is XY. If you draw an arc without specifying a change in the plane, it will be drawn on the XY plane. There is a plane for each combination of the linear axes XYZ:
The G17, G18, and G19 G-Codes select which plane is active. More on G17-G19 when we talk more about arcs.
You’ve now got the basics:
– You know how to visualize the coordinate systems relative to your machine using the left handed rule.
– You know how to express coordinates in G-Code.
– You know what units are used to measure the coordiantes.
– You know there is the possibility of both relative and absolute coordinates.
– You know that offsets let you shift the coordinate system around for various handy purposes.
– You know about planes.
We’ll shortly introduce the notion of MDI, which is a simple way to use G-Code as though you are still a manual machinist. It’s a good introduction to the basics of moving your CNC’s axes. But first, we need to get you set up on G-Wizard Editor so you’ll have a CNC Simulator to use for practice during these tutorial lessons.
Hey one more thing, just in case you were wondering. Most CNC g-code dialects also have Polar Coordinates. it’s a more advanced topic that we’ll get to later.
1. Get out the manual for your machine and find the diagram that shows how its coordinate system works. Make sure to leave the manual handy, whether it is paper or online. We’ll refer back to it a number of times as we go through the various exercises.
2. Bring up G-Wizard G-Code Editor. By default, you’re in Mill Mode. There are views for Perspective, Top, Front, and Right. Download the sample engraving file from our download page. You want the file called HomeSwitchRearPanelEngrave. Start up GWE and do a File Open to load the downloaded file. Have a look at it in each view.
– Top is a view from the XY plane
– Front is a view from the XZ plane
– Right is a view from the YZ plane
Would you like a handy cheat sheet that tells you everything you need to know to do quick and dirty CNC Tasks? That’s what this section is all about.
G-Code Cheat Sheet? Now You’re Talking!
I’m going to let you in on a little secret: with just 10 g-codes and something called “MDI”, you can use a CNC to do everything a manual machinist could do. If you’re a manual machinist looking to become productive fast on a CNC, this is your secret weapon and shortcut. If you’re a beginner starting out with CNC, this is a way to quickly do some simple things before having to learn all the rest of it.
The 10 g-codes are:
G00 to move rapidly through air
G01 to move while cutting
F to control feedrate in a cut
S, M03, and M07/M08 to control your spindle
M08 to switch on coolant
M05 to stop the spindle, M09 to stop coolant
That’s all the g-code you need to do most anything a manual machinist can. That’s not to say the other g-codes aren’t useful, but this is quite a shortcut if you can pick up these codes and start being productive quickly.
I want to refer you to a special G-Code Cheat Sheet article that goes over all of this in full details and even gives you printable cheat sheet charts you can use.
There are basically 4 kinds of CNC Programming Software that will help you produce good g-code for your CNC machine:
- CAM Software
- G-Code Editors
- G-Code Simulators
- Conversational Programming Software
Let’s go over each to see how it can help you be more productive as a CNC Programmer. BTW, there are a ton of other kinds of CNC Software besides packages aimed directly at CNC Programming. For a good overview, see our article, “Complete Guide to CNC Software:
21 Kinds of Digital Tooling for CNC“.
We talked a bit about CAM Software already. It’s used to convert CAD drawings (or 3D Models from CAD) into g-code. 3D Printers use software called “Slicers” that does the same job.
Since this Guide is largely about programming in g-code, we won’t spend a lot of time talking about CAM. But, if you want a quick run through of the basics, try our article, “Secrets of CAM for Beginners.” It’s got a lot of great info and even walks you through making the same part using two different CAM packages.
The most basic tool for cnc g-code programming is simply called a “G-Code Editor”. Our very own G-Wizard Editor is once such program.
Why not simply use a text editor for cnc programming? After all, g-code is just text.
You could use a Notepad or some similar text editor. Many CNC’ers do. But they’re missing out on the significant productivity gains a good G-Code Editor can provide, such as:
- Text Editing Features: These features determine how easy it is to edit the text of your program. The important thing is that these features know g-code syntax, and are not just the standard features any text editor has.
- Informational and Power Editing Features: These features give you extra power in understanding your program, or in creating it quickly.
- Program Revision Features: The features help you to make mass revisions to your program, for example, to renumber the program lines.
- G-Code Simulator and Backplotter Features: These features allow you to watch the g-code program execute in simulation.
- Error Checking Features: If your goal is to make sure the gcode is ready to run when it hits the CNC machine, you want to have comprehensive error checking right in your Editor/Simulator.
Informational Features: G-Code Hints
One of the best things about our G-Wizard Editor is it gives you hints in plain English that tell you what each line of g-code does. The hint appears right below the backplot:
In addition to the hint that G03 is a counter-clockwise arc, look at all the additional information that’s given:
- Arc endpoints
- Arc center coordinates
- Arc angle
None of that is obvious just looking at the g-code program, especially in a text editor.
Special Power Editing Features
As you learn more about g-code’s structure, you’ll come to appreciate the power editing features a good g-code editor can provide. For example, lines of g-code are often numbered. What a pain it is to renumber by hand, but the g-code editor can do that for you automatically.
There’s a lot more a good g-code editor can do. Dig more deeply into them with our article on CNC G-Code Editors.
G-Code Simulators, Debuggers, Backplotters, and Viewers
G-Wizard Editor’s Macro Debugger control panel…
While most G-Code Editors include a built-in Simulator, they’re not all of equal power. In addition, you can buy Simulators that don’t include an Editor. I’m not rule sure what the value of that might be, but they’re out there.
Simulators may also be called Debuggers, Backplotters or Viewers. Their role is the same. They simulate the execution of your g-code in the g-code dialect you’ve selected. You can see the graphical backplot of the toolpath in the G-Wizard Editor screen shot above.
You can tell at a glance from a backplot exactly what moves the tool will make when the program is run. A very high quality simulator (like the one in our G-Wizard Editor), has a more capabilities than just a backplot:
- Your CNC Machine has what are called alarms. Certain things will trigger an alarm even though the backplot looks fine. A good simulator has alarms too so you can check your program and fix all the alarms before they show up on the machine.
- Macro Debugger. This is the ability to test and thoroughly understand g-code macros. Macros are the ultimate in CNC Programming power and flexibility, but they can be quite complex to create, understand, and debug. Hence a Macro Debugger is a valuable assistant when dealing with them.
- A Post Processor, just like your CAM Software uses. Most simulators don’t have a post. They make a lot of assumptions, which means they can’t accurately identify problems relating to g-code dialect. You want a simulator that has a post.
G-Wizard Editor has all 3 capabilities and more.
Doesn’t my CAM Software already have a Backplot / Simulator for this purpose?
This is one more thing to look out for, because the qualified answer is, “Maybe.” The issue is how the CAM program’s simulation is generated. If your CAM program includes a true CNC g-code simulator, then yes, it does have a simulated backplot and by all means use it.
The thing is, most CAM programs don’t. They just plot the same geometry information that was used to create the g-code output by the postprocessor. This allows for subtle bugs to creep in that are not detectable in the CAM backplot.
Because of that, a lot of experienced machinists insist on a separate simulated backplot as a sanity check for their g-code before they’ll run it. It doesn’t cost very much or take very long to have this peace of mind, so it’s something you should consider.
For a really deep dive on CNC Simulators, check out our article.
Conversational Programming Software
Forget CAD and CAM. What if you just want to make a simple rectangular bracket with 4 holes? What if you need to turn a little spacer on your lathe? Do you really need the full power and complexity of those things to get those simple jobs done?
No, of course not. But, if you don’t have Conversational Programming, you may find yourself either dealing with CADCAM anyway, or slinking back to the old manual machines in the back of the shop to get those simple jobs done.
Conversational Programming is built right in to G-Wizard Editor…
With Conversational CNC, you pick a generic operation, such as making a hole, and you’re presented with a simple fill-in-the-blank Wizard. Answer a few questions, push a button, and you get g-code to do the job.
Conversational Programming can be its own software (it’s built right into our G-Wizard Editor, see how here) or it can be built into your machine’s controller. Either way, it’s very handy for simple jobs.
To learn more about Conversational Programming, check out our in-depth article.
Which one is right for you?
It’s a trick question. By now I hope you can see how you’d benefit from having all of these tools available. They will all save you time. Even if you don’t plan to do any hand editing of g-code, having a G-Code Editor/Simulator will help you make sure the CAM code is ready to run on your machine. It only takes a few seconds to check the code when it comes out of CAM, and it can surely save you some time.
With the right software, you don’t need too many packages though. Add G-Wizard Editor to your CADCAM software and it comes with the Editor, Simulator/Debugger, and Conversational Programming. Why be without it?
You’ve learned a lot. If you followed through with the last 5 chapters and spent a little time drilling down into our g-code cheat sheet, you’re ready to tackle some simple programs. There’s just one more thing you need to consider, and that’s the machine setup work that goes hand-in-hand with any CNC program.
Let’s Start With Part Zero (also known as Program Zero)
We’ve already discussed CNC Coordinate Systems in a previous chapter, so let’s talk about how to set up a machine coordinate system so it matches up to the part you want to make.
Let’s suppose you just got done drawing up a part in your CAD software, and you’re about ready to generate some g-code for it. One of the key things to understand is where Part Zero is going to be. Your CAD program has some sort of coordinate system, and your part is positioned in the drawing relative to that coordinate system. If you’ve never done any CNC work before, you may not have paid much attention to that positioning. Perhaps you stuck the part well away from the 0, 0, 0 origin in the CAD program so it would be easier to see without the axis lines being too close.
You might want to reconsider that idea, at least until you get very comfortable with all the different coordinate systems that you’ll be using for CNC. Instead, what you want to do is put your “Part Zero” (for now, the CAD System’s origin or 0, 0, 0) some place that makes sense when you get ready to machine the material. When your g-code program refers to X0 Y0 Z0, that’s your Part Zero. Later on we can get all fancy with Work Offsets and other ways of transforming the coordinates, but when you first start the machine, think of X0 Y0 Z0 as Part Zero.
There are a lot of different theories on where to put Part Zero, and it matters for how easy and natural your CNC work will be.
When milling, a lot of emphasis is on the Z axis. When Z = 0, where should that be in relation to the part?
One theory has Z = 0 being the top of the workpiece before machining. This facilitates knowing when your cutter is cutting workpiece and when it is cutting air. Of course as you start making chips, you’re also making air down below Z = 0, but it is still a comfort to know where that original boundary started.
Another theory prefers that Z = 0 be some feature that doesn’t move and will not be milled away. For example, it might be the top of a vise jaw. This is handy should you need to remove your part for some reason. You won’t have to re-reference the machine to a new Z0. It is also handy if you’re machining workpieces with slightly differing dimensions. For example, even if you’re making identical parts, you may be starting with rough sawn material. The exact coordinates of the top of such material will vary from workpiece to workpiece because sawing is not a precision operation.
Cookbook Recipe: I like to use a Part Zero that corresponds to the fixed jaw of my vise when I will use a vise for machining. Once you get used to making your CAD drawings with that in mind, it means you can walk up to the machine, stick a piece of material in the vise, load a g-code program designed with that notion of Part Zero, and immediately begin machining after just homing the machine. Since the vise generally stays put on the machine, there are no touch offs required, which is a nice productivity booster. If I do need to move the vise or change jaws, no worries, I can just rezero on that location again.
Whatever you decide to use for your Part Zero, you need to be aware of it, and it is worth thinking about how to choose a Part Zero that might save you a little time or make things easier to understand.
What’s the difference between machine zero, work zero, part zero?
Machine Zero is the origin of the coordinate system that corresponds to the machines axis travels. Work Zero and Part Zero are the same thing, and they are the origin of the Work Coordinate System. Put another way, Work Zero/Part Zero establish a WCS by defining its origin. Your CAM program will have a way of specifying the WCS or Part Zero. When you setup the job, you will use edge finders or other sensors to tell the machine exactly where Part Zero is.
When you start up the machine, it doesn’t necessarily know anything about your preferred coordinate system. What it does know is something called “Machine Coordinates”. This is a fixed coordinate system that is baked into the machine. When you “Home” the machine, or “Reference the Axes”, you are causing it to use its Home Switches to accurately locate itself relative to machine coordinates. If your machine doesn’t automatically home when you start it up, it’s a good idea to get used to the idea of homing it before you do much else. If you crash or emergency stop, it can also be a good idea to home the machine so it can pick up its lost position.
“Work Coordinates” are the coordinates you want to think about. In other words, Work Coordinates are the ones where the machine is at Part Zero when its display shows X0 Y0 Z0. For that reason, Part Zero may also be called Work Zero. You can establish Work Coordinates in a variety of ways. By “establish”, I mean you can tell the machine how to equate Work Coordinates to Machine Coordinates.
A Work Coordinate system is something your machine will remember from one invokation to the next, though you probably shouldn’t count on that unless you know for sure you can. Since I use the system of Part Zero matching a point on my vise jaws, I can start the machine and Home it and I know the Work Coordinates are what I expect. You also have the ability to establish multiple Work Coordinate systems, which is convenient for a lot of reasons. We’ll talk more about using multiple Work Coordinate systems in a later article. For now, let’s just focus on one.
Establishing a Work Coordinate System via “Touch Offs” or “Zeroing”
Let’s talk about establishing a Work Coordinate system via Touch Offs. We’ll use my vise jaw system, just to make the discussion concrete, but the principle works for any work coordinate system.
Simply put, a “Touch Off” is where you use the cutter to locate the Work Zero. We do it one axis at a time, so let’s start with the “Z” axis. There are lots of ways to do Touch Offs. Each has varying accuracy and requires you to work on your technique a bit. The Old School method uses paper–cigarette rolling papers were very thin and commonly available. Use a little dab of oil to hold the paper in place and slowly jog the spinning cutter until it moves the paper. Stop. The cutter is now located at the Zero, with the exception of the thickness of the paper. Some trial cutting and a micrometer will establish what that is. Be sure to use the same kind of paper each time so the thickness is repeatable.
A more modern and accurate method would involve the use of a gage block. Gage blocks are precision machined to a very high tolerance and will include an inspection report that tells how much error there is in the block.
DO NOT TRY TO TOUCH THE TOOL OFF THE GAGE BLOCK!
If you’re using gage blocks, your cutter should not be spinning. But whether the cutter is spinning or not, it’s bad for your expensive gage blocks and bad for your cutters. Instead, move the cutter up, stop moving, and try to slide the gage block between the cutter and the workpiece. At some point, you will have jogged the machine a little too far and can jog back until you can slide between the two.
Here’s another tip from a reader (thanks Paul!) if you don’t want to use gage blocks–try a wrist pin from an engine. They’re machined from hardened material, they’re precise, they usually have a fine finish, and you can roll them under the cutter to check the fit. In fact, from many standpoints, a cylinder or ball shape (large ball bearings are precise too!) makes a lot of sense for this measurement as they’re less sensitive to whether the surface under them is flat and level. Take your micrometer to establish the wrist pin’s diameter and make sure it isn’t worn too badly if it’s used.
When you have located the machine in one axis at a point you want to “Zero”, your CNC Control will have a way for you to tell it that’s the zero for that axis. This is an important operation, so make sure you know how to do it on your controller. There’s usually a one touch button to zero a given axis and perhaps another to zero all the axes.
Note that you don’t have to measure strictly Part Zero. Your controller will have a way for you to enter an arbitrary value in and tell it that is where the tool tip is currently located. This is convenient for many cases and something you’ll be doing fairly often as well as Zeroing. For example, you may want to enter the thickness of your cigarette paper instead of “0.0000.”
Edge Finders and Probes for Establishing Work Coordinates
You won’t be CNC’ing for long before you’ll be wishing for an Edge Finder or a Probe. These are tools that make it quick and easy to find the edge of some object so you can Zero on it. Edge Finders come in all shapes and sizes from simple spinning contraptions all the way to fancy, accurate, and easy to use contraptions such as the Haimer 3D “Taster”. Yep, that’s not a misprint, they call them “Tasters” from the original German.
Here is a good video tutorial from Tormach on how to use a simple edge finder:
A simple edge finder…
And here is a demonstration of a Haimer 3D Taster:
Haimer 3D Taster…
A probe can be the ultimate in convenience for doing these kinds of Zeroing operations. Here is a Renishaw probe setting up work offsets:
Renishaw probe setting up work offsets on a VMC…
Each of these tools is similar in purpose, just with increasing capability, automation, and expense. There are a wide variety of other tools available for precisely locating features on your parts and workpieces. Some are more specialized, such as the Blake Coaxial Indicator, which is used to locate bore centers.
You’ll want to have some of these gadgets all fixed up in a tool holder and ready to pop into your spindle for job setups.
For even more examples of how to find Part Zero, see our article which gives 8 ways to do locate Part Zero.
We won’t spend much more time on such things as they’re more properly part of CNC setup and general machinist measuring techniques than g-code programming per se.
Even More Ways to Establish Part Zero
1. Take out your CNC machine’s manual and figure out how to zero your CNC machine to establish Work Coordinates. Look up how to read Machine Coordinates versus Work Coordinates on the control panel too.
2. Try some touch offs on your machine. Use the corner of a piece of scrap stuck in a vise for starters until you get good at it.
3. If you have an edge finder, 3D Taster, or probe, give it a try as a way of precisely locating part zero.
4. Decide what your convention for Z = 0 and perhaps Part Zero will be and stick to it.
You’ve now got most all of the information you need to do basic CNC Programming. You can make the CNC do anything a manual machinist can do on a manual machine.
But don’t stop now. Next up is perfecting your skills by learning the remaining basic techniques. From there, take on some intermediate and advanced techniques. Don’t worry, CNCCookbook has the articles that will enable you to just keep right on learning, step-by-step.
Basic G-Code Program Structure: Blocks, sequence numbers, words, addresses, and the basic nuts and bolts of g-code line.
Linear Motion: G00 and G01: Moving your CNC in straight lines.
One-Shot G-Codes and Modal G-Codes: Some things in g-code are sticky and others are not.
Circular Arcs: G02 and G03: Moving your CNC along arcs.
Circular Arcs, Part 2: Tips for easier arc programming, arcs in CAM and Simulators, arc troubleshooting, and more.
Tool Length Offsets: Something every CNC machinist should know a lot about.
Tool Data Management: How do you keep up with all your tooling and the compensation information? What about tool presetting?
Basic CNC Lathe Programming: What’s different on a lathe versus a mill? Not too much, but it’s important and this article takes you through it.
Quiz on Basic GCode Programming: Test yourself with a quick quiz on this section. Each question has links to the answers so you can review what you missed.
Thread Milling: How to thread mill, NPT and tapered threads, When to thread mill instead of tapping
Intermediate Lathe Programming
Lathe Simple Canned Cycles: G90/G92/G94
Lathe Repetitive Roughing Cycles: The Poor Man’s Turning CAM
G71: Rough Turning Cycle: Type I
G70: Finishing Cycle
G72: Rough Facing Cycle
G73: Pattern Repeating Cycle
Lathe Threading Cycles
Gang Tooling for Lathes
Tool Compensation: The Poor Man’s Milling CAM
CNC Macro Programming and Fanuc Macro B: A Mini-Tutorial on G-Code Macro Programming and Fanuc Macro B.
Mirroring the Coordinate System
G-Code Scaffolding: Making CAM Better With G-Code Help
Multiple Part Strategies
G52: Temporary programmable work offsets
Interacting With the Operator
Tweaking CAM Programs: Right Angle Heads and More
Mill 4th Axis Programming
Lathe C-Axis Machining and Live Tooling
CNCCookbook Blog Posts Relating to GCode Programming: Lots more Tools, Examples, and detailed articles.
G-Wizard Calculator: A CNC Machinist’s Calculator
Sample G-Code Files: G-Code examples you can download and play with
CAM Toolpath Strategies: Where plunge roughing, trochoidal machining, and other odd beasts are explained.
3D CAM Toolpaths: Learn the ins and outs of which 3D toolpaths to use in which situations.
Rhino Tips: Some of my tips and techniques for being productive with Rhino 3D.
Like what you read on CNCCookbook?
Join 100,000+ CNC'ers! Get our latest blog posts delivered straight to your email inbox once a week for free. Plus, we’ll give you access to some great CNC reference materials including:
- Our Big List of over 200 CNC Tips and Techniques
- Our Free GCode Programming Basics Course
- And more!
Just enter your name and email address below:
100% Privacy: We will never Spam you!