Time to read: 6 min

M06? G54? S1000? Do those numbers mean anything to you? Knowing the most common commands in your G-code may not be at the top of your priority list, but it can go a long way in helping you understand what your machine is doing. CNC machines are really good at doing exactly what they’re told. It’s your job to make sure you’re telling it to do the right things — and it’s hard to communicate effectively with a machine if you can’t speak its language.

What is G-code?

G-code is the simple programming language for CNC machines. It consists of lines of code that are organized in blocks. Each block controls one CNC machining operation, such as a cutting operation with a specific tool. Each line in a block is labeled with the letter N and a number. These line numbers are generally only even numbers (N2, N4, N6, etc).

a 5 axis cnc machine with the door open and the code loaded on the display screen
G-code programs get loaded into the CNC machine to control the machining operations

Since computers became smaller, cheaper to use, and more commonly available, G-code has been generated using CAM, or computer aided manufacturing. A CAM program accepts your 3D CAD model, tool selections, and some other information as inputs. It then optimizes the tools paths and spits out G-code to tell the CNC machine what to do. It’s possible to write G-code programs by hand (I did a short one in grad school!) but for complex machining operations requiring tens of thousands of lines of code, unless you’re a perfectionist masochist, you probably don’t want to. 

Even if you aren’t manually writing the blocks of code, you can add comments to the lines of code, which can be useful to identify the operation for yourself. This isn’t necessarily practical if you have a long program, but can be a useful addition at the beginning of each program to track the project or version. To add a comment in the middle of a line between two commands, just use parentheses. For example: 

G0 X8 (x-coordinate) Y13 (y-coordinate). 

You can also add comments to the end of lines using a semicolon: 

S100 F200; set speed and feed

Why is it important to know G-code?

If I know anything about engineers (and I definitely do, because I am one) it’s that we like to understand how things work. G-code is the language that tells a CNC machine what to do, so understanding that language is required to really understand how a CNC machine works. 

Plus, If you ever need to talk to the programmer, operator, or machinist you’re working with, it’s good to know what you’re talking about. Similarly, when discussing an issue and they start talking about the CAM program, you won’t be bogged down trying to figure out what they’re saying.

And when you run into trouble working with a CNC machine, it’s useful to be able to check through the program to look for errors. Or, if the machine does something unexpected, you have a better chance of analyzing the issue correctly.

Finally, when you have a good understanding of how your CNC machine works, you can design your parts to be machined more efficiently. With fewer operations, setups, and tricky geometries, you can save both time and money

What are the critical G-code commands?

Every G-code command is a letter/number combo, and there are 5 main letters used in the language. Each letter corresponds to a different type of action for the machine to execute.  These are the letters you need to know::

  • M: machine action codes
  • G: motion codes
  • F: feed control
  • S: spindle speed
  • T: tool operations

Each of these letters is paired with a number to create an actual command. Here’s a list of the most common and important commands:

M commands

M commands control the machine, doing things such as starting or stopping the spindle or initiating a tool change. 

M00: program stop

M02: end of program

M03: spindle on, clockwise rotation

M04: spindle on, counter clockwise rotation

M05: spindle stop

M06: tool change

M08: flood coolant on

M09: flood coolant off

M30: end of program, rewind and reset modes

G commands

G commands prepare the machine to move in a certain way. They’re usually accompanied by coordinates so the machine knows where to move as well. There are also some G commands that are used at the beginning of the program as “safety” settings, to make sure the CNC machine is in the right mode. 

Movement commands

G0: rapid motion

Rapid motion is used to move to new coordinates to begin cutting or to move back to the tool carousel for a tool change. This command is only used when the tool is NOT in contact with your part.

G01: controlled motion

This command is used for cutting operations, when a feed rate is also specified.

G02: clockwise motion

Clockwise motion is for curved paths. This mode requires the end coordinates as well as the distance to the center point of the arc in both the X and Y directions. The I command is the X distance to the center point, and the J command is the Y distance to the center point. The starting point of the arc is the position of the machine when G2 is initiated, so you may need to first move the machine to the correct starting position. 

Example: G02 X10 Y7 I0 J-5

a diagram of xy coordinates
G02 prepares the machine for controlled clockwise motion

G03: counterclockwise motion

This command creates an arc between two points in the same way as G2, but in the counterclockwise direction. 

Safety Commands

G17, G18, G19: working plane

G17 sets the x/y plane as the plane to be machined. G18 sets the z/x plane, and G19 sets the y/z plane.

G20/21: units

G20 sets the units to inches, and G21 sets the units to millimeters. This HAS to match your CAD model!

G28: home position

G28 sends the machine directly to its home position. The machine will go straight there (possibly running into your part along the way). Provide an intermediate point and you can ensure the machine doesn’t crash into anything on its way home. This point can be just one coordinate, such as Z0, or fully defined, such as X0 Y0 Z0.

G90/G91

These commands control the mode by which the machine interprets coordinates. G90 puts the CNC machine in absolute mode, meaning it will go to the position of X10 Y10, for example, from anywhere else. Conversely, G91 tells the machine to operate in incremental, or relative mode. This means that the machine will move 10 units along the x-axis and 10 units along the y-axis for the same X10 Y10 command.

T: Tool Operations

T specifies the tool number to change to, along with an M06 (tool change) command. When you set up the CNC machine, each tool is assigned a number.

a rack of cnc machine tools ready to be loaded into the machine
These tools are ready to be loaded into a CNC machine and labeled for machining operations

S: Spindle Speed

This  sets the spindle speed to the number directly following the S. For example, S500 means 500 revolutions per minute (in typical units).

F: Feed Rate

F controls how quickly the machine moves when implementing the G movement modes to the specified coordinates. The units here depend on what you set using G20 or G21.

Most of these commands are standardized and will work on any CNC machine that uses G-code. However, some machines have their own unique commands as well, so make sure you learn your CNC machine’s particular vocabulary!

While you may not be writing the CAM program yourself, it is useful to know what commands control your CNC machine. Remember, your Haas or DMG Mori machine will do exactly what you tell it to do! Knowing G-code ensures that you’re telling it to do the right things. 

Tired of machining your own parts? Let Fictiv do it for you! Sign up and upload a part to see how fast we can deliver.