Skip to content
Skills / CNC Operation / CNC Setup and Process Control / Reading G-Code for CNC Operators
CNC Operation

Reading G-Code for CNC Operators

25 min read Training Guide

Read a CNC program line by line, know the common G and M codes, and catch mistakes before you press cycle start.

Table of contents

G-code is the plain-text language that tells a CNC machine where to move, how fast to cut and when to turn things on and off. Most programs are written by CAM (computer-aided manufacturing) software, so you rarely type one from scratch. You still need to read it. An operator who can read code knows which tool comes next, where it is going and how deep it will cut, and can catch a wrong offset number or a missing line before it reaches the part. The examples here use the Fanuc-style code found on most shop machines. Some codes differ between builders, so keep your machine's manual close.

What You Will Be Able to Do

  • Break a program into blocks and words and name what each letter does.
  • Explain modal codes and predict what stays active from line to line.
  • Read the common G and M codes, including work offsets, tool length and cutter compensation.
  • Read a canned drilling cycle, including what Q does in a peck drilling line.
  • Find the errors that an operator can catch by reading a program.

What a Program Is Made Of

A program is a list of lines called blocks. The control reads them from the top down, one at a time. Each block is made of words, and a word is a letter followed by a number.

For example, this block has five words:

N50 G01 X2.5 Y1.0 F12.0

It says: line 50, move in a straight line at a cutting feed, to X 2.5 and Y 1.0, at 12 inches per minute.

Letter What it means
O Program number
N Line number, for finding your place
G Preparatory code, sets a motion or mode
M Machine function, such as coolant or spindle
X Y Z Axis positions
I J K Arc center distances
F Feed rate
S Spindle speed
T Tool number
H Tool length offset number
D Tool diameter offset number
R Q P Values for a cycle, which change with the code

Anything in parentheses is a comment, and the machine ignores it. Good comments tell you the tool, the operation and the setup. Read them, but do not trust them over the code. Comments can go stale.

Most G codes are modal. Modal means a code stays active until another code in its group replaces it. The control does not need to be told again on every line.

G90
G01 X1.0 F10.0
Y2.0
X3.0

The last two lines have no G code at all. They are still G01 straight cuts at 10 inches per minute in absolute mode, because G90, G01 and F10.0 are all still in effect.

This is the most important habit in reading code: when you look at any line, you need to know what is still active from the lines above it. A few codes are not modal and act only on their own line. These include G04 (dwell), G28 (return home) and G53 (machine coordinates).

Modes are why a program usually starts with a safe start line that sets everything to a known state:

G20 G17 G40 G49 G80 G90

That line says inch mode, XY plane, cutter compensation off, tool length compensation off, canned cycle off, absolute positioning. If the last job left something active, this clears it.

Common G-Codes

Code Meaning Example
G00 Rapid move, no cutting G00 X1.0 Y2.0
G01 Straight cut at feed F G01 X3.0 F10.0
G02 Clockwise arc G02 X2.0 Y0 R1.0 F8.0
G03 Counterclockwise arc G03 X0 Y2.0 R1.0 F8.0
G04 Dwell, a timed pause G04 P0.5
G17 XY plane, normal for mills G17
G20 Inch mode G20
G21 Metric mode G21
G28 Return to machine home G91 G28 Z0
G40 Cutter compensation off G40
G41 Cutter compensation left G41 D05
G42 Cutter compensation right G42 D05
G43 Tool length compensation on G43 H03 Z1.0
G49 Tool length compensation off G49
G53 Move in machine coordinates G53 G00 Z0
G54 to G59 Work offsets G54
G80 Cancel a canned cycle G80
G81 Drilling cycle G81 Z-0.5 R0.1 F5.0
G83 Peck drilling cycle G83 Z-1.0 Q0.2 R0.1 F4.0
G84 Tapping cycle G84 Z-0.75 R0.1 F25.0
G90 Absolute positions G90
G91 Incremental positions G91
G94 Feed in inches per minute G94

Rapid and Feed Moves

G00 moves the axes at the machine's top speed. It is for getting into position, never for cutting. On many controls the axes run at their own full speeds, so the tool may not travel in a straight line. It can dogleg: one axis finishes first while the other keeps moving. That matters near clamps. G01 moves in a true straight line at the feed rate. G02 and G03 cut arcs, with the size given by R or by I, J and K.

Absolute and Incremental

G90 means every number is a position measured from work zero. G91 means every number is a distance from where the tool is now. X1.0 in G90 goes to the point X 1.0. In G91, the same word moves the tool 1 inch farther. A G91 left on by mistake sends the tool to a place nobody planned.

Work Offsets

G54 tells the control to use the first stored work zero. G55 through G59 are more slots. A table with two vises might use G54 for the left vise and G55 for the right one. The program is correct only if the right offset is called and the right values are stored there.

Tool Length Compensation

G43 H03 Z1.0 turns on tool length compensation using offset 3, and moves the tool to 1 inch above work zero. This is the first time a new tool comes down toward the part, so read this line every time. The H number should match the T number, unless your setup sheet says otherwise. If it does not match, the machine uses the length of a different tool.

Cutter Compensation

G41 and G42 shift the tool sideways by its radius. The programmer writes the shape of the part itself, and the control keeps the side of the cutter on that line, using the size stored under the D number. That lets the operator fine-tune a wall size with a D offset instead of asking for a new program.

  • G41 keeps the tool to the left of the line, as if you were standing behind the tool and looking the way it travels.
  • G42 keeps the tool to the right of the line.
  • G40 cancels compensation.

Compensation has to turn on during a move, called the lead-in, away from the part. It turns off during a lead-out. If the lead-in is too short for the tool radius, or the tool will not fit into an inside corner, the control may alarm, or the tool may gouge the part.

N210 G00 G54 X-1.0 Y-1.0 S3400 M03
N220 G43 H05 Z1.0 M08
N230 G00 Z0.1
N240 G01 Z-0.25 F15.0
N250 G41 D05 X0 Y-0.5 F20.0 (COMP ON DURING LEAD-IN)
N260 Y2.0
N270 X3.0
N280 Y0
N290 X-0.5
N300 G40 X-1.0 Y-1.0 (COMP OFF DURING LEAD-OUT)
N310 G00 Z1.0

This cuts around a 3 by 2 inch rectangle. The numbers in N260 to N280 are the real corners of the part, even though the center of the cutter never touches those points.

Returning Home With G28

G28 sends an axis to machine home, also called the reference position, and it goes there by way of an intermediate point given on the same line. The usual form is:

G91 G28 Z0
G90

G91 makes the zero incremental, so the intermediate point is "move 0 from here." The Z axis goes straight up to home from where it is. The G90 on the next line puts the machine back into absolute mode. It is needed because G91 is modal.

Warning: Read G28 lines carefully. In absolute mode (G90), G28 Z0 tells the machine to go through work Z zero on its way home. Work Z zero is usually the top of the part, so the tool can drive down into it first. Exact behavior varies by control, so check your manual.

On a lathe, the same idea is written G28 U0 W0, because U and W are the incremental names for X and Z.

Common M-Codes

Code Meaning
M00 Program stop, always
M01 Optional stop, only with the switch on
M03 Spindle on clockwise
M04 Spindle on counterclockwise
M05 Spindle stop
M06 Tool change
M07 Mist coolant on, on many machines
M08 Flood coolant on
M09 Coolant off
M30 End of program, reset to the top
M98 Call a subprogram
M99 Return from a subprogram

Canned Cycles

A canned cycle packs a repeated hole-making routine into one line. Once it is active, each new X and Y position makes another hole until G80 cancels it.

Word What it sets
X Y Hole location
Z Final depth of the hole
R Height of the R plane, where the feed starts
Q Depth of each peck, in peck cycles
F Feed rate
P Dwell at the bottom, in some cycles

The tool rapids down to the R plane, feeds to Z, and comes back up. Two codes choose how high it comes back between holes. G98 returns to the height where the cycle began. G99 returns only to the R plane, which saves time, but a clamp between holes can be hit. Check your clearances any time you see G99.

Cycle Use it for How it moves
G81 Shallow holes Feeds to Z and rapids out
G73 Medium holes Pecks with a short retract that snaps the chip
G83 Deep holes Pecks and retracts fully to clear chips
G84 Tapping Feeds in, reverses the spindle, feeds out

What Q Does in G83

In a peck drilling cycle, Q is how deep the drill cuts on each peck before it pulls out to clear chips. On most controls the count starts at the R plane. Here is a worked example:

G83 Z-0.75 Q0.2 R0.1 F6.0

The drill starts feeding at Z 0.1. It cuts down 0.2 inch to Z -0.1, pulls out, and goes back in. It goes on to Z -0.3, Z -0.5 and Z -0.7, retracting each time. The last peck is short and stops at the final depth of Z -0.75. A smaller Q takes longer but clears chips more often, so it is used for deep holes and stringy materials. A Q that is missing or too large can pack chips in the flutes and break the drill.

Tapping Feed

In G84, feed has to match the thread exactly. Feed in inches per minute equals RPM times pitch. A 1/4-20 tap has 20 threads per inch, so its pitch is 1 divided by 20, which is 0.05 inch. At 500 RPM, the feed is 500 x 0.05 = 25.0 inches per minute. If you see an F that does not fit the S and the tap size, stop and ask.

A Mill Program Line by Line

This program drills four holes with a 1/4-inch drill.

%
O1002 (BRACKET OP1 - DRILL FOUR HOLES)
(T3 = 1/4 DRILL)
N10 G20 G17 G40 G49 G80 G90
N20 G91 G28 Z0
N30 G90
N40 T3 M06
N50 G00 G54 X1.0 Y1.0 S2400 M03
N60 G43 H03 Z1.0 M08
N70 G99 G83 Z-0.75 Q0.2 R0.1 F6.0
N80 X3.0
N90 Y2.5
N100 X1.0
N110 G80
N120 G00 Z1.0 M09
N130 M05
N140 G91 G28 Z0
N150 G90
N160 M30
%
Line What happens
N10 Safe start: inch, XY plane, all compensation and cycles off, absolute
N20 Z goes straight up to home
N30 Back to absolute mode
N40 Tool 3 is loaded
N50 Selects G54, rapids over the first hole, starts the spindle at 2,400 RPM
N60 Applies tool 3's length, comes down to Z 1.0, flood coolant on
N70 Peck drills the first hole to 0.75 deep, returning to R between holes
N80 to N100 Each new position makes another hole with the same depth, peck and feed
N110 Cancels the cycle
N120 Rapids clear of the part, coolant off
N130 Spindle stops
N140, N150 Z returns home, back to absolute mode
N160 Program ends and resets

Notice how much you can read from this. The tool is a 1/4-inch drill, the holes are at (1.0, 1.0), (3.0, 1.0), (3.0, 2.5) and (1.0, 2.5), and they go 0.75 inch deep from work zero. Line N90 has only a Y word, because X stayed at 3.0.

A Short Lathe Program

A lathe program reads the same way, with a few differences.

  • X values are diameters on most lathes, so X1.0 means a 1-inch diameter, not a 1-inch distance from center.
  • The tool call has four digits. T0101 means tool 1 with offset 1.
  • G96 sets constant surface speed, so RPM rises as the tool nears center. G97 sets a fixed RPM.
  • G50 sets the maximum RPM in that mode. This limit protects you as the tool approaches center.
  • G99 means feed in inches per revolution, and G98 means inches per minute. This is a different meaning from a mill, where the same two codes set the return height of a cycle.
  • G41 and G42 apply tool nose radius compensation.
%
O2001 (PIN - FACE AND FINISH OD)
N10 G20 G40 G99
N20 G50 S2500
N30 T0101 (FINISH TOOL)
N40 G96 S450 M03
N50 G00 X1.6 Z0 M08
N60 G01 X-0.04 F0.006 (FACE PAST CENTER)
N70 G00 Z0.1
N80 G00 G42 X0.65
N90 G01 X1.0 Z-0.075 F0.006 (CHAMFER)
N100 Z-1.5 (TURN 1.000 DIAMETER)
N110 X1.6 (FACE UP THE SHOULDER)
N120 G00 G40 X2.0 Z0.2
N130 G28 U0 W0 M09
N140 M05
N150 M30
%

N20 limits the spindle to 2,500 RPM. N40 sets 450 surface feet per minute. N60 faces across the end. N80 turns on nose radius compensation on the way in, then N90 cuts the chamfer. N100 turns the diameter to a length of 1.5 inches. Larger lathe jobs use cycles such as G71 for roughing, G70 for finishing and G76 for threads, and the format of those cycles varies between controls.

Warning: A G96 line with no speed limit can let the RPM climb to the machine's maximum as the tool nears center. Check that the limit is there and is under the chuck's rated speed for that job.

Errors You Can Catch by Reading

You will not rewrite the program. If you find a problem, stop and tell the setup person or programmer. Editing a proven program without permission can cause a crash and loses track of the revision. These are the mistakes worth looking for before you press cycle start.

What you see What it means What could happen
T3 with H4 Wrong tool length used Tool crashes or cuts air
No G43 after a tool change No length applied Tool crash
G55 when the setup uses G54 Wrong work zero Runs in the wrong place
G91 with no G90 after it Stuck in incremental Tool goes to unplanned spots
G21 on an inch job Wrong units Moves badly wrong
Z0.75 where Z-0.75 belongs Sign error Cycle fails or cuts wrong
No G80 after a cycle Cycle stays active Extra hole at next position
G41 with no D, or wrong D Wrong tool size Wrong size or gouge
Missing F on the first cut No feed set Alarm or the old feed
M04 with a right-hand tool Spindle backward Tool rubs or breaks
X2 where X2.0 belongs Missing decimal On some controls a tiny move
Program number or revision is off Wrong program loaded Wrong part

A good reading routine takes a couple of minutes. Check the program number and revision. At each tool change, match T to H and D and match the tool to the setup sheet. Check the first Z move of each tool and the depth of each cycle. Check that G80 and G40 close out what was opened. Then press cycle start with confidence.

Check Your Understanding

1. What is wrong in this section of a program?

T5 M06 (1/2 DRILL)
G00 G54 X2.0 Y1.0 S1500 M03
G43 H4 Z1.0 M08
G81 Z0.5 R0.1 F5.0
X4.0
G00 Z1.0

There are three problems. H4 does not match tool 5, so the machine uses another tool's length. Z0.5 is a positive number, which is above the part and above the R plane, so the depth is almost surely a sign error. There is also no G80 to cancel the cycle.

2. A line reads G83 Z-1.1 Q0.25 R0.1 F5.0. How deep is each peck, and what is the hole's final depth?

Each peck cuts 0.25 inch before the drill pulls out to clear chips. The final depth is Z -1.1, which is 1.1 inches below work zero.

3. You see G90 G28 Z0 in a program. Why would you stop and ask?

In absolute mode the intermediate point is work Z zero, so the tool may go down to the part surface on its way home. The usual safe form uses G91 with the zero, followed by G90.

4. Line N80 of a program reads only X3.0, with no G code. How do you find out what it does?

Look back up the program. Modal codes such as G01 or an active canned cycle are still in effect, so the line could be a straight cut or another drilled hole.

Key Takeaways

  • A program is blocks of words, and most G codes stay active until replaced.
  • Read G43 lines every time. The H number must match the tool.
  • G41 and G42 let the programmer write the part's shape while the control offsets by the cutter radius.
  • G28 goes home through an intermediate point, so it is normally written with G91, and G90 follows.
  • In G83, Q sets the depth of each peck.
  • If you find an error, stop and report it. Do not edit a proven program without permission.