Master CNC programming with this complete G-code and M-code reference guide. Includes command syntax, real-world examples, usage tables, and controller compatibility (GRBL, Fanuc, LinuxCNC). Essential for CNC programmers in 2025.
Ultimate G-Code & M-Code Reference Guide (2025 Edition)
G-code is the fundamental language that drives CNC machines. Whether you’re using a hobbyist controller like GRBL or industrial platforms like Fanuc or LinuxCNC, knowing your G- and M-codes is essential for writing, editing, and understanding CNC programs. This guide includes comprehensive syntax tables, real-world code examples, and controller compatibility tips.
What is G-Code?
G-code (Geometric Code) controls motion and positioning. Each line of G-code is called a “block” and usually controls one or more machine actions (e.g., move, cut, dwell).
What is M-Code?
M-codes (Miscellaneous Codes) handle machine-level operations like spindle control, coolant, tool changes, or program flow.
Common G-Code Commands with Descriptions
G-Code | Description | Example | Compatible With |
---|---|---|---|
G00 | Rapid positioning | G00 X0 Y0 Z5 | GRBL, Fanuc, LinuxCNC |
G01 | Linear interpolation (cutting) | G01 X50 Y25 F1000 | All |
G02 | Clockwise arc | G02 X10 Y10 I5 J0 | All |
G03 | Counter-clockwise arc | G03 X0 Y0 I-5 J0 | All |
G17 | Select XY plane | G17 | Fanuc, LinuxCNC |
G20 | Inch units | G20 | All |
G21 | Millimeter units | G21 | All |
G28 | Go to home position | G28 X0 Y0 Z0 | GRBL, Fanuc |
G90 | Absolute positioning | G90 | All |
G91 | Incremental positioning | G91 | All |
G92 | Set position | G92 X0 Y0 Z0 | All |
Common M-Code Commands
M-Code | Description | Example | Usage |
---|---|---|---|
M00 | Program stop | M00 | Pause for user intervention |
M03 | Spindle on (clockwise) | M03 S1000 | Start spindle at 1000 RPM |
M04 | Spindle on (CCW) | M04 S500 | Reverse spindle direction |
M05 | Spindle stop | M05 | Turns off spindle |
M06 | Tool change | M06 T1 | Change to Tool 1 |
M08 | Coolant on | M08 | Start flood coolant |
M09 | Coolant off | M09 | Stop coolant flow |
M30 | Program end and reset | M30 | Return to start |
G-Code Modal Groups (Cheat Sheet)
Modal groups allow certain commands to stay active until changed. This table summarizes typical modal groups:
Group | Function | Example G-Codes |
---|---|---|
Motion | Move types | G00, G01, G02, G03 |
Distance | Absolute vs. incremental | G90, G91 |
Units | Inch/MM | G20, G21 |
Plane | Plane selection | G17, G18, G19 |
Tool length | Tool height offset | G43, G49 |
Real-World G-Code Program Example
G21 ; Set units to mm
G90 ; Absolute positioning
G0 Z5 ; Raise to safe height
G0 X0 Y0 ; Move to origin
M03 S1000 ; Start spindle at 1000 RPM
G1 Z-1 F100; Plunge into material
G1 X50 Y0 ; Cut straight line
G1 X50 Y50 ; Cut up
G1 X0 Y50 ; Cut left
G1 X0 Y0 ; Cut down
G0 Z5 ; Retract
M05 ; Spindle off
M30 ; End program
Useful G-Code Resources
Tips for Writing and Editing G-Code
- Use comments (e.g.,
(This is a comment)
) for clarity - Simulate your code before running on a real machine
- Keep backups of all versions
- Indent blocks logically for readability
Conclusion
Mastering G-code and M-code is essential for anyone serious about CNC programming. With this reference at your fingertips, you can create optimized toolpaths, debug problems faster, and gain control over any CNC machine—from GRBL to Fanuc. Bookmark this resource and explore more CNC programming tutorials at cnccode.com.
Leave a comment