G-Code Reference Guide for CNC Programming
Overview
This is an internal reference guide for common G-codes used in CNC machining. Use this when troubleshooting post processors, analyzing customer G-code files, or explaining CNC programming concepts.
Motion Commands
G00 - Rapid Positioning
Purpose: Moves the machine at maximum rapid speed to a specified position.
Usage: G00 X100 Y50 Z5
Notes:
- Does NOT cut material - used for positioning only
- Takes the fastest path (not necessarily a straight line)
- Commonly used to move to safe heights or position between cuts
Example:
G00 Z10.0 (Rapid to safe height) G00 X50 Y75 (Rapid to next position)
G01 - Linear Interpolation
Purpose: Moves the machine in a straight line at a controlled feed rate.
Usage: G01 X100 Y50 Z-10 F500
Notes:
- This is your cutting move
- Requires a feed rate (F parameter)
- Moves in a perfectly straight line
- Used for milling, profiling, drilling
Example:
G01 Z-10.0 F300 (Plunge into material at 300mm/min) G01 X100 Y50 F1500 (Cut to position at 1500mm/min)
G02 - Circular/Helical Interpolation (Clockwise)
Purpose: Cuts an arc or helix in the clockwise direction.
Usage: G02 X100 Y50 I25 J0 F1000
Parameters:
- X, Y, Z = End point of arc
- I, J, K = Offset from start point to arc center (I=X, J=Y, K=Z)
- R = Radius (alternative to I, J, K for simple arcs)
Notes:
- Direction is clockwise when viewed from the positive end of the perpendicular axis
- Commonly used for rounded corners, circular pockets, helical ramping
Example:
G17 (Select XY plane) G02 X50 Y50 I25 J0 F1200 (Arc from current position to X50 Y50, center at I25 J0)
G03 - Circular/Helical Interpolation (Counter-Clockwise)
Purpose: Cuts an arc or helix in the counter-clockwise direction.
Usage: G03 X100 Y50 I25 J0 F1000
Notes:
- Same as G02 but in the opposite direction
- All other parameters work identically to G02
Example:
G03 X100 Y0 R50 F1500 (Counter-clockwise arc with 50mm radius)
G04 - Dwell
Purpose: Pauses program execution for a specified time.
Usage: G04 P2.0 or G04 X2.0
Parameters:
- P = Dwell time in seconds (most common)
- X = Dwell time in seconds (some controllers)
Notes:
- Used to let the spindle reach full speed after a tool change
- Can be used to allow chips to clear
- Machine continues to hold position during dwell
Example:
M03 S18000 (Start spindle at 18,000 RPM) G04 P2.0 (Wait 2 seconds for spindle to reach speed) G01 Z-5 F500 (Begin cutting)
Plane Selection
G17 - XY Plane Selection
Purpose: Selects the XY plane for circular interpolation (G02/G03).
Usage: G17
Notes:
- This is the default plane on most machines
- Arc center offsets use I (X-axis) and J (Y-axis)
- Looking down at the table from above
G18 - XZ Plane Selection
Purpose: Selects the XZ plane for circular interpolation.
Usage: G18
Notes:
- Arc center offsets use I (X-axis) and K (Z-axis)
- Used for vertical arcs on the side of a part
- Less common than G17
G19 - YZ Plane Selection
Purpose: Selects the YZ plane for circular interpolation.
Usage: G19
Notes:
- Arc center offsets use J (Y-axis) and K (Z-axis)
- Rarely used in typical cabinet work
Units Selection
G20 - Inch System Selection
Purpose: Sets all coordinates and feed rates to inches.
Usage: G20
Notes:
- Typically appears at the start of a program
- All subsequent values are interpreted as inches until changed
- Feed rates are in inches per minute (IPM)
Example:
G20 (Switch to inches) G00 X4.0 Y3.5 (Move to X=4", Y=3.5")
G21 - Millimeter System Selection
Purpose: Sets all coordinates and feed rates to millimeters.
Usage: G21
Notes:
- More common internationally
- All subsequent values are in millimeters
- Feed rates are in millimeters per minute (mm/min)
Example:
G21 (Switch to millimeters) G00 X100.0 Y50.0 (Move to X=100mm, Y=50mm)
Cutter Compensation
G40 - Cancel Cutter Diameter Compensation
Purpose: Turns off cutter diameter compensation.
Usage: G40
Notes:
- Returns to programming the actual tool center path
- Should be called before tool changes
- Default state
G41 - Start Cutter Diameter Compensation Left
Purpose: Offsets the tool to the left of the programmed path (looking in the direction of travel).
Usage: G41 D01 or G41
Parameters:
- D = Tool diameter offset number (from tool table)
Notes:
- Machine automatically offsets by half the tool diameter
- Used when programming to the edge of a part rather than tool center
- Tool moves to the LEFT of the line
Example:
G41 D01 (Enable compensation left, using offset #1) G01 X100 F1000 (Tool center will be offset left of X100) G40 (Cancel compensation)
G42 - Start Cutter Diameter Compensation Right
Purpose: Offsets the tool to the right of the programmed path.
Usage: G42 D01
Notes:
- Same as G41 but offsets to the RIGHT
- Used for inside cuts vs. outside cuts
Tool Length Compensation
G43 - Tool Length Offset (Plus)
Purpose: Applies a tool length offset from the tool table.
Usage: G43 H01 or G43 Z5.0 H01
Parameters:
- H = Tool length offset number (from tool table)
- Z = Optional Z-position to move to after applying offset
Notes:
- Compensates for different tool lengths
- Offset value is ADDED to Z-position
- Required after every tool change
Example:
T01 M06 (Change to tool #1) G43 H01 Z10.0 (Apply tool length offset #1, move to Z10)
G49 - Cancel Tool Length Offset
Purpose: Cancels tool length offset compensation.
Usage: G49
Notes:
- Returns to machine coordinate Z-position
- Typically used before tool changes
- Not always necessary if G43 is reapplied after tool change
Coordinate Systems
G53 - Motion in Machine Coordinate System
Purpose: Temporarily moves in machine coordinates (ignoring work offsets).
Usage: G53 G00 Z0
Notes:
- Only active for the line it's on (non-modal)
- Used to move to machine home or tool change position
- Ignores G54-G59 work offsets
Example:
G53 G00 Z0 (Rapid to Z-home in machine coordinates)
G54-G59 - Work Coordinate Systems
Purpose: Selects a stored work coordinate system (work offset).
Usage: G54, G55, G56, G57, G58, G59
Extended Systems: G59.1, G59.2, G59.3
Notes:
- G54 is the default and most commonly used
- Each stores an X, Y, Z offset from machine home
- Allows you to have multiple setups on one table
- Set using machine probing or manual touch-off
Common Usage:
- G54 = Main work area / primary setup
- G55 = Secondary setup area
- G56-G59 = Additional fixtures or parts
Example:
G54 (Use work coordinate system 1) G00 X0 Y0 Z5 (Move to X0 Y0 in G54 coordinates)
Canned Cycles
G80 - Cancel Motion Mode (Cancel Canned Cycle)
Purpose: Cancels any active canned cycle.
Usage: G80
Notes:
- Must be called to exit a drilling cycle before moving to other operations
- Returns to normal G01 mode
G81 - Drilling Canned Cycle
Purpose: Simple drilling cycle (rapid in, feed to depth, rapid out).
Usage: G81 X50 Y50 Z-10 R2 F300
Parameters:
- X, Y = Hole position
- Z = Final drilling depth
- R = Retract plane (safe height above material)
- F = Feed rate
Cycle sequence:
- Rapid to XY position
- Rapid down to R plane
- Feed to Z depth
- Rapid back to R plane
Example:
G90 G54 G00 Z10 (Setup) G81 X10 Y10 Z-15 R2 F500 (Drill first hole) X30 Y10 (Drill second hole, same Z and R) X50 Y10 (Drill third hole) G80 (Cancel cycle)
G82 - Drilling with Dwell Canned Cycle
Purpose: Drilling cycle with a dwell at the bottom of the hole.
Usage: G82 X50 Y50 Z-10 R2 P1.0 F300
Parameters:
- P = Dwell time in seconds (in addition to G81 parameters)
Notes:
- Used when you need a clean hole bottom
- Allows spindle to clear chips
- Common for flat-bottom holes or counterbores
Example:
G82 X25 Y25 Z-12 R2 P0.5 F400 (Drill with 0.5 sec dwell)
G83 - Chip-Breaking (Peck) Drilling Canned Cycle
Purpose: Drilling cycle that retracts periodically to break/clear chips.
Usage: G83 X50 Y50 Z-30 R2 Q5 F300
Parameters:
- Q = Peck depth (depth of each drilling increment)
- All other parameters same as G81
Cycle sequence:
- Rapid to XY, then to R
- Feed down Q amount
- Rapid up slightly to break chip
- Rapid back down near previous depth
- Repeat until reaching final Z depth
Notes:
- Essential for deep holes (depth > 3× diameter)
- Prevents chip packing and tool breakage
- Slower but safer than G81
Example:
G83 X50 Y50 Z-40 R2 Q8 F500 (Drill 40mm deep, pecking 8mm at a time)
G84 - Right-Hand Tapping Canned Cycle
Purpose: Synchronized tapping cycle for threading holes.
Usage: G84 X50 Y50 Z-15 R2 F500
Notes:
- Spindle speed and feed rate must match thread pitch
- Formula: Feed Rate = RPM × Pitch
- Example: M6×1.0 thread at 500 RPM → F500
- Spindle reverses automatically to back out of thread
Example:
M03 S500 (Start spindle at 500 RPM) G84 X30 Y30 Z-15 R2 F500 (Tap M6×1.0 thread, 15mm deep) G80 (Cancel cycle)
G85 - Boring, No Dwell, Feed Out Canned Cycle
Purpose: Precision boring where the tool feeds in and feeds back out.
Usage: G85 X50 Y50 Z-20 R2 F200
Notes:
- Feeds in AND feeds out (no rapid retract)
- Better finish than G81 since tool doesn't retract rapidly
- Used for precision holes
- Slower than G81
G86 - Boring, Spindle Stop, Rapid Out Canned Cycle
Purpose: Boring cycle that stops the spindle at depth, then rapids out.
Usage: G86 X50 Y50 Z-20 R2 F200
Cycle sequence:
- Rapid to XY and R plane
- Feed to Z depth
- Spindle stops (M05)
- Rapid back to R plane
- Spindle restarts (M03)
Notes:
- Prevents tool marks on hole wall during retract
- Used for finished bore holes
- Requires spindle to restart after each hole
G87 - Back Boring Canned Cycle
Purpose: Boring from the back side of a part (advanced cycle).
Usage: Varies by controller - consult machine manual
Notes:
- Rarely used in cabinet work
- Complex cycle with multiple retracts and repositions
G88 - Boring, Spindle Stop, Manual Out Canned Cycle
Purpose: Boring cycle that stops for manual retraction.
Usage: G88 X50 Y50 Z-20 R2 F200
Notes:
- Program pauses at depth
- Operator manually retracts the tool
- Rarely used with modern machines
G89 - Boring, Dwell, Feed Out Canned Cycle
Purpose: Precision boring with a dwell at depth, then feed out.
Usage: G89 X50 Y50 Z-20 R2 P1.0 F200
Parameters:
- P = Dwell time at bottom of hole
Notes:
- Highest quality finish
- Combines feed-out (like G85) with dwell (like G82)
- Slowest but best for critical dimensions
Distance Modes
G90 - Absolute Distance Mode
Purpose: All coordinates are absolute positions from the work coordinate system origin.
Usage: G90
Notes:
- This is the default mode
- X50 Y50 means "go to position X=50, Y=50" (not "move 50mm from current position")
- Most common mode for CNC programming
Example:
G90 (Absolute mode) G00 X0 Y0 (Go to origin) G01 X50 Y50 (Go to X50, Y50)
G91 - Incremental Distance Mode
Purpose: All coordinates are relative to the current position.
Usage: G91
Notes:
- X50 Y50 means "move 50mm in X and 50mm in Y from current position"
- Useful for repetitive patterns
- Must switch back to G90 when done
Example:
G91 (Incremental mode) G01 X10 (Move 10mm in +X direction) G01 X10 (Move another 10mm in +X direction) G90 (Back to absolute mode)
G92 - Offset Coordinate Systems
Purpose: Temporarily shifts the work coordinate system.
Usage: G92 X0 Y0 Z0
Notes:
- Makes the current position equal to the specified coordinates
- Creates a temporary offset
- Use with caution - can cause confusion
- Generally avoid using G92; use G54-G59 instead
Example:
(Machine is at X100 Y50 in G54) G92 X0 Y0 (Tells machine "this position is now X0 Y0") (Machine coordinate display now shows X0 Y0, but you're still at physical X100 Y50)
Modal vs. Non-Modal Commands
Modal Commands (Stay active until changed)
- G00, G01, G02, G03 (motion modes)
- G17, G18, G19 (plane selection)
- G20, G21 (units)
- G40, G41, G42 (cutter comp)
- G43 (tool length offset)
- G54-G59 (work coordinates)
- G81-G89 (canned cycles)
- G90, G91 (distance mode)
Non-Modal Commands (Only active on the line they appear)
- G04 (dwell)
- G53 (machine coordinate move)
- G92 (coordinate offset - technically modal but shouldn't be)
Common G-Code Combinations
Standard Program Header:
% (Program start character) O0001 (Program number) (Program Name: Test Part) G21 (Metric) G90 (Absolute) G17 (XY plane) G54 (Work coordinate system 1) G00 Z10.0 (Safe height) M06 T01 (Tool change to tool 1) G43 H01 (Apply tool length offset) M03 S18000 (Spindle on CW at 18000 RPM) G04 P2.0 (Dwell 2 seconds)
Standard Tool Change Sequence:
G00 Z10.0 (Retract to safe height) M05 (Stop spindle) M06 T02 (Change to tool 2) G43 H02 (Apply tool length offset for tool 2) M03 S15000 (Start spindle at 15000 RPM) G04 P2.0 (Dwell for spindle ramp-up)
Standard Program Footer:
G00 Z10.0 (Retract) M05 (Spindle off) G53 G00 Z0 (Move to Z-home in machine coordinates) M30 (Program end and reset) %
Troubleshooting Common G-Code Issues
| Issue | Likely Cause | Check These G-Codes |
|---|---|---|
| Tool crashes into material | No tool length offset applied | G43, G49 |
| Arcs come out wrong | Wrong plane selected | G17, G18, G19 |
| Drill depth wrong | Using incremental mode instead of absolute | G90, G91 |
| Tool cutting on wrong side of line | Wrong cutter compensation direction | G41, G42, G40 |
| Coordinates way off | Wrong work offset active | G54-G59 |
| Feed rate way too fast/slow | Wrong units | G20, G21 |
| Program won't exit drill cycle | Forgot to cancel canned cycle | G80 |