G-code is the programming language used to control CNC machinery. A program is a sequence of codes and data that tells the machine what to do. The programmed codes, along with the right tooling in a CNC machine center, allow for correct and repeatable part manufacturing. G-code makes CNC machines consistent to program and flexible, able to produce different parts.
As a CNC machine runs, the code is displayed on the operator’s monitor and is seen scrolling along, being read and acted upon by the machine.
G-Code — The Master of Action
Simply put, G-code is a software programming language used to control a CNC machine. And, even with complex CNC machining, G-code is written in a straightforward, logical way.
A “G” is followed by a number, which is a command to change geometry. For instance, “G00” is a command for rapid movement. It moves the tool to part geometry at the maximum rate and is generally used to move the tool and part from near to far or vice versa.
Here are some examples of G-code:
G00: Rapid move to specific coordinate position
G01: Linear feed move
G02/G03: Clockwise/counterclockwise feed move
These codes, such as G00, are followed by a geometric location. The geometry is conveyed with coordinates on axes, so an “X” coordinate and a “Y” coordinate on a plane.
Let’s break down “G00G58X-120.Y-5.” You know the “G00” means a rapid move. “G58” specifies work coordinates, changing geometry relative to work offsets, like part origin. “X-120.” means to go to “-120mm” on the X axis, and “Y-5.” means to go to “-5mm” on the Y axis.
G-code can repeat an action indefinitely until it is stopped. For example, G01 followed by a sequence of X, Y, or Z positions, means all linear feed moves occur at G01. A typical machining example is face milling, where the sequence of positions is used to machine the part’s face contour.
The X and Y axes position the spindle precisely to perform a cut or other function.
M-Code — The Organizer of Functions
M-code controls miscellaneous functions, and typically these can be thought of as non-geometry machine functions. Examples include spindle rotation start and stop, coolant on and off, pallet change, etc. Some of these functions may vary by the specific machine.
Here are some examples of M-code:
- M00: Stop whatever function the program is currently doing
- M03: Rotate spindle clockwise
- M08/M09: Flood coolant on/off
Similar to G-code, M-code consists of a “M” and a number. Unlike G-code, however, M-code can be more specific for different machines and is more customizable. Both types are needed to have a CNC machine perform its functions.
Like any language, CNC has many other aspects to it. This chart is just a sampling of all the program lines that could be included in the code.
|N102M6T2||Block number 102, Tool change to tool 2|
|(3" FACEMILL)||Comment - not read by machine, but tells operator engineer tool 2 is a 3 inch facemill|
|M82||Pallet A Check|
|IF[#10002+#11002LT150.]GOTO2001||Tool height min check|
|IF[#10002+#11002GT154.]GOTO2002||Tool height max check|
|G00G90B267.||Rapid, absolute positioning, Pallet rotates to B 267 (degrees)|
Some examples of code programming lines and what they mean. Anything in parentheses is not read by the machine. These are comments for the programmer and operator.
Other Commands. Other Machines.
In addition to G- and M-codes and the X/Y/Z axes positioning, other commands are used in CNC machining. An M03 spindle rotation start command needs an “S” command, which controls spindle speed in rpm.
Here’s how that gets added to our current code sample: G00G58X-120.Y-5.M03S3820.
The “S3820” indicates a spindle speed of 3820 rpm. So far, the program has set the geometry and the spindle speed.
Other possible functions that could follow G-code:
F: Feed rate
T: Tool number
H: Tool length offset call up
Many other alphanumeric designations can be used, depending on the CNC machine’s capabilities.
Keep in mind, the specific G-code and M-code examples we’ve explored are based on horizontal and vertical machining center programs. CNC lathes use G-code and M-Code too, but you can expect some different commands and sequences for turning.
The Power of Software
While machines are frequently programmed directly with G-code, a CNC programmer often uses a high-level computer aided manufacturing (CAM) programming tool.
Mastercam is a code-producing software that helps programmers create the right code for the project. Standard in the CNC industry, Mastercam takes input from the programmer (everything it needs to do), and it produces the right G- and M-codes.
Here’s a simplified example of how Mastercam works. The programmer imports a machine model and the machining fixture. Then, the programmer selects the tools and the tooling paths of where the spindle will go, and the code is produced by the software. The programmer works with the CAD models, but refers to the print for the exact specifications and tolerances.
As powerful as Mastercam is, it could require two weeks of programming if the part is complex (large size and many tools needed), yet it could only take two hours for a simple part. Obviously, a 3-tool job is simple compared to a 100-tool job.
Mastercam software shows a visual of the entire project; at left are folders containing the code needed to perform various functions.
Who Needs to Know G-Code and M-Code?
Depending on your job responsibilities, maybe reading this article is all you need to know about CNC programming using G-code and M-code. Or, you may need to dig deeper to properly execute your daily duties.
Within a CNC machine shop, knowing something about code is helpful for many positions. Shops with a good internal training program will explain the basics of coding to new employees during the orientation process. Others may include it as part of on-the-floor training for operators.
Depending on the part and process, the CNC machine center and program run repeatedly with limited interaction. The operator loads, unloads, inspects, deburs parts, and packages the part.
In a perfect world, once the correct CNC code is programmed, it’s done, and no edits are needed on the floor. Many projects, however, require slight adjustments, like adjusting work offsets and tool wear.
Machine controls are locked to prevent accidental changes. Editing is possible on the shop floor, but it requires a key to enable editing. Editing is mostly reserved for experienced engineers, setup pros, and supervisors.
So, you’ve cracked the code! Well, at least you know more about G-code and M-code and some other related commands. Maybe you want to learn more about CNC tooling practices such as … What tool is needed? When should a shop invest in tools? How do you implement new tooling solutions?
It’s all answered in our guide: CNC Tooling Improvements and Partnerships: A Guide to Solving Specific Tooling Issues. Plus, read four real-life cutting tool improvement examples from Stecker’s shop. Click the image below to get your copy!