G and M Codes in CNC Programming: The Language That Drives Precision Machining

In CNC machining, whether you’re milling, turning, drilling, or cutting complex 5-axis parts, there’s one fundamental skill every programmer and operator must understand: G and M codes.

These codes are the language CNC machines speak. They tell the machine what to do, how to move, and when to perform specific actions. A well-written CNC program that uses G and M codes correctly can mean the difference between high-quality parts and costly scrap.

Let’s break down what these codes are, how they differ, and why mastering them is essential for reliable machining.

 

What Are G and M Codes?

CNC machines are essentially automated tools guided by instructions. These instructions are written in a structured language made up of G codes and M codes:

  • G codes — control the geometry, movement, and toolpath of the machine.

  • M codes — control miscellaneous machine functions that support the machining process.

Both work together in a CNC program to produce accurate, repeatable results.


Understanding G Codes: Geometry and Motion

The “G” in G code is often thought of as geometric or general code. These codes tell the CNC machine how and where the tool should move.

What G Codes Do

G codes define:

  • Tool motion type — rapid, feed, circular, or interpolated motion

  • Movement axes — X, Y, Z and rotational axes (A, B, C)

  • Feed rate (F), spindle speed (S), and other motion-related settings

Even though the same G code can sometimes behave slightly differently across machine brands or controllers, the basics remain consistent.


Common G Codes You Need to Know

G CodeFunction
G00Rapid positioning (move quickly to a point)
G01Linear feed move (controlled feed rate)
G02Circular interpolation (clockwise)
G03Circular interpolation (counterclockwise)
G17Select XY plane
G18Select XZ plane
G19Select YZ plane
G20Programming in inches
G21Programming in millimeters
G90Absolute programming
G91Incremental programming

These are just a subset — CNC machines can support dozens of G codes, especially when working with advanced axes or functions.

 

Understanding M Codes: Miscellaneous But Essential

While G codes control movement, M codes handle the support actions that make a machining program work in the real world. Think of them as machine control commands.

M codes manage functions such as:

  • Starting/stopping the spindle

  • Tool changes

  • Turning coolant on/off

  • Program stop or end

Unlike G codes, M codes are often machine-specific, meaning different CNC controls (Fanuc, Haas, Siemens, Mazak, etc.) may use slightly different M-code definitions.


Examples of M Codes in Use

Here are some common M codes and their purpose:

M Code Function
M00 Program stop
M01 Optional stop (only if enabled)
M02 End of program
M03 Spindle on, clockwise
M04 Spindle on, counterclockwise
M05 Spindle stop
M06 Tool change
M08 Coolant on
M09 Coolant off
M30 End program and reset

For example, M03 will start the spindle turning clockwise before a cut, while M05 will stop it when a move is complete. Knowing when and where to insert these codes is critical for safe and accurate machining.


How G and M Codes Work Together

A CNC program consists of lines or blocks of text, each containing a code that tells the machine something to do. A single line might include:

  • A line number (N)

  • One or more G codes

  • One or more M codes

  • Movement coordinates (X, Y, Z)

  • Optional parameters like feed (F) or speed (S)

Together, these instructions tell the machine move here, cut this way, start the spindle, stop the coolant — all in a step-by-step sequence.

For example:

 
N10 G00 X10 Y10 ; Rapid move to position
N20 G01 Z-5 F100 ; Feed down into the part
N30 M03 S2000 ; Start spindle at 2000 RPM
N40 G01 X50 ; Feed to X50
N50 M05 ; Stop spindle
N60 M30 ; End program

This structured language gives CNC machines the precision and repeatability that makes them so valuable in modern manufacturing.


Why Learning G and M Codes Matters

With today’s advanced CAD/CAM software, many programmers never hand-write code. Yet understanding what the code represents is crucial for:

  • Debugging and tweaking programs

  • Reading and editing generated code

  • Understanding machine behavior

  • Improving cycle times and quality

Even if you rely on CAM tools to generate programs, knowing how G and M codes function makes you a more capable and versatile machinist.


Final Thoughts

G and M codes are the backbone of CNC programming — guiding the machine’s motion and operations one command at a time. While modern software can generate these codes automatically, mastering them gives you confidence, control, and deeper understanding of the machining process.

With a solid grasp of G and M codes, you can write and troubleshoot programs, optimize machining strategies, and take your CNC skills to the next level.