G and M codes form the foundation of programming CNC machines. These codes are a universal language that tells the machine commands like “go here,” “go this fast,” and “run this function.” In this article, we’ve compiled the most commonly used G and M codes, their meanings, and how they’re used with simple examples.
What Is a G Code?
G codes (Geometric/Preparatory Function) are commands that tell the CNC machine how the cutting tool should move. They define geometric movements such as linear motion, circular motion, and rapid positioning. Each G code activates a specific motion mode in the machine’s control unit, and that mode stays in effect until it’s changed by another G code.
Most Commonly Used G Codes
| Code | Meaning | Description |
|---|---|---|
| G00 | Rapid positioning | The tool moves to the specified point at the fastest speed, without touching the workpiece. |
| G01 | Linear interpolation | The tool cuts along a straight line at the specified feed rate. |
| G02 | Circular interpolation (clockwise) | The tool cuts following a circular path in the clockwise direction. |
| G03 | Circular interpolation (counterclockwise) | The tool cuts following a circular path in the counterclockwise direction. |
| G04 | Dwell | Keeps the tool stationary for the duration specified in the program. |
| G17/G18/G19 | Plane selection | Activate the XY, XZ, and YZ planes, respectively, for circular interpolation. |
| G20/G21 | Unit selection | G20 activates inch units, G21 activates millimeter units. |
| G28 | Return to reference point | Sends the tool to the machine’s defined reference (home) point. |
| G40/G41/G42 | Tool radius compensation | G40 cancels compensation, G41 activates left compensation, G42 activates right tool radius compensation. |
| G54-G59 | Workpiece coordinate system | Used to define different workpiece zero points. |
| G90/G91 | Absolute / incremental programming | G90 defines coordinates relative to the zero point, G91 defines them relative to the previous point. |
| G92 | Coordinate system setting | Sets the tool’s current position to a specific coordinate value (lathe). |
What Is an M Code?
M codes (Miscellaneous/Machine Function) deal not with the machine’s motion but with its auxiliary functions: spindle rotation, turning coolant on/off, stopping the program, and so on. While G codes say “how to move,” M codes say “which additional function to run.”
| Code | Meaning |
|---|---|
| M00 | Stop program (manual resume) |
| M02 | End of program |
| M03 | Rotate spindle clockwise |
| M04 | Rotate spindle counterclockwise |
| M05 | Stop spindle |
| M06 | Tool change |
| M08 | Turn coolant on |
| M09 | Turn coolant off |
| M30 | End of program and return to start |
A Simple Example Program
Below is a simplified example program written to mill a straight slot on a milling machine:
G21 G90 G17 (unit: mm, absolute coordinates, XY plane)
G54 (workpiece coordinate system)
M03 S1500 (spindle clockwise, 1500 rpm)
G00 X0 Y0 Z5 (rapid move to start point)
G01 Z-2 F100 (plunge 2 mm into part, 100 mm/min feed)
G01 X50 F200 (50 mm linear cut)
G00 Z5 (raise the tool)
M05 (stop spindle)
M30 (end of program)
Things to Watch Out for When Learning G Codes
- Most G codes can vary slightly depending on the machine’s control system (Fanuc, Siemens, Heidenhain, etc.); always check the programming manual for the control unit of the machine you’re using.
- Before running a new program, always check it with a simulation or a “dry run.”
- When moving rapidly with G00, make sure the tool doesn’t collide with the workpiece or fixtures.
- Learn to distinguish between modal (persistent) and non-modal (single-line) codes; this saves a lot of time when reading code.
Conclusion
G and M codes are the basic alphabet of CNC programming. Learning these codes by understanding their logic, rather than memorizing them, lets you read programs generated by CAM software, spot errors, and make small manual edits when needed. Practicing by programming simple parts with the basic codes is the fastest way to progress in this field.
Related Posts
G01 Code. Linear Motion with Cutting
CNC Lathe Setup Instructions
CNC Lathe Workshop Work Instructions
The World’s Most Modern CNC Machines Video Series
Powering On a Fanuc CNC Lathe. How Is a CNC Lathe Powered On
CNC Facing Cycle – G94-G72-G70
Mechanical Clamping Elements on Machine Tools
What Is iMachining, Advanced CAD/CAM CNC Machining Technology
