Computer numerical control (CNC) machining, whether CNC milling, CNC lathe, or some other computer-controlled machine tool, has become the primary manufacturing process in modern machine shops. These wonders of technology can transform a rectangular workpiece into a complex and sophisticated finished product as if by magic.
However, it isn’t magic at work here because these beautiful machines sit uselessly until someone (or something) tells them what to do. And CNC programmers and machinists give the machines instructions through programming language that brings them to life and directs them to perform specific operations.
So, all these complicated processes cannot happen without CNC milling machines and lathes. And these machines cannot function until a computer and CNC programming tells them what to do in a language they understand. And we call that language G-Code.
What Does G-Code Stand for in CNC Machining?
The G in G-Code stands for geometry, giving the CNC machine directions on how and where to move. Although the system is called G-Code, other letters and numbers will designate various functions within the program. However, every G code contains directions informing the machine how to create variations of its essential functions. Typically, CNC machines have three basic operations that the system adapts to create the desired shapes:
- Move in a straight line rapidly
- Move in a straight line at a specific feed rate
- Move in a circle at a specified feed rate
What are the Fundamental G-Codes?
Here are four of the most common G-codes and how they look in a G-code program:
- G00 – Rapid positioning to a specific XY plane
- G01 – Linear feed move
- G02 – Clockwise circular interpolation move
- G03 – Counterclockwise (CCW) circular interpolation move
As you can see, the lines of code are organized in blocks, with each block controlling one operation, such as a milling operation with a specific cutting tool. Each line of a block is labeled with a line number, typically the letter N and an even number (N2, N4, N6, etc.).
If you are writing a G-code program manually, you must do it correctly. Otherwise, one miswritten G-code could crash your workpiece and tooling. Fortunately, computer-aided manufacturing software is available to optimize tooling paths and generate G-code, offering instructions to the CNC machine tool. CAM software can create thousands of lines of code, so you don’t have to do it by hand.
How Many G-Codes are There?
Although we have access to about one hundred G-code commands to control CNC machines, there are a select few we call a “core group,” and they are familiar to practically every machine.
G00 Rapid Travel
Use this command for a rapid move when the tool does not touch the part. Use it only when the cutter or tool is not removing material, such as when you’re going for a tool change. When using a rapid move, be sure no clamps, parts, or vises are in the path. You can avoid a crash by moving to your XY plane selection in rapid first and then down the Z-axis next.
G01 Linear Interpolation
Use the G01 command to cut in a straight line, although it requires a feed rate command (F) before it will move. For example, the programmer can designate a starting point and move the cutting tool in a straight line along the X-axis or Y-axis to the endpoint.
G02 and G03 Circular Interpolation
G02 is used to machine an arc or radius in a clockwise direction, and G03 is counterclockwise. Remember that when you use G02 with G01 and G03, you can machine any shape, making these three G-codes the foundation of G-code programming.
G04 – Dwell
Occasionally, we must pause the cutting tool by inserting a G04 into the code for a short time. For example, you can add a dwell when a flat-bottom drill reaches the bottom of the hole, pausing the Z-Axis movement briefly, with the drill running, to clear up any chatter.
G40, G41, and G42 Cutter Compensation
Cutter compensation considers the diameter of the tool when the programmer creates a cutting path, offsetting the tool’s radius, depending on the cut’s direction. Here are the three G-codes controlling it:
- G40 Cancels cutter compensation
- G41 Left cutter compensation
- G42 Right cutter compensation
G43 Tool Length Compensation
Tool length compensation enables the CNC machine to account for the length of each of your tools entered and stored in the machine. The machine calculates the changes based on the program locations and the tool length. If a G43 code is inactive, the machine will move the end of the spindle instead of the end of the cutting tool.
Canned cycles allow the programmer to write G-code to drill and bore multiple holes with fewer lines of G-code. All the information is on a single line, with the following lines being positional. These are the cycles:
- G80 Cycle
- G81 Drilling Cycle
- G82 Counter Bore Cycle
- G83 Peck Drilling Cycle
- G84 Rigid Tapping Cycle
- G85 Boring Cycle (bore in, bore out)
- G86 Boring Cycle (bore in, rapid out)
Check out the comprehensive G-code list here.
What are M Codes in CNC Milling?
M-code is another machine control language for CNC machining used with G-code to switch various machine functions off and on. The M in M-code commands informs the machine that a miscellaneous function follows. While G-code commands indicate positions using the Cartesian coordinate system, M-code directs the machine’s actions. Although M represents miscellaneous functions, some say it stands for “machine” code since it controls the operations of the machine tool.
G and M codes have specific uses in programming. For instance, you can direct a CNC machine’s cutting tools to a particular spot without M codes. Still, you can’t give it direct commands, such as an optional program stop, change cutting tools, turn on the flood coolant, or other actions beyond the three primary movements that G-codes control.
What are the Different M Codes?
M03 is a spindle on command and is typically preceded by an S code to set the spindle speed. The list of M-codes begins with M00 (program stop) and continues up to M99 (subprogram end), although every number is not assigned.
The following is a partial list of M-codes:
- M00 Program stop
- M01 Program stop–optional
- M02 End of program
- M03 Spindle start–forward, of clockwise rotation
- M04 Spindle start–reverse or counterclockwise rotation
- M05 Spindle stop
- M06 Tool change
To see a list of M-codes for milling machines and lathes, check here:
What are the Differences between G and M Codes?
Three fundamental ways differentiate G and M codes from one another.
- G-code directs the motion and function of the CNC machine, while M-code controls the operations not involving movements.
- G-code activates the CNC machine, and M-code activates the machine’s programmable logic controller.
- G-code commands often differ in CNC machines, while most M-code commands remain the same.
CNC machines have transformed the manufacturing industry and simplified the process of producing precise and consistent parts. However, these machine tools would not know what to do in the absence of G-code and M-code.
In essence, G-Code instructs the machine how to perform its operations, and M-code carries out the non-geometric activities of a machine. G-code and M-code must work together if the CNC machine is to function correctly.