G-code is the language CNC machines understand. Whether the program was written by hand, generated by CAM software, or created at the machine control with conversational programming, the machine executes G-code commands one line at a time. Understanding the basic codes lets you read any program, make simple edits at the machine, and troubleshoot problems without going back to the programmer.
\nThis guide covers program structure, the coordinate system, motion commands, canned cycles, and the essential codes that every CNC operator should know by memory. The focus is on the ANSI/ISO standard codes that work across Fanuc, Haas, Mazak, and most other common controls.
Program Structure: Safety Block to End
A well-structured CNC program follows a consistent pattern: program start, safety block, tool calls with cutting operations, and program end. The safety block at the beginning cancels any modal codes left over from a previous program and establishes known starting conditions.
\nA typical safety block for a milling program: G17 G20 G40 G49 G80 G90. This sets the XY plane (G17), inch mode (G20), cancels cutter compensation (G40), cancels tool length compensation (G49), cancels any canned cycle (G80), and sets absolute positioning (G90). Every program should start with some version of this block to prevent unexpected behavior from leftover modal states.
\nEach tool operation follows a pattern: call the tool (T1 M06), set the spindle speed and direction (S3000 M03), activate coolant (M08), set the work offset (G54), activate tool length compensation (G43 H1 Z1.0), execute the cutting moves, retract, turn off coolant (M09), and stop the spindle (M05). Repeat for each tool.
\nThe program ends with M30 (end of program and rewind) or M02 (end of program without rewind). M30 is more common because it resets the program to the beginning, ready for the next part.
O0001 (PROGRAM NUMBER)
G17 G20 G40 G49 G80 G90 (SAFETY BLOCK)
T1 M06 (TOOL CHANGE)
S3000 M03 (SPINDLE ON)
G54 (WORK OFFSET)
G43 H1 Z1.0 (TOOL LENGTH COMP)
M08 (COOLANT ON)
... cutting moves ...
M09 (COOLANT OFF)
G28 Z0 (RETURN HOME Z)
M05 (SPINDLE OFF)
M30 (END AND REWIND)
CNC G-Code Quick Reference
Searchable CNC G-code and M-code reference. Generic descriptions, syntax examples, and mill vs lathe applicability for 70+ codes. Controller-agnostic per ISO 6983.
Coordinate Systems: G54 Through G59
CNC machines use a coordinate system where every point in the work envelope has a unique X, Y, Z address. The machine home position (where the machine goes on power-up or G28 command) is the machine zero. But you do not program parts from machine zero — you set up work coordinate offsets (G54 through G59) that define the part zero relative to machine zero.
\nG54 is the first (and most commonly used) work offset. When you touch off the part and enter the offset values, you are telling the machine: "When I say X0 Y0 Z0 in the program, I mean this physical location." G55 through G59 provide five additional offsets for multi-part setups, fixtures with multiple stations, or parts that require different zero points for different operations.
\nThe distinction between G90 (absolute) and G91 (incremental) is about how coordinates are interpreted. In G90, X2.000 means "go to X=2.000 from the work zero." In G91, X2.000 means "move 2.000 inches from the current position in the X direction." Most programs use G90 because absolute coordinates are less error-prone — a wrong move in absolute mode puts you in the wrong place, but a wrong move in incremental mode puts you in the wrong place AND every subsequent move is wrong by the same amount.
• Bolt patterns and hole patterns: one subroutine, called at different positions
• Pecking operations: retract amounts relative to current depth
• Repeat patterns: same move applied at multiple locations
Use G90 for everything else. Return to G90 after any G91 block to avoid confusion.
Motion Commands: G00, G01, G02, G03
G00 (Rapid Traverse): Moves the tool at the machine's maximum speed to the commanded position. Used for non-cutting moves: approaching the part, repositioning between features, retracting after a cut. Never use G00 during cutting — the uncontrolled speed produces no useful machining and risks crashing the tool.
\nG01 (Linear Feed): Moves the tool in a straight line at the programmed feed rate (F value). This is the primary cutting command. G01 X2.0 Y1.5 F10.0 moves the tool in a straight line to X2.000 Y1.500 at 10 inches per minute. Every straight-line cut, plunge, and ramp uses G01.
\nG02 (Circular Clockwise): Cuts a clockwise arc. Specify the endpoint (X, Y) and either the arc center (I, J) or the radius (R). G02 X2.0 Y0 I1.0 J0 cuts a clockwise 90-degree arc. The I and J values are incremental distances from the start point to the arc center.
\nG03 (Circular Counter-Clockwise): Same as G02 but in the opposite direction. G03 is used for counter-clockwise arcs. The same I/J or R syntax applies.
\nThese four commands account for the vast majority of all tool motion in a CNC program. Learn them cold and you can trace through any program path with a pencil and the print.
• Never rapid into a cut. Always switch to G01 before the tool contacts material.
• Rapid Z moves first (retract), then XY (reposition), then Z again (approach). This prevents dragging the tool across the part.
• Many controls move all axes simultaneously on G00. This means the tool takes a diagonal path, which may collide with clamps or fixtures that a straight-axis move would clear.
Job Setup Sheet Generator
Generate CNC job setup sheets with automatic speed, feed, and MRR calculations. Printable shop floor documents with chipload verification, workholding, and coolant notes.
Canned Cycles: G81, G83, G84, and More
Canned cycles are pre-programmed motion sequences for common hole-making operations. Instead of programming every move of a drilling operation (rapid to R plane, feed to Z depth, rapid out), you call a canned cycle with the required parameters and the control handles the motion automatically.
\nG81 (Drill): Simple drilling. Rapids to R plane, feeds to Z depth, rapids out. Used for shallow holes where chip clearance is not a concern. G81 X1.0 Y1.0 Z-0.500 R0.100 F5.0 drills a 0.500-deep hole at X1 Y1.
\nG83 (Peck Drill): Drilling with peck cycle for chip clearing. The tool feeds to the first peck depth (Q value), rapids out to R plane to clear chips, rapids back to the previous depth minus a small clearance, feeds the next peck, and repeats until reaching Z depth. Essential for deep holes (depth > 3x diameter).
\nG84 (Tapping): Rigid tapping cycle. The spindle speed, feed rate, and pitch must be synchronized. The control automatically calculates: F = S × pitch (in IPR) or S / TPI (in IPM with pitch in inches). Rigid tapping produces better thread quality than floating tap holders.
\nG85/G86/G89 (Boring): Various boring cycles with different retract behaviors — feed out (G85), rapid out with spindle stop (G86), or dwell and feed out (G89). Used for precision bores where the retract behavior affects surface finish.
\nCancel canned cycles with G80 before programming any non-drilling operation. Canned cycles are modal — they stay active until cancelled.
G83 X_ Y_ Z_ R_ Q_ F_
X, Y = hole location
Z = final depth (negative)
R = reference plane (start of feed)
Q = peck depth increment
F = feed rate
Example: G83 X1.0 Y1.0 Z-1.500 R0.100 Q0.250 F4.0
Pecks 0.250 per pass to 1.500 deep at 4 IPM.
CNC G-Code Quick Reference
Searchable CNC G-code and M-code reference. Generic descriptions, syntax examples, and mill vs lathe applicability for 70+ codes. Controller-agnostic per ISO 6983.