CNC machining has revolutionized how we make products and mechanical parts. However, have you stopped to consider how a CNC machine works?
G-code is the backbone of your equipment, but do you know what it does and why it’s important? Well, after reading this article, you will.
G-code is the primary programming language of CNC machines. It relates to the geographical coordinates of the machine arm so that it knows how and where to move. Without this code, your CNC machine would be a stationary device, such as a router or lathe.
In addition to defining and breaking down specific examples of G-code, we’ll illustrate how you can use it more efficiently in your workshop.
What is G-Code?
G-code stands for “geometric code,” and it tells the machine how to move. This programming language is designed to be simple and straightforward, although it’s not always efficient (more on that later). The way it works is that each line starts with a G number, such as G00. This section is followed by coordinates on the X, Y, and sometimes Z-axis (if your equipment can move on those axes).
G-code also works for 3D printing. Instead of cutting material, the machine adds layer upon layer.
Examples of G-Code in CNC Machining
As you might have guessed, there are only 99 possibilities for G-code commands since there are only two numerals after the G. However, since the code only tells the machine how to move (not precisely where), this limitation is not a problem. Let’s look at some of the most common G-code commands you’ll find in CNC machining.
G00 – Rapid Positioning
Before you can start cutting or shaping your material, the machine needs to be in the right spot. G00 codes tell the equipment to move to a specific point on the X and Y axes.
When entering this movement, you need to provide the axis points as well. The program will locate these coordinates and move the cutting head in a straight line to that point. No cutting is involved with the G00 movement – it’s only designed to get the head from one point to the next.
G01 – Linear Movement
Although this code translates to linear movement like G00, the machine is actually cutting and shaping. As with G00, you have to put the final coordinates in – then the machine will cut along that line until it reaches its stopping point. You also have to input the cutting speed, represented by the letter F.
One element to point out with linear movement is that measurements with a G20 code translate to inches per minute. Measurements with G21 codes are millimeters per minute.
G02 – Clockwise Circular Movement
Straight lines are helpful for many pieces, but the beauty of CNC machining is that you can create any shape you like. G02 codes transcribe clockwise circular movement. To help the machine understand the shape of the curve, you need to input I and J values. These values will indicate the apex of the curve, thus telling the machine how to move.
Rather than putting coordinates in for I and J, their values are relative to the starting point of the command. So, if you input I6 and J-7, that means six points away from the X-axis and seven points down from the starting point on the Y-axis. Adjusting these values can widen or narrow the curve.
G03 – Counterclockwise Circular Movement
G03 works the same way as G02, except the movement is counterclockwise instead of clockwise. You still have to use I and J values to set the center point and affect the shape of the curve.
Plane Selection
While you’re working, you can cut in one of three planes – XY, XZ, and YZ. The G-code you use tells the machine in which plane to work. G17 is the XY plane (aka a flat surface). G18 is the XZ plane, and G19 is the YZ plane. You can use coordinates on all three planes to work in a three-dimensional space.
As you can imagine, bundling all the different G-codes can create relatively complex programs. However, understanding each element individually can help you maximize your CNC machining capabilities. Learning how to input G-code takes a while to master, but it’s not as complicated as it may seem at first.
G28 – Return Home
Once the machine finishes working, you need it to return to its starting position. G28 codes indicate that the head should go back to zero. However, while you’re working, you may create potential collision points. If that is the case, you can add an intermediate point so that the head avoids damaging your piece. Home position is represented by the # sign (e.g., X##, Y##, Z##).
G90 and G91 – Absolute vs. Relative Programming
One of the benefits of G-code is that you can string multiple commands on top of each other to create more complex designs. For that, you’ll have to use the G91 code, which is relative programming. In this case, the movement of the machine is relative to the last input. This way, you can save time and avoid potential collisions.
G90s codes represent absolute programming, meaning that the machine always starts at zero. This method is ideal for cutting and shaping multiple pieces in sequence. For example, if you have to cut 1,000 gears, you want the machine to return to its starting point after each gear so that they’re all consistent.
Pros and Cons of G-Code
Although G-code is the industry standard, it’s far from perfect. Let’s break down the advantages and disadvantages you can expect from this programming.
Pro: Easy to Understand
Because G-code is written straightforwardly and logically, it’s easier for non-programmers to understand how it works. As we showed above, various elements can string together to create complex shapes and designs. Unlike other programming language, you don’t need years of instruction to master G-code.
Con: Not Intuitive
One of the biggest drawbacks of G-code is that the machine doesn’t look at the big picture. Instead, it focuses on one line of code at a time. So, that can lead to various problems. For example, if you forget to input intermediate points when returning the machine to zero, you could damage your piece (or the equipment itself).
Another problem you can encounter is inefficient production. Since CNC machines don’t take a global view of your product, it may take longer to create a piece than it would for an experienced artisan. For that reason, it’s imperative to use a skilled CNC machinist who knows G-code. This way, they can create optimal paths and designs that won’t double back or retread similar pathways, thus extending the production time.
G-Code vs. M-Code
As we’ve seen, there are many components to consider when programming your CNC machine. M-codes relate to non-cutting operations, and they’re essential for production. We won’t dive into M-code too much, but it controls everything else the machine does, including tool changes, coolant flushes, and stopping operations. You’ll use both codes in tandem with each other for each project. Neither G-code nor M-code can work independently of the other.
Other Letters to Know
We’ve covered G, M, I, J, X, Y, and Z. But, there are other letters involved in the programming process that you’ll have to understand. Here’s a quick overview of them:
- A – Directs the tool around the X-axis
- B – Directs the tool around the Y-axis
- C – Directs the tool around the Z-axis
- D – A number that shows the amount of offset for the tool diameter
- F – The speed of the machine
- G – The position of the tool
- I and J – The midpoint for any arcs created by the machine
- L – Refers to looping operations and the number of times to repeat a specific action or sequence
- M – Non-cutting operations, such as starting or stopping
- N – The line number
- P – A time command, including pauses or jumps in time
- R – The radius of arcs made by the machine
- S – The spindle speed
- T – The tool to use
- X, Y, and Z – The different axes you can work in
Now that you know G-code and how it works, happy machining!