Complete G-Code & M-Code Reference for CNC Programming: With Real Examples and Controller Compatibility
Meta Description: Learn the full list of G-codes and M-codes used in CNC machining. Includes practical examples, controller-specific notes (FANUC, Siemens, Haas), and cycle breakdowns for real-world applications.
G-code and M-code are the backbone of CNC machine control. Understanding their structure, application, and variations across controller types is essential for both beginner and professional CNC programmers. This guide provides a complete reference with real-world examples, common pitfalls, and best practices.
🧾 What Are G-Codes and M-Codes?
- G-Codes (Preparatory Functions): Control the motion and positioning (linear moves, arcs, cycles).
- M-Codes (Miscellaneous Functions): Control machine-level operations (spindle, coolant, tool changes).
📘 Commonly Used G-Codes (ISO 6983 Format)
| G-Code | Function | Example |
|---|---|---|
| G00 | Rapid positioning | G00 X0 Y0 Z0 |
| G01 | Linear interpolation (feed move) | G01 X50 F100 |
| G02 / G03 | Clockwise / Counter-clockwise arc | G02 X20 Y10 I5 J0 |
| G17/G18/G19 | Plane selection (XY/XZ/YZ) | G18 |
| G90 / G91 | Absolute / Incremental positioning | G90 |
| G28 | Return to machine home | G28 U0 W0 |
🛠️ Common M-Codes
| M-Code | Function | Example |
|---|---|---|
| M03 | Spindle on clockwise | M03 S1000 |
| M04 | Spindle on counter-clockwise | M04 S500 |
| M05 | Spindle stop | M05 |
| M06 | Tool change | M06 T2 |
| M08 / M09 | Coolant on/off | M08 / M09 |
| M30 | End of program | M30 |
📋 Sample CNC Milling Program (Fanuc-style)
%
O1000
G21 G90 G17 G40 G80 G94
T1 M06
G00 X0 Y0 Z5
M03 S1200
G43 H01 Z2
G01 Z-5 F100
G01 X50 Y0
G02 X60 Y10 R10
M09
G00 Z100
M30
%
This sample includes tool change (M06), spindle activation (M03), and circular interpolation (G02) on the XY plane.
📊 G-Code Variations Across CNC Controllers
| Function | FANUC | Siemens | Mach3 / GRBL |
|---|---|---|---|
| Arc CW | G02 | G02 | G2 |
| Return to Home | G28 | G28 / G74 | G28 |
| Tool Change | M06 | TOOL CALL | M6 |
| Program End | M30 | M30 / END | M30 |
💡 Best Practices
- Use absolute (G90) mode unless incremental (G91) is required.
- Always cancel modal codes with G40, G49, G80 at program end.
- Use G43 with H offsets for safe Z-axis motion with tool length compensation.
- Comment lines with
(description)for readability.
📈 Future of G-code: Smart Controllers and Post-Processors
With the advent of AI-assisted CAM systems and Industry 4.0 connectivity, G-code is being augmented with meta-data layers, digital twins, and real-time feedback systems. Post-processors are becoming smarter, producing optimized code for hybrid manufacturing (additive + subtractive), robotic arms, and 5-axis machining systems.
While G-code may eventually be abstracted by higher-level interfaces, its foundational role in motion control, toolpath definition, and machine interfacing remains irreplaceable.
📘 Conclusion
This reference guide is designed to be your go-to source for G-code and M-code understanding. Whether you’re programming a FANUC-based 5-axis mill or a GRBL-driven desktop CNC, the principles and commands covered here apply universally.
Download G-code cheat sheets, test files, and controller-specific syntax breakdowns in the G-Code Tools section of cnccode.com.
Leave a comment