G-code is the programming language of your 3D printer. Quickly learn all G-Code commands using our tutorial and reference manual.
Using G-code, a computer tells a printer when, where, how to move and how much to extrude throughout the entire print process.
If you have never dealt with it so far, that’s normal. Slicers like Cura and Simplify3D generate G-code “automagically” from CAD models, so most users never see or program a single line of code. However, if you want to develop a deeper understanding of 3D printing, it is essential to know about this programming language.
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:
Let’s get started!
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.
For those who don’t know about programming languages, you can think of G-code as sequential lines of instructions. Each line tells the printer to do a specific task. The printer executes the line one by one until it reaches the end.
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 material at a rate of 0.0377 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
Each argument tells the printer about how to execute the command. The arguments start with an English letter and then specify a value. For example, X-9.2 means a destination X coordinate of -9.2. F3000.0 means a Feed rate(F) of 3000.0. E0.0377 means an Extrusion rate(E) of 0.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 material at the rate 0.02”, then you have already learned how to read G-code commands!
G-code commands which start with the letter G are geometric commands. They tell the printer head how to move, but this is clearly not enough to control all aspects of a 3D printer. What if you needed to tell the printer to turn the motor off or raise the bed temperature? For these non-geometric tasks, G-code implementations also define another set of commands which start with the letter M. They are aptly called M Codes. For example, the command M140 sets the bed temperature, and the command M190 tells the printer to wait for the temperature to reach the target.
Each English letter that you encounter in the code will have a specific meaning. For example, we learned that G means a geometric command, M means a non-geometric command, X means the X coordinate, Y means the Y coordinate, F means Feed rate and so on. For your reference, here’s a table with the meaning of every letter.
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.
If you understand the cutter’s movements, you will also know how to move a print head.
In the last section, we discussed the G1 command, which means “move the nozzle in a controlled fashion in a straight line”. This is just one of the many G-code commands. In this section, we will discuss other important commands that are used frequently.
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
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 at an extrusion rate specified by the extrusion rate 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
These G-code commands set the plane in which the nozzle should move. Typically, G17 is the default for most machines and it denotes the X-Y plane. G18 denotes the Z-X plane and G19 denotes the Y-Z plane.
These G-code commands set the units. G20 denotes inches while G21 denotes millimeters. This makes a big difference because
G20 G0 X7 Y18
means “move rapidly to X=7 inches and Y=18 inches” while
G21 G0 X7 Y18
means “move rapidly to X=7 mm and Y=18 mm”.
A G28 command tells the machine to go to its home position. A home position can be defined by the G28.1 command as follows.
G28.1 X0 Y0 Z0
Absolute mode tells the machine to interpret coordinates as absolute coordinates. This means a G-code command
will send the machine head to the coordinate X=10.
The relative mode is the opposite of the absolute mode. G91 tells the machine to interpret coordinates as relative coordinates. If the machine is currently at X=10, then the following G-code commands
G91 G0 X10
tell the machine to move 10 units in the X direction from its current position. At the end of the operation, the machine head will be located at X=20.
G2 tells the machine to move clockwise starting from its current location. The endpoint is specified by the coordinates X and Y. The center of rotation is specified by the parameter I, which denotes the X offset of the current position from the center of rotation. J denotes the Y offset of the current position from the center of rotation.
G21 G90 G17 G0 X6 Y18 G2 X18 Y6 I0 J-12
Just like the G2 command, the G3 command creates a circular motion but in the counterclockwise direction.
G21 G90 G17 G0 X-5 Y25 G3 X-25 Y5 I0 J-20
If you look at any real world G-code file, you will find that in addition to G-code commands and arguments, it also contains things written in plain English. Here’s an example:
G0 X-25 Y5 ; rapid movement to X=-25 and Y=5
The English text will always be preceded by a semicolon, as you can see in the above line.
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.
Here is another example of a line that has a code comment.
G1 X-25 Y5 ; I am a code comment!
We are now in a good position to look at actual code that is used for printing a 3D model.
Most G-code programs contain three important sections. The first section initializes the printer for the printing process. The second section instructs the printer to print the model. The third section resets the printer to its default configuration after the print finishes. Let’s take a look at these sections one by one.
Certain tasks need to be performed before a print can begin. For example, we need to heat the print bed, heat the extruder, purge the nozzle, bring the nozzle to the start position etc. These tasks form the first section of any program.
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.
G90 M82 M106 S0 M140 S100 M190 S100
The first line sets the coordinates to absolute positioning. The second line tells the extruder to interpret the extrusion rate as absolute values. The third line turns the fan on, but sets the speed to 0, which essentially means that the fan is off. The fourth line sets the bed temperature to 100 degrees. The fifth line tells the printer to wait till the bed temperature reaches the desired value, in this case, 100.
During the initialization phase, the printer will not extrude any material except when it is purging the nozzle. This is an easy to way to figure out when the initialization phase stops and the actual printing begins. During the actual printing, the printer will be extruding material at almost every step.
A 3D printer prints a model layer by layer. Slicers like Simplify3D or Cura typically slices a 3D model into many horizontal layers that stack on top of each other to create the final print.
Therefore, the print phase consists of many movements in the X-Y plane (printing a single layer), then one movement in the Z direction (move to next layer) followed by many movements in the X -Y plane again (print the next layer).
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
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
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:
[ ] [ ] [ ] can be ok, rs or !!.
In addition to these 3 responses, the printer might also report printer parameters like temperature, coordinates of the nozzle etc. to the computer.
Temperature is reported in response to a M105 G-Code command. The format of the response is
where T indicates the extruder temperature and B indicates the bed temperature. If the machine does not have a temperature sensor, then -273 is returned as a value.
The coordinates are reported in response to M114 and M117 G-code commands. The format of response is
C: X:9.2 Y:125.4 Z:3.7 E:1902.5.
Here, C stands for “coordinates follow”. This is followed by current X, Y, Z coordinates and other information.
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.
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.
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!
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.
License: The text of "2018 3D Printer G-Code Commands – Tutorial & Manual" 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…