An Overview of G-Code and M-Code Used in CNC Machining

G-Code and M-Code Used in CNC Machining
Jack Lie CNC machining expert

Specialize in CNC milling , CNC turning, 3D printing, urethane casting, rapid tooling, injection molding, metal casting,

CNC machines, which employ computer numerical control, are among the most precise and popular machining tools available today. They use computer programs such as G-code and M-codes to manufacture components without human intervention.

The CNC cutting tools are moved in response to G-codes and M-codes, which are just computer instructions in a programming language. Since the specifications of each machine are unique, developing and deploying these programs may be challenging even for an accomplished CNC programmer. However, the fundamentals are easier to grasp than you may imagine and generally adhere to a common practice among experts.

To help you get started with CNC machining, this article will introduce you to G-codes and M-codes. By the time you finish this read, you’ll know how to write basic machining programs to make your components.

The Basics of G-Code and M-Code

What is G-Code: The Master of Action?

The most widely used language for controlling CNC machines is G code (also known as RS-274D). Since G denotes geometry in G code, most instructions begin with that letter. When producing a component, CNC machines are directed by G-code, which specifies their starting and stopping points as well as their path of motion.

However, machinists may find G code programming challenging due to the fact that various machines have varied requirements for how G codes should be formatted. The majority of machines only vary in how many spaces are used between commands and how many zeros are placed between letters and numbers. A machine may utilize G3, whereas another may choose G03. Machinists should constantly be well-versed in the technology they are operating. If the command is wrong, the manufacturing of the component might be severely disrupted.

While G is the most common letter in G codes CNC programming, other letters also mean different commands depending on the capabilities of the machine.

What is M-Code: The Organizer of Functions?

Code regulates a wide variety of non-geometric machine operations. Some of these functions are spindle rotation on/off, coolant on/off, pallet change, etc. The precise machine may have slight variations in some of these functions.

When configuring the CNC lettering program with G and M codes, keep in mind that each M code block should only include a single command. The reason is that their major function is to power the device on and off. This means that utilizing them more than once in the same block might lead to unexpected behavior in the code.

Even while each machine has its own unique set of G codes, M codes are likewise machine-specific. The zero seen between letter and number may be skipped in some machines but not in others.

What are the Differences?

Machinists employ both G and M codes in every CNC machining process, although they serve different purposes. Some ways in which they differ are highlighted below:

  • The majority of CNC machines have a tendency to use G code instructions that are different from one another, whereas M codes are more likely to be the same.
  • In order to control the machine’s motion and functioning, G codes are used, whereas M codes are used to control the operation’s external motions.
  • While the G code starts up the computer numerically controlled machine, the M code starts up the PLC (programmable logic controller).

Programming CNC with Additional Commands

There are also other supplementary commands that may be implemented in CNC programs. For instance, the letter F may be used to adjust the rate at which the machine feeds or moves the workpiece. Similarly, the letter T instructs the machine to utilize a certain cutting tool. Last but not least, you may set the spindle speed with the S command.

Let’s say you want to drive a CNC machine to the coordinates (X = -100, Y = -50, Z = 35) at a cutting speed (feed rate) of 10 inches per minute; this example will help you see how to use the additional instructions. Simply enter G01X-100.Y-50.Z35.F10 into your computer’s command prompt to make a linear feed move.

Say you need your spindle to turn clockwise at 3000 revolutions per minute (rpm). For this, you may use the M03 (spindle movement commencement) and S (spindle speed) commands by inputting G00X-100.Y-50.Z35M03S3000. Always remember that the CNC milling machine’s spindle won’t start turning until it is positioned at (X = -100, Y = -50, Z = 35).

How CNC Programming Controls CNC Machines

Modern machines and programs allow machine operators to simply provide instructions to the system. The machine-readable G codes and M codes will be generated by the software. Coding machine operations have become a breeze with the help of CAD and CAM programs.

A programmer is programming for a CNC machine.

In order to get started, the programmer requires sophisticated computer-aided software. Following this, the programmer brings the model of the machine and the manufacturing fixture into the program. He or she may also decide which tools will be used and which pathways the spindle will take when cutting. The necessary G and M codes for the machine’s operation are then generated by the software.

This is a brief overview of how computer numerical control (CNC) programs direct the actions of CNC machines. Although the steps outlined above appear easy enough, generating the codes for a complex component may take several weeks.

List of G Code and M Code Commands

In this part, we’ll look at several instances to better understand the meaning of the fundamental G and M codes.

G-Code List (Mill)

G-code CommandFunction
G00Rapid Motion
G01Linear Interpolation Motion
G02CW Interpolation Motion
G03CCW Interpolation Motion
G09Exact Stop
G10Programmable Offset Setting
G12CW Circular Pock Milling (Yasnac)
G13CCW Circular Pock Milling (Yasnac)
G17XY Plane Selection

G-Code List (Lathe)

G-code CommandFunction
G00Rapid Position Motion
G01Linear Interpolation Motion or Linear Motion,
Chamfer and Corner Rounding – Modal
G02CW Circulation Interpolation Motion – Modal
G03CCW Circular Interpolation Motion – Modal
G04Dwell (P) P=Seconds. Milliseconds
G05Fine Spindle Control Motion (Live Tooling) – Optional
G09Exact Stop
G10Programmable Offset Setting

M-Code List (Mill)

M-code CommandFunction
M00Program Stop
M01Optional Program Stop
M02Program End (Setting 39)
M03Spindle On, Clockwise (S) (Setting 144)
M04Spindle On, Counterclockwise (S) (Setting 144)
M05Spindle Stop
M06Tool Change (T) (Setting 42, 87, 155)
M08Coolant On (Setting 32)
M09Coolant Off
M104th Axis Brake On

M-Code List (Lathe)

M-code CommandFunction
M00Program Stop – Modal
M01Optional Program Stop – Modal
M02Program End – Modal
M03Spindle on Forward (S) – Modal
M04Spindle on Reverse (S) – Modal
M05Spindle Stop – Modal
M08Coolant On – Modal
M09Coolant Off – Modal
M10Chuck Clamp – Modal


CNC machining relies heavily on the use of CNC machines. Nevertheless, without G and M codes, which tell the machines what to perform, they would not be able to accomplish their jobs. It is crucial to the CNC machining process and the effective manufacturing of components that you understand how to establish these codes. Knowing these codes will put you ahead of the competition in the competitive field of CNC programming.

Turn to Runsom for CNC Machining Services

But only knowing the basis of CNC programming with G-code and M-code is far away enough. Working with a machine shop (or machinist) that is knowledgeable about GD&T (Geometric Dimension and Tolerancing) and computer-aided manufacturing (CAM) will greatly contribute to the success of your machining project. Need a machine shop (or machinist) to handle your CNC machining project?

Runsom is a leading manufacturer offering excellent CNC machining services. With us, you’ll get the premium services you crave at an affordable price with incredibly good quality. Our team of expert CNC machinists, engineers, and programmers is capable to review your 3D CAD model and accurately manufacture your parts through CNC programming and machining.

Other Articles You May be Interested in: