CNC Machine G Code and M Code

Jan 29, 2026

Leave a message

Introduction

CNC machine programming uses G-code and M-code to control the process. G-codes, or "Geometric codes," control the movements of the CNC machine, defining actions such as tool placement, velocity, and path. By contrast, M-code, or "Machine code," handles other functions such as spindle operations, coolant control, and tool change.

Combined, G-codes and M-codes constitute the structure of CNC programming for consistent and concise automation throughout the features of diverse machining functions. In this article, we will describe the significance of G-codes and M-codes.

 

Role of CNC Programming in Precision Manufacturing

Computer numerical control programming is an integral part that determines CNC devices. It outlines how a CNC device will perform numerous operations during production. Using G-codes for motion and M-codes for functions of the CNC machine, the tool instructs the movement of the axes, the tool change, the spindle rotation, and the coolant. These codes are accurate sets of directions that ensure the machine accomplishes certain operations that are needed by the design.

Moreover, CNC programming is directly linked to efficiency and manufacturing process accuracy. G-code controls straight and arc motion. It directs how a cutting tool is supposed to move. M-codes, on the other hand, control other non-primary machine activities, such as changing tools or activating the cooling system, allowing the machine to function independently. G-codes and M-codes simplify the process, create a clear sequence, and reduce mistakes in production.

 

What do G-Codes mean in CNC Programming?

G-codes or preparatory codes are among the CNC codes. When it comes to machine control, these codes are responsible for the act of governing the motions of the cutting tool on the X, Y, and Z planes and many other functions required in the making of a desired part or component. Each G-code entails a shooting/moving mode through which the machine can execute several operations accurately.

In CNC drilling, milling, or turning off parts, G-codes are very important in the operations of CNC machines. These codes refer to linear or circular motions or any other particular operation, for example, tool change or speed variation.

X, Y, and Z planes

Common G-Codes in CNC Programming.

Here are some key aspects of G-codes and their role in CNC programming:

G0 (Rapid Traverse):

These codes control the position of its tool at a specific location as far as possible, although without cutting. It remains for non-cutting motions to position the tool about the starting point of a cut.

G1 (Linear Interpolation):

For controlled cutting operations, G1 moves the cutting tool along a straight line toward a target position with continuous cutting. It is very useful for the control of material removal during the course of a machining operation.

G2 (Clockwise Circular Interpolation) and G3 (Counterclockwise Circular Interpolation):

These codes are used when the CNC machine is performing circular operations. G2 and G3 are suitable for machining arc or curved surfaces to manufacture round or symmetrical goods in the machining operation.

G4 (Dwell):

The G4 code sets a point where the CNC machine should pause or stay for a certain time interval. It is useful in processes where the delay is necessary, for instance, tool cooling, material, etc.

G20 (Inches) and G21 (Millimeters):

These codes show the number of measurements taken during the machining operations. G20 makes the machine work in inches, and G21 makes the machine work in millimeters to be in harmony with the design.

 

How G-Codes Manage the Movements of CNC Machines

G-codes are a series of instructions. They instruct the machine how to move, where to go, and what to do. These codes are used together with other CNC programming components, including M-codes. For example, the relative movement of the cutting tool can be initiated by a G0 code that quickly moves the tool, and then, by a G1 code, it helps refine linear cutting. As the tool advances through the material, it can perform other G2 or G3 codes to generate curved surfaces.

However, you may encounter a few challenges. Some of the problems that result from improper G-coding include: the operator overshooting a target position, incorrect cutting paths, or even machine crashes. As a result, CNC programmers need to use G-codes appropriately and provide the best solutions for a particular machine and tools.

 

Key G-Codes in CNC Programming and Their Functions

Here is a technical table outlining common G-codes used in CNC programming, their functions, and typical applications:

G-Code Function Description Typical Application
G0 Rapid Traverse Moves the tool to a specified position quickly without cutting. Tool positioning before or after cutting operations.
G1 Linear Interpolation Moves the tool in a straight line while cutting along a defined path. Controlled cutting for precise material removal.
G2 Clockwise Circular Interpolation Move the tool in a clockwise arc or circle. Machining curved surfaces or arcs.
G3 Counterclockwise Circular Interpolation Move the tool in a counterclockwise arc or circle. Creating circular features or arcs in the opposite direction.
G4 Dwell Pauses the tool for a specified time at a given position. Allowing time for tool cooling or material settling.
G20 Programming in (inches) Sets the programming to use inches for measurements. Useful for projects designed using inch-based measurements.
G21 Programming in (Millimeters) Sets the programming to use millimeters for measurements. Ideal for designs created in metric units (millimeters).
G90 Absolute Positioning Positions the tool relative to a fixed zero point, not its current position. Used for accurate positioning in milling and drilling.
G91 Incremental Positioning Positions the tool relative to its current position, not a fixed zero point. Used for repetitive motions or minor adjustments in the path.
G53 Machine Coordinate System Uses machine coordinates instead of program coordinates. Help identify operational performance using the machine's coordinates.
G94 Feedrate per Minute Specifies feed rate in units per minute. Used for precise cutting and drilling at constant speeds.
G95 Feedrate per Revolution Specifies feed rate in units per revolution of the spindle. Useful for turning operations, especially when cutting threads.

 

 

What are M-Codes in CNC Machining Programming

M-codes are miscellaneous codes. These codes are employed to regulate secondary processes of CNC machines, such as starting and halting, the tool change, the on and off of the supply of coolants, etc. Through M-codes, there can be much control and maneuverability with CNC programming so that the machine runs seamlessly across different operations.

 

Common M-codes used in CNC programming

Here are some common types of M-codes used in CNC programming:

M01: Optional Stop

This code is similar to M00. However, the program only stops if the "optional stop" switch is on. The MO1 code helps permit some program stops while making others optional.

M02: End of Program

The M02 code indicates the program has ended. It instructs the CNC machine to halt and go back to the initial position of operation. The machine will be set up and running again right from the first program line when it is restarted.

M03: Spindle ON ( Clockwise)

When the M03 code is entered into a CNC machine, it brings the spindle in a clockwise direction. A setup is required before starting most cutting or drilling operations in a specific area.

M04: Marked as Spindle On: It describes the counter-clockwise spindle rotation to be followed during the process execution. Moreover, the code is used to rotate the spindle and may be needed for certain tools and operations.

M05: Spindle Stop

M05 is used to stop the spindle and therefore ceases the rotation. This is applied whenever a cutting operation is over or when using a different tool.

M06: Tool Change

The M06 code instructs the CNC machine to go for an automatic tool change. In addition, it allows the machine to get to the tool changer and replace the current tool with another specified tool for the next operation.

M08: Coolant On

M08 turns on the coolant system. During the cutting process, coolant is applied to the workpiece or the tool to minimize heat generation and transfer.

M09: Coolant Off

M09 turns off the coolant system by stopping the circulation of coolant when it is not needed or before a change of tool.

M30: Program End and Reset

M30 is similar to M02, which informs that the program has ended, but, in addition to this, it returns the CNC machine to its zero position, for the next process.

 

Key M-Codes in CNC Programming and Their Functions

M-Code Function Description Typical Application
M00 Program Stop Halts the program at the current position. Allows operators to inspect or intervene manually during a cycle.
M01 Optional Stop Stops the program only if the optional stop switch is enabled. Used for temporary stops during specific operations.
M02 End of Program Marks the end of the program and stops the machine. Used at the end of a program to conclude the machining operation.
M03 Spindle On (Clockwise) Starts the spindle in a clockwise rotation. Required for most cutting or drilling operations.
M04 Spindle On (Counterclockwise) Starts the spindle in a counterclockwise rotation. Used for specific tools that require counterclockwise rotation.
M05 Spindle Stop Stops the spindle rotation. Used to halt the spindle after cutting or tool changes.
M06 Tool Change Commands the machine to perform an automatic tool change. Essential for changing tools between different operations.
M08 Coolant On Turns on the coolant system during machining. Keeps tools cool during machining to reduce heat and wear.
M09 Coolant Off Turns off the coolant system after machining. Used when coolant is no longer required.
M30 Program End and Reset Ends the program and resets the machine to its home position. Used to end the program and prepare for

 

Comparing G-Codes and M-Codes in CNC Programming

Here's a detailed comparison between the two:

Functionality

G-Codes: Default G-codes mainly determine the movement and the CNC machine's direction. These define the motions on various planes (X, Y, Z) and regulate the operational tool. G-codes control the processes, which include straight movements, drilling, and rates of motion.

M-Codes: In contrast, M-codes provide control over additional machine functions. Such include initiating or shutting down the spindle, turning on coolant, changing tools, and interrupting the program. Although M-codes are not used to control the movement, they are responsible for important 'off-cutting' functions.

 

Types of Operations

G-Codes:

Motion Control: These are G0 (fast trace) and G1 (linear move), which directly determine the path and CNC machine speed.

Drilling Cycles: Some codes, such as G81 (drilling cycle) and G82 (drill with dwell), are reserved for use in hole-making operations.

Coordinate Control: Some common codes include G9,0, which means absolute positioning, and G91 for incremental positioning of the coordinate system.

 

M-Codes:

Spindle Control: M-codes like M03 up to spindle on in the clockwise direction and M05 stop spindle.

Coolant Control: M08 and M09 are used to control the coolant to cool the tools and to wash the chips.

Tool Change: M06 is used to indicate a tool change in the machines that support the process.

 

Intended Use

G-Codes: G-codes are much more complicated in a CNC program. They are usually required to be fine-tuned for a particular machine, as well as dependent on the typical process being run. For example, the G-code for CNC milling is entirely different from the G-code for turning or the G-code for drilling, each of which entails requesting the machine to make a certain number of moves in a specific manner.

M-Codes: M-codes are easier and usually do not need fine-tuning. Because they operate steady functions such as halting the spindle or toggling the coolant on or off. They are mostly implemented at certain time instances within the program. For example, after the execution of a machining cycle or before a tool is changed.

 

Execution

G-Codes: G-codes are normally called from the initial stage up to the final stage. These codes influence the way the material is to be cut or formed and are therefore critical to the determination of the workpiece geometry.

M-Codes: Common M-codes are executed at specific intervals during the machining process to support operations. For example, M06 will cause the machine to perform a tool change only at a specific time, while M03 or M05 will perform spindle control.

 

GreatCNC's Expertise with G-Codes and M-Codes

We at GreatCNC have had considerable practice in the G-code and M-code used in controlling the CNC machines. More so, our professional staff programs the movements and the cutting of the material using G-codes that will afford a higher accuracy in offering complex designs. We also use M-codes to control other activities that are not cutting, like coolant flow and tool change, to improve the performance of the machine. By properly applying these two codes, GreatCNC can give efficient and precise machining services that meet the needs of each project, and provide the best and fastest service to all its customers.