G and M Codes: CNC Programming Language for Custom Parts

Crafting precision-made parts with a CNC machine requires more than hardware; it demands a language that machines understand – the G and M codes

These codes form the bedrock of CNC programming and have a pivotal role in the manufacturing industry. From guiding a simple drilling task to managing complex cutting tool paths, they are at the heart of every operation.

CNC, standing for computer numerical control, goes beyond just machine tools. It envelops a broader spectrum, including computer-controlled measuring devices and industrial robots. 

Rooted in the 1950s, CNC machining was born from a partnership between John T. Parsons, MIT, and IBM. The need for precision in the aviation industry fueled this invention.

The journey since then has been monumental. CNC machining now underpins numerous industries, shaping complex products with precision. Yet, the machinery is at the heart of this process, powered by G and M codes. 

Despite using a blend of control CNC machines and codes in most workshops, they remain unique in their functions. Curious about the differences and their role in the operation of CNC machines? Stick with me as we dive into these intriguing codes.

What is G-code?

G code, also known as RS-274D, is the cornerstone of CNC programming. The “G” in G code? It stands for geometry, and it’s vital. It’s the string of commands that controls how a CNC machine moves. It guides the machine on where to start, the path to take, and the point to stop while crafting a part.

But mastering G code isn’t always a walk in the park. Why? Because different machines interpret G codes in unique ways. This can be as subtle as the number of zeros that follow the “G” in a command or whether there are spaces between commands. 

So, while one machine might read “G3”, another might prefer “G03”. As a machinist, you’ve got to know your machine inside out. One wrong command can throw a wrench in the entire production process if you’re not careful.

And while “G” may be the show’s star in G code CNC programming, it doesn’t work alone. Other letters also have their roles, representing a range of commands that broaden the G code program’s capacity to manage the making of parts.


And one such letter is the “M” code. Now let’s talk about M code. Just like the G code starts with “G,” the M code begins with “M.” 

What is M Code?

Aria CNC Machining Works

M code is a command used to control various auxiliary functions and machine operations. It oversees the non-geometric functions of the machine.While G-codes primarily handle motion and positioning, M-codes handle additional actions such as tool changes, coolant control, spindle control, and machine setup.

From stopping programs, and dousing the machine with coolant, to powering down when the temperature drops, the M code is there, pulling the strings. These auxiliary commands play a crucial role in maintaining the machine’s smooth running.

And there’s one thing you need to remember when you’re crafting a CNC program with G and M codes. Only use one command per block of information. Why? Because their main role is to switch the machine on and off. 

If you try to squeeze more than one command into the same straight line in a single block, you’re asking for trouble. It can lead to programming hiccups that can disrupt the entire operation.

G code CNC

Here are some examples of letters used in G-code commands along with their meanings:

G: This is the most common letter in G-code commands and represents the “preparatory” or “geometric” functions. It controls various motion-related operations, such as rapid positioning, linear interpolation, circular interpolation, and more.

X, Y, Z: These letters represent the Cartesian coordinates in a three-dimensional space. X denotes the position along the horizontal X-axis, Y represents the position along the vertical Y-axis, and Z indicates the position along the depth Z-axis.

F: The letter “F” is used to set the feed rate or the speed at which the tool moves during linear interpolation or cutting operations. It determines how quickly the tool moves along a particular path.

S: The letter “S” is used for controlling the spindle speed, typically in RPM (revolutions per minute). It is commonly used with commands related to turning the spindle on/off, changing direction, or setting a specific speed.

T: The letter “T” is used for tool-related commands, primarily for tool selection and tool changes. It is often followed by a number to specify the tool number or identifier.

What is difference between G-Code and M-Code?

G-code primarily deals with commands related to tool movements, such as linear and circular interpolation, rapid positioning, feed rates, and tool offsets. G-codes determine the path and speed at which the cutting tool moves during machining operations.

M-code handles commands that are not directly related to tool movements but are essential for the overall operation of the CNC machine. M-codes control actions like tool changes, spindle control (on/off, spindle speed control, direction), coolant control (on/off), program stops, subprogram calls, and program ends.

Who Needs to Know G-Code and M-Code?

Curious about who needs to roll up their sleeves and know about G-code and M-code? The short answer is – if you’re working in a machine shop or envision yourself navigating the CNC programming waters in the future, you’ll need to equip yourself with both codes. 

Grasping the fundamentals of G-code and M-code is a must for every machine shop individual, regardless of rank or role.

The extent of your code knowledge can vary based on your role. Perhaps a cursory understanding of the basics of G and M-codes is all you need. Or perhaps, your role necessitates a comprehensive mastery of both programming languages. 

A rudimentary understanding of CNC machining codes is beneficial for virtually any position in a CNC machine shop. Some proactive machine tool shops even provide training to enhance their employees’ CNC programming language skills.

Think of a CNC machining center as a finely tuned machine designed to run its program consistently and autonomously. However, the smoothness of operation depends heavily on the manufacturing process and the specific part. 

Once the CNC programming is locked in, alterations are seldom required. But minor tweaks might be necessary if the final products fail to meet precision standards. 

This delicate editing process is the purview of the experts – think seasoned engineers, setup maestros, and vigilant supervisors. 

CNC Codes Best Practice for Custom Parts

There is one common question most of us have in mind when it comes to CNC codes. What’s the best design practice for custom CNC machined parts?

Well, you’re about to find out.

Rapid Travel

General Tolerances

First up, we need to talk about general tolerances. This refers to the level of precision required for the part being made. Typically, a company needing CNC machined parts will provide the manufacturers with detailed instructions, like a drawing or specification sheet. 

They’ll also indicate a tolerance standard, such as ISO2768-mK, a commonly used tolerance. If there’s no specific tolerance provided, the CNC machine shops will adhere to their default set of general tolerances.

Part Tolerances

Now let’s look at part tolerances. This term means the acceptable range of dimensions for the machined part. It’s decided on a case-by-case basis, often tighter than general tolerances. The part’s designer determines the tolerance range, considering the part’s form, fit, and function.

Size Limitations

Thirdly, consider size limitations. The machine’s capabilities and the required depth of cut determine part size. 

Depending on the part’s features and size, this could also dictate a part’s height. The shaping capabilities of the CNC machined parts are bound by their space, build, and length or the part’s diameter.

Material Selection

Material selection is crucial to the function and cost of the CNC machined parts. The part’s designer needs to define the essential characteristics that the material needs to have.


Complexity can complicate things. If a part has many complexities or limitations, the designer will have to deal with intricate geometry and multiple faces to cut. 

As the complexity increases, so does the cost of machining the final product. However, you can mitigate this by reducing the need for tool changes.

5-Axis CNC Machining

This process allows the part to move in five different ways at once, enabling the manufacture of very complex parts more efficiently. 

Minimizing setups and tooling paths and achieving faster cutting speeds can improve surface finishes and generate more efficient and tool path paths.

G Codes List for CNC Milling

Navigating through CNC milling can be tricky without a handy guide to all the G-Codes used. To make your work easier, we have prepared a comprehensive list of G-Codes used in CNC milling.

G02 code




Rapid Positioning


Linear Interpolation


Circular Interpolation (Clockwise)


Circular Interpolation (Counter-Clockwise)




Exact Stop Check


Data Setting


XY Plane Selection


ZX Plane Selection


YZ Plane Selection


Inch System Selection


Metric System Selection


Return to Home Position


Return from Reference Point


Return to Secondary Home Position


Thread Cutting


Cutter Compensation Cancel


Cutter Compensation Left


Cutter Compensation Right


Tool Length Compensation Plus


Tool Length Compensation Plus


Tool Length Compensation Cancel


Machine Coordinate System Setting


Work Coordinate System (WCS) Setting


Cancel Canned Cycle


Drilling Cycle


Counter Boring Cycle


Deep Hole Drilling Cycle


Tapping Cycle


Absolute Programming


Incremental Programming


Coordinate System Setting


Feed rate per Minute


Feedrate per Revolution


Initial Level Return in Canned Cycle


R Point Level Return in Canned Cycle

Cutter Compensation

M Codes List For CNC Milling & Turning

As essential as G-Codes, M-Codes also play a vital role in the CNC milling & turning. They control different machine functions, providing a powerful tool to perfect your milling process. Below is a compiled thorough list of M-Codes commonly used in CNC milling & turnining for your convenience.




Program Stop


Optional Program Stop


Program End


Spindle Start (Clockwise)


Spindle Start (Counter-Clockwise)


Spindle Stop


Tool Change


Coolant On


Coolant Off


Spindle On (Clockwise) + Coolant On


Spindle On (Counter-Clockwise) + Coolant On


Program End and Reset


Program End, Rewind, and Reset


Call Subprogram


Subprogram Return


Wrapping things up, I can’t stress enough how crucial it is to get a solid handle on G-Codes and M-Codes if you’re dipping your toes into CNC machining or even if you’ve been at it for a while. 

These codes are like the secret language for CNC machines, guiding them to make precise and efficient moves. From understanding the complexities of designing parts to selecting appropriate materials, every step hinges on our command of these codes. 

Furthermore, the utilization of advanced techniques, such as 5-axis CNC machining, could open new doors to manufacturing efficiency and innovation. 

Always remember that embracing these codes and adhering to best practices can enhance the quality and precision of our work. In the realm of CNC machining, being fluent in G-Code and M-Code isn’t just helpful. It’s fundamental.


Gavin Leo is a technical writer at Aria with 8 years of experience in Engineering, He proficient in machining characteristics and surface finish process of various materials. and participated in the development of more than 100complex injection molding and CNC machining projects. He is passionate about sharing his knowledge and experience.