3D Printer G-Code Commands List & Tutorial - All3DP
3D Printer G-Code Commands List & Tutorial - All3DP
3D Printer G-Code Commands List & Tutorial - All3DP
PRO
All3DP is an editorially independent publication. Editorial content, on principle, can not be bought or
influenced. To keep All3DP free and independent, we finance ourselves through advertising and affiliate
revenues. When you purchase using a shopping link on our site we earn an affiliate commission. Learn more
Get an A in G-code
by Dibya Chakravorty
Jan 22, 2020
ADVERTISEMENT
709
SHARES
Understanding G-code commands is the key to your 3D printer. Quickly learn this code
using our tutorial. Includes a list of all commands.
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
know 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:
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 speci ed 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 speci c 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:
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.
The code snippets that appear after the command are called arguments.
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(E) of 0.0377 mm.
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!
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 de ne
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 speci c 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.
(source: RepRapWiki)
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.
Example: G0 X7 Y18
Image: Make Magazine
G20
G0 X7 Y18
G21
G0 X7 Y18
G0 X10
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.
Example:
G0 X6 Y18
G2 X18 Y6 I0 J-12
Example:
G0 X-5 Y25
G3 X-25 Y5 I0 J-20
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 guring 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.
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 rst 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 con guration after the print
nishes. Let’s take a look at these sections one by one.
1. Initialization phase
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 rst section of any program.
Here are the rst ve 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 rst line sets the coordinates to absolute positioning. The second line tells the
extruder to interpret extrusion 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 fth 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 gure 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.
2. Printing phase
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 nal
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).
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.
<response> [<line number to resend>] [<current printer parameters>] [<Some debugging or other
information>]
• Ok means that no error has been detected. This prompts the computer to send the
next line of code to the printer.
• Rs means “resend the instruction”. This is usually followed by the line number to
resend.
• Two exclamation marks(!!) implies hardware error. The machine shuts down
immediately in this case and the print job is aborted.
In addition to these 3 responses, the printer might also report printer parameters like
temperature, coordinates of the nozzle etc. to the computer.
T:value B:value,
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
Here, C stands for “coordinates follow”. This is followed by current X, Y, Z coordinates and
other information.
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 nd 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.
This problem is usually solved by connecting the Slicer, which generates the code, to a
machine-speci c post-processing driver. The post-processor detects the incoming code
avor and converts the code to the speci c avor that the rmware 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.
(source: Wikipedia)
License: The text of "3D Printer G-code Commands List & Tutorial" by All3DP is licensed under a Creative Commons
Attribution 4.0 International License.
SUBSCRIBE TO UPDATES
Stay informed with noti cations from All3DP.
ADVERTISEMENT
TOPICS
3D PRINTING TUTORIAL
Comments that breach our community
guidelines will be removed.
Please read our Comment Policy
before commenting.
1
Sort by Best
LOG IN WITH
OR SIGN UP WITH DISQUS ?
Name
ADVERTISEMENT
All3DP
Information
ABOUT US
CONTENT POLICIES
PRIVACY POLICY
TERMS OF USE
IMPRINT
Links
CRAFTCLOUD
ADVERTISE WITH US
WRITE FOR US
JOB OPPORTUNITIES
PRIVACY SETTINGS
Follow us
FACEBOOK
TWITTER
NEWSLETTER
SUBSCRIBE