In this article, I’ll present a guide to the most common G-code commands used in 3D Printing, what they mean, and how to work with them.
So you just got yourself a fancy new FFF (Fused Filament Fabrication) 3D Printer and started immersing yourself in this amazing technology for your hobby, work, or school and you wonder what this .gcode means at the end of the files that the slicer spits out and you use them to run in your 3D Printer? If so, then read on to learn the most common G-code commands that our 3D Printers use and what we can do with them.
What is G-code
G-code is how computer and CNC machines communicate digitally, it’s a programming language for computer numerical control (CNC). In simpler terms, it’s the language spoken by a computer controlling a machine, and it communicates all commands required for movement and control of the machine.
G-code is a standardized language for different desktop and industrial machinery, and you may not have dealt with it so far, which is actually normal, since 3D slicers generate the code automatically for our printers. Yet, if you want to develop a deeper understanding and mastery of 3D printing, it’s necessary to learn the basics of G-code. This knowledge will allow you to troubleshoot and control print processes much better, while also enabling the customization of 3D printer firmware like RepRapFirmware, Klipper, Marlin, and others.
For those without knowledge of programming in general, think of G-code as sequential lines of instructions, each telling the 3D printer to perform a specific task. These lines represent commands, and the printer executes them one by one until reaching the end of the code.
The G-code commands used in 3D Printing can be divided into two groups: general and miscellaneous commands. General command lines are used for types of motion in a 3D printer. Such commands are identified by the letter ‘G’, as in G-commands. Besides controlling the three or more axes movements performed by the printhead, they are also in charge of filament extrusion.
Meanwhile, the miscellaneous commands tell the machine to perform non-geometric tasks. In 3D printing, such tasks include heating commands for the nozzles and bed, fan control, and many other actions and parts of the machine. Miscellaneous commands are identified with the letter ‘M’.
Every G-code command line follows a certain syntax. Each line corresponds to only one command, and that’s the reason why, for the human reader, a G-code file looks awfully lengthy.
The first argument of a line is the command code itself. As we have seen, it can be either a ‘G’ or an ‘M’ code type, followed by a number that identifies the command. For example, “G1” corresponds to a linear move command.
After that comes the parameters that more accurately define the command. For a G1 linear move, that includes the final position and how fast it moves, also identified by upper-case letters. Each command has its own set of parameters as we’ll see soon.
A Note on G-code Comments
Before we get started, when we go over the various commands, you’ll see semicolons after a letter and number with additional text that explains what the code does. Here’s an example of a line that has a code comment:
G1 X50 Y20 ; This is a code comment!
Programmers often need to include explanations in plain English so that other programmers can understand certain lines or sections of code. It might also happen that you forget why you coded things in a certain way, resulting in a difficult time figuring things out again.
To solve this problem, code comments were created. Comments include anything (on the same line) following a semicolon and are completely ignored by the machine when it executes the G-code. In this way, they are meant for the programmers’ eyes.
Important commands for 3D Printing
While there are literally hundreds of G-code commands, in the following several sections, we’ll cover the most basic and important ones. Once you get experienced with them, you’ll be able to explore other commands from reference sheets on your own.
G0 & G1: Linear Motion
The G0 and G1 commands both perform linear movements. By convention, G0 is used for non-extrusion movements like initial and travel moves, while G1 encompasses all the extruding linear motion.
Both commands function the same. The parameters for G0 or G1 include the final positions for all the X, Y, and Z-axis, the amount of extrusion to be performed during the move, and the speed, specified by the feed rate in the set units.
G1 X101 Y50 Z0.5 F3000 E1
The command line above tells the printer to move in a straight line (G1) towards the final coordinates X = 101 mm, Y = 50 mm, Z = 0.5 mm at a feed rate (F) of 3,000 mm/min while extruding (E) 1 mm of material in the extruder.
Most linear moves are performed within a single layer, meaning that the Z coordinate is usually omitted from the command line as axes that are not moving stay in their previous set positions.
G90 & G91: Absolute & Relative Positioning
The G90 and G91 commands tell the machine how to interpret coordinates used for movement. G90 establishes “absolute positioning”, which is usually the default, while G91 is for “relative positioning”.
Neither command requires any parameters and setting one automatically cancels the other. The way positioning works is quite simple. Let’s say we want to move the printhead to X=30 in a line. In absolute positioning mode, that would look like this:
G90 ; sets absolute positioning G0 X30 ; moves to the X = 30 coordinate
This simple move would tell the printer to move the printhead so that it is positioned at X = 30. Now, for a relative positioning move, we need to know where the printhead is currently. Let’s assume it’s at X = 10:
G91 ; sets relative positioning G0 X20 ; moves +20 mm along the X-axis
G91 first tells the machine to interpret the coordinates as relative to the current position (X = 10). By knowing that, the machine simply needs to move 20 mm in the X-axis positive direction, thus reaching X = 30, as we wanted.
G28 & G29: Auto Home & Bed Leveling
We call “homing” the process of setting the physical limits of all movement axes. The G28 command will perform this task by moving the printhead until it triggers end-stops to acknowledge the limits.
Homing is important not only for the machine to orient itself but also to prevent the printhead from moving outside the boundaries. The G28 command is usually performed before every print process.
Specific axis can be individually homed by including X, Y, or Z as parameters. Otherwise, G28 alone will home all three.
G28 X Y ; home the X and Y axes only G28 ; home all axes
Another command, G29, starts the automatic bed leveling sequence. There are many different methods for leveling a bed prior to printing, as this is usually set by firmware and not by the final users. For this reason, we won’t get into details surrounding the methods and command parameters. Just know that G29 is usually sent after an auto-home (G28) and should perform the automatic bed leveling as determined by the firmware.
G29 ; perform automatic bed leveling sequence
M104, M109, M140, & M190: Set Temperature
These are essential miscellaneous commands, which don’t involve any motion.
To start, the M104 command sets a target temperature for the hot end to reach and keeps it until otherwise instructed. Some of the parameters include the actual temperature value (S) and which printhead (T) to heat (for multiple extrusion setups).
M104 S210 ; set target temperature for hot end to 210 degrees
This command-line instructs the machine to heat up its only hot end to 210 °C. After setting the target temperature, the printer will go on to perform the next command line while heating the hot end.
Alternatively, if we wanted to wait until that target is reached before moving on to the next line, we can use the M109 command:
M109 S210 ; set target temperature for hot end to 210 degrees and do nothing until reached
Setting the bed temperature is very similar to the hot end, but instead with the M140 and M190 commands:
M140 S110 ; set target temperature for bed to 110 degrees M190 S110 ; set target temperature for bed to 110 degrees and do nothing until reached
M106 & M107: Fan Control
Yet another essential task for 3D printers, the miscellaneous M106 and M107 commands provide fan control.
M106 turns a fan on and sets its speed. This is especially useful for the part cooling fan, as different speeds are required during the printing process during the first layer and bridging. The speed parameter must be a value between 0 and 255. A 255 value provides 100% power and any number within this range will specify a percentage accordingly.
M106 ; turn on a fan at maximum (100%) speed M106 S128 ; turn on a fan and set it to 50% power
Multiple speed-controlled fans can be defined by the index (P) parameters, as each fan is assigned an index by the firmware. The M107 command will power off a specified fan. If no index parameter is provided, the part cooling fan is usually the one to be shut down.
We’re now in a good position to look at an actual piece of code that’s used for 3D printing. G-code programs can be divided into three distinct sections, as we’ll see next.
It’s worth noting that if you use a text editor to open a G-code file generated by a 3D slicer, it might be that it won’t immediately start with G- or M-commands. For example, a slicer like Cura or Simplify3D starts code by including some of the printing process parameters defined previously within comments. These lines won’t affect the printing but instead present a quick reference for parameters like layer height, for example.
Phase 1: Initialization
The first section of any program includes the preparation tasks required prior to starting printing the model. The following are the first six lines of initialization G-code commands from an actual 3D printing job.
G90 M82 M140 S80 M104 S200 G28 G29
The first line says that movements should use absolute positioning, while the second line tells the extruder to also interpret extrusion in absolute terms.
The third and fourth lines start heating the bed and nozzle to their target temperatures. Note that it won’t wait for the target temperature, meaning that the printer will auto-home and level the bed while heating up.
Some initialization routines (e.g. the one used by PrusaSlicer) also include a nozzle purging process, like printing a single straight line before jumping into the printing process.
Phase 2: Printing
Here’s where the magic happens. If you look at a sliced G-code file, you’ll see that it’s impossible for us to make out what the nozzle is actually doing.
3D printing is a layer-by-layer process, so you’ll find that this phase includes many movements within the XY plane while printing a single layer. Once that’s done, one tiny movement in the Z direction will define the beginning of the next layer.
G1 X103.505 Y153.291 E4.5648 ; movement and extrusion in XY plane G1 X103.291 Y153.505 E4.5804 ; movement and extrusion in XY plane G1 Z0.600 F3000 ; change layer G1 X104.025 Y154.025 F9000 ; movement in XY plane G1 X95.975 Y154.025 E0.4133 F1397 ; movement and extrusion in XY plane
Phase 3: Reset the Printer
Finally, when printing is done, some final lines of G-code commands bring the printer to a reasonable default state. For example, the nozzle might go to a pre-defined position, the hot end and bed heaters are turned off, and the motors are disabled, among other actions.
M107 ; turn fan off G1 Z10 ; move nozzle away from print M104 S0 ; turn hot end heating off M140 S0 ; turn bed heating off M84 ; turn motors off
Terminal inputs and outputs
Until now, we’ve only talked about the computer sending G-code commands to the printer (usually transferred via an SD card). However, this isn’t the only method of communication. Some control software, like Pronterface and OctoPrint, allows direct communication with the 3D printer, in which case you can input commands manually.
For obvious reasons, it wouldn’t be practical to print anything by sending lines of codes individually. But sometimes this method of communication is needed for other purposes, like retrieving valuable information for calibration or even when the 3D printer lacks a display screen. For example, the M105 “report temperatures” command will retrieve the current nozzle and bed temperatures (which might then be displayed by something like OctoPrint).
This communication is also very useful for seeing and changing EEPROM settings that are hardcoded at the firmware level. Parameters like a motor’s steps/mm, maximum feed rates, or PID can be visualized via M503 (“report settings”), changed manually, and then saved via M500 (“save settings”).
By now, you should be able to read and understand G-code much better. Still, you can also benefit from writing it. This G-code online visualization is a great tool for testing your skills, as you can write G-code commands and simulate them accordingly.
There are many types of 3D printing firmware, and each might have different “flavors” of G-code. This can lead to major compatibility issues, as commands that work for one machine might not work for another, but we’ll leave that for a future post, as for the basic stuff you don’t really need to worry about it.