Understanding G and M Codes for CNC Machining

CNC machining is one of the core processes in manufacturing that produce intricate and sophisticated products that have applications in various industries. At the heart of CNC machining’s unmatched efficiency and accuracy are the G and M codes – the critical programming languages that guide CNC machines.

Unlike common perception, G codes and M codes hold distinct roles in CNC operations. G codes primarily instruct the machine on the geometry of the cut, while M codes control the hardware aspects, like turning the spindle on or off. This nuanced difference is pivotal for understanding the full potential of CNC machining.

In this article, we delve into the specifics of these codes, unraveling their unique functions and how they synergize to optimize CNC machine performance.

What’s CNC Machining?

cnc programming g and m codes

Understanding CNC machining is fundamental before delving into the specifics of CNC programming codes. CNC machining, or Computer Numerical Control machining, utilizes computerized controls and machine tools to produce precise and intricate parts from various materials. The technology has significantly transformed the landscape of modern manufacturing as it offers increased efficiency, and accuracy, and can create complex geometries.

Its key advantages are: 

  • Precision: CNC machining offers high precision and accuracy in producing complex parts.
  • Efficiency: Automated processes and optimized tool paths contribute to efficient material removal.
  • Versatility: Suitable for a wide range of materials and applications.
  • Repeatability: CNC machines can produce identical parts with consistent quality.

How Does CNC Programming Control CNC Machines?

Before the advent of computers, machinists used cards or tapes to control machine movements. They punched holes in these cards in a specific order to create the codes. While this was also effective at the time, it was quite tedious. Also, these cards were prone to damage or getting lost in the machine shops. This led to several problems in production at the time.

When machinists started using computers for numerically controlled machines, they still came across a few problems. This was because they had to input the codes manually. This would, of course, be very tedious when they were making quite sophisticated parts that required a lot of instructions.

cnc programming control

The advent of advanced computers and software has revolutionized CNC machining. Machinists now simply input instructions into software, which then generates the necessary G codes and M codes for the machines. This process, greatly simplified by CAD and CAM software, has made code generation and machine operation more efficient and accessible, enhancing both precision and complexity in manufacturing.

To start the process, the programmer needs high-level computer-aided software. The programmer then imports the machine model and the machining fixture into the software, then selects the tools and the tooling paths of the spindle. Once these parameters are set, the software efficiently generates the requisite G and M codes, which are essential for the CNC machine to operate effectively.

What Are G-Codes in CNC Programming?

g codes cnc programming

G code (also RS-274D) is the most popular CNC programming language. Most G code commands are in alphanumeric format and start with G which stands for geometry. They are responsible for the movements of CNC machines, telling the machine where to start, how to move, and when to stop when fabricating a part.

However, G code can be quite complicated for machinists because different machines read G codes in different formats. Most machines’ differences are in the presence or absence of spaces between commands and the number of zeros between the letter and number in the commands. For example, a machine might use G3 while another uses G03. Machinists must always be conversant with the type of machine they’re using. Otherwise, errors in the command can lead to serious problems in parts production.

Beyond G codes, programmers use other letters that signify distinct functions as well. These letters diversify the language of CNC programming, enabling a wide range of commands for precise and intricate machining tasks.

  • A: It directs the tool around the x-axis.
  • R: It gives the radius of the arcs the machine makes.
  • X, Y, Z: These three values indicate the tools’ position in three dimensions – X and Y represent the horizontal and vertical dimensions, respectively, while Z represents the depth.
  • I and J: Both values designate the incremental center of any arc the machine makes.
  • N: N gives the line number.

The code also uses other letters which depend on the machine’s capabilities.

BlockDescriptionPurpose%Start of programStart ProgramO00001 (Project 1)Program number (Program Name)Start Program(T1 0.25 END MILL)Tool description for operatorStart ProgramN1 G17 G20 G40 G49 G80 G90Safety block to ensure the machine is in safe modeStart ProgramN2 T1 M6Load Tool #1Change ToolN3 S9200 M3Spindle Speed 9200 RPM, On CWChange ToolN4 G54Use fixture Offset #1Move to PositionN5 M8Coolant onMove to PositionN6 GOO X-0.025 Y-0.275Rapid above partMove to PositionN7 G43 Z1. H1Rapid to the safe plane, use tool length Offset #1Move to PositionN8 ZO.1Rapid to feed planeMove to PositionN9 G01 Z-0.1 F18Line move to cutting depth at 18 IPMMove to PositionN10 G41 Y0.1 D1 F36CDC left Lead in Line, Dia. Offset #1, 36 IPMMachine ContourN11 Y2.025Line MoveMachine ContourN12 X2.025Line MoveMachine ContourN13 Y-0.025Line MoveMachine ContourN14 X-0.025Line MoveMachine ContourN15 G40 X-0.4Turn CDC off with lead-out moveMachine ContourN16 G00 Z1Rapid to safe planeMachine ContourN17 MSSpindle OffChange ToolN18 M9Coolant OffChange Tool(T2 0.25 DRILL)Tool description for operatorChange ToolN19 T2 M6Load Tool #2Change ToolN20 S3820 M3Spindle Speed 3820 RPM, On CWChange ToolN21 M8Coolant OnMove to PositionN22 X1 Y1Rapid above holeMove to PositionN23 G43 Z1 H2Rapid to safe plane, use tool length, Offset 2Move to PositionN24 Z0.25Rapid to feed planeMove to PositionN25 G98 G81 Z-0.325 RO.1 F12Drill hole (canned) cycle. Depth Z-.325, F12Drill HoleN26 G80Cancel drill cycleDrill HoleN27 Z1Rapid to safe planeDrill HoleN28 MSSpindle OffEnd ProgramN29 M9Coolant OffEnd ProgramN30 G91 G28 Z0Return to Machine Home Position in ZEnd ProgramN31 G91 G28 X0 Y0Return to Machine Home Position in XYEnd ProgramN32 G90Reset to absolute positioning mode (for safety)End ProgramN33 M30Reset the program to the beginningEnd Program%End ProgramEnd Program

What Are M-Codes in CNC Programming?

m codes cnc programming

M code, akin to G code, commences with the letter ‘M’ and encompasses a series of auxiliary commands vital for controlling a CNC machine’s non-geometric functions. These codes, often referred to as miscellaneous codes, manage essential operations like halting the program, activating coolant systems, and powering down the machine post-operation.

In CNC programming, it is crucial to use M codes with precision. Typically, each block of program information should contain only one M code. This practice is imperative because M codes often serve to activate or deactivate various machine functions. Overlapping these commands within a single block can lead to programming conflicts and operational errors.

Similar to G codes, M codes vary across different CNC machines. This variance can include differences in the numerical formatting of the codes, such as the inclusion or exclusion of leading zeros. For instance, one machine might recognize an M code as ‘M3’, while another requires ‘M03’. Therefore, machinists must be well-versed in the specific coding requirements of the equipment they operate to ensure seamless and error-free machining processes.

A List of G and M Codes for CNC Machining 

This section illustrates a range of basic G and M codes, highlighting their distinct functionalities. While some codes have similar meanings across both lists, others differ significantly in application and interpretation in CNC machining.

Commonly Used of G Codes

G-codes in CNC machining transform complex operations into methodical tasks, with standardized codes ensuring consistency and peak performance. Here’s a look at some key G-Codes crucial for anyone working with CNC machines.

  • G00 – Rapid Positioning: This command is used for swiftly moving the tool to specified coordinates at maximum speed. Primarily, it positions the tool without engaging in material cutting, optimizing the machine’s efficiency for non-cutting movements.
  • G01 – Linear Interpolation: This command directs the tool to move in a straight line between two points at a set feed rate. Predominantly utilized for straight-line cutting, G01 is one of the most frequently used G codes in CNC machining.
  • G02 – Circular Interpolation (Clockwise): This command facilitates the creation of arcs and circles by guiding the tool along a circular path in a clockwise direction. It ensures precise movement to a specified endpoint, essential for circular machining tasks.
  • G03 – Circular Interpolation (Counter-Clockwise): This command mirrors G02, but with the tool moving along a circular path in a counter-clockwise direction. It’s essential for crafting arcs and circles that require a counter-clockwise approach.
  • G04 – Dwell: This command instructs the CNC machine to temporarily pause at its current position for a predefined period. The dwell function is particularly useful in scenarios such as allowing a cutting tool to cool down or enabling the spindle to attain the desired speed.

A List of Other Function G Codes

CodeCategoryFunctionModalFor Turning or MillingG17Plane SelectionXY Plane SelectionYesBothG96Speeds and FeedsConstant Surface SpeedYesTurningG91Positioning and ModesIncremental ModeYesBothG03Circular Interpolation (CCW)Create arcs and circles (Counter-Clockwise)YesBothG04DwellPause for a specified durationNoBothG18Plane SelectionXZ Plane SelectionYesTurningG19Plane SelectionYZ Plane SelectionYesTurningG20Unit SystemInch SystemYesBothG21Unit SystemMetric SystemYesBothG40Cutter CompensationCancel Cutter CompensationYesMilling

For additional information on G codes, please refer to this resource.

Commonly Used M Codes

Although CNC machines typically use M-codes akin to G-codes, standardization across models isn’t universally adopted. Thus, CNC programmers must be cautious about machine-specific codes. Yet, certain M-codes consistently retain the same meaning across all machines.

  • M00 – Program Stop: To stop the CNC program temporarily. It often requires operator intervention to resume the program.
  • M02 – Program End: To end the CNC program. After executing this code, the control will stop, and the operator may need to reset or restart the machine.
  • M03 – Spindle On, clockwise: To start the spindle rotation in the clockwise direction. It is often followed by a speed command (S) to set the spindle speed.
  • M04 – Spindle On, Counterclockwise: Similar to M03, M04 is used to start the spindle, but it rotates in the counterclockwise direction.
  • M05 – Spindle Stop: To stop the spindle rotation. It is often employed when a tool change or other operation requires the spindle to be stationary.

A List of Other Function M Codes

CodeCategoryFunctionModalFor Turning or MillingM08CoolantCoolant flood or onNoBothM42Auxiliary FunctionsHigh Gear SelectNoTurningM19Spindle ControlChange spindle orientationsNoMillingM00Program ControlProgram StopNoBothM02Program ControlProgram EndNoBothM03Spindle ControlSpindle On, ClockwiseNoBothM04Spindle ControlSpindle On, CounterclockwiseNoBothM05Spindle ControlSpindle StopNoBothM06Tool ChangeTool ChangeNoBothM09CoolantCoolant OffNoBoth

For additional information on M codes, please refer to this resource.

Let’s Summarize The Difference Between G and M Codes


  • Direct the motion and function of the CNC machine.
  • Describe positions and movements, such as rapid positioning to a specific XY plane, linear feed movement, and circular interpolation.
  • Related to geometric codes, serve in product design.
  • Activate the CNC machine.


  • Control operations not involving movements, such as stopping programs, changing tools, turning the spindle on or off, and activating coolant systems.
  • Relate to machine functions and serve in various miscellaneous operations.
  • Activate the machine’s programmable logic controller (PLC).

RapidDirect’s Expertise in CNC Machining

cad design cnc machining

Explore CNC machining solutions with RapidDirect, where understanding and precision meet. Our team is skilled in the intricacies of G and M codes, ensuring that every project is handled with attention to detail and expertise. We believe in offering high-quality results that are both effective and affordable.

Our user-friendly platform streamlines your experience, offering instant quotations and a straightforward project tracking process. Managing your CNC machining needs becomes effortless with our efficient and accessible system.

Let’s collaborate to achieve your manufacturing goals.


Using CNC machines is one of the most important processes in CNC machining. However, these machines cannot function without G codes and M codes which instruct them on what to do. Understanding how to generate these codes is vital to the CNC machining process and successful parts production. Mastery of these codes gives you a head start in your CNC programming career.

Source link