Explore the ultimate reference for G-code and M-code commands used in CNC machining. Includes real-world examples, command descriptions, modal group classifications, safety tips, and usage tables for Fanuc, Haas, and Siemens controllers.
Complete G-Code & M-Code Reference for CNC Machinists with Real Examples
CNC machines rely on two major code sets: G-codes (preparatory functions) and M-codes (miscellaneous functions). These codes form the language of the machine, allowing it to perform precise movements, tool changes, spindle control, coolant operations, and more.
This comprehensive reference covers:
- Full G-code and M-code list with descriptions
- Real machining examples in Fanuc-style syntax
- Tables for modal groups
- Controller-specific notes (Fanuc, Haas, Siemens)
- Safety and best practice tips
- G-code and M-code cheat sheet (printable)
🔧 What Are G-Codes?
G-codes control movements, coordinate systems, and modes like drilling cycles or arc movements. These are usually modal (stay active until changed).
| G-Code | Description | Example |
|---|---|---|
| G00 | Rapid Positioning | G00 X100 Y50 |
| G01 | Linear Interpolation (cut) | G01 X50 Y25 F200 |
| G02 | Clockwise Circular Interpolation | G02 X20 Y10 R5 |
| G03 | CCW Circular Interpolation | G03 X0 Y0 R5 |
| G17 | XY Plane Select | G17 |
| G21 | Metric Units (mm) | G21 |
| G20 | Imperial Units (inch) | G20 |
| G28 | Return to Home | G28 Z0 |
| G40 | Cancel Cutter Compensation | G40 |
| G41 | Cutter Comp Left | G41 D01 |
| G42 | Cutter Comp Right | G42 D02 |
🧰 G-Code Modal Group Table
Modal groups define how G-codes stay active. Only one G-code from each group can be active at once.
| Modal Group | Function | Common Codes |
|---|---|---|
| Group 1 | Motion | G00, G01, G02, G03 |
| Group 2 | Plane Selection | G17, G18, G19 |
| Group 3 | Distance Mode | G90, G91 |
| Group 5 | Units | G20, G21 |
| Group 6 | Tool Length Offset | G43, G49 |
| Group 7 | Cutter Radius Compensation | G40, G41, G42 |
🔄 G-Code Movement Example
G21 G90 G17 ; Metric, absolute, XY plane
G0 Z5 ; Retract tool
G0 X0 Y0 ; Rapid to origin
M3 S1000 ; Spindle on clockwise at 1000 RPM
G1 Z-2 F150 ; Feed down
G1 X50 Y0 ; Linear cut
G2 X50 Y50 R10 ; CW arc
G3 X0 Y50 R10 ; CCW arc
G1 X0 Y0 ; Close loop
G0 Z5 ; Retract
M5 ; Spindle stop
M30 ; End of program
⚙️ What Are M-Codes?
M-codes control machine-specific functions like spindle, coolant, and program flow. These vary more across controllers.
| M-Code | Function | Example |
|---|---|---|
| M00 | Program Stop | M00 |
| M01 | Optional Stop | M01 |
| M02 | End of Program | M02 |
| M03 | Spindle ON (CW) | M03 S1200 |
| M04 | Spindle ON (CCW) | M04 S1200 |
| M05 | Spindle OFF | M05 |
| M06 | Tool Change | T02 M06 |
| M08 | Coolant ON | M08 |
| M09 | Coolant OFF | M09 |
| M30 | End and Reset | M30 |
📐 Coordinate Systems – G54 to G59
You can set multiple work coordinate systems using G54–G59:
G54 ; Use WCS #1
G0 X0 Y0 Z0 ; Go to origin
G55 ; Switch to WCS #2
🛠️ Tool Length Offset Example
T1 M06 ; Tool 1 selection
G43 H01 Z50 ; Apply tool length offset H01
G1 Z-5 F200 ; Feed to depth
| Code | Meaning |
|---|---|
| G43 | Apply tool length offset |
| H01 | Uses offset stored in H01 |
🧮 Drilling Cycle Example (G81)
G21 G90 G17
G0 X10 Y10
M03 S800
G81 R2 Z-5 F100
X20 Y10
X30 Y10
G80
M05
M30
| G-Code | Description |
|---|---|
| G81 | Drilling Cycle |
| G82 | Dwell Cycle |
| G83 | Peck Drilling Cycle |
| G84 | Tapping Cycle |
🗃️ Common G/M-Codes Cheat Sheet
| Command | Description | Type |
|---|---|---|
| G00 | Rapid positioning | G |
| G01 | Linear feed move | G |
| G90 | Absolute mode | G |
| G91 | Incremental mode | G |
| M03 | Spindle ON (CW) | M |
| M05 | Spindle OFF | M |
| M08 | Coolant ON | M |
| M30 | Program end + reset | M |
🛡️ Safety Best Practices
- Always add
G0 Z5before XY movement to avoid crashes - Use
M05andG28at program end for safe tool return - Simulate the code in a CNC simulator before real run
- Avoid using G90 and G91 mixed in the same block
📉 Troubleshooting Tips
| Symptom | Cause | Fix |
|---|---|---|
| Machine does nothing | Missing feedrate or M-code | Check G1 Fxxx and spindle |
| Arc not cutting | Missing I/J or R parameters | Recalculate center coords |
| Tool crashes | G00 used with incorrect Z height | Add retract before rapid |
📊 Printable G-Code/M-Code Table
You can convert the above tables to a PDF and place them near your CNC station for quick reference. If needed, we can also provide downloadable formats in your future content.
📌 Conclusion
Having a solid understanding of G-code and M-code is essential for safe, efficient, and precise CNC machining. This guide provides machinists and programmers with a reliable reference they can apply immediately on any CNC controller—Fanuc, Haas, or Siemens. For deeper topics like macro programming, probing, and advanced canned cycles, stay tuned to cnccode.com.
Leave a comment