What are G-Code and M-Code in CNC Machining? An Overview

by | Nov 19, 2021 | CNC Machining

Computer numerical control (CNC) machines are among today’s most accurate, precise, and widely used machining technology. They eliminate human error by relying on computer programs like G-code and M-codes to fabricate parts.

G-codes and M-codes are simply software programming languages (or computer instructions) that dictate the motion of the CNC cutting tools to create the desired part. These programs can be complex to implement since they vary from machine to machine and require the expertise of a skilled CNC programmer. The basics, however, are much more straightforward than you think and, for the most part, follow an industry standard.

In this article, we will cover the basics of G-codes and M-codes in CNC machining. You will have enough information to create simple machining programs to fabricate your parts by the end of this read.

What is the Difference Between G-Code and M-Code?

CNC machine monitor display with program code

G-code, also known as geometric code, controls the machine’s motion. For example, it might direct the machine to move in a line or an arc. A G-code command is usually written in an alphanumeric format, starting with “G” and followed by a two-digit number (for example, G00).

In contrast, M-codes control the machine or miscellaneous functions of the CNC machine, such as spindle rotation, pallet change, and telling the tools when to operate or cease operation. M-codes are also written in alphanumeric format, beginning with “M” and followed by two digits.

G-code and M-code commands work together to create your desired part. In the following sections, we will present some G- and M-code commands you should know.

Understanding G-code Commands

Table 1 lists some of the standard G-code commands and their functions.

Table 1. G-code commands and their functions

These standard G-code commands are followed by a geometric location, usually identified using the cartesian coordinate system. So, for example, let’s say you want to rapidly move the machine to the coordinate location (X = -100, Y = -50, Z= 35) without cutting any material. You could use the G00 rapid position command to achieve this by writing G00X-100.Y-50.Z35.

Understanding M-code Commands

As we have mentioned earlier, M-code controls the machine functions of CNC machining. Table 2 presents some of the common M-code commands and their functions.

Table 2. M-code commands and their functions

Still considering the previous example, let’s say you want your spindle to start rotating immediately after your machine is located at (X = -100, Y = -50, Z= 35). You can simply include the M03 spindle command by writing G00X-100.Y-50.Z35M03. However, keep in mind that you also need to specify the speed at which you want your spindle to rotate.

To implement this, you need to understand some additional CNC programming commands that don’t start with the alphabets “G” and “M.”

Additional Commands for CNC Programming

CNC Operator programming lathe machine

There also exist some additional commands useful for CNC programming. For example, the command, F, allows you to control the speed at which the machine moves or feeds the piece. Likewise, the command “T” tells the machine which tool to use to achieve the desired cutting operation. Finally, the S command allows you to specify spindle speed.

To help better understand how to implement these additional commands, let’s consider a scenario where you’re looking to move the CNC machine to the coordinate location (X = -100, Y = -50, Z = 35) in a straight line and at a feed rate of 10 inches per minute. You could use the G01 (or linear feed move) command to achieve this by writing G01X-100.Y-50.Z35.F10.

Similarly, let’s say you want your spindle to rotate clockwise at a speed of 3000 rpm. To achieve this, you could implement the M03 (start moving the spindle) command and S (spindle speed) command by writing G00X-100.Y-50.Z35M03S3000. Keep in mind that this spindle rotation begins only after the CNC milling machine is located at (X = -100, Y = -50, Z= 35).

CNC Programming with G-code and M-code: Gensun Can Help

Now that you understand the basics of CNC programming using g-codes and m-codes, you can proceed to fabricate any part using CNC machining, right? Wrong! The success of your machining project primarily depends on the machine shops and machinists you work with. You want to work with a machine shop (or machinist) that is knowledgeable about GD&T (Geometric Dimension and Tolerancing) and computer-aided manufacturing (CAM).

Gensun Precision Machining is a leading provider of CNC machining services across Asia. We have a team of highly experienced CNC machinists, programmers, and engineers capable of reviewing your 3D CAD model and fabricating your parts accurately through CNC programming and machining.

Learn more about our CNC machining services.

Make Your Machined Parts With Us

Learn about our CNC milling and turning services.

You Might Also Like…