G-Code Reference Guide
A complete reference for G-code commands used in CNC machining. Each command includes a description, syntax, and example. Use CutViewer to visualize any of these commands in 3D.
Contents
G-Codes
- G00 — Rapid Move
- G01 — Linear Move
- G02 — Clockwise Arc
- G03 — Counter-Clockwise Arc
- G04 — Dwell
- G10 — Set Coordinate Data
- G17/G18/G19 — Plane Selection
- G20/G21 — Units
- G28 — Return to Home
- G40/G41/G42 — Cutter Compensation
- G43/G49 — Tool Length Offset
- G53 — Machine Coordinate Move
- G54–G59 — Work Coordinates
- G80–G89 — Canned Cycles
- G90/G91 — Absolute/Incremental
- G92 — Set Position
- G93/G94/G95 — Feed Rate Mode
G-Codes (Preparatory Commands)
G00 — Rapid Positioning
Moves the tool to the specified position at the machine's maximum traverse rate. Used for non-cutting moves — repositioning between cuts, moving to the start position, or retracting the tool. The path is not guaranteed to be a straight line on all machines.
G00 X1.0 Y2.0 Z0.5
Rapid move to X=1.0, Y=2.0, Z=0.5
G01 — Linear Interpolation
Moves the tool in a straight line to the specified position at the programmed feed rate (F). This is the primary cutting command — use it whenever you need the tool to follow a controlled, straight-line path through material.
G01 X2.0 Y3.0 F100
G01 X2.0 Y3.0 Z-0.25 F50
Linear move to X=2.0, Y=3.0 at feed rate 100. Second line adds a Z plunge.
G02 — Circular Interpolation (Clockwise)
Moves the tool in a clockwise arc. The arc center is defined relative to the start point using I/J/K offsets, or by specifying the radius with R. I is the X offset to center, J is the Y offset, and K is the Z offset.
; Arc using center offsets (I, J)
G02 X2.0 Y0.0 I1.0 J0.0 F100
; Arc using radius (R)
G02 X2.0 Y0.0 R1.0 F100
Both produce a clockwise arc with radius 1.0 ending at X=2.0, Y=0.0.
G03 — Circular Interpolation (Counter-Clockwise)
Same as G02 but the arc goes counter-clockwise. Uses the same I/J/K or R syntax.
G03 X2.0 Y0.0 I1.0 J0.0 F100
G04 — Dwell
Pauses program execution for the specified duration. The parameter P specifies the dwell time. On most controllers, P is in seconds; on some older machines, it may be in milliseconds — check your controller's documentation.
G04 P1.5
Pauses for 1.5 seconds.
G10 — Set Coordinate Data
Sets work coordinate offsets or tool data programmatically without manually editing the controller's offset table. Commonly used to set work offsets (G54–G59) from within a program.
; Set G54 origin to X=0, Y=0, Z=0
G10 L2 P1 X0 Y0 Z0
L2 = work coordinate offset, P1 = G54 (P2 = G55, etc.)
G17 / G18 / G19 — Plane Selection
Selects the active plane for arc commands (G02/G03). The plane determines which two axes define the arc and which axis is the helical axis.
| Code | Plane | Arc Axes | Helical Axis |
|---|---|---|---|
| G17 | XY plane | X, Y | Z |
| G18 | XZ plane | X, Z | Y |
| G19 | YZ plane | Y, Z | X |
G17 (XY plane) is the default on most machines and the most commonly used.
G20 / G21 — Units Selection
Sets the unit of measurement for all subsequent commands.
| Code | Units | Feed Rate Units |
|---|---|---|
| G20 | Inches | inches/minute |
| G21 | Millimeters | mm/minute |
Always set units at the start of your program. Mismatched units are one of the most common causes of CNC errors.
G28 — Return to Home Position
Returns the machine to its home (reference) position. If X, Y, or Z values are specified, the machine moves to that intermediate position first, then moves to home. This is important for clearing the workpiece before the axes move to home.
; Retract Z first, then go home
G28 G91 Z0
G28 X0 Y0
G40 / G41 / G42 — Cutter Radius Compensation
Offsets the tool path by the tool's radius so you can program the actual part geometry instead of the tool center path. Most hobbyist CAM software handles this in the CAM output, so you may not see these in your programs.
| Code | Function |
|---|---|
| G40 | Cancel cutter compensation |
| G41 | Cutter compensation left (tool left of path) |
| G42 | Cutter compensation right (tool right of path) |
G43 / G49 — Tool Length Offset
Applies (G43) or cancels (G49) a tool length compensation value from the tool table. Used on machines with automatic tool changers to account for different tool lengths.
; Apply tool length offset for tool 1
G43 H1 Z1.0
; Cancel tool length offset
G49
G53 — Machine Coordinate Move
G53 is not a standalone command — it's prepended to a G00 or G01 move to make that single move use machine coordinates instead of work coordinates. Unlike G54–G59, G53 is non-modal: it only applies to the line it appears on, then the active work coordinate system takes over again on the next line.
This is most commonly used for safe retracts and tool changes, where you need to move to an absolute machine position regardless of where the work origin is set.
; Retract Z to machine home (top of travel)
G53 G00 Z0
; Move to machine X0 Y0 for tool change
G53 G00 X0 Y0
; Next line is back in work coordinates (G54)
G00 X1.0 Y1.0
Important: G53 requires the machine to be homed first. The coordinates are relative to the machine's home position, not any work offset. Always use G53 with G00 (rapid) or G01 (linear) — it has no effect on its own.
G54 – G59 — Work Coordinate Systems
Selects one of six available work coordinate systems. Each stores a different origin offset, letting you set up multiple parts or fixtures on the same machine. G54 is the default and most commonly used.
| Code | Work Coordinate System |
|---|---|
| G54 | Work coordinate 1 (default) |
| G55 | Work coordinate 2 |
| G56 | Work coordinate 3 |
| G57 | Work coordinate 4 |
| G58 | Work coordinate 5 |
| G59 | Work coordinate 6 |
G80 – G89 — Canned Cycles
Canned cycles automate repetitive operations like drilling, tapping, and boring. Each cycle combines multiple motions into a single command. G80 cancels any active canned cycle.
| Code | Cycle |
|---|---|
| G80 | Cancel canned cycle |
| G81 | Simple drilling cycle |
| G82 | Drilling with dwell (spot drill / counterbore) |
| G83 | Peck drilling cycle (deep hole drilling) |
| G84 | Tapping cycle |
| G85 | Boring cycle (feed out) |
| G86 | Boring cycle (spindle stop, rapid out) |
| G89 | Boring cycle with dwell |
; Peck drill 5 holes at 0.25" depth increments
G83 Z-1.0 R0.1 Q0.25 F10
X1.0
X2.0
X3.0
X4.0
G80
Z = final depth, R = retract plane, Q = peck depth increment.
G90 / G91 — Absolute / Incremental Positioning
Controls how coordinates are interpreted.
| Code | Mode | Meaning of "X1.0" |
|---|---|---|
| G90 | Absolute | Move to X=1.0 (from work origin) |
| G91 | Incremental | Move 1.0 units in the +X direction from current position |
Most CNC programs use G90 (absolute). G91 is useful for subroutines and specific operations like retracting the tool a fixed amount.
G92 — Set Position (Coordinate System Offset)
Sets the current position to the specified values without moving the machine. This effectively shifts the coordinate system. On GRBL machines, G92 is commonly used to set a temporary work origin.
; Set current position as X=0, Y=0
G92 X0 Y0
Note: G92 behavior varies between controllers. On some machines it persists across power cycles; on others it doesn't. When possible, use G54–G59 work coordinates instead.
G93 / G94 / G95 — Feed Rate Mode
Controls how the F (feed rate) value is interpreted.
| Code | Mode | F Value Meaning |
|---|---|---|
| G93 | Inverse time | Moves per minute (each move must specify F) |
| G94 | Units per minute | inches/min or mm/min (most common) |
| G95 | Units per revolution | inches/rev or mm/rev (lathes) |
G94 is the default on most machines. G93 is used with multi-axis machining; G95 with lathes.
M-Codes (Miscellaneous Commands)
M00 / M01 — Program Stop
M00 — Unconditional program stop. The machine halts and waits for the operator to press cycle start to resume.
M01 — Optional stop. Same as M00 but only stops if the operator has enabled the optional stop switch on the controller.
M02 / M30 — Program End
M02 — Ends the program. Stops spindle and coolant.
M30 — Ends the program and rewinds to the beginning. On most machines, M30 is preferred over M02 because it resets the program counter.
M03 / M04 / M05 — Spindle Control
| Code | Function |
|---|---|
| M03 | Spindle on, clockwise (CW) |
| M04 | Spindle on, counter-clockwise (CCW) |
| M05 | Spindle off |
M03 S12000
; Start spindle clockwise at 12,000 RPM
M06 — Tool Change
Initiates a tool change. On machines with automatic tool changers, this swaps the current tool for the one specified by the T word. On manual machines, it pauses and waits for the operator to change the tool.
T2 M06
; Select tool 2 and perform tool change
M07 / M08 / M09 — Coolant Control
| Code | Function |
|---|---|
| M07 | Mist coolant on |
| M08 | Flood coolant on |
| M09 | All coolant off |
Other Codes
F — Feed Rate
Sets the speed at which the tool moves during cutting (G01, G02, G03). The unit depends on the active unit mode: inches per minute with G20, or millimeters per minute with G21 (when in G94 mode).
G01 X2.0 Y3.0 F100
Moves at 100 units/minute. Once set, the feed rate stays active until changed.
S — Spindle Speed
Sets the spindle rotation speed in RPM (revolutions per minute). Used with M03 or M04 to start the spindle.
S18000 M03
Starts the spindle at 18,000 RPM clockwise.
T — Tool Select
Selects a tool from the tool magazine or turret. The tool is not loaded until M06 is called. Tool numbers correspond to positions in the machine's tool table.
T3 M06
N — Line Number
Optional line number for reference and program organization. Line numbers don't affect execution — they're labels for human readability and for use with GOTO commands on some controllers.
N10 G00 X0 Y0
N20 G01 X1.0 F100
N30 G01 Y1.0
O — Program Number
Identifies the program with a unique number. Used on machines that store multiple programs. Typically appears as the first line of a program.
O1001
Example Program
Here's a complete G-code program that cuts a simple square pocket. You can paste this into CutViewer to see it in 3D.
(Square pocket — 1" x 1" x 0.25" deep)
G21 G90 G17 (Metric, absolute, XY plane)
G54 (Work coordinate system 1)
M03 S10000 (Spindle on at 10,000 RPM)
G04 P2 (Dwell 2 seconds for spindle to spin up)
(Rapid to start position)
G00 X0 Y0
G00 Z5.0 (Safe height)
(Cut the square)
G01 Z-3.0 F200 (Plunge to depth)
G01 X25.0 F500 (Cut along X)
G01 Y25.0 (Cut along Y)
G01 X0 (Cut back along X)
G01 Y0 (Cut back to start)
(Retract and end)
G00 Z5.0 (Retract)
M05 (Spindle off)
M30 (Program end)
Try It in CutViewer
Paste any G-code into CutViewer to see a real-time 3D visualization of the toolpath.
Launch CutViewer
Comments
Comments are ignored by the controller and are used to document the program for human readers. The two common comment styles:
; Semicolon-style comment (GRBL, many controllers)
(Parentheses-style comment — standard on most controllers)
G01 X1.0 (inline comment) F100
Parentheses comments are part of the G-code standard and work on most machines. Semicolon comments are common on GRBL-based machines and hobby controllers.