Skip to content

What Coding Language Is Used on CNC Machines? 

machine cutting metal

For anyone wondering whether or not they should control their CNC machines with hand-written code or with CAM-generated code, the first thing to do is to learn all about that coding language. Understanding what it is, how it’s made, and how to read it will make that decision easier.

CNC Machining 101

Generating G-code can be done manually or with CAM software. Taking a part from idea to completion usually follows this path:

CAD software or a programmer creates a 3D model à CAM software OR a programmer interprets the CAD model, optimizes and applies toolpaths, and writes the G-code for the CNC machine à CNC machine uses G-code to direct its cutting.

The vast majority of shops have done away with manually writing G-code. Most manufacturing is automated by computers by way of CAD/CAM software these days, speeding the process and eliminating most of the opportunity for human error. A look at what G-code really is will explain why.

What is G-code?

G-code, simply put, is the language that CNC machines understand. In this piece, we refer to all of this language as G-code, although it can be broken down into smaller pieces according to function: G, M, F, S, and T. Each letter differentiates different commands for the machining process:

  • G: machine motion
  • M: miscellaneous machine action
  • F: feeds
  • S: speeds (in reference to spindle)
  • T: tools

This letter will be followed by a number that pairs with a specific command. For example, S400 would translate to “400 spindle revolutions per minute” while M06 would signal a tool change. The most important commands are usually G commands. You can find the most commonly used G commands here: G-Code Tutor.

The lines of code are organized into blocks, labeled with N and then a number (usually even) to count them out. Each block translates to one machining operation. If you are looking for a specific roughing operation, for example, knowing that its label is N14 will save you time skimming through code. Most parts will require thousands of lines of code, so understanding its labeling system is crucial.

Why is it important to know G-code?

You know already that understanding how G-code is labeled in blocks can be useful when searching for operations. It can also help to simplify and clarify the lines of code, too. Programmers who can “speak” G-code are able to insert comments into the code without affecting how the machine reads it. These comments can help identify motion, label coordinates, or even give reminders. To add a comment between two commands, use parentheses. To add one at the end of a line, use a semicolon.

Even for machinists who won’t be altering the code at all, it’s helpful to understand it. This way everyone on the team can help troubleshoot and optimize machining. Being able to skim through and quickly find specific blocks is vital when time is of the essence, especially for parts with longer or more complex machining processes.

Try Mastercam for Yourself

Give Mastercam a try with our free Learning Edition demo. You can get your download here.