Contents:
If you’ve decided to use CNC equipment, there’s a good chance you’ll encounter G-code. You’ll need to work with it if you have a router or, for example, a 3D printer. So, it’s a widely used tool! In this article, we’ll go over the basic G-code commands so you can understand it and start writing your own.
What is G-code
G-code stands for Geometric Code, which tells the machine where and how exactly to move the working part—in our case, the spindle. This language consists of commands and coordinates.
However, it’s not the only code you’ll need to use. There are primary (preparatory) commands known as G-code and additional (auxiliary) commands known as M-code. But we’ll cover that a bit later.
Regularly, the command in the line looks like this: G02 X291.984360 Y31.819630 Z-2.000000 F200.000000.
Note the four letters that designate the axes and feed rate, respectively. It’s quite simple! The numbers following these letters indicate coordinates or the feed rate, which in this case would be 200 mm/min. G02, though, is a specific command.
Basic G-code commands
G00. Rapid movement
This is a rapid move of the spindle at maximum speed. In this case, all axes of the machine are engaged, resulting in movement along a straight line. This command is used to quickly reach another point on the worktable.
G01. Controlled linear movement
Here, everything works the same as in the previous case, but now you can set the feed rate or speed. This command is used when movement along a straight line is required, but with material removal.
G02 and G03. Going’ clockwise
These two commands tell the machine to move clockwise or counterclockwise, respectively. However, in this case, new letters need to be included in the command. Here’s an example: G02 X4 Y6 I10 J-10.
In this command, X and Y represent the endpoint, while I and J indicate the center of rotation, which needs to be calculated.
G17, G18, and G19. What kind of surface are we working with?
Imagine you place a square piece of wood on the worktable, and you need to work on one of its sides instead of the top. How do you communicate this to the machine?
Each dimension corresponds to specific axes. G17 represents the top plane, or movement along XY, G18 represents movement along XZ, and G19 represents YZ. Of course, G17 is the most commonly used in work.
G20 and G21. What’s the scale?
With these commands, we can tell the machine which measure to use for all movements: millimeters or inches. Depending on the system you’re accustomed to, you’ll need to specify G20 for inches or G21 for millimeters at the start of the program.
If you forget to specify, the machine will use its default settings.
G28. Honey, I’m home
This command is used to return the spindle to the home position. The main thing is to remember to check where the tool is returning from and where that point is to avoid any collision.
If adjustments are needed, you can specify an intermediate point along the X, Y, and Z axes where the spindle will pause before returning home.
G90 and G91. Absolute or relative
These two commands tell the machine where to measure the specified coordinates from: the point of zero or the current spot.
Imagine the spindle is positioned at X10 and Y20.
If we use the G90 command, it will send the spindle exactly to the specified location in relation to the axes. So, if you set it to X80 and Y60, the spindle will move to those exact coordinates.
However, if you use the G91 command with the same initial spindle position, it will move to X90 and Y80. This means it adds the new values to the current coordinates.
M-code commands
Now let’s talk about M-code. These are auxiliary commands, but they shouldn’t be overlooked. For example, they’re the ones that control the cooling of your machine!
It’s also important to note that the commands can vary in numbering since there’s no universal standard. They may differ slightly depending on the manufacturer. So, what are the main and important ones?
M00. The command that stops the program.
M02. And this is the end of the program. There may be a M30 command that stands for the same.
M03. This command turns on the spindle, clockwise.
M04. This does the same, just counterclockwise.
M05. Makes the spindle stop.
M06. Used when the tool change is needed.
M08. The command to turn the coolant on.
M09. This causes the coolant to turn off.
Rules
Now we just need to understand how all these commands work together and the rules that apply.
The first line in the code will almost always consist of four elements where you specify the measurement system for operations, the processing surface, absolute or relative positioning, and the feed rate. It might look something like this: G20 G18 G90 F200.
In some codes, especially those from external resources, you might see the letter N followed by numbers at the start of each command. This simply indicates line numbering, nothing more. You can use it yourself if you find it helpful for navigating the code.
When writing G-code, remember that an entered command will remain active until you cancel it or replace it with another. This means you don’t need to rewrite it every time you change coordinates. Code lines might look like this:
G01 X6 Y7
X10 Y90
You don’t need to repeat the command; just change the data after it. This rule also applies to other settings, such as feed rate or speed, which we specify at some point in the code. It’s important to remember this to update them as needed or avoid writing unnecessary lines!
Conclusion
- Knowledge of G and M codes are an important part of working with CNC router machines.
- G-code tells the machine where and how exactly to move the spindle. This language consists of commands and coordinates. There are primary (preparatory) commands known as G-code and additional (auxiliary) commands known as M-code.
- The first line in the code will almost always consist of four elements where you specify the measurement system for operations, the processing surface, absolute or relative positioning, and the feed rate.
- In some codes you might see the letter N followed by numbers at the start of each command. This simply indicates line numbering, nothing more. When writing G-code, remember that an entered command will remain active until you cancel it or replace it with another. You don’t need to repeat the command; just change the data after it.