Part 4 of the CNC Machining Mini-Course. Start with Part 1 if you're just joining.
---
G-code is the native language of CNC machines. Every movement, every spindle command, every tool change — it's all expressed as lines of text that the machine controller reads and executes sequentially.
The name comes from the "G" commands that make up most of the code — preparatory words that set the machine's operational mode. Mixed in are "M" codes (miscellaneous functions) that control things like spindle direction, coolant, and program end.
You will rarely write G-code by hand today. CAM software (covered in the next post) generates it automatically from your 3D model. But you will need to read it, edit it, understand what went wrong when something breaks, and occasionally write a short program for a simple job. This post gives you that foundation.
---
The Structure of a G-Code Program
Here's a simple G-code program that drills a single hole:
```
%
O0001 (DRILL SINGLE HOLE)
G20 G40 G49 G80 G90
T1 M6
G54
S1500 M3
G0 X1.0 Y1.0
G43 Z0.5 H1
G81 Z-0.75 R0.1 F5.0
G80
G0 Z1.0
M5
M30
%
```
Let's go line by line.
---
Breaking Down the Code
`%` — Program start/end delimiter. The controller looks for this to know where the program begins.
`O0001 (DRILL SINGLE HOLE)` — Program number (O0001) and a comment in parentheses. Comments are ignored by the machine.
`G20 G40 G49 G80 G90` — Safety initialization block. These are "state" codes that ensure the machine starts in a known condition:
- • `G20` — inch units (use G21 for metric)
- • `G40` — cancel cutter radius compensation
- • `G49` — cancel tool length offset (will set it explicitly with G43 later)
- • `G80` — cancel any active canned cycle
- • `G90` — absolute positioning mode (coordinates are absolute, not relative to current position)
`T1 M6` — Select tool 1 (T1) and execute a tool change (M6). The machine will either automatically change to the tool or prompt you to change it manually.
`G54` — Activate work coordinate system G54 (where you told the machine your part is).
`S1500 M3` — Set spindle speed to 1500 RPM (S1500) and start spindle clockwise (M3). M4 would be counterclockwise.
`G0 X1.0 Y1.0` — Rapid move (G0) to X1.0, Y1.0. This positions the tool above the hole location at whatever Z it currently is.
`G43 Z0.5 H1` — Apply tool length offset H1 (G43) and rapid to Z0.5 (half an inch above part surface). G43 tells the machine to apply the tool length compensation stored in offset H1.
`G81 Z-0.75 R0.1 F5.0` — Drill cycle (G81). Drill to Z-0.75 (three-quarters inch deep), with a retract plane (R) at 0.1 above Z zero, at a feed rate of 5.0 inches per minute. G81 is a "canned cycle" — a built-in routine that handles the peck/retract motion automatically.
`G80` — Cancel the canned cycle.
`G0 Z1.0` — Rapid to Z1.0 (safe height above part).
`M5` — Stop spindle.
`M30` — End program and reset to beginning.
---
The Core G-Codes You Need to Know
Motion Codes
| Code | What It Does |
|---|---|
| G00 | Rapid positioning (fastest speed, no feed rate) |
| G01 | Linear interpolation (controlled feed rate in a straight line) |
| G02 | Circular interpolation, clockwise |
| G03 | Circular interpolation, counterclockwise |
| G28 | Return to machine home |
Mode Codes
| Code | What It Does |
|---|---|
| G17 | XY plane (for arcs — default on most mills) |
| G20 | Inch units |
| G21 | Metric units |
| G40 | Cancel cutter radius compensation |
| G43 | Tool length compensation positive (apply TLO) |
| G49 | Cancel tool length compensation |
| G54–G59 | Work coordinate systems |
| G80 | Cancel canned cycle |
| G90 | Absolute positioning |
| G91 | Incremental positioning (moves relative to current position) |
Canned Cycles (Drilling and Boring)
| Code | What It Does |
|---|---|
| G81 | Standard drill cycle |
| G82 | Drill with dwell (pauses at bottom — good for flat-bottomed holes) |
| G83 | Peck drilling (retracts to clear chips — use for deep holes) |
| G84 | Tapping cycle |
| G85 | Boring cycle |
Common M-Codes
| Code | What It Does |
|---|---|
| M03 | Spindle on, clockwise (standard milling direction) |
| M04 | Spindle on, counterclockwise |
| M05 | Spindle stop |
| M06 | Tool change |
| M08 | Coolant on |
| M09 | Coolant off |
| M30 | End program, reset |
---
Absolute vs. Incremental Positioning
G90 — Absolute: Every coordinate is relative to work zero. If work zero is the corner of your part and you say G00 X2.0 Y3.0, you go to a point 2 inches right and 3 inches back from that corner. Always.
G91 — Incremental: Every coordinate is relative to the current position. G91 G00 X2.0 means "move 2 inches in the X direction from wherever I am right now." This is useful for certain repetitive patterns and for some manual MDI (manual data input) commands, but absolute positioning is what you'll use for most programs.
Beginners: always program in G90. Always. G91 errors cascade — if one position is wrong, everything after it is wrong by the same amount.
---
Reading a Linear Move
```
G01 X3.5 Y2.0 Z-0.25 F12.0
```
This says: move in a straight line (G01) to the position X=3.5, Y=2.0, Z=-0.25 (quarter inch below Z zero, i.e., into the part) at a feed rate of 12 inches per minute.
G01 requires a feed rate. You must specify F (feed rate) at some point before or on the line that needs it. Once set, the feed rate is modal — it stays active until changed.
G00 does not use feed rate — it moves at the machine's maximum rapid speed. Never use G00 to position a tool into a part. That's a crash waiting to happen.
---
Reading an Arc
```
G02 X4.0 Y1.0 I0.5 J0.0 F8.0
```
G02 is clockwise circular motion. The endpoint is X4.0, Y1.0. The I and J values define the arc center relative to the arc start point — I is the X offset, J is the Y offset. This takes more mental math to read by hand, but CAM generates these correctly automatically.
---
MDI Mode: Manual G-Code Entry
Every CNC controller has an MDI (Manual Data Input) mode — a text field where you can type a single line of G-code and execute it immediately. This is incredibly useful for:
- • Manually probing to find part edges
- • Running a quick spindle speed test
- • Moving to a specific coordinate after a crash
- • Verifying offsets
In Haas controllers, press the MDI button, type your command, press CYCLE START. In Tormach PathPilot, there's an MDI tab. Learning MDI gives you direct control over the machine without running a full program.
---
The Practical Reality
Modern shops almost never write G-code by hand for complex parts. CAM software (Fusion 360, Mastercam, Vectric) reads your 3D model, you define toolpaths, and the software outputs a complete G-code file. We cover that in the next post.
But here's what still requires G-code reading ability:
- • Editing posted code — CAM output isn't always perfect. You'll edit feed rates, add dwell calls, modify tool change sequences.
- • Debugging crashes — When something goes wrong, you need to find the offending line.
- • Simple jobs — A single hole pattern, a simple facing operation — faster to type 10 lines of MDI than to open CAM.
- • Understanding what your machine is doing — If you can't read the program, you're running blind.
Learn to read G-code. Not to write every program from scratch — but enough to understand what's happening and catch problems before they become expensive.
— Dr. Scott