Ultimate G-Code & M-Code Reference: CNC Programming Explained with Examples
Meta Description: The most complete guide to G-codes and M-codes in CNC programming. Includes definitions, examples, compatibility charts, and advanced usage tips for both milling and turning machines.
G-code and M-code are the core languages of CNC programming. Mastering them unlocks the full potential of CNC machines—whether you’re running a 3-axis mill, a 2-axis lathe, or a 5-axis machining center.
📘 G-Code Basics: Motion & Positioning
| G-Code | Description | Example |
|---|---|---|
| G00 | Rapid Positioning | G00 X100 Y50 |
| G01 | Linear Interpolation | G01 X50 Y20 F100 |
| G02 | Circular Interpolation Clockwise | G02 X30 Y30 I10 J0 |
| G03 | Circular Interpolation Counter-Clockwise | G03 X30 Y30 I-10 J0 |
| G17 / G18 / G19 | Plane Selection (XY / ZX / YZ) | G18 for lathe (ZX plane) |
🔧 G-Codes for Tool Handling & Offsets
| G-Code | Description | Notes |
|---|---|---|
| G43 | Tool Length Compensation + | Always used with H-word (e.g. H01) |
| G49 | Cancel Tool Length Offset | Used before tool change |
| G54–G59 | Work Coordinate Systems | Useful for multi-part fixtures |
| G28 | Return to Machine Zero | Use with caution; can cause crashes if unplanned |
⚙️ M-Code Reference: Machine Commands
| M-Code | Description | Example |
|---|---|---|
| M03 | Spindle ON Clockwise | M03 S1200 |
| M04 | Spindle ON Counter-Clockwise | M04 S800 |
| M05 | Spindle OFF | Used before tool change |
| M06 | Tool Change | T02 M06 |
| M30 | End of Program + Reset | Standard program terminator |
| M08 / M09 | Coolant ON / OFF | M08 = Flood ON |
🎯 Real-World Example: Tool Change with Safe Z
T01 M06 ; Select tool 1
G00 G43 Z100 H01 ; Move up with tool offset
G54 ; Use work coordinate 1
G00 X0 Y0 ; Rapid to start
G01 Z-5 F150 ; Feed down into material
Explanation: Tool 1 is activated, moved up to safe Z=100 mm, then offset H01 is applied. G54 work origin is used and tool cuts to Z-5 mm.
📊 Compatibility Chart (Fanuc vs Haas vs Siemens)
| Code | Fanuc | Haas | Siemens |
|---|---|---|---|
| G54 | ✅ | ✅ | ✅ |
| M06 | ✅ | ✅ | ⚠️ (Often M06 + TOOL CALL) |
| G43 | ✅ | ✅ | ⚠️ Tool Lengths may be handled via Cycle800 |
| M30 | ✅ | ✅ | ✅ |
📈 Future Trends in G-code Usage
- CAM-Generated Codes: Post-processors now optimize G-code for better tool life and shorter cycle time
- High-speed G-code: HSM-specific codes (like G05.1 for Fanuc) are becoming more common
- Parametric Programming: G65, G66 and custom macros are replacing hard-coded toolpaths
- Probing Cycles: Codes like G31, G1000+ used with Renishaw probes for automatic setup
📚 Downloadable PDF G-code Cheat Sheets
cnccode.com provides downloadable PDF guides for:
- Fanuc 3-axis G-code reference
- Haas-compatible canned cycles
- Lathe-specific M-code charts
- Parametric programming examples (G65)
These are ideal to print and keep near CNC panels or to use in training environments.
Leave a comment