Reference Guide

G-code Reference

Common CNC commands explained with examples

What is G-code?

G-code is the programming language used to control CNC machines. It tells the machine where to move, how fast to move, and what actions to perform (like turning the spindle on or off).

Your CAM software (VCarve, Fusion 360, Carbide Create, etc.) generates G-code automatically from your designs. Understanding G-code helps you:

Tip: Use our G-code Viewer to visualize any G-code file and see exactly what each line does before running it on your machine.

G-code Structure

A G-code file is a plain text file containing commands, one per line. Each line typically contains:

G1 X100 Y50 F1000 ; Linear move to X100 Y50 at feed rate 1000

Lines are also called "blocks" and are typically numbered:

N10 G0 X0 Y0 N20 G0 Z5 N30 G1 Z-2 F500

Line numbers (N10, N20...) are optional and mainly used for reference.

Motion Commands

These are the most common commands—they control how the tool moves.

Command Name Description
G0 Rapid Move Move to position as fast as possible. Used for non-cutting moves (repositioning). Never use while the tool is in material.
G1 Linear Move Move in a straight line at the specified feed rate. This is the main cutting command.
G2 Clockwise Arc Cut a circular arc moving clockwise. Requires endpoint and either radius (R) or center point (I, J).
G3 Counter-Clockwise Arc Cut a circular arc moving counter-clockwise.

G0 - Rapid Move

Moves the tool at maximum speed. Use only when the tool is clear of the material.

G0 X50 Y25 ; Rapid move to X50 Y25 G0 Z10 ; Rapid move up to Z10 (safe height)

G1 - Linear Move (Cutting)

Moves in a straight line at a controlled feed rate. This is used for actual cutting.

G1 X100 Y50 F1500 ; Cut to X100 Y50 at 1500 mm/min (or ipm) G1 Z-3 F500 ; Plunge to Z-3 at slower feed rate

G2 / G3 - Arc Moves

Cut circular arcs. G2 is clockwise, G3 is counter-clockwise (when viewed from above).

; Arc using radius (R) G2 X20 Y0 R10 F1000 ; Clockwise arc to X20 Y0, radius 10 ; Arc using center offset (I, J) G3 X20 Y0 I10 J0 F1000 ; CCW arc, center is 10 units in X from start

Arc direction matters: G2/G3 direction is defined looking down at the XY plane (from positive Z). Clockwise when viewed from above = G2.

Coordinate Commands

Command Name Description
G90 Absolute Mode Coordinates are relative to the origin (work zero). Most common mode.
G91 Incremental Mode Coordinates are relative to the current position. Each move is an offset from where you are.
G28 Return to Home Move to machine home position.
G53 Machine Coordinates Use machine coordinates instead of work coordinates for this line only.
G54-G59 Work Offsets Select different work coordinate systems. G54 is the default.

Absolute vs Incremental

; Absolute mode (G90) - coordinates are from origin G90 G0 X10 ; Move to X=10 G0 X20 ; Move to X=20 G0 X30 ; Move to X=30 ; Incremental mode (G91) - coordinates are offsets G91 G0 X10 ; Move 10 units in X (now at X=10) G0 X10 ; Move another 10 units (now at X=20) G0 X10 ; Move another 10 units (now at X=30)

Unit Commands

Command Description
G20 Set units to inches. All coordinates and feed rates are in inches.
G21 Set units to millimeters. All coordinates and feed rates are in mm.

Important: Always verify your file's units match your machine's expectation. Running a metric file on a machine expecting inches (or vice versa) will result in cuts that are ~25x too large or too small.

Spindle & Tool Commands

Command Description
M3 Spindle ON, clockwise rotation. Usually followed by S parameter for speed.
M4 Spindle ON, counter-clockwise rotation. Rarely used in routing.
M5 Spindle OFF.
S Spindle speed in RPM. Example: S18000 = 18,000 RPM
T Tool select. Example: T1 = select tool 1
M6 Tool change. Pauses for manual tool change on most hobby machines.
T1 M6 ; Select tool 1 and perform tool change M3 S18000 ; Spindle on clockwise at 18000 RPM ; ... cutting operations ... M5 ; Spindle off

M Codes (Machine Commands)

M codes control machine functions other than motion.

Command Description
M0 Program Pause. Stops execution until user resumes. Useful for manual checks or tool inspection.
M1 Optional Pause. Only pauses if optional stop is enabled on controller.
M2 Program End. Ends the program.
M3 Spindle ON (clockwise)
M5 Spindle OFF
M6 Tool Change
M7 Mist Coolant ON
M8 Flood Coolant ON
M9 Coolant OFF
M30 Program End and Reset. Ends program and resets to beginning.

Common Parameters

Parameter Description Example
X X-axis position X100.5
Y Y-axis position Y50.25
Z Z-axis position (depth) Z-2.5
F Feed rate (units/min) F1500
S Spindle speed (RPM) S18000
I Arc center X offset (from start) I10
J Arc center Y offset (from start) J0
R Arc radius R12.5
T Tool number T1
N Line number (optional) N100

Example Program

Here's a complete G-code program that cuts a simple square pocket:

; Simple square pocket - 50mm x 50mm, 5mm deep ; Generated for demonstration purposes G21 ; Set units to millimeters G90 ; Absolute positioning G17 ; XY plane selection T1 M6 ; Select tool 1, tool change M3 S18000 ; Spindle on at 18000 RPM G4 P2 ; Dwell 2 seconds for spindle to reach speed ; Move to start position G0 X5 Y5 ; Rapid to start XY G0 Z5 ; Rapid to safe height ; First pass - depth 2.5mm G1 Z-2.5 F500 ; Plunge to first depth G1 X45 F1500 ; Cut along X G1 Y45 ; Cut along Y G1 X5 ; Cut back along X G1 Y5 ; Cut back along Y (close square) ; Second pass - depth 5mm G1 Z-5 F500 ; Plunge to final depth G1 X45 F1500 ; Cut along X G1 Y45 ; Cut along Y G1 X5 ; Cut back along X G1 Y5 ; Cut back along Y ; Retract and end G0 Z20 ; Rapid up to safe height M5 ; Spindle off G0 X0 Y0 ; Return to origin M30 ; Program end

Quick Reference Cheat Sheet

Motion

G0 - Rapid move
G1 - Linear cut
G2 - Arc CW
G3 - Arc CCW

Position

G90 - Absolute
G91 - Incremental
G28 - Go home
G54 - Work offset 1

Units

G20 - Inches
G21 - Millimeters

Spindle

M3 Sxxxx - On CW @ RPM
M5 - Off
M6 - Tool change

Program Control

M0 - Pause
M2 - End
M30 - End & reset

Parameters

X Y Z - Position
F - Feed rate
S - Spindle RPM
R / I J - Arc

Practice safely: Always verify G-code in a simulator before running on your machine. Use our G-code Viewer to visualize toolpaths and check for errors.