g code on a screen for cnc machine

13 Useful G-Code and M-Code Commands You Might Not Know

Like most coding languages, G-code looks a bit weird when you first see it. There’s no apparent rhyme or reason, the numbers don’t seem to relate to each other, and the whole thing can appear a bit haphazard. It gets even stranger when you try to relate the codes themselves to the machines they work with. What do alphanumeric combinations have to do with x-y-z coordinate systems and toolpaths?

G-code programming is part of the firmware on all (or nearly all) machine tools. Lathes, mills, 3D printers – they’ll all have a machine controller with a native g-code language. While those languages can vary from manufacturer to manufacturer, they all follow the same general principles, and a majority of the actual codes remain the same.

What are G-Code commands?

G-code is a complex but simple programming language that uses Cartesian coordinate systems to tell a machine tool where to go and what to do with the workpiece. Each command begins with an alphanumeric combo or code that starts with G or M. By string together lines of g-codes, operators can automate the entire machining process, identifying work coordinates to ensure precise cuts.

G-codes are more than just “move here” and “cut this” commands. They include subprograms and subroutines (more on those later), as well as programs related directly to the machine itself. A lathe will have g-code commands to turn the spindle on and adjust spindle speed, while a milling machine with multiple tools will specify the tool number and tool length for each operation. 3D printers will have their own commands, like ones to heat the extruder to a designated temperature.

Individual G-code languages are typically available online and are open-source. Some, like the RepRap project for 3D printers, have been available for years, with commands

READ MORE:  Meeting High Quality Control Standards with the CNC 1340 Lathe

How many G-Code Commands are there?

The full list of G-codes runs to about a hundred commands, not including M-codes, subroutines, etc. For this article, we’ve selected 13 of the most common and most useful G-codes and M-codes for programmers, including some that may not be as familiar. For this article, we’ve selected 13 of the most common and most useful G-codes and M-codes for programmers, including some that may not be as familiar.

G-Code Commands

G20/G21 – Units Designation

Millimeters or inches? Use the G20 and G21 commands to select your unit of measurement. This needs to be done at the beginning of the program, otherwise, most machines will default to base settings or the previous program.

G0/G1 – Linear Movement

Move the tool from position A to position B. That’s the idea behind the linear move commands. Unsurprisingly, these are incredibly common commands found throughout most G-code programs; some estimates say that up to 90% of a given g-code program will consist of these straight-line moves.

The basic G-code move command will rely on a set of destination coordinates. Those coordinates (X, Y, and Z) specify where to finish the move.

G17/G18/G19 – Planar Selection

With these g-codes, operators can set the plane in which the rest of the commands will be executed. The X-Y plane (horizontal and vertical) is the default for most machines. But by designating an XZ or YZ plane, operators can achieve a slightly different range of operations.

G00 – Fast move

Fast move is straightforward; get the machine into position ASAP. It’s especially useful at the beginning or end of a program, or to reset the cutting head mid-way through. Note the current position, the end position, and let the rapid move take care of the rest.

G90 – Absolute Positioning

Many g-code commands rely on positioning – G0 X20 is a simple command to move the tool to a given coordinate on the X-axis. Absolute mode keeps things simple. There’s a set start point, and then X20 is 20 units down that axis. Absolute positioning is the default, but it does pose a challenge under certain conditions.

G91 – Relative Positioning

If the program relies on a series of actions that build on each other, relative positioning might prove helpful. rather than move to a pre-ordinated “X20” point, relative positioning instructs the machine to move 20 units from its previous position.

Most g-codes are modular – that is, they initiate an action that remains in effect until another action supersedes it. G90/91 are good examples. Specifying relative positioning cancels absolute positioning and vice versa.

READ MORE:  What is CNC Milling?

G28 – Auto Home

Go home! The G28 command sends the tool back to where it all began. It’s useful both as a reset device, and to set the limits and parameters of the cutting area. Operators can specify a midpoint in the G28 command to clear obstacles.

G02/G03 – Clockwise and Counter-clockwise arcs

When using the G02 and G03 commands, you’ll need to specify a start point, end point, and a mid-point. That establishes all the parameters of the arc on the plane. You can perform an arc on any plane. Specify the x-axis, y-axis, or z-axis using the planar selection commands above.

G81-G89 – Canned Cycles

The picture is on the tin. Canned cycles are default mini-programs – you need to program certain parameters, but the basics of the program are pre-made. Canned cycles are mostly drilling cycles and boring cycles, including some threading operations. Using canned cycles speeds up the programming process and allows operators to copy/paste identical commands into different parts of the program.

M-Code Commands – Useful Interlopers

Most g-codes are standardized, at least for certain categories of machine tools. G-codes for milling machines should be broadly similar; the same with g-codes for lathes, 3D printers, and so forth.

M-codes are more complex. These are miscellaneous codes from the manufacturer – most of them vary widely from machine to machine, but there is a handful that stays consistent.

M00 – Program Stop

Want to run the program to a certain point then stop it completely for an inspection? M00 is the command for you. It shuts down the current operation and the machine itself, allowing you to change tools, rotate the part, clean the machine – whatever you need to do.

M06 – Tool Change

Operators for CNC machines with automatic tool changers use the M06 command to switch tools within the program. For machines that require a manual tool change, the M06 command usually indicates to the machine that there’s a new tool at work.

READ MORE:  The Functions of a CNC 1236 Lathe can Speed up Machining Greatly

M01 – Optional Program Stop

If you might need to do a tool change, and you think you’d like a closer look at everything, then M01 is the right call. Insert an M01 command at regular intervals in your program to create potential checkpoints. Most CNC machines will have an optional stop button; press it, and the machine won’t shut down immediately. It will execute the program until it reaches the next M01 and then shut down.

M30 – Program reset/Return to start

M30 codes come in handy for continuous operation. They reset the program, telling the machine to go back to the beginning with the tool and start again.

A Code for All Seasons

There are dozens of other codes, used to specify spindle speed, set incremental moves, and identify or reset the home position and endstop for the tool. Each code has the potential to influence the rest of the program, particularly with modal codes that remain active until a new code supersedes them.

Other codes control how fast different actions are performed, such as feed rates, constant surface speed, and cutter compensation. Still, others set the length of time for certain operations, down to milliseconds.

Despite being apparently complex, a well-built g-code file contains everything the machine needs to conduct an operation without direct human intervention. 3D printing, 3D milling, and turning centers, and a huge range of other machine tools all rely on g-code commands.

You can find g-code programming tutorials online, from actual classes to informal YouTube training sessions. The specific g-codes mentioned above are standardized across machine tools, but you’ll need to look more closely at machine-specific lists to improve your skills.

About Peter Jacobs

Peter Jacobs is the Senior Director of Marketing at CNC Masters, a leading supplier of CNC mills, milling machines, and CNC lathes. He is actively involved in manufacturing processes and regularly contributes his insights for various blogs in CNC machining, 3D printing, rapid tooling, injection molding, metal casting, and manufacturing in general. You can connect with him on his LinkedIn.

Leave a Comment

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.