G-Code is the most commonly used CNC programming language. A G-Code file is a series of instructions that come together to produce different movements and events on a CNC machine.
The name G-Code comes from the name of a list of codes that we commonly call throughout the programming of a job. These G-Codes all perform different tasks and you will see certain G-Codes a lot more than others.
It’s the G-Code that automates the CNC machine. When you see a CNC machine cutting out a complex part; behind the scenes thousands upon thousands of lines of G-Code are being executed sequentially one after the other.
The best way to understand how it works is just to jump right in with an example:
This is a line of G-Code, and we read it from left to right. Starting off we see the letter ‘G‘ and the number ‘0‘. The G means that we’ll be using G-Code number 0.
If you look at this page you’ll find that G-Code 0 is: Rapid Positioning. For the record if you see G00 its the same as G0. It’s also the same for the rest, G01 is the same as G1 and so on. The extra zero you see is purely for formatting; makes it easier to read.
Rapid positioning means that we will be telling the machine to travel at its rapid velocity to our desired position (The rapid velocity will often be a specially configurable setting in your CNC software).
Right, so what’s our desired position we want to rapidly speed to? Well the G0 code requires at least one parameter to work and that’s what the X100 is.
The X refers to the axis we want to move on, in this case it is the X axis. When looking front on to a CNC machine, the X axis is left to right movement.
Now we know we want to move along the X axis, but how far? That’s what the number following the axis is for. In this case it is: 100. If your CNC is configured to use metric units then the 100 will refer to 100mm, otherwise if it’s configured to use imperial units that’ll mean it’s 100 inches. It’s a big difference, you’re going to want to make sure your software is configured to use your desired measurement system.
In summary the G-Code line: “G0 X100” means we want to do a rapid move on the X axis to position 100. It’s all very simple when you know how it works but at first it can be confusing. The G0 and G1 positioning commands will take multiple parameters so I could also do the following:
G0 X100 Y50
This follows the same format as above, but the behaviour of the CNC machine will be a little different. The machine will move the axes at different speeds so that both the X and Y axes end up at the desired location at the same time.
This means in this example the Y axis will run at 50% the speed of the X axis because the X axis has twice the amount of distance to cover than the Y axis.
If you wanted the axes to be driven one-by-one instead of at the same time you’d break up the line in to the following two lines:
Absolute and Relative Positioning
Next it is important to understand absolute positioning and relative (also called incremental) positioning. In the above example where we moved to position 100, that was assuming that we were at position 0 before we began to move. This is called absolute positioning because we will move exactly to the commanded position. Lets say we begin at position 50 and are commanded to move to position 100 we would only move 50 units to get to position 100.
Relative positioning means we will move the amount commanded in addition to whatever our current position is. Lets say we were at position 50 and we were commanded to move to position 100, we would then add 100 to our current position and would end up at position 150.