G-code Explained | List of Most Important G-code Commands (2022)

If your work or hobby correlates with CNC machines or 3D printers, then understanding what G-code is and how it works is essential for you. So, in this tutorial we will learn the basics of the G-code language, what are the most important or common G-code commands and we will explain how they work.

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.

In case of a machine tool such as lathe or mill, the cutting tool is driven by these commands to follow a specific toolpath, cutting away material in order to get the desired shape.

Similarly, in case of additive manufacturing or 3D printers, the G-code commands instruct the machine to deposit material, layer upon layer, forming a precise geometric shape.

How to read G-code Commands?

At first glance, when you see a G-code file, it might look a quite complicated but it is actually not that hard to be understood.

If we take a closer look at the code, we can notice that most of the lines have the same structure. It seems that the“complicated” part of the G-code are all those numbers we see, which are just Cartesian coordinates.

Let’s take a look at a 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 G01which 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.000000F400tells 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

So, now as we know how to read a line of G-code, we can take a look at the most important or commonly used G-code commands. We will learn how each of them work through several examples, and by the end of this tutorial we will be able to fully understand how G-code works, how to read, how to modify and even how to write our own G-code.

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 point.

(Video) G codes and M codes for CNC programming | important G codes | Important M codes | G and M codes

G-code Explained | List of Most Important G-code Commands (2)

The G00 is a non-cutting movement, and its purpose is to just quickly move the machine to the desired position to begin some kind of job, like cutting or printing.

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.

G-code Explained | List of Most Important G-code Commands (3)

Unlike the G00 command which is used just for positioning, the G01 command is used when the machine is performing its main job. In case of lathe or mill, cutting material in straight line, and in case of a 3D printer, extruding material in straight line.

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.

For better understanding, we will add the G02 command after the G01 command from the previous example.

G-code Explained | List of Most Important G-code Commands (4)

So, in the example first we have the G01 command which moves the machine to the X5, Y12 point. 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 get to this end point using a circular motion or using an arc, we need to define its center point. We do that using the I and J parameters. The values of the I and J are relative to the starting point, or the end point of the previous command. So, to get the center point to the X5 and Y7, we need to make an offset of 0 along the X axis, and offset of -5 along the Y axis.

Of course, we can set the center point anywhere else, thus we will get a different arc which ends at the same end point. Here’s an example of that:

G-code Explained | List of Most Important G-code Commands (5)

So, here we still have the same end point as the previous example (X10, Y7), but the center point is now at different position (X0, Y2). With this we got wider arc compared to the previous one.

See also:

G00, G01, G02 Example – Manual G-code Programming

Let’s take a look at a simple CNC milling example using these three main G-code commands, G00, G01 and G02.

(Video) TOP FIVE ** MUST KNOW ** G Codes You Will Use For & How To Use Them on your cnc, Router Laser, gcode

G-code Explained | List of Most Important G-code Commands (6)

To get the toolpath for the shape shown in the image above we need to following G-code commands:

G00 X5 Y5 ; point BG01 X0 Y20 F200 ; point CG01 X20 Y0 ; point DG02 X10 Y-10 I0 J-10 ; point EG02 X-4 Y-8 I-10 J0 ; point FG01 X-26 Y-2 ; point B

Code language: Arduino (arduino)

With the first G00 command, we quickly bring the machine from its home or initial position to the point B(5,5). From here we start with “cutting” at a feed rate of 200 using the G01 command. We can note here that for getting from point B(5,5) to the point C(5,25) we use values for the X and Y relative to the starting B point. So, +20 units in Y direction will get us to point C(5,25). Actually, this depends whether we have selected the machine to interpret the coordinates as absolute or relative. We will explain this in later section.

Once we reach the point C(5,25), we have another G01 command to reach the point D(25,25). Then we use the G02 command, a circular motion, to get to point E(35,15), with a center point (25,15). We actually have the same center point (25,15) for the next G02 command, to get to point F(31,7). However, we should note that the I and J parameters are different from the previous command, because we offset the center from the last end point or the point E. We finish the toolpath with another G01 command which gets us from point F(31,7) back to point B(5,5).

So, that’s how we can manually program the G-code for making this shape. Though, we need to note that this is not a complete G-code, because we are missing few more basic commands. We will make the complete G-code in a later example as we first need to explain those G-code commands.

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.

G-code Explained | List of Most Important G-code Commands (7)

So, with these three main G-code commands, G01, G02 and G03 we can generate a toolpath for, literally, any shape we want. You might be wondering now how is that possible, but that’s actually an easy task for a computer and a CAM software. Yes, it’s true we can sometimes manually make a G-code program, but most of the time we do that with appropriate software which far more easier and safer.

Nevertheless, now explain few more important and commonly used commands and at the end make a real G-code example.

G20/ G21 – Units Selection

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

  • G20 = inches
  • G21 = 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 plane
  • G18 – XZ plane
  • G19 – YZ plane
G-code Explained | List of Most Important G-code Commands (8)
(Video) Learn G-Code for 3D Printing

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##

G-code Explained | List of Most Important G-code Commands (9)

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.

In absolute mode the positioning of the tool is always from the absolute point or zero. So the command G01 X10 Y5 will take the tool to that exact point (10,5), no matter the previous position.

On the other hand, in relative mode, the positioning of the tool is relative to the last point. So if the machine is currently at point(10,10), the command G01 X10 Y5 will take the tool to point (20,15). This mode is also called “incremental mode”.

See Also
Arduino CNC Foam Cutting Machine

More Commands and Rules

So, the G-code commands we explained above are the most common ones but there are many more. There are commands like cutter compensation, scaling, work coordinate systems, dwell etc.

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

In case of a 3D printer:

  • M104 – Start extruder heating
  • M109 – Wait until extruder reaches T0
  • M140 – Start bed heating
  • M190 – Wait until bed reaches T0
  • M106 – Set fan speed

Some of these commands need appropriate parameters. For example, when turning on the spindle with M03, we can set the spindle speed using the S parameter. So, the line M30 S1000 will turn on the spindle at speed of 1000 RPM.

We can also note that many codes are modal, which means they remain in effect until cancelled or replaced by another code. For example, say we have a code for linear cutting movement G01 X5 Y7 F200. If the next movement is again a linear cutting, we can just type the X and Y coordinates, without the writing G01 at the front.

(Video) G & M Code - Titan Teaches Manual Programming on a CNC Machine.

G01 X5 Y7 F200X10 Y15X12 Y20G02 X5 Y5 I0 J-5X3 Y6 I-2 J0

Code language: Arduino (arduino)

The same applies for the feed rate parameter F. We don’t have to include it in every line unless we want to change its value.

In some G-code file you can also see “N##” in front of the commands. The N word is simple to number the line or block of code. That can be helpful for identifying a specific line in case of an error in a huge program.

Simple G-code Program Example

Nevertheless, after reading all of this, now we are able to manually make a real, actual code. Here’s an example:

%G21 G17 G90 F100M03 S1000G00 X5 Y5 ; point BG01 X5 Y5 Z-1 ; point BG01 X5 Y15 Z-1 ; point CG02 X9 Y19 Z-1 I4 J0 ; point DG01 X23 Y19 Z-1 ; point EG01 X32 Y5 Z-1 ; point FG01 X21 Y5 Z-1 ; point GG01 X21 Y8 Z-1 ; point HG03 X19 Y10 Z-1 I-2 J0 ; point IG01 X13 Y10 Z-1 ; point JG03 X11 Y8 Z-1 I0 J-2 ; point KG01 X11 Y5 Z-1 ; point LG01 X5 Y5 Z-1 ; point BG01 X5 Y5 Z0G28 X0 Y0M05M30%

Code language: Arduino (arduino)

Description of the G-code program:

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

Here’s how this code looks ready to be sent to our CNC machine through the Universal G-code Sender software:

So, using these main G-code commands explained above, we managed to write our own fully-fledged G-code. Of course, this example is quite simple and for more complex shapes we definitely need to use a CAM software. Here’s an example of a complex G-code of a Horse shape:

For comparison, this code has around 700 lines, but all of it generated automatically. The G-code was made using Inkscape as an example for my DIY Arduino CNC Foam Cutting Machine. Here’s how the shape came out:

You can check my particular tutorial for more details about this machine.

(Video) Basic G-Code programming for CNC routers


So, we covered the basics of G-code, explained the most important and common G-code commands and manually made our own actual G-code. After all, I think understanding G-code is not that hard. Of course, there are so many other commands and features used in CNC machining or 3D printing that we should be aware of, but more on that in some other tutorials.

If you found this article useful, share it other CNC and 3D printing enthusiasts. Also feel free to ask any question in the comments section below.


G-code Explained | List of Most Important G-code Commands? ›

Table 1 - G-Code Commands for Turning and Milling Operations
Code -->Interested in CNC Machining Services? Try out Xometry's Instant Quoting Engine℠ to make your project come to life. Free shipping in the USA.Turning
G01Linear interpolation
G02Circular Interpolation CW
G03Circular interpolation CCW
75 more rows

What are the most common G codes? ›

Commonly used G-Codes
  • g00 rapid positioning.
  • g01 linear interpolation.
  • g02 circular/helical interpolation (clockwise)
  • g03 circular/helical interpolation (counterclockwise)
  • g07 Lathe diameter mode.
  • g08 Lathe radius mode.
  • g04 dwell.
  • g17 xy plane selection.

What are Gcode commands? ›

Simply put, G-code is a software programming language used to control a CNC machine. And, even with complex CNC machining, G-code is written in a straightforward, logical way. A “G” is followed by a number, which is a command to change geometry. For instance, “G00” is a command for rapid movement.

What are the 3 basic G codes? ›

Nevertheless, there are three basic types of G-codes: G00 is used for rapid, non-cutting movements. G01 is used for linear movements at a programmed feed speed, usually used to cut material. G02 is used for circular movements at a feed speed.

Is G-code a list? ›

1. For all milling type machines, G17 is default. 2. For detailed descriptions, please refer to G-Code Usage Description.
CNC Mill & Machining Center G-Codes.
CodeDescriptionGroup No.
G00*Rapid positioning01
G01Linear interpolation01
G02Circular interpolation CW01
G03Circular interpolation CCW01
68 more rows

What is G90 G-code? ›

G90: Absolute Positioning

First up, G90 is the G-code to set a machine to absolute positioning mode. Absolute positioning means that your machine tool moves relative to a set and stationary point, known as the origin. For 3D printers, this is typically the center of the print bed, or a specific corner.

What is G28 code? ›

The G28 code returns all axes (X, Y, Z, A and B) simultaneously to the machine zero position when no axis is specified on the G28 line. Alternatively, when one or more axes locations are specified on the G28 line, G28 will move to the specified locations and then to machine zero.

What is meant by G28 U0 W0? ›

G28 U0 W0 (moves directly to machine zero because the initial incremental move is zero) ; G28 U-1. W-1 (moves incrementally -1. in each axis then to machine zero) ; Feedback.

How many G codes are there? ›

There are approximately one hundred G-codes, with separate codes for turning and milling. Many codes are the same for both machine types, though there is some variation among code for machines from different manufacturers.

What is M105 G-code? ›

M104: Set Hotend Temperature. M105: Report Temperatures. M106: Set Fan Speed. M107: Fan Off. M108: Break and Continue.

How many G codes are there in CNC? ›

All CNC machines utilize G code CNC programming. This instructs the machine on when, where, and how to move. Q: How many G codes are there? A: There are about a hundred G codes, Turning and milling machines each have separate codes, although both machines share some of these codes.

What are G0 and G1 in G code? ›

If G1 is active, the speed of motion is the current feed rate (or slower if the machine will not go that fast). If G0 is active, the speed of motion is the current traverse rate (or slower if the machine will not go that fast).

What is G4 in G code? ›

G4 G-code. G4 means “pause for a given duration of time”.

What is G96 code? ›

G96 constant surface cutting speed

The G96 command is used when we require a constant surface speed or cutting speed. This speed indicates the distance that the tool moves along the components surface per minute.

What is G80 in G-Code? ›

G80 cancels all active canned cycles. NOTE. G00 or G01 also cancel canned cycles.

What does M1 mean in G-Code? ›

1. M Code Quick Reference Table
M0 M1Program Pause
M2 M30Program End
M60Pallet Change Pause
M3 M4 M5Spindle Control
19 more rows

What is F code in CNC? ›

F. Defines feed rate. Sets the feed rate when machining lines, arcs or drill cycles. Feed rate can be in Inches per Minute (G94 mode) or Inverse Time (G93 mode).

What are the commands in basic? ›

BASIC Commands
  • Statements. There were fifteen statement types in the original BASIC. ...
  • Arithmetic Expressions. Besides the four standard arithmetic operations, BASIC includes raising-to-the-power, the symbol of which is “^”. ...
  • Variables. ...
  • Arrays. ...
  • PRINT Statements. ...
  • Defined Functions. ...
  • Example Programs. ...
  • Commands.
Jul 20, 2018

What are the basic commands of a computer? ›

List of basic computer shortcut keys:
  • Alt + F--File menu options in the current program.
  • Alt + E--Edits options in the current program.
  • F1--Universal help (for any sort of program).
  • Ctrl + A--Selects all text.
  • Ctrl + X--Cuts the selected item.
  • Ctrl + Del--Cut selected item.
  • Ctrl + C--Copy the selected item.
Mar 17, 2019

What are commands in computer? ›

In computers, a command is a specific order from a user to the computer's operating system or to an application to perform a service, such as "Show me all my files" or "Run this program for me." Operating systems such as DOS that do not have a graphical user interface (GUI) offer a simple command line interface in ...

What is the difference between command and statement? ›

A command is what other languages call a function, routine or reserved word, and can be defined by the "proc" command or in C or whatever. A statement is an invocation of a command. Using traditional definitions, a statement in Tcl is a command followed by zero or more arguments.

Which is the BASIC coding language? ›

JavaScript. This is the world's most popular programming language. Coders mainly use it on the world wide web to create web interfaces or websites. It can be an easy-to-learn language.


1. G-Code Lesson 1 What is G-Code?
(Sean Owen)
2. Creating a custom GCODE command - Extending Marlin Firmware
(Mikey's Lab)
3. explain cnc g code & m code || Most important code use in cnc machine || vmc operating
(Amazing Gyan Tech)
4. G Code M Code Reference Chart
5. CNC Turning G codes | CNC Programming | Fanuc Control | Explained with example
(Techmentool Consultants)
6. Introduction to Marlin Commands & G Code - G104, G140, G109, G190, G106 - A Tutorial
(Makers Mashup)

You might also like

Latest Posts

Article information

Author: Rueben Jacobs

Last Updated: 08/21/2022

Views: 5724

Rating: 4.7 / 5 (57 voted)

Reviews: 80% of readers found this page helpful

Author information

Name: Rueben Jacobs

Birthday: 1999-03-14

Address: 951 Caterina Walk, Schambergerside, CA 67667-0896

Phone: +6881806848632

Job: Internal Education Planner

Hobby: Candle making, Cabaret, Poi, Gambling, Rock climbing, Wood carving, Computer programming

Introduction: My name is Rueben Jacobs, I am a cooperative, beautiful, kind, comfortable, glamorous, open, magnificent person who loves writing and wants to share my knowledge and understanding with you.