Table of Contents
The next level of 3D printing will be unlocked by understanding G-code commands. Continue reading to grasp this code’s fundamentals quickly!
G-code is a computer numerical control programming language (CNC) in its simplest form. In other words, it’s the language a computer uses to operate a machine and transmits all the instructions needed to move and perform other tasks.
Although G-code is the common language for various desktop and industrial machines, our 3D printers may be the reason we are most familiar with it. It’s possible that you haven’t dealt with it yet, which is understandable, given that 3D slicers produce the code “automatically.”
However, learning the fundamentals of G-code is imperative if you want to gain a deeper understanding of 3D printing. You will also be able to change the firmware of 3D printers like RepRap and Marlin and be much better able to troubleshoot and control the printing process.
This article will go over the fundamentals of G-code, including how to read, comprehend, and write a few lines of commands, giving even complete beginners in coding a strong foundation. Then, without further ado, let’s begin at the beginning!
What Is It?
Those who are new to programming, in general, consider G-code to be a series of instructions that each direct the 3D printer to carry out a particular task. The printer runs each of these lines, referred to as commands until it reaches the end of the code.
“G-code” can mean the whole programming language, but it can also mean “general” commands, one of the two types of commands used in 3D printing.
General command lines control motion types in a 3D printer. They are in charge of filament extrusion and the movement of the printhead’s three-plus axes. The letter “G,” as in “G-commands,” is used to designate such commands.
The letter “M” designates commands that are not explicitly classified. Contrarily, the various commands give the machine instructions to carry out non-geometric tasks. We’ll see that some of these tasks in 3D printing include fan control, nozzle and bed heating commands, and many others.
Each G-code command line adheres to a specific syntax. Because there is only one command per line, this can result in long codes.
Any given line’s first argument is always the command code itself. As we’ve seen, it can be either a “G” or a “M” code type, then a command-specific number. For instance, the command “G0” denotes a linear move.
The command is then further defined by its parameters. As we’ll see in a moment, each command has a unique set of parameters. These parameters for a G0 linear move include the destination position and the velocity, denoted by capital letters.
A Note on G-code Comments
Before we begin, you’ll notice semicolons after a letter and a number that describe what the code does when we go over the various commands. Here is an illustration of a line with a code comment: I am a code comment: G1 X25 Y5
For other programmers to understand specific lines of code or chunks of code, programmers frequently need to include explanations in plain English. You might also lose track of the rationale behind why you programmed something a certain way, which makes it challenging to understand it again.
When the machine runs the G-code, it completely disregards anything (on the same line) after a semicolon, known as a comment. Code comments can help to solve this issue. They are exclusively intended for programmers’ eyes in this way.
IMPORTANT COMMANDS FOR 3D PRINTING
We’ll focus on the most fundamental and significant G-code commands in the following sections because hundreds of them exist. Once you know it well, you’ll be able to use reference sheets to look up other commands.
G0 & G1: Linear Motion
Both the G0 and G1 commands carry out linear motion. Conventionally, G0 is used for initial and travel moves that do not involve extrusion, while G1 includes all extruding linear motion.
Both commands function the same, though. The parameters for G0 or G1 include the final positions for all the X-, Y-, and Z-axes, the amount of extrusion to be performed during the move, and the speed, specified by the feed rate in the set units.
Example
G1 X90 Y50 Z0.5 F3000 E1 instructs the printer to move in a straight line (G1) in the direction of the final coordinates (X = 90 mm, Y = 50 mm, and Z = 0.5 mm, while extruding (E) 1 mm of material in the extruder at a feed rate (F) of 3,000 mm/min.
Since most linear moves are carried out within a single layer, the Z coordinate is typically left off the command line.
G90 & G91: Absolute & Relative Positioning
The machine is instructed to interpret movement coordinates by the G90 and G91 commands. G91 establishes “relative positioning,” while G90 establishes “absolute positioning,” which is typically the default.
Both commands are automatically canceled when one is set, and neither needs any parameters. Let’s get started since positioning is relatively easy to understand.
Example
Say we want to move the printhead to the 30th position in a line. According to absolute positioning mode, that would appear as follows:
G90 ; sets absolute positioning
G0 X40 ; moves to the X = 40 coordinate
This straightforward action would instruct the printer to position the printhead at X = 40. We need to know the printhead’s current location to move relative to positioning. Suppose it’s at X = 20:
G91 ; sets relative positioning
G0 X20 ; moves +20 mm along the X-axis
The first command from G91 instructs the computer to interpret the coordinates relative to the current position (X = 20). Knowing that, all the machine needs to do to get to the desired X = 40 is move 20 mm in a positive X-axis direction.
G28 & G29: Auto Home & Bed Leveling
Determining the physical boundaries of each movement axis is referred to as “homing.” The G28 command will move the printhead forward until the end-stops are triggered, which means the limits have been reached.
In addition to helping the machine find its orientation, homing helps keep the printhead from straying outside the lines. Typically, the G28 command is run before each print operation.
G29, a different command, initiates the automatic bed leveling process. There are many different ways to level a bed before printing because firmware controls this rather than end users. Because of this, we won’t go into specifics about the methods and command parameters. Just be aware that G29, which is typically sent after G28, the automatic home, should level the bed automatically in accordance with the firmware.
Example
G28 X Y ; home the X and Y axes only
G28 ; home all axes
X, Y, or Z can be used as parameters to individually home a particular axis. Otherwise, G28 will defeat all three by himself.
G29 ; perform automatic bed leveling sequence
Remember to send G29 after homing if you want to run an automatic bed leveling sequence.
M104, M109, M140, & M190: Set Temperature
These are necessary auxiliary commands that, once again, don’t involve motion.
The M104 command first sets a temperature goal for the hot end to reach and keep until told otherwise.
Some of the parameters are the actual temperature (S), the printhead to be heated (T), and others (for setups with more than one extrusion).
Example
M104 S200 ; set target temperature for hot end to 200 degrees
This command line assumes only one hot end in this extrusion setup and tells the machine to heat its hot end to 200 °C. After setting the target temperature, the printer will carry out the subsequent command line while the hot end is heated.
The M109 command can also delay moving on to the next line until that target has been reached.
M109 S200 ; set target temperature for hot end to 200 degrees and do nothing until reached
Using the M140 and M190 commands, you can set the bed temperature similarly to how you would the hot end:
M140 S60 ; set target temperature for bed to 60 degrees
M190 S60 ; set target temperature for bed to 60 degrees and do nothing until reached
M106 & M107: Fan Control
The other M106 and M107 commands control the fan, yet another crucial function for 3D printers.
M106 activates a fan and regulates its speed. This is especially helpful for the cooling fan for the part because different speeds are needed for the first layer and bridging during printing.
A number between 0 and 255 must represent the speed parameter. Any number falling within this range will specify a percentage in the same manner as a value of 255, which has 100% power.
Example
M106 ; turn on a fan at maximum (100%) speed
M106 S128 ; turn on a fan and set it to 50% power
Because the firmware gives each fan an index, the index (P) parameters can be used to set the speed of more than one fan.
The M107 command will also turn off the selected fan. In the absence of an index parameter, the part cooling fan is typically the first to be turned off.
PROGRAM STRUCTURE
We’re in a good position to examine a real 3D printing code. As we’ll see in the next section, G-code programs can be split into three categories.
It’s essential to be aware that if you open a G-code file created by a 3D slicer in a text editor, it might not immediately begin with G- or M-commands. For instance, when starting code, a slicer like Cura or Simplify3D will include some of the printing process parameters that have already been defined in comments. Instead of affecting printing, these lines are a quick reference for details like layer height.
3D PRINTER G-CODE COMMANDS: LIST & TUTORIAL
Phase 1: Initialization
Any program’s first section lists the steps that must be taken to get ready to start printing the model. The first six lines of initialization G-code from a real 3D printing job are shown below.
G90 M82 M140 S80 M104 S200 G28 G29
We now understand that the first line instructs movements to use absolute positioning and the second line instructs the extruder to interpret extrusion similarly.
The bed and nozzle begin to warm up to the desired temperatures thanks to the third and fourth lines. Because it won’t wait for the desired temperature, the printer will automatically home and level the bed as it heats up.
Some ways to start a 3D printer, like the one used by PrusaSlicer, involve “purging” the nozzles by printing a single straight line before starting to print.
Phase 2: Printing
The magic takes place here. We can’t discern what the nozzle is actually doing if you look at a sliced G-code file.
Since 3D printing is a layer-by-layer process, you’ll notice that this phase involves numerous movements in the XY plane even though only a single layer is being printed. Once complete, the start of the following layer will be defined by a slight movement in the Z direction.
Here is an illustration of how G-code instructions might appear during the printing stage:
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
When printing is finished, a few last lines of G-code instructions return the printer to a reasonable default state.
The hot end and bed heaters could be turned off, the nozzle could move to a predetermined position, the motors could be disabled, and so on.
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 & Outputs
Until now, all that has been discussed is how the computer instructs the printer using G-code (usually transferred via an SD card). This isn’t the only means of communication, though.
Some control software, like Pronterface and OctoPrint, lets you type in commands by hand. This lets you talk directly to the 3D printer.
Obviously, it would be impossible to print anything by sending lines of codes one at a time. However, there are times when this communication method is necessary for other reasons, such as getting important data for calibration or even when the 3D printer doesn’t have a display screen.
Using the M105 “report temperatures” command, for instance, you can get the nozzle and bed temperatures right now (which might be displayed by something like OctoPrint).
Viewing and modifying firmware-level EEPROM settings using this communication is also very helpful. Through M503 (“report settings”), parameters like a motor’s steps/mm, maximum feed rates, or PID can be seen, changed by hand, and saved (“save settings”).
Writing G-code
You should be much better at reading and comprehending G-code by this point. However, writing it can also be advantageous to you.
You can write G-code commands and simulate them using this online visualization of G-code, which is a great resource for testing your abilities. It’s a lot of fun!
You should also be able to gain some understanding of how G-code functions for 3D printing by looking at exported G-code files from slicers. Explore the code while keeping a reference sheet of commands close by.
Compatibility
We anticipate that once you comprehend G-code instructions, you’ll be a more knowledgeable and effective 3D printing user. Despite not being the most complex computer language, learning G-code takes time and effort.
Before this article ends, discussing G-code compatibility in more depth might be helpful.
There are numerous varieties of 3D printing firmware, and those variations may include various “flavors” of G-code. This could cause major compatibility issues because commands on one machine might not work on another.
The code is routed through machine-specific post-processing drivers by the slicer software to handle this. The post-processor determines what kind of code is being sent and changes it so that the firmware can understand it.
We sincerely hope you have enjoyed this brief guide. If you like this article,
Let us know in the comments below or on our Facebook page to let us know your ideas, and we would appreciate seeing pictures of your works of art!