For those of you who are manual machinists and still wondering what CNC can do for you, there are 9 basic g-codes you must learn today. If you know them, and they are easy, they will turn the CNC Machine into the equivalent of a manual machine with power feeds and DRO’s on every axis.
They do this because you can use them as simple immediate commands without having to write a g-code program.
You just type in one of these 9 gems and the machine will perform that action immediately through the magic of what’s called “MDI” or “Manual Data Input.”
MDI is like the command line prompt from DOS for CNC and it is handy as can be for quick and dirty operations.
Each of these 9 g-codes is super easy to understand so let’s go through them, one at a time:
Move in a Straight Line: G01
G01 is the very first g-code you should learn as it is the one you’ll use the most. It causes the machine to move in a straight line based on the coordinates you enter afterward. For example, let’s say you’ve got a piece of material in the milling vise, and the top left corner is at 0, 0, 0. You want to move the cutter tip to that location, but a little above the work as you’re not ready to cut yet. You could use this MDI command:
G01 X0 Y0 Z0.5
You can see how that would move to X = 0, Y=0, and Z=0.5. Congratulations, you’ve just learned your first g-code and it was pretty obvious. Some things to take note of:
– G-Code ignores spaces. You could’ve also typed (and you will often see in programs) this: G01 X0Y0Z0.5
– The Z-axis is positive in the “up” direction. As you can imagine, it’s extremely important to get used to which way the spindle will move, up or down, based on your commands. If it moves the opposite of what you’d expect and you’re at all close to anything, you may crash into it.
– The g-code dialect will determine whether you need that leading zero or could write “G1” and whether you need a decimal point no matter what like “X0. Y0. Z0.5”. If things act odd, be sure to check on that for your machine. Many CNC’ers just always use a leading zero because the next machine they use may require it.
For an understanding of how to establish the part corner being at 0,0,0, consult your machine’s manual, but I will tell you in advance it’s just like using a DRO. You position the tip of the cutter somewhere and press buttons that set the “DRO’s” to be zeroed. Easy, but just a little different on each machine.
Helpful Tip: Use G01, not G00 to avoid high speed crashes
The experienced CNC’er reading this will wonder why I didn’t start with G00 instead of G01. After all, most g-code programs start with G00. This is a bit of extra credit, but an important bonus point. G00 is a rapids move in a straight line. It has the same syntax as G01, so that last move would be:
G00 X0 Y0 Z0.5
The difference is that G01 moves at feed rate and G00 moves at rapids, which is the fastest your machine can use. When I am operating a CNC manually in MDI mode, I prefer not to use G00. It’s just too fast and G01 is safer. Plenty of time to hit that red button if you type in the wrong thing.
You can move pretty fast too in feed rate, so let’s make our next g-code the “F” word (hey, what did you think that meant?).
“F” word, eh? No, not that F-word, silly!
In g-code we refer to words and addresses. The word is the letter and the address is the number that follows. “G01” is word “G” and address “01”. Hence, we call it the “F-word” because it uses the letter “F”. This is the g-code needed to set your feedrate.
For example, on a machine using Imperial units, “F100” tells the machine to move at 100 inches per minute. When I am just moving the tip around in air, I generally will use “F100” as the rate I move. I consider that to be my “rapids” speed for MDI work. It’s slow enough you have plenty of time to react and fast enough so you don’t grow too old waiting.
You specify the F-Word before the move. So, to plunge down from our last move at a feedrate of 5 inches per minute to a depth of 1/2″ after our last MDI command we’d enter something like this at the MDI prompt:
Some things to note:
– We didn’t have to specify G01 again, although we could have. CNC is modal, meaning it remembers a lot of things you do. In particular, the motion is modal. If we had just run a G00 and no other motion commands since, any coordinates we enter result in G00 moves. In this case, we depended on the prior G01 mode to still be in effect. This is another reason I dislike using G00 for MDI work. I’m just the kind of guy who’d do a G00, get distracted, think I was in G01, and enter a move. DOH! Broke another tool, darn it!
– By convention, we often run our machines with Z=0.0 corresponding to top of material. So, that Z-0.5 commands the tool to feed downward half an inch into the material. Any time you enter a negative Z value, be in the habit of checking carefully that the feedrate is set properly and that you really do want to put the cutter into the material at that location.
– Not only does g-code not care about spaces, it doesn’t care about any white space. I could’ve put the F-Word on the same line:
Okay, you’re cooking with fire now. You can move the tool and control how fast it moves. Hopefully you like my use of a big F-word instead of G00, and I also hope you have a good tool like G-Wizard CNC Calculator to help you determine the right F-Word to use. In other words, the correct Feeds and Speeds.
Spindle Speed: S
Speaking of Speeds, it’s time to set the spindle rpm. For that, we’ll use the S-Word and follow it up with rpms. “S1000” gives 1000 rpm on the spindle.
Except it doesn’t.
You type that in and the spindle just sits there. All S is doing is telling the spindle that when it starts spinning, it will do so at 1000 rpm. Kind of like “F” not causing any motion, just telling the CNC that when the motion happens, that’s how fast it should move.
To actually start the spindle spinning you will need:
Clockwise Spindle: M03
As soon as you enter the “M03”, the spindle will start to rotate clockwise at its programmed speed.
Now you really are in business.
You can move the tool tip at feed speeds and you can turn on the spindle. Pretty easy so far, right?
You can stop the spindle with M05 if need be too.
And we’ve got most of it down. Just one thing, you probably don’t want to be cutting without some coolant, right?
Good idea, let’s don’t cut without coolant unless we really understand dry machining, and even then, we probably want an air blast.
There are definitions for what M07 and M08 are supposed to do, but I am not going to tell you what they are. People can hook up the machines to do all sorts of things. For example, M07 could turn on an air blast or mist and M08 could turn on full flood coolant. Suffice to say M07 and M08 turn on two different kinds of coolant. Give each one a try to see what it does. M09 turns off the coolant, which is another good one to know.
That ought to do it. You should be ready to cut. But wait, there’s one thing missing. How about selecting a tool?
Tool Changing: T+M06
You could certainly manually insert a tool in the spindle taper. That’s not hard and you need to know how to do that. But if your machine has a tool changer, there’s a bunch of them sitting there and probably several would be convenient to access.
With just two more g-codes, you’ll know how.
Tool Changing is a two step process on a mill (and on some lathes, but most lathes don’t use the M06).
First, you select the tool with a T-Word followed by the tool number. Then you perform the tool change with M06. You can put them on two lines or write them together:
Select tool in slot #12 and swap it into the spindle.
One more for extra credit: Peck Drilling
With those 9 g-codes, you can do a lot. You can run circles around your manual machinist friend, for example. Let me give you one more, just because it is often convenient to drill holes using MDI g-code, and when you drill a hole deeper than one diameter, you really ought to peck drill. G- Code includes what are called “Canned Cycles.” Canned cycles do all sorts of miraculous things in one command instead of requiring you to write a bunch of g-code. Imagine how painful peck drilling would be if you were programming each in and out cycle all the way down the hole with G01. You could do it, but it wouldn’t be much fun. Canned cycles are available for all sorts of functions, but right now let’s learn how to do a simple G83 deep hole peck drilling cycle. I picked that one because it is fairly universal. There are faster cycles for special cases and slower cycles for other special cases, but this one will work for a lot of holes.
A typical G83 looks like this:
G83 XYZ R P Q F L
XY: Coordinates of the hole
Z: Hole bottom
R: Retract position in Z. Motions from initial Z to R are performed at rapids speeds. From R to hole bottom is done at feed speed.
P: Dwell time at bottom of hole.
Q: Depth to increase on each peck.
F: Cutting feedrate
L: Number of repeats
I’ve left out the addresses associated with X, Y, Z, R, P, Q, F, and L. Once the drill reaches the bottom of hole and has finished dwelling, retraction is at rapids speeds.
Now here is a real example:
G01 X0 Y0 Z0.5
G83 X0Y0Z-1.0 R0.1 P0.1 Q0.1 F2
We’ll ignore L for the time being. Repeats are probably not something you will use much for MDI. I stuck the G01 in so we would have a frame of reference.
So, the tool tip moves to X0Y0 and is 1/2″ above 0, which I usually program to be 1/2″ above my workpiece. We’re going to peck every 0.1″ on the way down. What that means is the drill will retract every 0.1″ of additional depth drilled. And, we’re going to dwell a tenth of a second at the bottom of the hole. This just cleans up the bottom and makes sure there’s no chips left down there.
Important: Once you complete a drill cycle, every move will start a new drill cycle at the new location. Cancel the drill cycle by running “G80”.
You’re now armed with 10 basic g-codes you can use at the MDI prompt of your machine to do all sorts of things. Go ahead and practice them a bit with G-Wizard G-Code Editor and Simulator. Then try it at your machine. It’s very convenient to drill a quick hole or to face off the top of some material without having to go fire up your CAD/CAM software each time.
Freebie: G-Code Cheat Sheet
Would you like to download a nice pdf cheat sheet you can print and tape to your machine enclosure?
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!