If your job or hobby is related to CNC machines or 3D printers, then it is important for you to understand what G-code is and how it works. In this tutorial, we will learn the basics of the G-code language and common G-code commands, and explain how these G-code commands work.
You can test your G-Code commands using this free online simulator.
1.What is G-code?
G-code is the programming language for CNC (Computer Numerical Control) machine tools. G-code stands for Geometric Code. We use this language to tell the machine what to do or how to do something. G-code commands tell the machine where to move, how fast to move, and what path to follow.
For CNC machines, the cutting tool is driven by these G-code commands to cut the material along a specific path to obtain the desired shape.
Similarly, with a 3D printer, G-code commands instruct the machine to deposit material layer by layer to create a precise geometric shape.
3.How to read G-code commands?
When you first look at a G-code file it may look quite complex, but it is actually not that difficult to understand.
If you look closely at the code, you can see that most of the lines have the same structure. It looks like the “complex” part of the G-code is mostly the numbers, and those numbers are the Cartesian coordinates.
Let’s look at one line of code and explain how it works.
1|G01 X247.951560 Y11.817060 Z-1.000000 F400.000000
The line has the following structure:
1|G#x##Y#Z## F##
(1)The first is the G-code command, in the example above it is G01, which means “move in a straight line to a specific position”.
(2)We declare the X, Y, and Z coordinates of the location we want to move to.
(3)Finally, with the F value we set the feed rate, that is, the speed at which the moves are performed.
To summarize, the code G01 X247.951560 Y11.817060 Z-1.000000 F400.000000 requires the CNC machine to move in a straight line from its current position to the coordinates X247.951560, Y11.817060 and Z-10000000 at a speed of 400 mm/min.
Note that the units are mm/min because in the previous G-code example, we used the command G21 which set the units to millimeters. If you want to use inches, you can use the G20 command instead.
3.Common G-code commands
Now that we understand how to read a line of G-code, we can move on to learning the most important or commonly used G-code commands. We will go through several examples to see how each G-code command works, and by the end of this tutorial, we will be able to fully understand how G-code works, how to read it, how to modify it, and be able to write our own G-code programs.
3.1 G00 – Rapid positioning
The G00 command moves the machine from its current position to the specified coordinates at maximum speed. The machine will move all axes simultaneously so that the travel is completed at the same time. The result is a straight line move to the new position point.
G00 is a non-cutting motion whose purpose is to quickly move the machine to the desired position to begin some kind of work, such as cutting or printing.
3.2 G01 – Linear interpolation
The G01 command instructs the machine to move in a straight line at a set speed. We specify the final position with X, Y, and Z values, and the speed with an F value. The CNC controller calculates (interpolates) the coordinates of the intermediate points to be passed through to get the straight line. While these G-code commands are simple, intuitive, and easy to understand, behind the scenes the CNC controller performs thousands of calculations per second to make these moves.
Unlike the G00 command, which is used only for positioning, the G01 command is used when the machine is performing a primary task, such as a machine tool cutting material in a straight line, or a 3D printer extruding material in a straight line.
3.3 G02 – Clockwise circular interpolation
The G02 command asks the machine to move clockwise in a circular pattern. It is the same concept as the G01 command and is used when the appropriate machining process is performed. In addition to the end point parameter, here we also need to define the center of rotation, or the distance from the arc’s starting point to the arc’s center point. The starting point is actually the end point of the previous command or the current point.
To understand better, we will add a G02 command after the G01 command in the previous example.
So, in our example, first we move the machine to point X5, Y12 with the G01 command. Now, this will be the starting point for the G02 command. With the X and Y parameters of the G02 command, we set the end point.
Now, in order to reach the end point with a circular motion or an arc, we need to define its center point. We do this with the I and J parameters. The I and J values are relative to the starting point or the end point of the previous command. So, to get the center point of X5 and Y7, we need an offset of 0 along the X axis and an offset of -5 along the Y axis.
Of course, we can set the center point somewhere else, and we will get a different arc that ends at the same endpoint. Here is an example:
So here, we still have the same end point as in the previous example, (X10, Y7), but the center point is now in a different location (X0, Y2).
Hence we get a wider arc.
3.4 G00, G01, G02 Example – Manual G Code Programming
Let’s take a look at a simple CNC milling example using the three main G-code commands, G00, G01, and G02.
To get the path of the shape shown above, we need to follow the G-code commands:
1|G00 X5 Y5 ; point B
2|G01 X0 Y20 F200 ; point C
3|G01 X20 Y0 ; point D
4|G02 X10 Y-10 I0 J-10 ; point E
5|G02 X-4 Y-8 I-10 J0 ; point F
6|G01 X-26 Y-2 ; point B
The first G00 command quickly brings the machine from its starting position to point B (5,5). From here, we use the G01 command to “cut” at a feed rate of 200. We can notice here that to get from point B (5,5) to point C (5,25), we use X and Y values relative to the starting point, point B. So +20 units in the Y direction will get us to point C (5,25). In reality, it depends on whether we interpret the coordinates as absolute or relative. We will explain this in a later section.
Once we reach point C (5, 25), we use another G01 command to reach point D (25, 25). We then use a G02 command (circular motion) to reach point E (35, 15) with the middle point at (25, 15). In fact, we have the same center point (25, 15) for the next G02 command to reach point F (31, 7). However, it should be noted that the I and J parameters are different from the previous commands because we offset the center from the last end point or point E. We complete the entire path with another G01 command that returns us from point F (31, 7) to point B (5, 5).
The above is the G-code program we wrote to make this shape. However, it should be noted that this is not a complete G-code program because it is still missing several more basic commands. We will write a complete G-code program in the following examples.
3.5 G03 – Counterclockwise Circular Interpolation
Like G02, the G03 command instructs the machine to move in a circular pattern, the difference being that G03 is a counter-clockwise motion. All other functions and rules are the same as the G02 command.
Using these three main G-code commands, G01, G02 and G03, we can theoretically generate paths of any shape. You may now be wondering how this is possible, but it is actually a simple task with a computer and CAM software. Yes, we can sometimes make G-code programs manually, but most of the time, we use simpler and safer software to generate G-code programs.
Anyway, let’s continue by explaining the commonly used commands and implement a real G-code example before the end of the tutorial.
3.6 G20/G21 – Unit Selection
The G20 and G21 commands define the G-code units, either inches or millimeters.
(1) G20 = inches
(2) G21 = mm
We need to note that the units must be set at the beginning of the program. If no units are specified, the CNC will take into account the default values set by the previous program.
3.7 G17/G18/G18 – Work surface selection
With these G-code commands we select the work plane for the machine:
(1) G17 – XY Plane
(2) G18 – XZ plane
(3) G19 – YZ plane
The default for most CNC machines is G17, but the other two can also be used to achieve specific motions.
3.8 G28 – Return home
The G28 command requires that the machine will move to its reference point or home position. To avoid collisions, we can include an intermediate point with X, Y, and Z parameters. The tool will pass through this point before going to the reference point. G28 X##Y##Z##
The home position can be defined with the command G28.1 X# Y # Z #.
3.9 G90/G91 – Positioning Mode
Using the G90 and G91 commands, we tell the machine how to interpret the coordinate values. G90 is absolute mode and G91 is relative mode.
In absolute mode, the positioning of the tool is always relative to an absolute point or zero point. Therefore, the command G01 X10 Y5 will move to the exact point (10, 5), regardless of the previous position.
In relative mode, the tool is positioned relative to the last point. So if the machine is currently at point (10, 10), the command G01 X10 Y5 will point the tool to point (20, 15). This mode is also called “incremental mode”.
3.10 More commands and rules
The G-code commands we explained above are the most common ones, but there are many more, such as cutting machine compensation, scaling, working coordinate system, etc.
In addition to G-code, M-code commands are also required to generate a truly complete G-code program. The following are some common M-code commands:
(1) M00 – Program Stop
(2) M02 – Program End
(3) Spindle On – Clockwise
(4) Spindle On – Counterclockwise
(5) Spindle Stop
(6) Tool Change
(7) Enable Flood Colant
(8) Disable Flood Colant
(9) Program End
Additional commands for 3D printers:
(1) M104 – Start extruder heating
(2) M109 – Wait until extruder reaches T0
(3) M140 – Start base plate heating
(4) M190 – Wait until base plate reaches T0
(5) M106 – Set fan speed
Some of these commands require appropriate parameters. For example, when turning on the spindle with the M03 command, we can set the spindle speed using the S parameter. So, M30 S1000 will turn on the spindle at a speed of 1000 RPM.
We can also notice that many codes are modal, which means they remain in effect until canceled or replaced by another code. For example, let’s say we have a linear cut motion G01 X5 Y7 F200 code. If the next motion is also a linear cut, we can type in the X and Y coordinates directly without writing G01 in front.
1|G01 X5 Y7 F200
2|X10 Y15
3|X12 Y20
4|G02 X5 Y5 I0 J-5
5|X3 Y6 I-2 J0
The same applies to the feed rate parameter F. You do not have to include it in every row unless you want to change its value.
In some G-code files you may also see “N##” in front of the command, this is the numbering of the line or block of code and helps to identify a specific line of code if an error occurs in a large program.
4.G-code Program Example
After learning the above, we can now manually create a real G-code program. Here is an example:
1 | %
2 | G21 G17 G90 F100
3 | M03 S1000
4 | G00 X5 Y5 ; point B
5 | G01 X5 Y5 Z-1 ; point B
6 | G01 X5 Y15 Z-1 ; point C
7 | G02 X9 Y19 Z-1 I4 J0 ; point D
8 | G01 X23 Y19 Z-1 ; point E
9 | G01 X32 Y5 Z-1 ; point F
10 | G01 X21 Y5 Z-1 ; point G
11 | G01 X21 Y8 Z-1 ; point H
12 | G03 X19 Y10 Z-1 I-2 J0 ; point I
13 | G01 X13 Y10 Z-1 ; point J
14 | G03 X11 Y8 Z-1 I0 J-2 ; point K
15 | G01 X11 Y5 Z-1 ; point L
16 | G01 X5 Y5 Z-1 ; point B
17 | G01 X5 Y5 Z0
18 | G28 X0 Y0
19 | M05
20 | M30
21 | %
Description of G-code program:
(1) Code initialization. This character (%) always exists at the beginning and end of a program.
(2) Safety Line: Set program parameters such as metric system (millimeters), XY plane, absolute positioning, and feed rate of 100 inches/minute.
(3) Rotate clockwise at 1000 RPM.
(4) Quickly locate B (5, 5).
(5) Control the movement in the same position, but lower the tool to -1.
(6) Linear cutting movement position C (5, 15).
(7) Move in a clockwise circular motion to point D (9, 19), with the center point being (9, 15).
(8) Linear cut to point E (23, 19).
(9) Linear cut to point F (32, 5).
(10) Continue cutting in a straight line to point G (21, 5).
(11) Continue cutting in a straight line to point H (21, 8).
(12) Interpolate counterclockwise to position I (19, 10), with the center point at (19, 8).
(13) Linear cut to point J (13, 10).
(14) Cut counterclockwise in a circular motion to position K (11, 8), with the center point being (13, 8).
(15) Make a linear cut to position L (11, 5).
(16) The final linear cutting motion is to position B (5, 5).
(17) Lift the tool.
(18) Return to home.
(19) The spindle is off.
(20) The main program ends.
Here is what this code looks like in the Universal G-code Sender software ready to be sent to our CNC:
So, using these main G-code commands explained above, we have written a complete G-code program. Of course, this example is simple, for more complex shapes we will definitely need to use CAM software. Here is an example of a complex G-code program for cutting a horse shape:
The above code has about 700 lines, but it was automatically generated using Inkscape. The result is as follows:
Original link: G-code CNC machine programming tutorial — BimAnt
Welcome to check out our article, this article is translated from the above link. If you want to know more, you can click the link above. You are also welcome to consult us, we will serve you wholeheartedly. Click here to contact us now.
ZHUANXIN Precision Factory provides comprehensive customized processing services, including rapid prototyping, low-volume manufacturing, design evaluation, CNC machining, CNC routing, surface treatment, and complimentary assembly. Our experienced CNC machinists guarantee high-quality results tailored to your unique requirements.