What Is G-Code?
G-code (formally known as RS-274) is the standardized programming language used to control CNC machines. When your CAM software generates a toolpath, it exports that toolpath as a G-code file — a plain text file filled with short, cryptic-looking lines of code. Each line gives the machine a specific instruction: move here, turn on the spindle, change the feed rate, stop.
While most modern CNC work is done with CAM software doing the heavy lifting, understanding G-code is invaluable. It lets you edit programs on the fly, troubleshoot errors, write quick utility programs by hand, and truly understand what your machine is doing.
The Structure of a G-Code Line
Each line in a G-code program is called a block. A typical block looks like this:
N010 G01 X2.500 Y1.250 Z-0.125 F15.0
- N010 — Line number (optional, used for reference)
- G01 — A G-code command (in this case, linear interpolation / controlled feed move)
- X2.500 Y1.250 Z-0.125 — Target coordinates
- F15.0 — Feed rate (15 inches per minute)
Essential G-Codes
These are the commands you'll encounter in virtually every CNC program:
| Code | Name | Description |
|---|---|---|
| G00 | Rapid Positioning | Moves the tool at maximum speed. Use for non-cutting moves only. |
| G01 | Linear Interpolation | Straight-line cutting move at a specified feed rate. |
| G02 | Circular Interpolation (CW) | Arcing move in the clockwise direction. |
| G03 | Circular Interpolation (CCW) | Arcing move in the counter-clockwise direction. |
| G17 | XY Plane Selection | Sets circular interpolation to the XY plane (most common). |
| G20 / G21 | Inch / Metric Mode | Sets units for all coordinate values. |
| G28 | Return to Machine Home | Sends the machine to its home position. |
| G40 | Cancel Cutter Comp | Turns off tool radius compensation. |
| G90 | Absolute Positioning | All moves are relative to the work zero point. |
| G91 | Incremental Positioning | All moves are relative to the current position. |
Essential M-Codes
M-codes control machine functions rather than movement:
| Code | Function |
|---|---|
| M03 | Spindle ON — Clockwise |
| M04 | Spindle ON — Counter-Clockwise |
| M05 | Spindle OFF |
| M06 | Tool Change |
| M08 | Coolant ON |
| M09 | Coolant OFF |
| M30 | End of Program (rewinds to start) |
A Simple Example Program
Here's a complete G-code program that cuts a 1-inch square pocket to a depth of 0.1 inches:
G21 (Metric mode)
G90 (Absolute positioning)
G00 Z5.0 (Rapid to safe height)
M03 S12000 (Spindle on at 12,000 RPM)
G00 X0 Y0 (Rapid to start position)
G01 Z-2.54 F300 (Plunge to depth)
G01 X25.4 F600 (Cut along X)
G01 Y25.4 (Cut along Y)
G01 X0 (Return on X)
G01 Y0 (Return on Y)
G00 Z5.0 (Retract to safe height)
M05 (Spindle off)
M30 (End program)
Tips for Writing and Editing G-Code
- Always start with a safe retract height — Never begin a program with the tool at cutting depth.
- Use G90 (absolute) by default — Incremental mode (G91) is easy to lose track of and cause crashes.
- Comment your code — Most controllers accept comments in parentheses ( ) or after a semicolon. Use them.
- Verify with a simulator — Tools like NC Viewer or CAMotics let you visualize G-code before sending it to the machine.