The acronym CNC in the term CNC machine stands for Computer Numerical Control. When we think of CNC machining, we usually imagine CNC machine tools, but modern CNC machines actually represent a broader term. Industrial robots and some computer-controlled measuring machines are also CNC machines.
Due to the need for precise machining of parts in the aviation industry, John T. Parsons developed the first CNC milling machine in 1952 in collaboration with IMB (International Business Machines Corporation) and MIT (Massachusetts Institute of Technology). From then on, smarter industrial production spread and CNC machining became available to all industries. At the same time, CNC programming languages – G-Code and M-Code – were introduced and used.
What are G Code and M Code?
Over the 70-year history of CNC machine development, several programming languages have been used to program them, not just G-Code and M-Code. Thus, the pioneering Parsons machine was first programmed using drilling belts, and later in 1956, a special programming language (APT programming language) was developed for CNC machining.
RS-274 or more commonly known as G-Code, which is also discussed in this article, was developed in the same decade. G-Code was developed from the APT programming language, which contained data about the position or geometry of the cutter (tool) and other non-geometric machine functions such as spindle speed, cooling, etc. The RS-274 functions (G and M codes) adopt the fundamentals of the APT programming language with minor changes that make the programs shorter and more convenient for machine control.
ADEQUATE | G and M codes |
GO TO {X, Y, Z} | G1, G2, G3 {X, Y, Z} |
FEDRAT | F |
AXIS THE RIGHT WAY / Counterclockwise | P M3 / M4 |
COOLING ON/OFF | M8 M9 |
FINI | M30 |
The RS-274 programming language is often called “G code” because it mainly consists of G functions (G codes). However, it itself also contains equally important M functions (M codes). For this reason, it is necessary to define these terms, that is, to answer the following questions clearly and directly:
- What are G codes?
- What are M codes?
- What are the differences between Code G and Code M?
What are G codes?
The G code, that is, the G functions that differ from the M code, mainly refers to the definition of geometry. It is obvious that “G” in the name “G-Code” stands for the word “Geometry”. Thus, G codes would contain functions for selecting the coordinate system, work plane, programming units, programming mode configuration (absolute or incremental), tool movement in fast and work modes, etc.
What are M codes?
M codes or M functions refer to functions related to machine operation. It can be said that “M” in the term “M code” means the word “machine”. Normally, when programming CNC machines, the following functions can be included in these functions: turning the main spindle on or off, turning the coolant on or off, automatic tool change, etc.
What are the differences between Code G and Code M?
As we have already explained, strictly speaking, G codes refer to the geometry of the machining process of a part and M codes refer to individual machine functions. We could say that G codes show “how the tool moves in relation to the part” and M codes show “what the machine does in the machining process”.
In addition to the most important G-code and M-code functions in the RS-274 programming language, there are numerous additional functions:
List of basic G codes
Main functions |
Description |
Additional functions |
G0 |
Fast positioning (linear) |
X Y Z |
G1 |
Linear interpolation |
X, Y, Z, F |
G2 |
Clockwise circular interpolation |
X, Y, Z, I, J, K, F |
G3 |
Circular interpolation counterclockwise |
X, Y, Z, I, J, K, F |
EXAMPLE 1:
The phrase in the G code: G1 X10 Y50 F100
Explanation: The tool moves linearly to the desired point at a speed of 100 millimeters per second (coordinates: X=10 mm, Y=50 mm).
The phrase in the G code: G2 X10 Y50 I10 J40 F100
Explanation: The tool moves in a circle (clockwise) to the desired point (coordinates: X = 10 mm, Y = 50 mm) at a speed of 100 millimeters per second. Position of the center of the circle in relation to the starting point: +10 mm on the X axis and +40 mm on the Y axis.
The phrase in the G code: G02 X50 Y10 I40 J-10 F75
Explanation: The tool moves in a circle (clockwise) to the desired point (coordinates: X = 50 mm, Y = 10 mm) at a speed of 75 millimeters per second; Position of the center of the circle in relation to the starting point: +40 mm on the X axis and -10 mm on the Y axis.
Main function |
Description |
Additional functions |
G17 |
Select XY as work plane |
/ |
G18 |
Select ZX as work plane |
/ |
G19 |
Select YZ as work plane |
/ |
G20 |
Units: inch (inch) |
/ |
G21 |
Units: Millimeters (mm) |
/ |
G41 |
Tool radius compensation – left |
D, X, Y, Z |
G42 |
Tool radius compensation – right |
D, X, Y, Z |
G40 |
Tool radius compensation – disabled |
/ |
G43 |
Tool height deviation compensation |
H, Z |
G49 |
Tool height offset compensation – disabled |
/ |
G54 |
Machine coordinate system |
/ |
G55 |
Work coordinate systems |
/ |
G56 |
Work coordinate systems |
/ |
G57 |
Work coordinate systems |
/ |
G58 |
Work coordinate systems |
/ |
G59 |
Work coordinate systems |
/ |
EXAMPLE 2:
G code phrase: G21 G17 G55
Explanation: Set the programming units to millimeters; Select the XY work plane; Select G55 coordinate system
G-code phrase: G43 H1 Z100
Explanation: Set the tool height deviation compensation to H1 (“H1” is a position where the tool height value is stored in the memory of the CNC machine) and take a new position (coordinate: Z = 100mm)
G code phrase: G41 D1 X15
Explanation: Set the left tool radius compensation to D1 (“D1” is a position where the tool radius value is stored in the memory of the CNC machine) and take a new position (dimension: X = 15 mm); As the tool goes to the desired position, it slowly moves to the left, so tool radius compensation is completed when the tool reaches the desired position.
G code phrase: G40 G49
Explanation: Cancels tool radius compensation; Cancel Tool Height Deviation Compensation
Main function |
Description |
Additional functions |
G90 |
Absolute programming: coordinates relative to the work coordinate system |
/ |
G91 |
Incremental programming: coordinates relative to the i coordinate system at the current position |
/ |
List of basic M codes
Main function |
Description |
Additional functions |
M00 |
Program stop |
/ |
M03 |
Zone on (clockwise) |
s |
M04 |
Spindle on (counterclockwise) |
s |
M05 |
Axis off |
/ |
M06 |
Automation tool change |
T |
M08 |
refrigerator |
/ |
M09 |
Soda outside |
/ |
M30 |
End of program |
/ |
M98 |
Subprogram call |
“subprogram name” |
M99 |
Subprogram return |
/ |
How to program a CNC machine
CNC programming is the alphanumeric formatting of data (G and M codes) about the geometry and technology of the parts that need to be processed on such a machine, but also other programming that is normally carried out by machine manufacturers.
Programming CNC machines with G code is generally based on blocks, as can be seen from the previous examples where the explanation of G functions was done as an interpretation of the blocks of a program written in G code. There are several CNC programming methods, i.e. generation of G and M codes:
- Manual programming,
- Programming in operation and
- Programming with CAD (Computer-Aided Design)/CAM (Computer-Aided Manufacturing) systems.
Manual programming
In manual CNC programming, a programmer, based on his own knowledge and experience, as well as available information about machines, tools, machine accessories, processing modes and using programming instructions, manually writes the geometric and technological information necessary for filling of the alphanumeric program. Manual programming can be divided into the following basic activities:
Programming in operation
In order to constantly improve the CNC programming method and satisfy the desire to perform programming directly on the machine in order to further expand and simplify CNC applications, some manufacturers offer controls that allow programming directly on the machine support. Although the possibility of immediate programming on the machine in so-called MDI (Manual Data Input) mode has existed since the beginning of CNC application, on-the-fly programming represents a completely new programming method.
With this method, the support control has integrated additional functions that avoid classical programming with G-Code. Programming here takes place through an interactive dialogue between the operator and control via keyboard and graphical user interface, which can generate simple machining contours and typed machining cycles. The controller performs the necessary route calculations based on the queries entered and automatically generates the G-code.
Programming with CAD/CAM systems
The geometry of parts used, for example, in aerospace, military and other industries is very complex and has second-order surfaces that require multi-axis machining (3-axis, 4-axis and 5-axis CNC processing). Complex programs for processing such surfaces require several thousand blocks (phrases), which are very difficult and time-consuming to create manually. Therefore, it was necessary to use computers for technological preparation, that is, programming CNC machines for machining the above parts based on G code and M code.
CAD/CAM programming can be classified as computer-aided CNC programming. The integration of CAD and CAM has led to improvements in NC programming, particularly in terms of programming simplification. Now the programmer no longer needs to think about defining the geometry; CAD/CAM software takes care of this. What remains for the programmer is to add more technical information such as cables, bases, clamps, cutting tools and processing modes through active interaction with the system in the program. It should be noted that there is an integration of CAD/CAM and CAPP (Computer Aided Process Planning), which also develops a solution for these requirements.
This is the procedure for programming a machine in a CAD/CAM environment:
- modeling CAD models,
- Machining planning using available CAM strategies for machining and tool path calculation and retrieval of the CL (Cutter Location) file,
- Tool path simulation and material removal simulation for program verification and detection of possible errors and collisions,
- CL file post-processing and G-code recovery using the post-processor of the selected machine,
- Transfer the G code to the controller. Prepare tools and workpieces. Place the workpiece into the machine,
- Check processing conditions and modes, check speeds and accelerations that can significantly affect processing quality and
- Processing a workpiece on a machine.
University Degree
Although there are programming methods today that do not require knowledge of G and M functions (on-the-fly programming and CAD/CAM CNC programming), a programmer must know them because a special situation would sometimes require manual programming. The positive thing is that the programming language of G and M codes can be called universal, which means that with knowledge of G and M codes you can program any CNC machine, with minimal differences in syntax, which can be mastered quickly if you know the basics of G and M codes. Are you worried about finding a professional CNC programmer for your project? Don't worry, WayKen is a manufacturer with over 20 years of experience in the CNC machining industry, with advanced manufacturing technology and experienced CNC engineers ready to meet your product design needs. Get an instant quote today!