G-Code & M-Code Cheat Sheet (With Real Examples for Fanuc, Haas, Siemens)
G-code and M-code are the core programming languages for CNC machines. Understanding these codes is essential for precision machining, automation, and CNC troubleshooting. This cheat sheet provides a detailed, professional reference for commonly used G and M codes — with real-world examples for Fanuc, Haas, and Siemens controllers.
📌 What Are G-Code and M-Code?
- G-code (preparatory functions) control motion, such as positioning, feed rate, cycles, and planes.
- M-code (miscellaneous functions) control machine operations like spindle, coolant, tool changes, etc.
🧾 G-Code Quick Reference Table
| G-Code | Description | Example | Notes |
|---|---|---|---|
| G00 | Rapid positioning | G00 X50 Y25 | Non-cutting fast move |
| G01 | Linear interpolation | G01 X100 Y50 F200 | Cutting with feed rate |
| G02 | Clockwise arc | G02 X50 Y50 I20 J10 | Requires I and J |
| G03 | Counter-clockwise arc | G03 X20 Y60 R25 | R or I/J |
| G04 | Dwell | G04 P500 | P in milliseconds |
| G17 | XY Plane selection | G17 | Default for mills |
| G18 | ZX Plane selection | G18 | For lathe or advanced ops |
| G19 | YZ Plane selection | G19 | Rare, for 5-axis jobs |
| G20 | Inch programming | G20 | Imperial units |
| G21 | Metric programming | G21 | Millimeters |
| G28 | Return to home | G28 U0 W0 | Often used with lathe |
| G40 | Cancel cutter compensation | G40 | End of contour |
| G41 | Cutter compensation left | G41 D01 | Used with tool radius |
| G42 | Cutter compensation right | G42 D01 | Mirror of G41 |
| G43 | Tool length compensation + | G43 H01 Z100 | Needs H value |
| G49 | Cancel tool length comp. | G49 | After tool finish |
| G80 | Cancel canned cycle | G80 | Always before new cycle |
| G81 | Drilling cycle | G81 R1 Z-10 F100 | Basic hole drilling |
| G83 | Peck drilling cycle | G83 R2 Z-20 Q5 F150 | Deep holes |
| G90 | Absolute positioning | G90 | Default mode |
| G91 | Incremental positioning | G91 | Moves relative to current |
| G92 | Set position | G92 X0 Y0 Z0 | Useful for fixture offsets |
🧾 M-Code Quick Reference Table
| M-Code | Description | Example | Notes |
|---|---|---|---|
| M00 | Program stop | M00 | Manual resume |
| M01 | Optional stop | M01 | With Optional Stop ON |
| M02 | End of program | M02 | Older standard |
| M30 | End of program & rewind | M30 | Common in modern use |
| M03 | Spindle ON clockwise | M03 S1200 | Requires speed (S) |
| M04 | Spindle ON counter-clockwise | M04 S800 | Reversal for left-hand tools |
| M05 | Spindle stop | M05 | Stop rotation |
| M06 | Tool change | M06 T03 | Automatic changers |
| M07 | Mist coolant ON | M07 | Not always available |
| M08 | Flood coolant ON | M08 | Standard coolant |
| M09 | Coolant OFF | M09 | After M08 |
| M17 | End of subprogram | M17 | Ends subroutine |
| M30 | End & rewind | M30 | Repeats program on cycle start |
⚙️ Real CNC Controller Examples
✅ Fanuc Example:
“`gcode
G21 G90 G17
T1 M06
G00 X0 Y0
G43 H01 Z100
G01 Z-5 F100
G01 X50 Y50 F250
G00 Z100
M05
M30
✅ Haas Example:
G20 G90
T2 M06
G00 G54 X0 Y0
G43 H02 Z50
G81 R2 Z-10 F75
X10 Y20
X30 Y60
G80
M09
M30
✅ Siemens Example:
G71
G90
T=”TOOL_1″ M6
M3 S1200
G0 X0 Y0
G1 Z-5 F150
G1 X100 Y50
G0 Z100
M5
M30
📂 Canned Cycles Explained (G81 to G89)
| Code | Cycle | Use |
|---|---|---|
| G81 | Simple Drilling | Hole making |
| G82 | Drilling with dwell | Controlled depth |
| G83 | Peck Drilling | Deep holes |
| G84 | Tapping | Thread cutting |
| G85 | Boring | In & out, no dwell |
| G86 | Boring with spindle stop | Stops rotation |
| G87 | Back boring | Reverse tools |
| G88 | Boring with manual return | Manual |
| G89 | Boring with dwell | Similar to G82 |
📘 G/M Code Compatibility (Fanuc vs Haas vs Siemens)
| Code | Fanuc | Haas | Siemens |
|---|---|---|---|
| G00 | ✅ | ✅ | ✅ |
| G81 | ✅ | ✅ | ✅ |
| M06 | ✅ | ✅ | ✅ |
| M08 | ✅ | ✅ | ✅ |
| G83 | ✅ | ✅ | ⚠️ (varies) |
| G95 | ✅ | ✅ | ❌ (uses feed per rev differently) |
| G40–G42 | ✅ | ✅ | ⚠️ (depends on cycle) |
| M30 | ✅ | ✅ | ✅ |
💡 Expert Tips
- Always cancel previous cycles or tool compensation (use G80, G40, G49).
- G90 and G91 switching can cause crashes if used improperly.
- Verify arc directions (G02/G03) with preview or simulator.
- Siemens syntax differs slightly; it supports structured programming (e.g., SINUMERIK cycles).
- Always simulate first with a G-code simulator before real machining.
🧰 Recommended Tools & Simulators
- NC Viewer – Free online G-code visualizer.
- CAMotics – 3D CNC simulator.
- Haas NextGen Simulator – Real controller emulator.
- Fanuc FAPT Ladder – Professional simulator.
- Siemens SinuTrain – Official Siemens CNC simulator.
❓ FAQ – Frequently Asked Questions
Q1: Can I use the same G-code on Fanuc and Haas?
A: Mostly yes, but always double-check machine-specific M-codes and offsets.
Q2: What’s the difference between G02 and G03?
A: G02 is clockwise arc; G03 is counter-clockwise. Both need arc center defined.
Q3: Is M30 the same as M02?
A: No. M02 ends the program, M30 ends and rewinds for a new cycle start.
Q4: How do I simulate G-code safely?
A: Use simulators like NC Viewer, CAMotics, or OEM-specific simulators before live run.
✅ Conclusion
Mastering G-code and M-code is essential for any CNC machinist, programmer, or operator. Whether you’re working on a Fanuc-controlled mill, a Haas lathe, or a Siemens 5-axis machine, knowing the right codes — and using them properly — will improve machining precision, speed, and safety.
Bookmark this cheat sheet, or download it as a reference PDF for shop-floor use.
Leave a comment