G-Code is the standard programming language used to control CNC (Computer Numerical Control) machines. Every CNC mill, lathe, laser cutter, plasma cutter, and even 3D printer relies on G-Code instructions to move the tool, set speeds, and perform cutting operations. In this comprehensive guide, you will learn the fundamental G-Code commands, see practical examples, and understand the differences between major CNC controllers.
A G-Code program is a plain text file read line by line by the CNC controller. Each line (called a "block") contains one or more commands that tell the machine what to do. The controller processes these blocks sequentially, moving the cutting tool along precise paths at specified speeds.
A typical G-Code program follows this structure:
| Code | Description | Example |
|---|---|---|
| G0 | Rapid positioning (no cutting) | G0 X100 Y50 |
| G1 | Linear interpolation (cutting move) | G1 X50 Y30 F500 |
| G2 | Circular interpolation clockwise (CW) | G2 X20 Y0 I-10 J0 |
| G3 | Circular interpolation counter-clockwise (CCW) | G3 X20 Y0 I-10 J0 |
| G17 | XY plane selection | G17 |
| G20 | Inch unit system | G20 |
| G21 | Metric unit system (mm) | G21 |
| G28 | Return to reference point | G28 G91 Z0 |
| G40 | Cancel cutter radius compensation | G40 |
| G83 | Peck drilling cycle | G83 Z-20 R2 Q5 F100 |
| G90 | Absolute coordinate system | G90 |
| G91 | Incremental coordinate system | G91 |
| Code | Description |
|---|---|
| M3 | Spindle on clockwise |
| M4 | Spindle on counter-clockwise |
| M5 | Spindle stop |
| M6 | Tool change |
| M7 | Mist coolant on |
| M8 | Flood coolant on |
| M9 | Coolant off |
| M30 | Program end and rewind |
The following program cuts a simple 50x30mm rectangular pocket:
Defines how fast the tool moves during cutting, measured in mm/min (or inches/min with G20). Typical values: 500 mm/min for aluminum, 200 mm/min for steel. Too fast causes tool breakage; too slow wastes time and generates excessive heat.
Sets the tool rotation speed in RPM. Calculated using the formula: S = (Vc x 1000) / (PI x D), where Vc is the cutting speed (m/min) and D is the tool diameter (mm).
Used with G2 and G3 commands to define the arc center relative to the current position. I is the X-axis offset, J is the Y-axis offset, and K is the Z-axis offset.
While G-Code is standardized (ISO 6983), each controller manufacturer has variations:
The most widely used CNC controller. Programs start with O-number (e.g., O0001). Comments use parentheses. Fanuc is the de facto standard that most CAM software targets first.
Uses semicolons for comments. Includes built-in CYCLE commands (CYCLE81 for drilling, POCKET3 for pocketing). Variables use R-parameters. Program structure differs slightly with DEF and MSG functions.
Uses a completely different conversational programming syntax. Linear moves use L commands, circles use C/CC commands, and the coordinate system references differ significantly from ISO G-Code.
Fanuc-compatible with additional proprietary features. Includes G28 safe return, setting-based customizations, and macro programming capabilities similar to Fanuc.
CAM232 creates G-Code for 13 different operations with support for 8 CNC controllers, 3D simulation, and free usage.
Try It FreeG-Code is the foundational language of CNC machining. Understanding G0-G3 motion commands, M codes, and feed/speed parameters gives you the knowledge to read, modify, and troubleshoot CNC programs. For generating complex G-Code automatically, CAM software like CAM232 can produce optimized toolpaths in seconds from your DXF designs.