3D Printer G-Code Commands List & Tutorial - All3DP

Download as pdf or txt
Download as pdf or txt
You are on page 1of 24
At a glance
Powered by AI
G-code is a programming language that controls 3D printers by providing instructions for movement and extrusion. Understanding G-code can help troubleshoot prints and customize the printing process.

G-code stands for 'Geometric Code' and is used to instruct 3D printer movements and actions like extrusion in 3 dimensions. It works by having the printer execute lines of code sequentially to complete a print.

A typical G-code command starts with a letter code like G1, followed by values for coordinates, feed rate, and other settings like amount of extrusion. These values are known as arguments that provide more details to execute the command properly.

Get it 3D printed for the be… Learn More

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

3D Printer G-code Commands List & Tutorial

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:

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

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

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.

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!

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)

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.

If you understand the cutter’s movements, you will also know how to move a print head.

CNC PROGRAMMING G-C…


G-C…
The most important G-code Commands

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.

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 speci ed 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 a speci ed speed from the current
position to the coordinated speci ed by the G-code command. The speed is speci ed 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 speci ed by the extrusion parameter E. Most of the
3D printing happens while executing this command. If you open the G-code le 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”


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.

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

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

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


A G28 command tells the machine to go to its home position. A home position can be
de ned by the G28.1 command as follows.
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”


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.

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


G2 tells the machine to move clockwise starting from its current location. The endpoint is
speci ed by the coordinates X and Y. The center of rotation is speci ed 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.

Example:

G21 G90 G17

G0 X6 Y18

G2 X18 Y6 I0 J-12

Image: Make Magazine

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


Just like the G2 command, the G3 command creates a circular motion but in the
counterclockwise direction.

Example:

G21 G90 G17

G0 X-5 Y25

G3 X-25 Y5 I0 J-20

Image: Make Magazine

G-code commands #10: Code comments


If you look at any real-world G-code le, you will nd 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 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.

Here is another example of a line that has a code comment.

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

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

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

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 nal 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> [<line number to resend>] [<current printer parameters>] [<Some debugging or other
information>]

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

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

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


response is

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

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.

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.

Exploring G-code for 3D printing


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

Use G-code Preview to prevent 3D Printing f…


f…
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 rmware. There are many types of rmware, and
developers of these rmware tend to implement different avors 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-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.

Appendix 2: G-code list

(source: Wikipedia)

Appendix 3: M-Code list


(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

RECOMMENDED FOR YOU


All3DP Comment Policy
Keep discussion on-topic and respectful.


Comments that breach our community
guidelines will be removed.
Please read our Comment Policy
before commenting.

Comments Community 🔒 Login


1

 Recommend 5 t Tweet f Share

Sort by Best

Join the discussion…

LOG IN WITH
OR SIGN UP WITH DISQUS ?

Name

aplsw • 2 months ago


Hi. Good explanation.
However, I have a question that
hope to get help. Here i have a
project need to turn the eject nozzle
into a valve. How can i use G-code
to send like a pulse signal? for
example, can i turn the heater on for
20 ms and turn off?
△ ▽ • Reply • Share ›

Lukas Dlugos • 9 months ago


Hi,

I would like to ask you if the only


way to program my 3D printer is
with the "G-code" or there are other
programming methods?

Thank you very much for your reply.


△ ▽ • Reply • Share ›

Tyler Vonsmite • a year ago


The g does not stand for geometric.
It stands for general. In cnc there
are general and miscellaneous
codes - what we call g & m code or
g code for short. The code that
controls your 3d printer is basically
the same code that has controlled
cnc mills, lathes, etc. for many
many years. It absolutely has the
ability to do basic programming
constructs such as loops, while, if,
etc. Being able to hand program is
an important skill, one that someone
with some rudememtry knowledge
should be discussing - not you!
△ ▽ • Reply • Share ›

Vasiliy Dulskiy > Tyler


Vasiliy Dulskiy > Tyler
Vonsmite • 2 months ago

Authors, I think, it would be


beneficial to this website to
evaluate the comment
below, research and make
corrections to the original.
△ ▽ • Reply • Share ›

Matt Bearup • a year ago


Coolest looking "puppy" I've ever
seen ;-)
△ ▽ • Reply • Share ›

Rumah Grafis • a year ago


thank you
△ ▽ • Reply • Share ›

Mary Mansfield • 2 years ago


HOW DO I UNSCRIBE FROM
THIS?
△ ▽ • Reply • Share ›

Anatol Locker Mod >


Mary Mansfield
• 2 years ago
To unsubscribe you have to
visit www.all3dp.com and
locate the bell icon in the
header menu navigation. It
can be found in the right side
of the menu.
Click on this icon once to
unsubscribe to notifications.
△ ▽ • Reply • Share ›

Jürgen Rudolf Schulberger


• 2 years ago
Very useful, thanks a lot. I am just
starting to know gcode To improve

ADVERTISEMENT
All3DP

The World's #1 3D Printing Magazine for Beginners and Professionals.

Information

ABOUT US

CONTENT POLICIES

PRIVACY POLICY

TERMS OF USE

IMPRINT

EDITORIAL ADVISORY BOARD

Links

CRAFTCLOUD

ADVERTISE WITH US

WRITE FOR US

JOB OPPORTUNITIES

PRIVACY SETTINGS

Follow us

FACEBOOK
TWITTER

NEWSLETTER

SUBSCRIBE

You might also like