How to read G-code: a beginner's guide for new machinists

Your first week in a CNC shop, someone hands you a printout full of lines like G43 H01 Z1. M08 and expects you to know what the machine is about to do. It looks like a secret code, but it’s simpler than it seems. A CNC program is just a list of instructions, read from top to bottom, one line at a time. Once you know how a line is built and the twenty or so codes that show up in almost every program, you can read most of what comes off a CAM system or out of a programmer’s head.

This guide covers the basics for Haas and Fanuc controls, the two you’re most likely to meet in a North American shop.

How a line is built

Each line is called a block. A block is made of words: a letter followed by a number. The letter says what the number is.

N40 G01 X3. Y1.5 F20.

reads as: line 40, move in a straight line at feed (G01) to X 3.0, Y 1.5, at 20 inches per minute (F20.).

Letter Meaning
N Line number (optional, for your reference)
G Preparatory code: the type of move or mode
M Miscellaneous code: machine functions like spindle, coolant, program end
X, Y, Z Positions along the axes
U, W Lathe only: incremental moves in X and Z
I, J, K Arc centre distances (also used by some cycles)
R Arc radius, or the retract plane in a drilling cycle
F Feed rate
S Spindle speed (RPM, or surface speed on a lathe with G96)
T Tool number
H, D Mill only: tool length offset and cutter diameter offset numbers
P, Q Dwell time, subprogram number, or the start and end lines of a cycle

Text in parentheses is a comment. The machine skips it, so programmers use it for notes: the tool in each station, the stock size, the part number.

Modal: codes that stay on

Most G codes are modal. Once you call G01, every following move is a feed move until another code in the same group (like G00) replaces it. The same goes for G90 (absolute), G20 (inch), feed rates and spindle speeds. That’s why many lines contain only an X or a Z: everything else is still in effect from the lines above.

When you read a program, keep track of what’s active. A surprising number of crashes come from a mode left on: G91 incremental still active when the next tool starts, or a drilling cycle that was never cancelled.

The G codes you’ll see every day

Code What it does
G00 Rapid move, as fast as the machine goes. Never into material.
G01 Straight line at the feed rate (F).
G02 / G03 Arc clockwise / counter-clockwise.
G04 Dwell (pause) for P seconds or milliseconds, depending on the control.
G17 / G18 Work plane: XY (mill) / XZ (lathe).
G20 / G21 Inch / metric.
G28 Return to machine home.
G40 / G41 / G42 Cutter (or tool nose radius) compensation off / left / right.
G43 / G49 Mill: tool length offset on / off.
G54 – G59 Work offsets: where the part zero is.
G80 Cancel a drilling cycle.
G81 / G83 Mill: drill / peck drill cycle.
G90 / G91 Absolute / incremental positions (mill).
G96 / G97 Lathe: constant surface speed / constant RPM.
G50 Lathe: maximum spindle speed (needed with G96).
G98 / G99 Mill: return to the start or R plane after each hole. Lathe (Fanuc / Haas): feed per minute / per revolution.

Watch out for that last one: the same code can mean different things on a mill and a lathe.

The M codes you’ll see every day

Code What it does
M00 / M01 Program stop / optional stop (only if Optional Stop is on).
M03 / M04 / M05 Spindle clockwise / counter-clockwise / stop.
M06 Mill: tool change.
M08 / M09 Coolant on / off.
M30 End of program, reset to the top.
M97 / M98 / M99 Local subprogram (Haas) / call a subprogram / return.

Walk through a real program

Here is a short Haas mill program: a 1/2” end mill cuts a slot with a rounded end 0.1” deep, then a drill makes two holes. The part zero is the top left front corner of a 4 × 3 × 1 inch block.

%
O01070 (FIRST PROGRAM - HAAS MILL)
(STOCK 4.0 X 3.0 X 1.0 - X0 Y0 TOP LEFT FRONT CORNER)
(T1 1/2 FLAT END MILL - FLUTE 1.0 - STICK OUT 1.5)
(T2 #7 DRILL)
G20 G17 G40 G49 G80 G90
T1 M06
G54 G00 X0.5 Y1.5 S3000 M03
G43 H01 Z1. M08
Z0.1
G01 Z-0.1 F10.
X3. F20.
G02 X3. Y0.5 R0.5
G01 X1.
G00 Z1. M09
M05
G28 G91 Z0.
T2 M06
G90 G54 G00 X1.5 Y1. S2500 M03
G43 H02 Z1. M08
G98 G81 Z-0.3 R0.1 F8.
X2.5
G80 G00 Z1. M09
M05
G28 G91 Z0.
G28 G91 Y0.
G90
M30
%

Line by line:

  1. % and O01070: start of the file and the program number. The comment after it is the program name.
  2. The comments: stock size, where zero is, and what each tool is. Read these first. They tell you what should be in the machine.
  3. G20 G17 G40 G49 G80 G90: the safe-start line. Inch, XY plane, no cutter comp, no length offset, no canned cycle, absolute. It puts the control in a known state no matter what ran before.
  4. T1 M06: load tool 1.
  5. G54 G00 X0.5 Y1.5 S3000 M03: use work offset G54, rapid to the start point above the part, spindle on clockwise at 3000 RPM.
  6. G43 H01 Z1. M08: turn on tool length offset 1 and come down to 1” above the part, coolant on. Mismatching T1 and H01 is a classic crash; they should always agree.
  7. Z0.1: rapid to 0.1” above the top. Still G00, because G00 is modal.
  8. G01 Z-0.1 F10.: feed down 0.1” into the material at 10 in/min.
  9. X3. F20.: cut a straight line to X3 at 20 in/min.
  10. G02 X3. Y0.5 R0.5: clockwise arc with a 0.5” radius, ending at X3 Y0.5. This rounds the end of the slot.
  11. G01 X1.: cut back along the bottom of the slot.
  12. G00 Z1. M09, M05, G28 G91 Z0.: lift clear, coolant off, spindle off, send Z home for the tool change. With G91, Z0. means “no extra move on the way”, so Z goes straight up to home and X and Y stay put.
  13. T2 M06 and G90 G54 G00 X1.5 Y1. …: load the drill, switch back to absolute (G91 is still on from the line before!), move over the first hole.
  14. G98 G81 Z-0.3 R0.1 F8.: drilling cycle. Rapid to R0.1, feed to Z-0.3, rapid back up. X2.5 drills the second hole with the same cycle.
  15. G80: cancel the cycle. Without it, the next move would drill another hole.
  16. G28 G91 Z0., G28 G91 Y0., G90, M30: Z home, bring the table forward, back to absolute, end of program.

What’s different on a lathe

A lathe program reads the same way, with a few changes:

  • X is a diameter. X1. means a 1” diameter, not 1” from the centre.
  • Tools are called with four digits: T0101 is tool 1 with offset 1. There is no M06.
  • Speeds and feeds: G96 S500 is a constant 500 surface feet per minute, so the RPM rises as the diameter gets smaller. G50 S3000 caps it at 3000 RPM. Feeds are usually per revolution (G99), like F0.006.

For example, facing the end of a 1.25” bar:

T0101 (CNMG 432)
G50 S3000
G96 S500 M03
G00 X1.35 Z0. M08
G01 X-0.06 F0.006
G00 Z0.1

Tips for reading programs

  • Read the comments and the tool list first, then check them against what’s actually in the machine.
  • Check the units and the work offset. An inch program run in metric, or the wrong G54, ruins a part before the first chip.
  • Follow the modes. Mark where G91, G41/G42 and drilling cycles start and stop.
  • Look at every G00. Rapids are where tools hit things: they don’t slow down when something is in the way.
  • Prove it before you cut. Run single block with the rapid override turned down, watch the distance to go, and keep a hand on feed hold.

See it before the machine does

The fastest way to learn G-code is to watch each line move a tool. NC Proof plays a program line by line on 3D stock in your web browser: click any line to see where the tool is, step forward and back, and see what every move cuts. It also flags rapids into the stock and other mistakes with the line number. Paste in the program above and step through it. NC Proof runs on Haas and Fanuc lathes and mills, and you can try it free for 7 days at app.ncproof.com. The user guide shows you around the app.

More from the blog