CNC1 Module 9: G-code

Manufacturing Training Center/Shop Trainings/CNC Training/G-code


CNC1, Module 9 of 9. G-code is the list of instructions the machine controller follows, one line at a time. Fusion writes it for you when you post a program, so you'll rarely type it yourself. You still need to be able to read it, because the controller does exactly what the code says, mistakes included.

Most of a program is cutting moves you'll never read line by line. The first and last ten or so lines are different. They put the machine into a known, safe state, load the right tool and offsets, and bring everything back home at the end. Most first-run crashes come from these lines, so they're the focus of this module and of your design review.

By the end of this module you should be able to:

  • read a line of G-code and say what each part does,
  • recognize the common G and M codes,
  • explain every line of a program's safe start and program end, and
  • spot the common start and end mistakes before they reach the machine.

Anatomy of a line

Each line (a block) is made of words: a letter followed by a number.

N120 G01 X1.25 Y-0.5 Z-0.1 F40. (FIRST PASS)
Word Meaning
N120 Line number. Optional; just a label.
G01 Move in a straight line at the feed rate.
X1.25 Y-0.5 Z-0.1 Where to go.
F40. Feed rate: 40 inches per minute.
(FIRST PASS) A comment. The controller ignores anything in parentheses.
Letter Means
G Preparatory code: type of motion or mode
M Miscellaneous code: spindle, coolant, tool change, program end
X, Y, Z Axis positions
I, J, K Arc center position
F Feed rate
S Spindle speed (RPM)
T Tool number
H, D Tool length and tool diameter offset numbers
O Program number (used on some controllers such as Haas; not on Mach3)

Most codes are modal: once set, they stay active until something changes them. If one line says G01 and F40., every following move is a straight feed move at 40 IPM until another code replaces it.

This is why the start of the program matters so much. Any mode left over from the last program, or from someone jogging the machine, stays active unless your program resets it.

Common codes

Codes are listed in numerical order. Most work on all the shop's machines; differences are noted.

G codes

Code Meaning
G00 Rapid move: as fast as possible, not cutting. Dangerous if the path isn't clear.
G01 Straight cutting move at feed rate F
G02 Arc, clockwise
G03 Arc, counterclockwise
G04 Dwell: pause for a set time (P)
G10 Set offsets or tool table values from the program
G17 XY plane for arcs (the normal milling plane)
G18 XZ plane for arcs (used for lead-in and lead-out arcs)
G19 YZ plane for arcs
G20 Inch units
G21 Millimetre units
G28 Return to home through an intermediate point. Use it with G91 so the axis goes straight home.
G40 Cancel cutter diameter compensation
G41 Cutter compensation, tool on the left of the path
G42 Cutter compensation, tool on the right of the path
G43 Apply tool length offset (with H#)
G49 Cancel tool length offset
G54–G59 Work coordinate systems (where the part zero is)
G80 Cancel drilling cycle
G81 Drilling cycle
G82 Drilling cycle with a dwell at the bottom
G83 Peck drilling cycle: retracts fully to clear chips
G84 Tapping cycle (only on machines that support it)
G85 Boring or reaming cycle: feeds in and out
G90 Absolute positions
G90.1 Arc centers (I, J, K) are absolute (Mach3)
G91 Incremental positions
G91.1 Arc centers (I, J, K) are incremental, measured from the arc's start point (Mach3)
G93 Inverse time feed
G94 Feed in units per minute
G98 Drilling cycles retract to the starting Z
G99 Drilling cycles retract to the R plane

M codes

Code Meaning
M00 Program stop. Press Cycle Start to continue.
M01 Optional stop. Only stops if Optional Stop is turned on.
M02 Program end, without rewinding
M03 Spindle on, clockwise
M04 Spindle on, counterclockwise
M05 Spindle off
M06 Tool change
M07 Mist coolant on
M08 Flood coolant on
M09 Coolant off
M30 Program end and rewind
M48 Enable feed and speed overrides (Mach3)
M49 Disable feed and speed overrides (Mach3)
M98 Call a subprogram
M99 Return from a subprogram

What is a post processor?

Toolpaths in Fusion are stored in a general format that no machine can run directly. The post processor is a small program inside Fusion that translates them into the exact G-code dialect a specific controller understands. Clicking Post Process runs it and writes the file you load onto the machine.

Every controller speaks a slightly different dialect. The Tormach and the routers all run Mach3, so programs for them must be posted with the Mach3Mill post processor. A program posted for a different controller can contain codes Mach3 reads differently, which can cause wild moves.

The start of the program

Before the tool goes anywhere near the part, the start of every program must answer four questions: what units and modes, which tool, where is the part, and how high is the tool?

This is the real start of a router program posted from Fusion with the shop's Mach3 post. The Tormach also runs Mach3, so its programs start the same way.

(0006)
(NEW CNC TRAINING)
(T1 D=0.25 CR=0. - ZMIN=-0.635 - FLAT END MILL)
G90 G94 G91.1 G40 G49 G17
G20
G28 G91 Z0.
G90
Line What it does What to check
(0006) Program number, written as a comment Mach3 doesn't use O numbers or %.
(NEW CNC TRAINING) The program name from Fusion Confirms you loaded the right file.
(T1 D=0.25 CR=0. - ZMIN=-0.635 - FLAT END MILL) Tool list: tool 1 is a ¼" flat end mill with no corner radius, and its deepest cut is 0.635" below zero Check the depth makes sense for your stock. Here the sheet is 0.625" MDF, so the tool cuts 0.010" into the spoilboard to break through. A ZMIN deeper than the spoilboard can take means something is wrong.
G90 G94 G91.1 G40 G49 G17 The safe start block: resets every important mode (see below) Should be near the top of every program.
G20 Inch units Must match the units Mach3 and the tool table are set to.
G28 G91 Z0. Rapid Z straight up to the home position G91 makes it go straight up, instead of first moving to Z0 of the part.
G90 Back to absolute positioning Essential. Without it, every following move is incremental.

The safe start block, code by code

Code What it does What goes wrong without it
G90 Absolute positioning: X1. means "go to X = 1 from part zero" If G91 (incremental) were left on, X1. would mean "move 1" further from wherever you are," and every move would land somewhere else.
G94 Feed rate in inches per minute A leftover G93 (inverse time) makes every F value mean something completely different.
G91.1 Arc centers (I, J, K) are measured from the arc's start point Mach3 can read arc centers either way depending on a setting. Fusion writes them incremental, so this forces Mach3 to match. If they disagree, arcs swing around the wrong center: wild moves or an "arc radius" error.
G40 Cutter diameter compensation off Fusion already offsets the toolpath by the tool radius. If G41/G42 were active, the controller would offset it again, and the part would come out wrong by a tool radius.
G49 Tool length offset cancelled Clears any old length offset until the program applies the correct one with G43 after the tool change.
G17 XY plane for arcs If G18 or G19 were left on (for example from the lead-out arc at the end of the last program), the next XY arc would be cut in the wrong plane.

Loading the tool and coming down

The next lines load the tool and bring it down to the part:

T1 M6
S4500 M3
G54
G0 X... Y...
G43 Z... H1
  • T1 M6: Mach3 stops here for the tool change.
    • Router: change the bit, re-zero Z with the paper method, then press Cycle Start.
    • Tormach: load the TTS holder for that tool, then press Cycle Start.
  • S4500 M3: spindle on.
    • Router: check whether the spindle is started by the program or by hand.
    • Tormach: check the belt is in a pulley range that can reach the programmed RPM.
  • G54: selects the work offset, i.e. where your part zero is.
  • G0 X... Y...: moves to the start position in X and Y while the tool is still high.
  • G43 Z... H1: applies the tool length offset and comes down in Z.
    • Tormach: the H number must match the T number and the tool table entry for that TTS holder.
    • Router: since Z is re-zeroed for each bit, check the tool table entry is zero so the length isn't applied twice.

The order matters

  1. Reset modes.
  2. Retract Z.
  3. Change tool.
  4. Start the spindle.
  5. Pick the work offset.
  6. Move X and Y while high.
  7. Apply the length offset and come down in Z last.

If Z comes down before X and Y are in position, the tool can drag across clamps or the vise and crash the machine.

Common start mistakes

  • Wrong units: G21 (mm) in a program for an inch setup, or the reverse. Every move is 25.4× too big or too small.
  • H doesn't match T: T3 M6 followed by G43 H1 uses tool 1's length for tool 3. The tool crashes or cuts air.
  • Missing G43: the machine assumes zero tool length, so the tip ends up much lower than expected.
  • Wrong post processor: always use Mach3Mill for the Tormach and the routers.

The end of the program

The end of the same program:

G18 G3 X1.95 Z-0.374 I0. K0.025
G0 Z1.
G17
M5
G28 G91 Z0.
G90
G28 G91 X0. Y0.
G90
M30
Line What it does Why it matters
G18 G3 X1.95 Z-0.374 I0. K0.025 The last cutting move. Switches to the XZ plane and makes a small arc up and away from the wall (the lead-out). Lifts the tool off the finished surface smoothly instead of leaving a mark.
G0 Z1. Rapid up to 1" above part zero The tool is clear before anything else moves.
G17 Back to the XY plane Leaves the machine in the normal plane for the next program.
M5 Spindle off Wait for the spindle to stop before opening the enclosure or reaching in.
G28 G91 Z0. then G90 Z straight up to home, then back to absolute Z always goes home first.
G28 G91 X0. Y0. then G90 X and Y to home, then back to absolute The table only moves after Z is fully up.
M30 End the program and rewind Ready to run again.

Notice the patterns:

  • Every G91 is immediately followed by G90. Incremental mode is used for exactly one move, then switched off.
  • Z always goes up before X and Y move. If XY moves first, the tool can drag through the part or hit a clamp on the way home.

Common end mistakes

  • XY moves before Z retracts: the tool drags across the part or into clamps.
  • G28 used without G91: G28 Z0. in absolute mode first moves to Z0 of your part (the top surface), then goes home. That intermediate move can drive the tool down onto the part or into a clamp sitting above it, or plunge it into the part if it's still below the top surface. G28 should always be written G28 G91 Z0. followed by G90.
  • M30 missing or replaced with M00: the program stops but doesn't reset, so the next Cycle Start may run from the wrong place.
  • Opening the door while the spindle is still turning: wait for M5 to take effect and the spindle to stop completely.

Differences between the shop's machines

Tormach PCNC 1100 Routers
Fusion post Mach3Mill Mach3Mill
Tool change T# M6 stops the program; swap the TTS holder by hand and press Cycle Start T# M6 stops the program; change the bit in the collet, re-measure, press Cycle Start
Tool length Mach3 tool table, one entry per TTS holder, applied by G43 H# Re-zeroed with the paper method after each bit change
Coolant / dust M8 / M9 (if fitted) Turn on dust collection by hand

Checklist before your design review

Open the posted file in a text editor and check, from top to bottom:

  • Posted with the Mach3Mill post processor.
  • Units (G20 or G21) match your setup.
  • The safe start block resets the modes.
  • Every tool in the header comment exists in the shop and has a holder.
  • Every T# is followed by G43 H# with the same number.
  • The work offset (G54) matches where you'll set part zero.
  • The spindle is on before the first cutting move.
  • ZMIN in the header makes sense for your stock and spoilboard.
  • Z retracts before XY moves, at both the start and the end.
  • The program ends with the spindle off, coolant off, Z home and M30.

Key takeaways

  • The controller does exactly what the code says. Read the start and end of every program.
  • Codes are modal: the safe start block resets everything a previous program could have left on.
  • Start: reset modes, retract Z, change tool, spindle on, work offset, move XY high, then bring Z down last.
  • End: retract Z first, spindle off, then home XY, and end with M30.
  • Every G91 is followed by G90, and every T# by a G43 H# with the same number.

Check your understanding

  1. What does "modal" mean, and why does it make the start of a program important?
  2. What does G90 G94 G17 G40 G49 do?
  3. What goes wrong if a program says G21 but your setup is in inches?
  4. A program has T3 M6 followed by G43 Z0.6 H1. What will happen?
  5. Why should X and Y move before Z comes down at the start?
  6. Why should Z retract before X and Y move at the end?
  7. What is the danger of G28 Z0. without G91?
  8. What does M30 do that M00 doesn't?
  9. What happens when the Tormach reaches T2 M6?
  10. Why must you post with the right post processor for each machine?

You've finished the CNC1 modules. When you're ready, go back to the CNC1 quiz.