Master every G-code and M-code command with real-world examples, compatibility charts, and updated explanations. Boost CNC programming skills and machine efficiency in 2025.
Complete G-Code & M-Code Reference Guide (2025 Edition) — CNC Programming Made Simple
G-code and M-code form the fundamental language used to control CNC machines. Whether you’re a beginner or an advanced machinist, this 2025 reference guide will walk you through the most important codes, their meanings, and real-world usage — including compatibility with popular CNC systems like Fanuc, Haas, LinuxCNC, and GRBL.
1. What Is G-Code?
G-code (Geometric Code) controls the movement and function of CNC machines. It tells the machine where to move, how fast, and in what way — such as linear or arc movements, coolant control, tool changes, and more.
2. What Is M-Code?
M-codes (Miscellaneous Codes) are used for non-geometric machine functions — like turning the spindle on/off, changing tools, and controlling coolant systems.
3. Core G-Code Commands (With Descriptions)
G-Code | Function | Example |
---|---|---|
G00 | Rapid positioning | G00 X0 Y0 Z0 |
G01 | Linear interpolation (cutting move) | G01 X100 Y100 F300 |
G02 | Clockwise circular interpolation | G02 X50 Y50 R20 |
G03 | Counterclockwise circular interpolation | G03 X50 Y50 R20 |
G04 | Dwell (pause) | G04 P2000 (2 sec) |
G20 | Inches as units | G20 |
G21 | Millimeters as units | G21 |
G28 | Return to machine zero | G28 X0 Y0 |
G90 | Absolute positioning | G90 |
G91 | Incremental positioning | G91 |
G92 | Set current position | G92 X0 Y0 Z0 |
4. Common M-Code Commands (With Use Cases)
M-Code | Function | Example |
---|---|---|
M00 | Program stop | M00 |
M01 | Optional stop | M01 |
M02 | End of program | M02 |
M03 | Spindle ON clockwise | M03 S1200 |
M04 | Spindle ON counterclockwise | M04 S1200 |
M05 | Spindle stop | M05 |
M06 | Tool change | M06 T02 |
M08 | Coolant ON | M08 |
M09 | Coolant OFF | M09 |
M30 | Program end & reset | M30 |
5. G-Code Modal Groups (How Codes Behave)
Modal commands stay active until replaced. Non-modal commands are executed only once. Understanding modal groups helps prevent code conflicts.
- Motion: G00, G01, G02, G03
- Distance Mode: G90 (absolute), G91 (incremental)
- Unit Selection: G20 (inches), G21 (mm)
- Plane Selection: G17 (XY), G18 (ZX), G19 (YZ)
6. CNC Compatibility Chart (2025)
Code | Fanuc | Haas | LinuxCNC | GRBL |
---|---|---|---|---|
G00/G01 | ✔️ | ✔️ | ✔️ | ✔️ |
G02/G03 | ✔️ | ✔️ | ✔️ | ✔️ |
G28 | ✔️ | ✔️ | ✔️ | Limited |
M06 | ✔️ | ✔️ | ✔️ | ❌ |
M08/M09 | ✔️ | ✔️ | ✔️ | ✔️ |
7. Best Practices for Writing G-Code in 2025
- Start each program with safety lines (G21 G90 G17 G94)
- Use comments generously for documentation (e.g., (Start pocket cut))
- Always test new code in simulation
- Avoid modal confusion by resetting modes at the end of operations
- Use subprograms to optimize repetitive tasks
8. Advanced Features in G-Code (For Experts)
- Parametric Variables: e.g., #100 = 5, then use X[#100]
- Loops & Conditionals: IF statements, WHILE loops (mainly in Fanuc)
- Macros: M98/M99 for subprograms, G65 for macro calls
Conclusion: Learn Once, Program Forever
Mastering G-code and M-code gives you total control over your CNC machine. Whether you’re building parts, automating workflows, or just getting started, this guide equips you with the knowledge to move from beginner to pro — all in one reference.
Bookmark this guide. Return to it often. And let your CNC creativity shape the future.
Leave a comment