G Code Introduction
G code is intimidating to a lot of new entrants to CNC. The good news is that the CAM software is going to write your G Code programs for you. Still, a well-rounded CNC’er should have some familiarity with G Code. This isn’t about being able to write your own program; it’s about being able to verify what your CAM program has written and make some tweaks if needed.
This G Code introduction won’t come close to being exhaustive in covering all the different G Codes, M Codes, and the quirks associate with them. Instead, I’m just hoping to give you a bit of an orientation on the topic and give you some examples of things to watch out for.
There is a Grbl-acceptable G Code list at the end of this post. You can also download this spreadsheet.
Modal versus Non-Modal
Lets start with an understanding of command modality. Think of modal as “constant” and non-modal as “temporary”. A modal command is going to stay set until you change to a different mode within that command group. Examples of command groups are motion commands and coordinate system selection commands.
Let’s consider an example. G53 is a command that addresses the machine coordinate system (more about that later), but only for that single line because it is a non-modal command. When the next g code line comes up, the control will have “forgotten” that you used the G53 command on the last line. Instead, it will reference the modal command within that group that was previously set.
Header Settings
Most CAM programs are going to write several settings at the top of the program. The header will usually declare the unit type, whether the coordinates are absolute or incremental, and the feed rate mode. Let’s break each down.
G code units will either be in inches or millimeters. The header should have a G20 if the units are in inches and a G21 if the units are in millimeters. There’s never a good reason to mix units within the same program.
The header should also declare whether the coordinates in the file are absolute (G90) or incremental (G91). This is best understood by example.
This code snippet will leave the machine at X10 Y10 in the selected work coordinate system:
G90 G0 X5 Y5
X10 Y10
This code snippet will leave the machine at X15 Y15 in the selected work coordinate system:
G90 G0 X5 Y5
G91 X10 Y10 (It was at X5 Y5, then it was commanded to move incrementally another 10 in X and Y, so it ends up at X15 Y15)
There are sometimes reasons to mix absolute and incremental within a program.
Finally, the header file will often declare the feed rate mode. G94 tells the controller that the feed rates are in units per minute. In other words, if we are in inches (G20) then F30 declares that cutting motion should occur at the rate of 30 inches per minute. If instead G93 is set, the controller will try to complete moves at a rate of 1/[F parameter] minutes. Wtih G93 set, F30 would mean that the move should be completed in 2 seconds (one minute divided by 30 equals 2 seconds). You’ll probably never want to use G93.
These commands are all modal.
Basic Motion Commands
There are four basic ways to command movement: Rapid moves (G0), linear cuts (G1), clockwise arcs (G2), and counterclockwise arcs (G3).
Rapid moves (G0) are for going to point A to point B as quickly as possible. Linear cuts (G1) are for cutting material at the specified feed rate. Arcs are used for making circles and curves while cutting. There are a few different formats for arcs, but your CAM software will generate the correct format as long as you have the correct post-processor selected in your CAM software. (Grbl is what you’d want to select). For those that are curious, the common way that arcs are written is by declaring the end point with X, Y, or Z and the center point of the arc with the relative distance from the start point with I, J, or K. For example, if the machine were at X0 Y8 then the command G2 X8 Y0 I0 J-8 would create a clockwise arc that is centered on X0 Y0 ends at X8 Y0. Don’t get intimidated by this though. Your CAM software will take care of your arcs.
G1, G2, and G3 are cutting commands. Unlike G0 (rapid) moves, which travel at the velocity programmed into the controller, cutting commands need to be told how fast to travel. This is specified with the F parameter.
These four commands are all modal, so they remain constant until another motion commands selects a new mode. Consider the following list of commands:
G90 G0 X5
X20 Y10
X0
G1 X5 Y5 F100
In the above command list, X20 Y10 and X0 are moved to at the rapid rate because the last motion command received was a G0 (rapid). X5 Y5 is moved to at a feed rate of 100 because a G1 is used.
Special Motion Commands
Keep a close eye on special motion commands when you examine your G Code programs. There is some room for error if you don’t have certain things set up, so let’s look at the common sticking points.
G28 will move the machine to the home position. The home position, which is different than the homing switch location, is set by G28.1 in Grbl. If you’ve never intentionally set G28.1 you will possibly experience a crash with a G28 command because it will travel to an unpredictable location. There are a lot of ways to use G28, but one common way you will see it in a program is like this:
G91 G28 Z0
This command will cause the machine to move directly to the Z0 point defined in the G28.1 position. It’s a useful command that helps clear the tool of any fixtures or stock on the table before executing moves in the XY plane, but there is something to watch out for here. Notice that it declares G91, incremental coordinates. G91 is modal so you need to set G90 if you intend for further commands to be interpreted as absolute positions.
Next, there is a class of motion commands called “canned cycles”. These are mainly used for drilling and tapping numerous holes using the same settings. The firmware used on the M3, Carve King, and Power Route does not support canned cycles. If you see a G81, G82, G83, or G84 in your program, you are almost certainly heading for an error. Interestingly though, bCNC (a machine interface program) will convert G81 commands before it sends them to the controller, so there is actually a way to use them. In most cases with a CNC router, however, you are better off using a boring operation with an end mill than a drilling operation with a drill bit.
Coordinate System Declaration and Selection
Coordinate system selection gets every new user at least once. This is really a topic unto itself, so I’ll refer you to this YouTube tutorial that I made that discusses it in more depth.
Essentially, your machine has several different coordinate systems that can be selected. The idea behind this is that you can use one set of commands to perform the same operation in several locations. To visualize this, imagine the Power Route with four different vises holding four pieces of stock with the same operation being done on each piece. Since I might move the vises around on the table later, I wouldn’t want to CAM the job with the tool paths moving over the distance to the next vise. I’d assign a coordinate system to each vise and select the first coordinate system, run the operation, select the next coordinate system, run the operation, and so on.
The common work coordinate systems are G54, G55, G56, G57, G58, and G59. The default coordinate system that is written to most G Code programs by your CAM software is G54. This is important to watch for because you might intend to run the work on a different coordinate system. For instance, if you’ve set up the work on the G55 coordinate system and your program selects the G54 system, you just might experience a crash.
In Grbl, the work coordinate system origins are declared with the G10 command. There are a couple different ways to use G10, but the easiest way to use G10 is to jog the machine to the exact location you want to declare as the origin of a work coordinate system then type G10 L20 P# X0 Y0 Z0, where # is the number of the coordinate system. Use a 1 for G54, 2 for G55, 3 for G56, etc. This is a tricky concept, so you really should watch this video.
Work coordinate system selections are modal. Machine coordinate selection is done with G53 and is non-modal. Let’s consider an example of this:
G0 G21 G90
G54 X-100 Y-100
G53 X-100 Y-100
G0 X-100 Y-100
The first line just sets rapid mode (G0), declares the units as millimeters (G21), and coordinates as absolute (G90). The second line selects the G54 work coordinate system and moves to X-100 Y-100 in that coordinate system. You might think that the third line, G53 X-100 Y-100 wouldn’t move the machine anywhere. It will though. It’s going to move to X-100 Y-100 in machine space, which is the same as saying -100 from the X homing switch and -100 from the Y homing switch. The final line will move back to X-100 Y-100 in the G54 system because G54 is modal, but G53 is non-modal.
Another coordinate system command that you should be familiar with is G92. This is the conventional method to zero your work coordinate system if don’t have homing switches (IE: G92 X0 Y0 Z0). G92 can be thought of as an “offset to the work offset”. You should understand that G92 applies only to the work coordinate system that you have currently selected. You can clear these temp offsets from the currently selected coordinate system using G92.1.
M Codes and Miscellaneous Info
G codes usually relate to how or where the machine will move. M Codes tell the machine specific things to do with its accessories or with the program itself. M3 turns the spindle on clockwise (at the speed specified by the S parameter) and M5 turns it off. M8 turns on coolant and M9 turns it off. Our current machines don’t operate with a M code-controlled spindle or a coolant system, but the signals are there on the control board and you can use these signals in ways outside of the explicit description. We use the M3 command to turn our laser system, for example. You could also use the “coolant on” signal generated by M8 to turn on a custom air blast or custom dust collection system. You can think out of the box with these signals if you wish.
One M code that you’ll often see is M6. This is often written to G code programs to command automatic tool changes, but this is not supported by our current machines. Certain versions of Universal G Code Sender will even generate a warning when this command is sent. If you see an M6 command more than once in your code, watch out: You have probably post processed operations with different tools.
You’ll often see G Code programs write G43 or G44 commands a line or two after an M6 command. These are tool height offsets and are not supported with our current firmware. G43.1, however, is support by Grbl. G43.1 is a dynamic tool offset and will be the subject of a future blog post.
Wrap Up
The goal here was not to make you an expert in G Code, but instead to introduce the basics and some common “gotchas” that you might run into. I’ll re-emphasize that you do not need to know how to write your own G Code to be a successful CNC’er. It’s just helpful to able to follow the flow of the programs as you examine what your CAM software outputs.
Below you’ll find a table of some G Codes that are acceptable to Grbl. Keep in mind that G Code is a loose standard, so what you see here will likely be a little different than the exact formats used on a different controller.
If you have some questions, I’d love for you to leave some comments and come join our community forum.
G Code Table
CODE | VALID PARAMETERS | EXPLANATION | EXAMPLE |
G0 | X, Y, Z | Rapid movement at the default feed rate. G0 movements are just for getting around at maximum speed. You don’t cut anything with a G0 set. | G0 X10 Y10 Z5 |
G1 | X, Y, Z,F | Linear movement at the specific cutting feed rate. | G1 Y200 F800 |
G2 | X,Y,Z,I,J,K,F | Clockwise arc movement. G2 and G3 movements are accompanied by X, Y, or Z, and I, J, or K words to tell the machine how exactly to arc. Few operators will need to understand how to program arcs manaually. Your CAM software will do this for you. | If at X1 Y0: G2 X0 Y-1 i-1 j0 |
G3 | X,Y,Z,I,J,K,F | Counter-clockwise arc movement. G2 and G3 movements are accompanied by X, Y, or Z, and I, J, or K words to tell the machine how exactly to arc. Few operators will need to understand how to program arcs manaually. Your CAM software will do this for you. . |
If at X0 Y-1: G3 X1 Y0 i0 j1 |
G4 | P | Pause for the number of seconds specified by the P parameter | G4 P2 |
G10 | L20, L2, P | Set work coordinate system (G54 – G59) offsets. L2 means that you are offsetting from the homing location. L20 means that you are offsetting from the current position as the reference. P designates the coordinate system. IE: P1 is for G54, P2 is for G55, etc. If you were to type G10 L20 P1 X0 Y0 Z0 then you would set the G54 system (0,0,0) point to exactly where the machine is positioned right now. | G10 L20 P1 X0 Y0 Z0 |
G20 | Coordinate units are in inches | G20 | |
G21 | Coodinate units are in millimeters | G21 | |
G28 | X,Y,Z | Return to home position that was set by G28.1. You must have homed the machine to use this command successfully. | G28 |
G28.1 | Designate the current position as the G28 position | G28.1 | |
G30 | X,Y,Z | Return to secondary home position that was set by G30.1. You must have homed the machine to use this command successfully. | G30 |
G30.1 | Designate the current position as the G30 position | G30.1 | |
G38.2 | X,Y,Z,F | Probe. You must have a probe set up to execute this successfully. The machine will move in the direction commanded until it reaches the commanded point or the probe is triggered. | G38.2 Z-50 F100 |
G53 | X,Y,Z | Move to the commanded position in the machine coordinate system. You must have already homed the machine to use this command successfully. If homing switches are on the high side of travel (as they are on the M3 and Carve King), then the XYZ points must be positive. This is a non-modal command, meaning that the selection of the machine coordinate system only persists for the one line. | G53 X-200 Y-200 |
G54 | Select the G54 coordinate system. This is a modal command, meaning that the system will remain selected until another coordinate system is addressed. Work coordinate systems are saved in Grbl memory. You can see the list of coordinate system offsets by typing $# into the command line. | G54 X0 Y0 Z0.2 | |
G55 | Select the G55 coordinate system. This is a modal command, meaning that the system will remain selected until another coordinate system is addressed. Work coordinate systems are saved in Grbl memory. You can see the list of coordinate system offsets by typing $# into the command line. | G55 | |
G56 | Select the G56 coordinate system. This is a modal command, meaning that the system will remain selected until another coordinate system is addressed. Work coordinate systems are saved in Grbl memory. You can see the list of coordinate system offsets by typing $# into the command line. | G56 | |
G57 | Select the G57 coordinate system. This is a modal command, meaning that the system will remain selected until another coordinate system is addressed. Work coordinate systems are saved in Grbl memory. You can see the list of coordinate system offsets by typing $# into the command line. | G57 | |
G58 | Select the G58 coordinate system. This is a modal command, meaning that the system will remain selected until another coordinate system is addressed. Work coordinate systems are saved in Grbl memory. You can see the list of coordinate system offsets by typing $# into the command line. | G58 | |
G59 | Select the G59 coordinate system. This is a modal command, meaning that the system will remain selected until another coordinate system is addressed. Work coordinate systems are saved in Grbl memory. You can see the list of coordinate system offsets by typing $# into the command line. | G59 | |
G90 | Absolute positioning. In G90, the coordinate that you designate with a movement command will reference the zero point in your coordinate system. For instance, if your machine is at X50 Y50 and you command G0 X20 Y10 then the machine will move -30 in X and -40 in Y to get to X20 and Y10. See G91 for an example of incremental positioning. G90 is modal, meanining once you set it, it persists until a G91 is sent. Note that some older versions of Universal G Code Sender switch to G91 when you use the jog arrows. | G90 X0 | |
G91 | Incremental positioning. In G91, the coordinate that you designate with a movement command will reference your current position and move incremetally from there. For instance, if your machine is at X50 Y50 and you command G0 X20 Y10 then the machine will move 20 in X and 10 in Y to get to X70 and Y60. See G90 for an example of absolute positioning. | G91 Z20 | |
G92 | X,Y,Z | Offset coordinate system and save parameters. Think of this as a quick, temporary coordinate system. You can use this to quickly create a zero point. | G92 X0 Y0 Z0 |
G92.1 | Clears the temporary offset created with G92 | G92.1 | |
G93 | This is called inverse time mode. You will probably never use it. Use G94 instead. G93 tells the controller that the move should be completed in one minute divided by the F parameter value | G93 | |
G94 | Tells the controller to move at the rate declared with the F parameter | G94 | |
M0 | Program End | M0 | |
M3 | S | Turn the spindle on, clockwise. | M3 |
M4 | In grbl 0.9 and earlier, this turns the spindle on counterclockwise (if capable). In Grbl 1.0 and later, this can be used to turn on lasers without a spindle start delay. | M4 | |
M5 | Turn off spindle or laser | M5 | |
M6 | T | Tool change command. This is a valid grbl command but most g code senders will want to ignore it. The tool number is specified by the T parameter. | M6 T2 |
M7 | Coolant off | M7 | |
M8 | Coolant on | M8 | |
M9 | Coolant on | M9 | |
M30 | Program End. Equivalent to M0. | M30 | |
S | Set spindle RPM (if your spindle speed is PWM controlled) or laser intensity. | S10000 | |
F | Feed rate (units per minute). The units are determined by whether G20 or G21 is set. | F2000 |
Joe
I guess I’m getting to old to follow. All I want to do is create a macro for UGS for zeroing my x, y, and z axis using my Millright touch plate. Does anyone know where the g code for the macro is posted, so I can copy and paste?
Derek Roberts
The g code for the macro is on the product listing for the touch plate. Here is a direct link: https://s24914.pcdn.co/wp-content/uploads/2021/01/Touch-Plate-XYZ-Macro.txt