For CNC machining, programmers use G and M codes, depending on what they want the machine to do.
While the most common programming language used by CNC software is G code, M code also has specific applications. When it comes to G code versus M code, one isn’t necessarily better than the other. Instead, both types are needed to have the system perform the desired operations.
The Role of Machine Programming in CNC Manufacturing
The C in CNC programming refers to the computer operating the system.
As with any computer, it operates based on a specific language the system understands to complete certain tasks. This operational language requirement is where instructions come into CNC programming. Operators may choose to input the programs manually or use CAM software.
Regardless of the method used for inputting the information, the codes need to be perfect.
Before computers, maintaining the correct programs for multiple machinings proved difficult due to the paper format of the NC program.
In the early days of machining, the devices were numerical controlled, NC. These required cards or tape with the codes created by punching holes into a specific order. Before computers became common, machine shops regularly used punch tape to control their devices. However, the physical medium would sustain severe damage in the active shops.
Grease stains, tears and loss all occurred regularly to these punched papers. Damaged papers would cause the NC machines to read the wrong codes, resulting in serious problems.
When computers came into use for operating NC machines, the operator would input the codes manually into the computer instead of feeding the punched tape into the system.
With CNC, computer numerical controlled, programming, these machines could produce much more intricate parts due to the use of more sophisticated machinery with them.
This advancement, however, posed a problem for the machinists. With increasingly complex machines capable of working on up to five axes, manually inputting the program required too much time.
To expedite the process, companies began using CAM or CAD software.
CAM, computer-aided manufacturing, or CAD, computer-aided design, is the software that automates the process of code generation. The machinist tells the software what to do, and the program creates the codes for the machine to understand.
Whether put into the system manually or generated by software, programs for CNC machining include G codes and M codes. Both have distinct functions, and neither can work independently of the other.
Understanding their differences is the foundation for CNC programming.
What Are G and M Codes in CNC Programming?
In CNC machining, G codes and M codes serve a critical role and function:
What is G Code?
A G code in CNC programming controls the movements of a machine, dictating how and where a machine should move to fabricate a part.
What is M Code?
An M code in CNC programming controls miscellaneous machine functions, including starting and stopping specific actions or programs.
These two codes work together to manufacture parts and components, making them essential in the manufacturing and industrial sectors.
CNC G Code
The original program language used by NC machines was G code.
G stands for geometry, and it’s the alphanumeric format the system uses to tell the parts what to do. It indicates where to start, how to move and when to stop. For machinists, the problem with using G codes is different machines use variations on a standard set of alphanumeric numbers. So, the system that tells one device an action may indicate something different on another piece of equipment.
Differences in G code among machines lie in the number of zeros between the letter and number or the presence of spaces between commands.
For instance, one device may use the generic G1 while another may use G01. Some machines require decimal points between pieces of information while others allow the operator to omit these. Because these differences in program dialect are so critical in executing the right program, operators must always know about the language of the machine they are using.
The Functions of CNC Machining G Code
As noted previously, G codes give the machine directions on how and where to move.
Different letters dictate how the machine moves and numbers set the parameters. While the system is called G code, G is not the only letter used. Other letters have different functions in the program, which tend to be the same, regardless of a machine’s dialect.
For example, the functions of the following letters in G code include:
- N: The N value gives the line number.
- G: The G number tells when the tool to move and stop.
- X, Y, Z: These values indicate the position of the tool in three dimensions — X for horizontal, Y for vertical and Z for depth.
- F: F relates to how quickly the machine feeds the piece.
- S: S tells the machine how quickly to move the spindle.
- T: The T tells the machine which tool to use.
- M: M values give miscellaneous functions or machine functions, telling the device how to act by giving on and off directions.
- I and J: These values indicate the incremental center of any arcs the machine makes.
- R: R gives the radius of arcs made by the machine.
- A: A values direct the tool around the x-axis.
- B: The number with B shows the rotational value around the y-axis.
- C: An auxiliary axis that rotates around the z-axis gets its positional value from the C value.
- D: This number shows how much the system offsets the tool diameter.
- L: L gives the system a value to indicate repeating operations and how many times they need to loop.
- P: The program jumps in time or a delayed time with the P command.
Other alphanumeric designations depend on the machine’s capabilities.
For four or five-axis devices, the G code may include extra coordinate directions.
Behind every G code are directions telling a machine how to create variations on its essential functions. Generally, CNC machines have three operations, and the systems show how to adapt those tasks to create a finished product.
These three operations include the following:
- Move in a straight line as quickly as possible.
- Move in a straight line at a specified rate.
- Move in a circle at a specified speed.
From these three movements, the program can tell the machine when to stop, change the rate, create circular cuts and other directions. Through complex programs, these simple directions to the device can create intricate products.
One part of choosing values for a program requires knowing about the machine’s responses to the program.
Generally, program directions tell the machining equipment to begin doing something. The device will continue doing that until it receives a directive to stop. Machinists have to know when to input computer instructions to halt as well as to start.
Machinists may not need to put in every line of information required, though.
Some shortcuts also exist to help, like canned cycles.
In addition to individual actions, operators have the option of using canned cycles. These cycles have a single alphanumeric combination that dictates a series of steps. For instance, one cycle code can replace four lines of the program to indicate drilling or boring. These canned cycles help programmers when they need to write out the actions a machine must take.
Understanding the functions of G code makes reading operation directions easier when you see examples of them. Even if you’re not fluent in CNC machine programming, you can still recognize the importance of the alphanumeric system and the complexity of the program required to create a machined product.
Examples of G Code for CNC Machining
While the codes used to depend on the machine, some basic letter and number combinations create generic directions. Each group of commands should only have a single G code to keep the device from getting confused. Should the operator require another G code, he will need another block of data, like in the following example:
- G0: Go as fast as possible to a specified coordinate.
- G1: Move in a straight line.
- G2: Create an arc in a clockwise direction.
- G3: Make a counterclockwise arc.
Though machinists once had to memorize these commands during their schooling, today many don’t because the commands they use depend on the type of machine. Additionally, machinists typically input G directions so often that they quickly memorize them. Should they need a reminder, though, most CNC devices have an accompanying programming book that indicates the exact dialect the machinery requires.
While G codes give the machine instructions on where to move, M values direct the device to do miscellaneous tasks.
The Most Important G Codes
All of the G codes have convenient uses and vital functions. Some of the most frequently used G Codes are:
G00: Rapid Positioning
The G00 positioning command rapidly moves the machine from its current position to the specified coordinates of the command. All axes move at once, so they simultaneously complete the action. The movement to the new point is a straight line.
G01: Linear Interpolation
The G01 command moves the machine in a straight line at your designated feed rate, which you can set with the F function. You must have your project’s X, Y and Z values before beginning this code to create a straight line. Once you’ve entered the code, the machine gets to work calculating these points to make the straight line.
G02: Circular Interpolation Clockwise
The G02 command moves the machine clockwise. For it to work properly, you must define the center of rotation, which is a similar measurement to the radius of a circle with a center point and the starting point of the arc. You must also define the endpoint parameters. The starting point is simply the ending point of the previous command, so it is easy to determine.
G03: Circular Interpolation Counterclockwise
The G03 command also moves the machine in a circular motion but counterclockwise instead. This command is similar to G02.
How to Read G Codes
When reviewing a G code file, break down its lengthy, complicated contents into the Cartesian coordinates and markers. Try looking for the codes you are most familiar with and use a key to remind yourself of codes you have forgotten.
Each string of numbers has an order, and the G codes follow a careful structure to carry out their functions properly.
CNC M Code
M code is the machine control language that controls the overall program, often called G code. While G commands describe positions, M code directs the machine’s actions. While M represents miscellaneous codes, some refer to it as machine code because it controls particular operations of the equipment.
Both G and M codes have specialized uses in programming a CNC machine.
Without M codes, you may direct tools to a spot and tell them to move in a line or arc, but you won’t have any success giving more direct commands, like to stop programs, change tools, flood the system with coolant or other actions beyond the three main movements G codes control. That’s why you can think of M codes as directing non-cutting operations.
The Functions of CNC Machining M Code
M codes give the machine instruction on when to turn on or off operations not involved with cutting. For specific processes, for example, the machine may need to flood the system with coolant and shut it off after the temperature drops. These are two separate actions in M code.
As with G codes, the dialect used for M codes varies by machine. When setting up programs, M codes should have one per block of information. These commands give on and off functions to the machine, so having multiple commands in a single group or block could cause program problems.
Using M commands allows the machinist to tell the device to change tools, turn on the spindle, send coolant to the system or open the doors. Dozens of M operations exist, and operators learn how to program to execute desired actions fast.
As with G codes, M codes depend on the machine.
Some machines require a zero between the M and the number while others omit this requirement. Again, knowing the specific requirements detailed in the machine’s coding instructions will help the operator understand which alphanumeric programs to use.
Examples of M Code for CNC Machining
M commands have generic values that include the following:
- M00: Stop whatever program is currently operating — axes, coolant flooding or spindle.
- M01: This command indicates an optional stop for the program.
- M02: End of the currently running program.
- M03: Begin to move the spindle clockwise.
- M04: Start moving the spindle counterclockwise or in reverse.
- M05: Stop moving the spindle.
- M06: Load on a different tool. Typically, this command comes with the T value. For example, you may see “T1 M06.”
- M08: Start flooding the system with coolant.
- M09: Stop the coolant flow.
These examples are only some of the M values you may see on a program to run a CNC machine.
The Most Important M Codes
Some of the most commonly used M codes are:
M00: Program Stop
The M00 command will terminate the current program, allowing you to switch equipment or run troubleshooting tasks.
M02: End of Program
The M02 command ends the program entirely. Ending your machine’s current program is the first step to beginning another.
M03: Spindle Start
The M03 command moves the spindle forward in a clockwise rotation. This command might seem similar to the function of the G02 command, but G codes move only the machine.
M04: Spindle Start
The M04 command moves the spindle in the reverse direction of the M03 command, taking it in a counterclockwise rotation. This is a necessary maneuver in many computer-aided manufacturing tasks.
M05: Spindle Stop
The M05 command stops the spindle so you can examine the current state of your work.
M06: Tool Change
The M06 command allows you to switch out your tools without damaging the equipment.
How to Read M Codes
M codes, or miscellaneous codes, are often supplemented by codes from other categories to activate the machine’s ideal settings. This makes lines with M codes look complex, although they are structured in a specific order to keep processes running smoothly. An S code can set the spindle speed as it is moved with an M code, for example. M codes are often used with G codes to operate a machine effectively.
The Main Difference Between G Code and M Code
G and M codes have particular, yet different uses for controlling CNC machines.
Some refer to the CNC program as G code, though the numbers accompanying G direct only the motion and function of the system. The M value is a part of the program that tells the machine how to do operations outside movements.
When looking at how these two types of commands work with a machine, think of G as for cutting and M as for miscellaneous operations.
While both control the equipment, they regulate different movements.
G commands tell the tool where and how to cut. M operations direct the machine to change tools, move the spindle or do other non-cutting tasks inside the equipment. If you want to make the process easier, you may consider CAD or CAM software, but this is not always a perfect solution.
Even if you have a CAD or CAM software program to change the directions you give to an alphanumeric program, you should still be familiar with CNC programming to make changes or amend the software based on the machine’s required input. The software will only do as much as the operator tells it to, and if you don’t understand the program it generates, you could have the machine execute an incorrect function, resulting in wasted time and material.
For some companies, having a professional source for CNC machining, like American Micro Industries Inc., alleviates the problem of creating programs on CNC machines.
Let Us Handle the Coding for Your CNC Machining Needs
You don’t need to know CNC code programming to get your machined project done quickly.
At American Micro Industries Inc., we have all the equipment, materials and experts available to get your machining project done fast. Don’t worry about the size of your job. Our experts can program tasks for small prototype orders or large bulk orders. Plus, our facility can handle even high-volume machining orders.
If you want to learn more about how we complete your machining projects, contact us online for a quote.