Master the most essential G-code and M-code commands for CNC programming. This complete reference explains the function, syntax, and application of each command, ideal for machinists, programmers, and hobbyists.
G-Code & M-Code Reference: The Complete Guide to CNC Programming Commands
G-code and M-code are the backbone of CNC machine operation. These machine-readable instructions allow you to precisely control motion, tools, spindles, coolant, and more. Whether you’re a beginner just starting out or an experienced machinist brushing up, this in-depth reference explains every major command you’ll need to know.
What is G-Code?
G-code, or “Geometric Code”, directs the movement and operation of CNC machine tools. It tells the machine where to move, how fast, and along what path. Each G-code command begins with a “G” followed by a number and optional parameters.
What is M-Code?
M-code, or “Miscellaneous Code”, controls machine operations like starting the spindle, turning on the coolant, or pausing a program. These commands begin with “M” and are just as critical as G-codes in automated machining.
Top 20 Essential G-Codes Explained
G-Code | Function | Example |
---|---|---|
G00 | Rapid positioning | G00 X50 Y25 |
G01 | Linear interpolation (cut) | G01 X100 Y50 F150 |
G02 | Clockwise arc | G02 X20 Y10 I5 J0 |
G03 | Counter-clockwise arc | G03 X0 Y0 I-5 J0 |
G04 | Dwell (pause) | G04 P200 |
G17 | Select XY plane | G17 |
G18 | Select ZX plane | G18 |
G19 | Select YZ plane | G19 |
G20 | Use inch units | G20 |
G21 | Use millimeter units | G21 |
G28 | Return to machine home | G28 |
G40 | Cancel cutter radius compensation | G40 |
G41 | Tool compensation left | G41 D1 |
G42 | Tool compensation right | G42 D2 |
G90 | Absolute positioning | G90 |
G91 | Incremental positioning | G91 |
G92 | Set current position | G92 X0 Y0 Z0 |
G94 | Feedrate per minute | G94 |
G95 | Feedrate per revolution | G95 |
G96 | Constant surface speed | G96 S300 |
Top 15 M-Codes Every CNC Operator Should Know
M-Code | Function | Example |
---|---|---|
M00 | Program stop | M00 |
M01 | Optional stop | M01 |
M02 | End of program | M02 |
M03 | Spindle on (clockwise) | M03 S1000 |
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 | Coolant off | M09 |
M30 | Program end and reset | M30 |
M98 | Call subprogram | M98 P1234 |
M99 | Return from subprogram | M99 |
M47 | Repeat program | M47 |
M82 | Absolute extrusion (3D printing) | M82 |
How to Read a G-code File
Here’s a sample G-code snippet and explanation:
“`gcode
G21 ; Set units to millimeters
G90 ; Absolute positioning
G00 Z5 ; Move Z axis up
G00 X0 Y0 ; Rapid move to X0 Y0
G01 Z-1 F100; Lower tool into material
G01 X50 Y50 ; Cut to point
M05 ; Stop spindle
M30 ; End program
This code sets units, positions the tool, performs a cut, and ends the program cleanly.
Best Practices in G/M Code Usage
- Always begin with unit and positioning setup (G20/G21, G90/G91)
- Use comments (;) for better readability
- Break long moves into smaller sections for safety
- Test new code in a simulator before running on a live machine
Tools to Simulate and Test G-Code
- NCViewer (Web-based)
- CAMotics
- OpenBuilds G-code Viewer
- Universal Gcode Sender (UGS)
Conclusion
Mastering G-code and M-code is crucial for CNC machine operation, programming, and customization. With this reference guide, you’re equipped with the knowledge to understand, write, and debug programs for any CNC setup. Save this post as a go-to resource and keep building smarter! “`
Leave a comment