The production of precision parts goes beyond merely possessing a CNC machine; it requires a programming language that the machine can interpret—specifically, G-codes and M-codes.
In 1952, to address the aviation industry’s need for accurate part machining, John T. Parsons partnered with IBM (International Business Machines) and MIT (Massachusetts Institute of Technology) to create the first CNC milling machine. This development initiated the widespread adoption of intelligent industrial production, allowing CNC machining to expand into various sectors. As a result, the emergence and implementation of CNC programming languages—G-codes and M-codes—naturally followed.This article will explore these essential G-codes and M-codes, examining their significance and applications in modern manufacturing.
What Is G-Code?

G-code, also known as RS-274D, is the most commonly utilized CNC programming language, primarily consisting of numerical instructions. The “G” in G-code stands for geometry, referring to the alphanumeric format that instructs the system on the part’s functions. In CNC programming, G-code governs the machine’s movements, specifying how and where the machine should operate to create the part—indicating the starting point, the required motions, and when to halt.
As a machinist, it’s crucial to have a comprehensive understanding of your machine. For instance, one machine may interpret “G3,” while another may require “G03.” Additionally, some machines mandate decimal points between commands, whereas others allow operators to omit them. These variations in programming languages are vital for executing the program accurately, so operators must always be aware of the specific language requirements for the machine they are operating.
What Is M-Code?

M-codes, or Miscellaneous Codes, are responsible for managing the non-motion-related functions of CNC machines. These codes control various auxiliary operations that are crucial during the machining process. While G-codes focus on the geometric aspects of machining—such as positioning and movement—M-codes govern auxiliary functions like starting or stopping the spindle, activating or deactivating the coolant system, changing tools, and managing program pauses (e.g., waiting for operator input).
When developing a CNC program, it’s important to remember to use both G and M codes. Each block of code should contain only one command, as their primary function is to toggle machine operations on and off. Since M-codes can differ between machines, operators must be familiar with the specific M-codes relevant to their equipment.
What Is Difference Between G-Code And M-Code?
G-codes and M-codes serve distinct but complementary roles in controlling CNC machines. Here’s a summary of their key differences:
G-code focuses on commands related to tool movement, governing how the machine operates, including the cutting path and speed. It defines positions and motions, such as rapid positioning to a specific XY plane, linear feed movements, and circular interpolation. These codes are geometric in nature and are essential for product design, as they initiate the CNC machine’s movements and dictate how the tool interacts with the material.
In contrast, M-codes manage machine operations that do not involve movement. They handle tasks such as stopping the program, changing tools, turning the spindle on or off, and activating the coolant system. M-codes also oversee functions related to the machine’s auxiliary operations and initiate the machine’s Programmable Logic Controller (PLC) to manage various miscellaneous tasks.
Commonly Used G-Codes And M Codes And Their Functions
This section introduces a series of essential G and M codes, highlighting their unique functions. While some codes share similar meanings across both lists, others differ significantly in their application and interpretation within CNC machining.
Common G-Codes
This section presents a selection of essential G and M codes, emphasizing their distinct functions. While some codes may have similar meanings across both categories, others vary significantly in their application and interpretation within CNC machining.
Common G-Codes
In CNC machining, G codes convert complex operations into structured tasks. The use of standardized codes ensures consistency and optimal performance. Here are some key G codes that are crucial for anyone working with CNC machines:
G00 – Rapid positioning: Moves the tool to a designated location at the machine’s maximum speed without cutting.
G01 – Linear interpolation (Cutting in a straight line): Moves the tool in a straight line at a specified feed rate.
G02 – Circular interpolation (Clockwise arc): Moves the tool in a clockwise arc from the starting point to the endpoint.
G03 – Circular interpolation (Counterclockwise arc): Moves the tool in a counterclockwise arc.
G04 – Dwell: Pauses the machine’s movement for a specified duration.
List of Other Functional G Codes:
G Code | Description | Function |
G17 | Select XY Plane | Defines the XY plane for circular interpolation. |
G18 | Select XZ Plane | Defines the XZ plane for circular interpolation. |
G19 | Select YZ Plane | Defines the YZ plane for circular interpolation. |
G20 | Programming in Inches | The machine interprets dimensions in inches. |
G21 | Programming in Millimeters | The machine interprets dimensions in millimeters. |
G28 | Return to Machine Home | Moves the tool to the machine’s reference zero position. |
G40 | Cancel Cutter Radius Compensation | Cancels the tool diameter offset compensation. |
G41 | Cutter Compensation Left | Offsets the toolpath to the left of the programmed path. |
G42 | Cutter Compensation Right | Offsets the toolpath to the right of the programmed path. |
G43 | Tool Length Compensation (+) | Compensates for the tool length, typically in the positive direction. |
G49 | Cancel Tool Length Compensation | Cancels the tool length offset compensation. |
G54 to G59 | Work Coordinate Systems | Selects different work coordinate systems for machining. |
G90 | Absolute Programming | All coordinates are specified relative to a fixed origin. |
G91 | Incremental Programming | Coordinates are specified relative to the current position. |
G92 | Set Work Coordinate System Origin | Sets the current tool position as the origin for the work coordinate system. |
Common M-Codes
Although CNC machines often use M codes similar to G codes, cross-model standardization has not been universally implemented. Therefore, CNC programmers must consider machine-specific codes carefully. However, certain M codes consistently maintain the same meaning across all machines.
M00 – Program stop: Halts the machine’s operations until the operator resumes the program.
M01 – Optional stop: Stops the machine if the optional stop is enabled in the control system.
M02 – Program end: Concludes the CNC program, stopping both the spindle and the coolant.
M03 – Spindle on (clockwise): Initiates the spindle’s rotation in a clockwise direction.
M04 – Spindle on (counterclockwise): Initiates the spindle’s rotation in a counterclockwise direction.
M05 – Spindle stop: Stops the spindle from rotating.
List of Other Functional M Codes:
M Code | Description | Function |
M06 | Tool Change | Commands the machine to change the tool. |
M07 | Mist Coolant On | Turns on mist-type coolant. |
M08 | Flood Coolant On | Turns on flood-type coolant. |
M09 | Coolant Off | Turns off all coolant systems. |
M30 | Program End and Reset | Ends the program, resets the program counter, and returns to the beginning. |
M48 | Enable Feed Rate Override | Allows the operator to adjust the feed rate manually. |
M49 | Disable Feed Rate Override | Disables manual feed rate control. |
M98 | Call Subprogram | Calls a subprogram to be executed. |
M99 | Return from Subprogram | Ends the subprogram and returns to the main program. |
The Importance Of G-Code And M-Code In CNC Machining
G and M codes are crucial for CNC machining because they provide the precise instructions necessary for high-precision manufacturing. Here are the key reasons these codes are essential:
- Precision and Control
G and M codes allow CNC machines to execute highly accurate movements and operations, which are vital for achieving tight tolerances and superior surface finishes. This precision ensures the production of high-quality machined parts.
- Standardization
These codes serve as a standardized programming language, facilitating communication between different machines, controllers, and programmers. This standardization is essential for smooth collaboration within the manufacturing industry, enabling operators to work efficiently across various CNC systems.
- Versatility
G codes cover a range of operations, such as linear and circular movements, tool compensation, and coordinate adjustments. M codes manage auxiliary functions, like coolant control and tool changes. This versatility allows CNC machines to handle tasks ranging from basic drilling to complex milling, making them suitable for diverse applications.
- Efficiency
Automating processes with G and M codes improves machining efficiency. CNC machines can run programs with minimal human intervention, reducing time and labor costs. This automation boosts productivity, leading to significant cost savings for manufacturers.
- Flexibility
These codes allow easy modifications to machining programs. Programmers can change parameters—such as feed rates or tool paths—without needing to rewrite the entire program. This flexibility is especially beneficial in job shops, where custom parts are frequently produced.
- Error Reduction
By using standardized codes, the chance of human error is minimized. G and M codes eliminate ambiguity in programming, enabling operators to focus on other critical aspects of the machining process. This leads to more accurate, reliable, and consistent production results.
In summary, G and M codes are foundational to CNC machining, offering precision, versatility, and efficiency while minimizing errors. Their standardization ensures seamless collaboration across the industry, driving productivity and innovation in modern manufacturing.
Example Of G-Code In CNC Machining

Code Explanation:
G21 – Set units to millimeters: This code instructs the CNC machine to interpret all dimensions and coordinates in millimeters.
G90 – Absolute positioning mode: All subsequent coordinates will be calculated relative to a fixed work coordinate system.
G00 X50 Y50 – Rapid move to X=50, Y=50: The tool rapidly moves to the specified coordinate point in the plane (without cutting).
G01 Z-10 F100 – Linear move to Z=-10 at a feed rate of 100 mm/min: The tool starts moving downward along the Z-axis and engages in cutting.
G01 X100 Y100 – Linear move to X=100, Y=100: The tool continues its cutting motion in the XY plane.
G00 Z5 – Rapid move to Z=5: The tool rapidly moves upward along the Z-axis, retracting from the workpiece.
M30 – End of program and reset: The program ends, and the machine resets.
Result Explanation:This program operates in absolute mode. First, the CNC machine is set to use millimeters as the unit of measurement. Then, the tool rapidly moves to the position of X=50 and Y=50, and then cuts downward to Z=-10 at the specified feed rate. Next, the tool moves in the XY plane to the coordinates of X=100 and Y=100 for further cutting. After completing the cutting operation, the tool retracts upward to Z=5, and the program concludes.
Example Of M-Code In CNC Machining

Code Explanation:
M06 – Tool Change: This command instructs the CNC machine to perform a tool change operation, typically done before starting machining or when a different tool is required.
M08 – Flood Coolant On: This code activates the flood coolant system, helping to cool the tool and remove chips during the machining process to prevent overheating.
G00 X50 Y50 – Rapid Move to X=50, Y=50: The tool rapidly moves to the specified coordinate position (without cutting).
G01 Z-10 F100 – Linear Move to Z=-10 at a Feed Rate of 100 mm/min: The tool begins to move downward along the Z-axis while cutting.
G01 X100 Y100 – Linear Move to X=100, Y=100: The tool continues its cutting motion in the XY plane.
M09 – Coolant Off: This command instructs the machine to stop the flow of coolant, typically done after the cutting operation is complete.
G00 Z5 – Rapid Move to Z=5: The tool rapidly moves upward along the Z-axis, retracting from the workpiece.
M30 – End of Program and Reset: The program ends, and the machine resets.
Operation Explanation:This program starts with a tool change to ensure the correct tool is installed on the machine. Next, the flood coolant system is activated to help control the tool’s temperature and remove chips during machining. The tool then rapidly moves to the specified machining position and performs a downward cutting operation along the Z-axis, followed by further cutting in the XY plane. Once the cutting is complete, the coolant is turned off, and the tool quickly moves upward to retract from the workpiece. Finally, the program concludes, and the machine resets.
How To Read G-Code And M-Code?
Understanding the structure and meaning of G and M codes is essential for interpreting CNC machining instructions. Here are the steps to follow:
Identify the Command
Start by locating the command letter—either G or M—and note the accompanying number to determine the specific function.
Interpret the Parameters
Examine any additional parameters that follow the command. These may represent key details such as position coordinates, feed rates, tool selection, or other relevant data.
Understand the Sequence
Pay close attention to the order of commands, as the sequence defines the workflow of the machining operations. A properly structured sequence ensures smooth and accurate execution.
Familiarize Yourself with Common Codes
Improve your reading speed by learning frequently used codes. For example:
G-codes: G00 (rapid move), G01 (linear cutting)
M-codes: M03 (spindle on), M05 (spindle off)
By mastering these steps, you’ll enhance your ability to read and interpret CNC programs accurately, ensuring effective machine operation.
Conclusion
G codes and M codes serve distinct yet complementary roles in CNC machining. G codes govern the machine’s movements and geometry, dictating the path and positioning of tools. In contrast, M codes control machine functions, such as spindle operation and coolant flow. Together, they allow CNC machines to manufacture a wide range of parts with precision and adaptability.
A thorough understanding of G and M codes is crucial for anyone working in CNC machining. These codes are the foundation of CNC programming, providing precise control over operations and optimizing manufacturing processes.In short, mastering G and M codes is indispensable for starting a career in a CNC machine shop, as they are essential for effective machine operation and programming.
Final Thoughts
With 15 years of experience in CNC machining service, I understand the critical role G and M codes play in achieving precision manufacturing. These codes not only efficiently control machine movements and functions but also ensure high-quality production of complex parts. For operators looking to gain a competitive edge in the industry, mastering these codes is essential.
I recommend UltiRapid for its advanced high-speed machining technology, which maximizes the use of G and M codes. It’s ideal for industries like aerospace and automotive that require high precision and fast turnaround in custom parts production.
FAQS
Do All CNC Machines Use G-Code?
While most CNC machines use G-code as the standard programming language for controlling operations, not all CNC machines rely exclusively on it. Some machines may use proprietary programming languages or other formats, depending on the manufacturer and machine type. Additionally, certain high-level CNC systems can interpret different languages or software inputs, but G-code remains the most widely adopted standard across various CNC applications.
How Many G-Codes Are There In CNC?
There are over 100 G-codes defined in various CNC programming standards, but the most commonly used ones range from G0 to G99. The specific codes available can vary depending on the CNC machine manufacturer and control software. Generally, the basic G-codes cover movements (like G0 for rapid positioning and G1 for linear interpolation), tool changes, and other fundamental functions.
What Do G-Codes Identify?
G-codes identify specific commands for CNC machines, including:
- Motion Control: Directs movements (e.g., G0 for rapid movement, G1 for linear cutting).
- Tool Functions: Commands for tool changes (e.g., G43 for tool height compensation).
- Spindle Control: Instructions for starting, stopping, or adjusting spindle speed.
- Miscellaneous Functions: Other tasks like programming cycles or setting offsets.
Each G-code corresponds to a specific operation, enabling precise control of the machining process.
Why Do We Use G-Code?
G-code is crucial for CNC machining as it provides a standardized language for precise machine instructions. This ensures high accuracy in movements and tool paths, allowing for exact specifications. Its widespread use promotes compatibility across different machines, enhancing programming efficiency. Additionally, G-code is flexible and can be easily generated from CAD/CAM software, making it adaptable to various designs and materials. By facilitating automation, it reduces manual intervention, increasing production speed and consistency.