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:
- Troubleshoot problems when cuts go wrong
- Make manual edits to generated files
- Understand what your machine is doing
- Verify files before running them
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:
- G or M code: The command type (G1 = linear move)
- Parameters: Values like X, Y, Z coordinates or F for feed rate
- Comments: Text after ; or in parentheses () is ignored
Lines are also called "blocks" and are typically numbered:
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.
G1 - Linear Move (Cutting)
Moves in a straight line at a controlled feed rate. This is used for actual cutting.
G2 / G3 - Arc Moves
Cut circular arcs. G2 is clockwise, G3 is counter-clockwise (when viewed from above).
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
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. |
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:
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.