Search This Blog

What is G code ?

 What is G-code?

G-code is a programming language for CNC (Computer Numerical Control) machines. G-code stands for “Geometric Code”. We use this language to tell a machine what to do or how to do something. The G-code commands instruct the machine where to move, how fast to move and what path to follow.


How to read G-code Commands?

Look single line and explain how it works

G01 X247.951560 Y11.817060 Z-1.000000 F400.000000

The line has the following structure:

G## X## Y## Z## F##

First is the G-code command and in this case that’s the G01 which means “move in straight line to a specific position”.We declare the position or the coordinates with the X, Y and Z values.Lastly, with the F value we set the feed rate, or the speed at which the move will be executed.

To wrap up, the line G01 X247.951560 Y11.817060 Z-1.000000 F400 tells the CNC machine to move in a straight line from its current position to the coordinates X247.951560, Y11.817060 and Z-1.000000 with speed of 400 mm/min. The unit is mm/min because if we take a look back at the G-code example image, we can see that we have used the command G21 which sets the units to millimiters. If we want the units in inches, we use the G20 command instead.


The most Important/ Common G-code Commands

G00 – Rapid Positioning

The G00 command moves the machine at maximum travel speed from a current position to a specified point or the coordinates specified by the command. The machine will move all axis at the same time so they complete the travel simultaneously. This results in a straight line movement to the new position 

G01 – Linear Interpolation

The G01 G-code command instructs the machine to move in a straight line at a set feed rate or speed. We specify the end position with the X, Y and Z values, and the speed with the F value. The machine controller calculates (interpolates) the intermediate points to pass through to get that straight line. Although these G-code commands are simple and quite intuitive to understand, behind them, the machine controller performs thousands of calculations per second in order to make these movements.

G02 – Circular Interpolation Clockwise

The G02 command tells the machine to move clockwise in a circular pattern. It’s the same concept as the G01 command and it’s used when performing the appropriate machining process. In addition to the end point parameters, here we also need to define the center of rotation, or the distance of the arc start point from the center point of the arc. The start point is actually the end point from the previous command or the current point.

G03 – Circular Interpolation Counterclockwise


Just like the G02, the G03 G-code command defines the machine to move in circular pattern. The only difference here is that the motion is counterclockwise. All other features and rules are the same as the G02 command.


G20/ G21 – Units Selection


The G20 and G21 commands define the G-code units, either inches or millimters.


G20 = inchesG21 = millimiters


We need to note that the units must be set at the beginning of the program. If we don’t specify the units the machine will consider the default set by the previous program.


G17/ G18/ G18 – G-code Plane Selection


With these G-code commands we select the working plane of the machine.


G17 – XY planeG18 – XZ planeG19 – YZ plane


The G17 is default for most CNC machines, but the other two can be also used for achieving specific movements.


G28 – Return Home


The G28 command tells the machine to move the tool to its reference point or home position. In order to avoid collision, we can include an intermediate point with X, Y and Z parameters. The tool will pass through that 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##.


G90/ G91 – Positioning G-code commands


With the G90 and G91 commands we tell the machine how to interpret the coordinates. G90 is for absolute mode and G91 is for relative mode.

More Commands and Rules

In addition to the G-code, there also M-code commands which are used when generating a real full-fledged G-code program. Here are few common M-code commands:


M00 – Program stop, M02 – End of program, M03 – Spindle ON – clockwise ,M04 – Spindle ON – counterclockwise, M05 – Spindle stop, M06 – Tool change, M08 – Flood colant ON, M09 – Flood colant OFF, M30 – End of program

Simple G-code Program Example 

% G21 G17 G90 F100 M03 S1000 G00 X5 Y5                 ; point B G01 X5 Y5 Z-1             ; point B G01 X5 Y15 Z-1            ; point C G02 X9 Y19 Z-1 I4 J0    ; point D G01 X23 Y19 Z-1          ; point E G01 X32 Y5 Z-1            ; point F G01 X21 Y5 Z-1            ; point G G01 X21 Y8 Z-1            ; point H G03 X19 Y10 Z-1 I-2 J0    ; point I G01 X13 Y10 Z-1           ; point J G03 X11 Y8 Z-1 I0 J-2     ; point K G01 X11 Y5 Z-1            ; point L G01 X5 Y5 Z-1             ; point B G01 X5 Y5 Z0 G28  X0 Y0 M05 M30 

Description of the G-code program:

Code initialization. This character (%)  is always present at the beginning and at the end of the program.Safety line: Set programming in metric system (all dimensions in mm), XY plane, absolute positioning and feed rate of 100 inches/min.Spindle on clockwise at speed of 1000 RPM.Rapid positioning to B(5,5).Controlled motion on the same position, but lowering the tool to -1.Linear cutting movement to position C(5,15).Clockwise circular motion to point D(9,19), with center point at (9,15).Linear cutting to point E(23,19).Linear cutting to point F(32,5).Same straight cutting to point G(21,5).One more straight cutting to point H(21,8).Counterclockwise circular interpolation to position I(19,10), with a center point at (19,8).Linear cutting to point J(13,10).Counterclockwise circular cutting to position K(11,8), with a center point at (13,8).Linear cutting to position L(11,5).Final linear cutting movement to position B(5,5).Rise up the tool.Go to home position.Spindle off.Main program end