The Complete G-Code and M-Code Reference Guide for CNC Programming (2025 Edition)
CNC programming begins with two vital command systems: G-codes and M-codes. G-codes control motion and positioning, while M-codes handle machine functions like spindle or coolant. This guide provides the most comprehensive, up-to-date, and real-world applicable reference to all major G and M codes in CNC.
🧠 What Are G-Codes and M-Codes?
- G-Codes (Preparatory Codes): Control positioning, motion, units, coordinate systems.
- M-Codes (Miscellaneous Codes): Control machine functions like spindle, coolant, and program flow.
🧾 G-Code List: Motion & Positioning Commands
| G-Code | Description | Modal | Example Use |
|---|---|---|---|
| G00 | Rapid positioning | Yes | G00 X0 Y0 Z100 |
| G01 | Linear interpolation (cutting move) | Yes | G01 X100 F150 |
| G02 | Circular interpolation (CW) | Yes | G02 X50 Y50 R25 |
| G03 | Circular interpolation (CCW) | Yes | G03 X0 Y0 R25 |
| G04 | Dwell (pause in seconds) | No | G04 P1.0 |
| G10 | Set offsets or parameters | No | G10 L2 P1 X0 Y0 Z0 |
| G17 | Select XY plane | Yes | G17 |
| G18 | Select ZX plane | Yes | G18 |
| G19 | Select YZ plane | Yes | G19 |
| G20 | Set units to inches | Yes | G20 |
| G21 | Set units to millimeters | Yes | G21 |
| G28 | Return to home position | No | G28 U0 W0 |
| G40 | Cancel cutter compensation | Yes | G40 |
| G41 | Cutter comp left | Yes | G41 D1 |
| G42 | Cutter comp right | Yes | G42 D1 |
| G43 | Tool length compensation + | Yes | G43 H1 |
| G49 | Cancel tool length compensation | Yes | G49 |
| G54 | Work offset coordinate 1 | Yes | G54 |
| G55–G59 | Work offset coordinate 2–6 | Yes | G55, G56… |
| G70 | Finishing cycle (turning) | Yes | G70 P1 Q2 |
| G71 | Roughing cycle (turning) | Yes | G71 U0.5 R1 |
| G80 | Cancel canned cycle | Yes | G80 |
| G81 | Drilling cycle | Yes | G81 R2 Z-10 F100 |
| G82 | Drilling with dwell | Yes | G82 R2 Z-10 P500 |
| G83 | Peck drilling cycle | Yes | G83 R2 Z-20 Q2 F75 |
| G84 | Tapping cycle | Yes | G84 R2 Z-15 F75 |
| G90 | Absolute programming | Yes | G90 |
| G91 | Incremental programming | Yes | G91 |
| G92 | Set current position | No | G92 X0 Y0 Z0 |
| G94 | Feedrate per minute | Yes | G94 |
| G95 | Feedrate per revolution | Yes | G95 |
| G96 | Constant surface speed (CSS) | Yes | G96 S200 |
| G97 | Cancel CSS | Yes | G97 S1000 |
| G98 | Return to initial point (drilling) | Yes | G98 |
| G99 | Return to R point (drilling) | Yes | G99 |
✅ Modal codes stay active until another code in the same group is called.
🔧 M-Code List: Machine Function Commands
| M-Code | Description | Example Use |
|---|---|---|
| M00 | Program stop | M00 |
| M01 | Optional stop | M01 |
| M02 | End of program | M02 |
| M03 | Spindle on (clockwise) | M03 S1500 |
| M04 | Spindle on (counter-clockwise) | M04 S500 |
| M05 | Spindle stop | M05 |
| M06 | Tool change | M06 T2 |
| M07 | Mist coolant on | M07 |
| M08 | Flood coolant on | M08 |
| M09 | All coolant off | M09 |
| M30 | Program end and rewind | M30 |
| M98 | Call subprogram | M98 P1234 L2 |
| M99 | Return from subprogram | M99 |
| M48 | Enable feedrate override | M48 |
| M49 | Disable feedrate override | M49 |
🛠️ Note: M-codes vary slightly by manufacturer — always check your machine’s manual.
🌀 Modal Groups: Code Conflicts to Avoid
| Group | Function Area | Codes Included |
|---|---|---|
| 1 | Motion | G00, G01, G02, G03 |
| 2 | Plane selection | G17, G18, G19 |
| 3 | Units | G20, G21 |
| 6 | Cutter compensation | G40, G41, G42 |
| 7 | Tool length compensation | G43, G49 |
| 8 | Canned cycles | G81–G89 |
| 10 | Feed modes | G94, G95 |
| 12 | Spindle control | M03, M04, M05 |
⚠️ Activating two codes from the same group in one block may result in errors.
🔄 Example: Real CNC Milling Program
%
O1000 (Face Milling)
G21 G17 G90 G40 G49 G80
G54
M06 T1
M03 S1500
G00 X0 Y0 Z5
G43 H01 Z2
G01 Z-2 F100
G01 X50 Y0
G01 Y50
G01 X0
G01 Y0
G00 Z5
M09
M05
M30
%
📚 FAQs
❓ What’s the difference between G43 and G44?
- G43 applies positive tool length compensation, most commonly used.
- G44 is negative, rarely used.
❓ Can G-codes be customized?
Yes, advanced controllers allow macro programming and custom cycles (e.g., G65, G66 with parameters).
❓ What if my machine ignores certain codes?
Some codes are machine-specific or require enabling via parameters. Always refer to your machine’s manual or parameter list.
🔮 G & M Code Evolution (2025+)
- AI-driven CAM tools generating optimized G-code
- Adaptive G-code structures for smart materials
- Cloud-based G-code validation and simulation
- M-codes extended for robotic arm, probe, and vision integration
✅ Final Notes
- Mastering G and M codes is essential for any CNC programmer.
- These codes act as the language of the machine, allowing full control over movement, safety, and efficiency.
- Combine this reference with practice, simulation, and manufacturer manuals for best results.
💡 Keep this guide bookmarked — it’s your CNC cheat sheet for 2025 and beyond.
▶️ Next Suggested Topic:
“Advanced G-Code Programming with Macros, Subroutines & Conditional Logic”
Should we continue with that?
Leave a comment