G-Code & M-Code Reference: Complete CNC Programming Commands with Real Examples (2025)
Meta Description: Master CNC programming with this ultimate G-code and M-code reference guide. Includes command explanations, usage tips, real machining examples, and charts you can use directly in your shop.
🔍 What is G-Code?
G-code (Geometric Code) is the language that CNC machines use to control movements such as positioning, cutting, drilling, and more. Each command begins with a “G” followed by a number. Example: G01 = linear feed motion.
🧠 What is M-Code?
M-code (Machine Code) performs machine-level operations like turning the spindle on/off, changing tools, or coolant control. These are essential for managing the state of the machine. Example: M03 = spindle on clockwise.
📘 Essential G-Codes and Their Functions
| G-Code | Function | Example |
|---|---|---|
| G00 | Rapid Positioning | G00 X100 Y50 – moves tool quickly to position |
| G01 | Linear Interpolation | G01 X100 Y50 F200 – move at feedrate |
| G02 / G03 | Circular Interpolation (CW/CCW) | G02 X100 Y50 I20 J0 – clockwise arc |
| G17 / G18 / G19 | Plane Selection (XY/XZ/YZ) | G18 – switch to XZ plane |
| G20 / G21 | Unit Selection (inch/mm) | G21 – set units to millimeters |
| G28 | Return to Home | G28 U0 V0 |
| G40 | Cutter Radius Compensation Cancel | G40 |
| G43 | Tool Length Compensation + | G43 H01 |
| G90 / G91 | Absolute / Incremental Positioning | G90 = absolute, G91 = incremental |
| G94 | Feedrate per Minute | G94 F300 |
📘 Essential M-Codes and Their Functions
| M-Code | Function | Example |
|---|---|---|
| M00 | Program Stop | M00 – operator intervention required |
| M03 / M04 | Spindle On CW / CCW | M03 S1200 – start spindle at 1200 RPM CW |
| M05 | Spindle Stop | M05 |
| M06 | Tool Change | T01 M06 – change to Tool 1 |
| M08 / M09 | Coolant On / Off | M08 – coolant on, M09 – coolant off |
| M30 | End of Program + Reset | M30 |
🧪 Real G-Code Program Example
G21 G90 G94 ; mm units, absolute, feed per minute G17 G40 G49 ; XY plane, cancel radius/tool comp T01 M06 ; Tool 1 change G00 X0 Y0 Z5 ; Rapid move to start G43 H01 Z5 ; Apply tool length offset M03 S1500 ; Spindle on CW at 1500 RPM G01 Z-1 F200 ; Feed into material G01 X50 Y0 ; Linear cut G02 X50 Y50 I0 J25 ; CW arc G01 X0 Y50 ; Linear cut M09 ; Coolant off G00 Z5 ; Retract M05 M30 ; Spindle stop, program end
This sample produces a square with a rounded corner—ideal for testing arcs and feedrate control.
📊 G & M Code Cheat Sheet (Printable)
| Function | Code |
|---|---|
| Rapid Position | G00 |
| Feed Move | G01 |
| Arc CW/CCW | G02 / G03 |
| Spindle On CW | M03 |
| Tool Change | M06 |
| End Program | M30 |
🚀 Future of CNC Programming
- Support for 5-axis G-code commands (e.g.
G68,G43.4) in low-cost controllers - Integration with AI-generated toolpaths
- Automatic syntax correction in CAM post-processors
The trend in CNC programming is moving toward automation, AI-enhanced logic, and safer command verification, especially in high-precision and high-speed machining environments.
✅ Conclusion
Knowing your G-codes and M-codes is fundamental to CNC mastery. Whether you are debugging, programming from scratch, or verifying post-processor output, this guide gives you the tools you need to succeed.
Bookmark this page and explore more live code examples at cnccode.com.
Leave a comment