Jan 5, 2019

3D Printer G-Code Commands – 2019 Programming Tutorial

G-code is the programming language of your 3D printer. Quickly learn all G-code commands using our tutorial. Includes a G-code list of all commands.



A knowledge of G-code commands will give you 3D printing superpowers. People who this are able to troubleshoot their printers better, control every aspect of the print process and identify and prevent print failures much before they happen.

If that sounds interesting, this post is for you. Our aim is to get you started with the basics. After reading this post, you will be able to:

  • Read and understand G-code commands
  • Write it yourself and test it online
  • Use the preview functionality of Slicers to troubleshoot complicated prints

Let’s get started!

What are G-code Commands?

G-code stands for “Geometric Code”. Its main function is to instruct a machine head how to move geometrically in 3 dimensions. However, it can also instruct a machine to do non-geometric things. For example, G-code commands can tell a 3D printer to extrude material at a specified extrusion rate or change its bed temperature.

In formal terms, it is a numerical control programming language. For those who know how to program, it’s an easy programming language. It is rudimentary and does not have advanced constructs like variables, conditionals, and loops.


How to read G-code Commands

So, how does a line of code look like? Here is a typical example:

G1 X-9.2 Y-5.42 Z0.5 F3000.0 E0.0377

This particular line tells the printer to move in a straight line towards the destination coordinates X=-9.2, Y=-5.42, and Z=0.5 at a feed rate of 3000.0. It also instructs the printer to extrude 0.0377 mm of material while it is moving.

How did we read and interpret that? It’s quite easy. Every line starts with a command. In this case, the command is G1.

G1 X-9.2 Y-5.42 Z0.5 F3000.0 E0.0377

It means “move in a straight line in a controlled fashion”. You can look up the meaning of every G-Code command in a table that we have provided at the end of the article. We will also go through the most important G-Code commands in a later section.

The code snippets that appear after the command are called arguments.

G1 X-9.2 Y-5.42 Z0.5 F3000.0 E0.0377


Try reading the following line of code now.

G1 X5 Y5 Z0 F3000.0 E0.02

If you interpreted it to mean “move towards X=5, Y=5, and Z=0 in a straight line at a feed rate of 3000.0 while extruding 0.02 mm of material”, then you have already learned how to read G-code commands!



show table

(source: RepRapWiki)

G-code Commands: A Simple Example

Now that you know how to read a line of code, let’s look at a simple example in action. The following video shows G-code commands at work in a cutting machine (not a 3D printer). The cutting machine will cut a circular edge in a rectangular slab. The G-code commands instruct the cutter on how to move to achieve the desired result.

Do not worry that the video is about a cutting machine. The geometric aspects of G-code commands work similarly for all machines that have a machine head. In the case of the 3D printer, the nozzle is the head. For the cutting machine, the head is the cutter. That’s the only difference. All other geometric aspects of the code remain the same.


The most important G-code Commands


G-code commands #1: G0 or “rapid motion”

The G0 command tells the print head to move at maximum travel speed from the current position to the coordinates specified by the command. The head will move in a coordinated fashion such that both axes complete the travel simultaneously. The nozzle will not extrude any material while executing this command. This G-code command is usually used to bring the nozzle rapidly to some desired coordinates at the start of the print or during the print.

Example: G0 X7 Y18

Image: Make Magazine

G-code commands #2: G1 or “controlled motion”

The G1 command tells the print head to move at specified speed from the current position to the coordinated specified by the G-code command. The speed is specified by the Feed rate parameter F.  The head will move in a coordinated fashion such that both axes complete the travel simultaneously. The printer can extrude material while executing this G-code command with extrusion specified by the extrusion parameter E. Most of the 3D printing happens while executing this command. If you open the G-code file for an actual 3D printing process, you will see a lot of G1 commands.

Example: G1 X7 Y18 F500 E0.02

Image: Make Magazine

G-code commands #3: G17/G18/G19 or “set planes”


G-code commands #4: G20/G21 or “set units”

These G-code commands set the units. G20 denotes inches while G21 denotes millimeters. This makes a big difference because


G0 X7 Y18



G0 X7 Y18


G-code commands #5: G28  or “homing”


G28.1 X0 Y0 Z0

G-code commands #6: G90 or “absolute mode”

Absolute mode tells the machine to interpret coordinates as absolute coordinates. This means a G-code command

G0 X10

will send the machine head to the coordinate X=10.

G-code commands #7: G91 or “relative mode”



G0 X10


G-code commands #8: G2 or “clockwise motion”



G21 G90 G17

G0 X6 Y18

G2 X18 Y6 I0 J-12

Image: Make Magazine

G-code commands #9: G3  or “counterclockwise motion”



G21 G90 G17

G0 X-5 Y25

G3 X-25 Y5 I0 J-20

Image: Make Magazine

G-code commands #10: Code comments




Programmers often need to write down explanations in plain English so that other programmers can understand the motivation behind a certain line or section of code. In fact, forget about other programmers! If you are looking at your own code after a year, chances are that you will have forgotten why you coded things in a certain way and would have a hard time figuring things out again.

To solve this problem, you can include code comments. Comments are written after adding a semicolon punctuation mark. You can write anything after adding a semicolon, but most often it is used to explain the rationale behind the code in a human-friendly way.  Anything that appears after a semicolon character in a line is ignored by the printer while executing the G-code commands and is only meant for human eyes.


G1 X-25 Y5  ; I am a code comment!

G-code Commands: The Structure of a Full-fledged Program

We are now in a good position to look at actual code that is used for printing a 3D model.


1. Initialization phase


Here are the first five lines of initialization G-code commands from an actual 3D printing task. You should be in a position to read and understand them at this point, with help from the reference table at the end.



M106 S0

M140 S100

M190 S100



2. Printing phase



Here is how the G-code commands look like.

G1 X108.587 Y111.559 F525 ; controlled motion in X-Y plane

G1 X108.553 Y111.504 F525 ; controlled motion in X-Y plane



G1 Z0.345 F500 ; change layer

G1 X108.551 Y111.489 F525 ; controlled motion in X-Y plane

G1 X108.532 Y111.472 F525 ; controlled motion in X-Y plane

3. Reset the printer

Finally, when the printing is over, some final lines of G-code commands bring the printer to a reasonable default state. For example, the nozzle is brought back to the origin, the heating is turned off (both for the bed and the extruder) and the motors are disabled.

G28 ; bring the nozzle to home

M104 S0 ; turn off heaters

M140 S0 ; turn off bed

M84 ;  disable motors

G-code Commands: Input and Output

Till now, we have only talked about the computer sending G-code commands to the printer, so it seems like the communication is one way. But 3D printing actually involves a two-way communication between the computer and the printer. Here’s how it works.

When you hit the print button on your computer, the 3D printing software starts sending the G-code commands to the printer, one line at a time. The printer executes the line and responds back to the computer. If the response indicates no error, the computer then sends the next line of code to be executed.

The printer’s response usually follows the following format:


<response> can be ok, rs or !!.


Temperature is reported in response to a M105 G-Code command.  The format of the response is

T:value B:value,


The coordinates are reported in response to M114 and M117 G-code commands. The format of response is



G-code Commands: Visualization Tools

Now that you know how to write G-code, it’s your turn to write some G-code commands and test your understanding. You can use an online visualization tool, where you can write some G-code commands and see the machine head move according to your instructions. It’s a lot of fun! We recommend that you try out this online visualization tool to test your skills.

Slicing software like Simplify3D or Cura also come with a G-code viewer. In the viewer, you will be able to visualize the path of the extruder for actual 3D printing tasks. Check out this must-see video for an excellent demonstration of the G-code viewer in Simplify3D.

G-code Commands: Preventing Print Failures

The G-code viewer can be the difference between a successful and failed print for tricky 3D models. In general, whenever you want to print a complicated 3D model, we advise that you run the viewer and go through the print simulation step by step.

We need to do this because the automatically generated code is often not ideal. You will often find that there are problematic areas that do not have enough support, leading to a failed print. In this case, you need to modify the code to ensure successful printing. Most of the time, this can be done by adding additional support structures using the graphical interface. Here is a video that shows how to do this for a complicated model of a 3D puppy.

G-code Commands: Conclusion

In conclusion, we learned about how a 3D printer prints a CAD model by following an instruction set written in G-code. We learned how to read the G-code commands, and saw some realistic examples. We discussed the most common G-Code commands and some ways of visualizing and testing them. Finally, we introduced G-code viewer, a common feature of Slicers, which can be used to prevent failed prints.

We hope that an understanding of G-code commands helps you become a more knowledgeable and powerful user of your 3D printer. If you found this article useful, share it with other 3D printing enthusiasts and spread the word. Do you have some questions or remarks? Let us know in the comments below!

Appendix 1: Compatibility notes

Each 3D printer comes with a firmware. There are many firmware’s, and developers of these firmware’s tend to implement different flavors of G-code commands. This leads to major compatibility issues. The G-code commands that work for one machine might not work for another.

This problem is usually solved by connecting the Slicer, which generates the code, to a machine specific post-processing driver. The post-processor detects the incoming code flavor and converts the code to the specific flavor^ that the firmware understands.

Therefore, the G-code commands that you see on the Slicer might not necessarily be the code being executed on the machine because of this subtle implementation detail.

Appendix 2: G-code list

show table

(source: Wikipedia)

Appendix 3: M-Code list

show table

(Source: Wikipedia)


License: The text of "3D Printer G-Code Commands – 2019 Programming Tutorial" by All3DP is licensed under a Creative Commons Attribution 4.0 International License.

Subscribe to updates from All3DP

You are subscribed to updates from All3DP

You can’t subscribe to updates from All3DP. Learn more… Subscribe

You can’t subscribe to updates from All3DP. Learn more…