Whether you’re a CNC machinist, programmer, or manufacturer looking to optimize production, understanding M codes and G codes is non-negotiable. These two types of commands form the foundation of CNC programming, enabling precise control of machine operations like cutting, tool changes, coolant flow, and motion paths.
This article provides a comprehensive guide to common G and M codes, how they function, and where they're used—especially within FANUC-controlled CNC systems.
G Codes (Geometric Codes) define machine movement and tool paths.
M Codes (Miscellaneous Codes) handle machine functions such as turning on coolant, stopping the spindle, or tool changing.
Think of it this way:
G codes = How the machine moves.
M codes = What the machine does while moving (or before/after).
Both are essential for a successful CNC program.
G Code | Function | Description |
---|---|---|
G00 | Rapid positioning | Moves tool quickly from point to point without cutting |
G01 | Linear interpolation | Moves tool in a straight line at a set feed rate |
G02 | Circular interpolation (clockwise) | Cuts arcs or circles in a clockwise direction |
G03 | Circular interpolation (counter-clockwise) | Cuts arcs or circles counter-clockwise |
G04 | Dwell | Pauses the machine for a set time |
G17 | Select XY plane | Default plane for 2D machining |
G18 | Select ZX plane | Useful in turning centers |
G19 | Select YZ plane | For vertical or sidewall machining |
G20 | Inch units | Sets machine units to inches |
G21 | Metric units | Sets machine units to millimeters |
G28 | Return to machine zero | Sends tool to home position |
G40 | Cancel cutter compensation | Turns off tool radius offset |
G41 | Cutter compensation left | Offsets tool to the left of path |
G42 | Cutter compensation right | Offsets tool to the right of path |
G43 | Tool length compensation + | Compensates for tool height |
G49 | Cancel tool length compensation | Resets tool length offset |
G54 – G59 | Work coordinate systems | Allows multiple part setups |
G80 | Cancel canned cycle | Stops any active drilling cycle |
G81 | Drilling cycle | Basic vertical drilling operation |
G82 | Drilling with dwell | Pauses at bottom of drill before retract |
G83 | Peck drilling cycle | Breaks chips while drilling deep holes |
G90 | Absolute programming | Coordinates based on part origin |
G91 | Incremental programming | Coordinates based on last position |
G94 | Feed per minute | Feed rate in mm/min or in/min |
G95 | Feed per revolution | Feed rate based on spindle RPM |
Note: G codes can vary slightly depending on the machine type (mill vs. lathe) and the controller (FANUC, Siemens, Haas, etc.)
M Code | Function | Description |
---|---|---|
M00 | Program stop | Stops machine until manually restarted |
M01 | Optional stop | Stops only if optional stop is enabled |
M02 | Program end | Ends the program |
M03 | Spindle on (CW) | Starts spindle clockwise |
M04 | Spindle on (CCW) | Starts spindle counter-clockwise |
M05 | Spindle stop | Stops the spindle rotation |
M06 | Tool change | Automatically changes to next tool |
M08 | Coolant on | Activates coolant flow |
M09 | Coolant off | Stops coolant flow |
M30 | Program end and rewind | Ends program and resets to the top |
M98 | Call subprogram | Runs a separate subroutine |
M99 | End of subprogram/loop | Returns from subprogram or loops program |
Pro Tip: Always check your machine's specific M-code list in the operator’s manual. Not all machines treat M-codes the same way
.
Feature | G Code | M Code |
---|---|---|
Purpose | Controls movement | Controls machine functions |
Examples | G01, G02, G90 | M03, M06, M30 |
Affects | Motion/geometry | Auxiliary machine tasks |
Editable by User? | Yes | Yes |
Sequence | Must be precise | Typically follows G code actions |
Milling machines
Turning centers (lathes)
Multi-axis machining centers
Drilling machines
Robotic systems with CNC integration
They are especially prevalent in FANUC-based systems, where standardization and efficiency are critical for large-scale automation.
Start with simulation software like NC Viewer or FANUC’s CNC Guide.
Practice writing simple programs on a test machine or emulator.
Use cheat sheets like this article as a quick reference.
Understand the logic, not just the syntax—know why each code is used.
Read real CNC programs from your shop or online libraries.
gcodeCopyEditG21 ; Set to metric G90 ; Absolute positioning G00 X0 Y0 ; Rapid to start point M03 S1200 ; Spindle on clockwise at 1200 RPM G01 Z-5 F100 ; Feed to depth G02 X50 Y50 R25 ; Cut clockwise arc M09 ; Coolant off G00 Z100 ; Retract tool M30 ; End program
Understanding G codes and M codes is like learning the alphabet of CNC programming. Without these foundational instructions, machine tools wouldn't know where to move, when to start, or how to perform key functions like tool changes or coolant control.
Whether you're maintaining a FANUC-controlled line or writing programs from scratch, mastering these codes improves both productivity and precision.
Q1: Are G codes and M codes the same across all CNC machines?
Not always. Many are standardized, but some codes vary by controller brand (FANUC, Haas, Siemens) and machine type.
Q2: Can I create custom M codes?
Yes, some controllers allow custom M-code programming, especially for macro-based automation or user-defined tasks.
Q3: What happens if I forget to include an M30 at the end of a program?
The machine may not rewind to the top of the program or may not stop correctly. Always include a proper program end code.
Q4: Is it possible to run a CNC program with only G codes?
Technically yes, but you would lack control over machine functions like spindle, coolant, or tool changes—limiting its usefulness.
Q5: Where can I download a complete G/M code manual for FANUC?
You can get official documentation from FANUC’s support center, your machine tool builder, or authorized dealers.