Ultimate Marlin G-Code Guide – How to Use Them for 3D Printing
G-Codes are widely used in 3D printing, especially through the Marlin firmware. Many people wonder how to use G-Codes to their benefit, so I decided to write this article to help readers out.
There are some useful details about G-Code in the rest of this article, so keep reading for more.
What Are G-Codes in 3D Printing?
G-Code is simply a programming language for CNC (Computer Numerically Controlled) machines like 3D printers, CNC mills, etc. It contains a set of commands that the firmware uses to control the printer’s operation and the printhead’s motion.
How is G-Code Created?
G-Code for 3D printers is created using a special application called a slicer. This program takes your 3D model and slices it into thin 2D layers.
It then specifies the coordinates or path for the printhead to pass through to build up these layers. It also controls and sets specific printer functions like turning on the heater, fans, cameras, etc.
Popular slicers on the market include PrusaSlicer and Cura.
Types of G-Code
Although the general name for CNC commands is G-Code, we can broadly divide the commands into two categories; They include:
- G-Code
- M-Code
G-Code
G-Code stands for Geometry code. Its primary function is to control the print head’s motion, position, or path.
Using G-code, you can move the nozzle in a straight line, position it in a specific place, raise or lower it, or even move it through a curved path.
They are prefaced by a G to show that they are G-Code.
M-Code
M-Code stands for Miscellaneous commands. They are machine commands controlling the printer’s other functions apart from the printhead’s motion.
Things they are responsible for include; turning the motors on and off, setting the fan speed, etc. Another thing M-Code is responsible for is setting the bed’s temperature and the nozzle’s temperature.
They are prefaced by an M, which stands for miscellaneous.
What are G-Code ‘Flavors’?
The G-Code flavor refers to the way your printer’s firmware (Operating System) expects its G-Code to be formatted. Different flavors exist due to the different G-Code standards and firmware that various printer brands use.
For example, standard commands like the move, heater on, etc., are common among all printers. However, some niche commands aren’t the same, which can lead to print errors if used with the wrong machine.
To counter this, most slicers have options to set up your printer profile so that you can choose the right flavor for your machine. The slicer will then translate the 3D file into the appropriate G-Code for your machine.
Some examples of G-Code flavors include RepRap. Marlin, UltiGcode, Smoothie, etc.
List of Main G-Codes in 3D Printing
There are numerous G-Code commands out there available for different 3D printer firmware. Here are some of the common ones you might come across while printing and how to use them.
Marlin M0 [Unconditional stop]
The M0 command is known as the unconditional stop command. It stops the printer’s operation after the last motion and turns off the heaters and the motors.
After stopping the printer’s operation, it either sleeps for a set period or waits for user input to come back online. The M0 command can take three different parameters.
These parameters are:
- [P<time(ms)>]: This is the amount of time you want the printer to sleep in milliseconds. For example, if you want the printer to sleep for 2000ms, you’ll use the M0 P2000
- {S<time(s)>]: This is the amount of time you want the printer to sleep in seconds. For example, if you want the printer to sleep for 2 seconds, you’ll use the M0 S2
- [message]: You can use this parameter to display a message on the printer’s LCD while it’s paused. For example, M0 Press the center button to restart the print.
Note: The M0 command is the same as the M1 command.
Marlin M81
The M81 command shuts down the printer’s PSU (power supply unit). This means that all the heaters, motors, etc. will not be able to work.
Also, if the board doesn’t have any alternative source of power, it also shuts down too.
Marlin M82
The M82 command puts the extruder in absolute mode. This means if the G-Code calls for the extruder to extrude 5mm of filament, it extrudes 5mm irrespective of any previous commands.
It overrides G90 and G91 commands.
The command affects only the extruder, so it’s independent of other axes. For example, consider this command;
M82;
G1 X0.1 Y200.0 Z0.3 F1500.0 E15;
G1 X0.4 Y20 Z0.3 F1500.0 E30;
The extruder is set to absolute mode using M82 in line 1. In line 2, it draws the first line by extruding 15 units of filament.
After line 2, the extrusion value isn’t set back to zero. So, in line 3, the E30 command extrudes 30 units of filament using the E30 command.
Marlin M83
The M83 command sets the printer’s extruder to relative mode. This means if the G-Code calls for a 5mm filament extrusion, the printer extrudes 5mm cumulatively, based on previous commands.
The M83 command doesn’t take any parameters. For example, let’s run the last example’s command back with M83.
M83;
G1 X0.1 Y200.0 Z0.3 F1500.0 E15;
G1 X0.4 Y20 Z0.3 F1500.0 E30;
After the E15 command on line 2, the E value isn’t set back to zero; it remains at 15 units. So, on line 3, instead of extruding 30 units of filament, it will extrude 30-15 = 15 units.
Marlin M84
The Marlin M84 command disables one or more of the stepper and extruder motors. You can set it to either disable them immediately or after the printer stays idle for some time.
It can take four parameters. They include:
- [S<time(s)>]: This specifies the amount of idle time before the command kicks in and disables the motor. For example, M84 S10 disables all the steppers after being inactive for 10 seconds.
- [E], [X], [Y], [Z]: You can use either one or more of these to choose a specific motor to idle. For example, M84 X Y idles the X and Y motors.
Note: If you don’t use any parameters with the command, it immediately idles all the stepper motors.
Marlin M85
The M85 command shuts down the printer and firmware after a period of inactivity. It takes in a time parameter in seconds.
If the printer is idle with no movement for longer than the set time parameter, then the printer will shut down. For example, if you want to shut down your printer after it’s idle for 5 minutes, you can use the command:
M85 S300
Marlin M104
The M104 command sets a target temperature for the printer’s hotend and starts heating it. After setting the target temperature, the command doesn’t wait for the hotend to reach the temperature.
It immediately moves on to run other G-Code commands while the hotend heats up in the background. It takes five parameters, which are:
- [S<temp (°C)>]: It specifies the target temperature for the extruder in Celsius.
- [T<index (0 | 1)>]: If the printer has more than one nozzle, you can use this to choose the nozzle you want to set the temp for.
- [B< temp (°C)>]: It sets the maximum temperature the hotend is allowed to reach. You can use this with the S parameter to set a temperature range for the hotend.
For example, if you want to heat the second nozzle of a dual-extruder machine up to 200°C, you’ll use the command M104 S200 T1.
Marlin M105
The M105 G-Code command requests a temperature report to be sent to the host. It’s reported that some hosts can hide the reply from the M105 command, so it’s better to use the M155 command.
You can get the extruder temperature, bed temperature, and even build chamber temperature in degrees Celsius. It’s a standalone command, so all you need to do is send the M105 command to the printer.
The report should include the actual and target temperature of the available heaters.
- T – Extruder temperature
- B – Bed temperature
- C – Chamber temperature
Marlin M106
The M106 command turns on the printer’s fan and sets its speed. You can select the fan and set its speed by using its parameters.
These parameters include:
- [S<0-255>]: This parameter sets the speed of the fan with values ranging from 0 (off) to 255 (full speed).
- [P<index (0, 1, …)>]: It determines the fan you want to turn on. If left blank, it defaults to 0 (print cooling fan). You can set it to 0, 1, or 2 depending on the number of fans that you have.
For example, if you want to set the nozzle cooling fan to 50% speed, the command is M106 S127. The S value is 127 because 50% of 255 is 127.
You can also use the M106 command without any parameters to set the cooling fan’s speed to 100%.
Note: The fan speed command doesn’t come into effect until G-Code commands preceding it are done.
Marlin M107
The M107 shuts down one of the printer’s fans at a time. It takes a single parameter, P, which is the index of the fan that you want to shut down.
If the parameter isn’t given, P defaults to 0 and shuts down the print cooling fan. For example, the command M107 shuts down the print cooling fan.
Marlin M109
Like the M104 command, the M109 command sets a target temperature for the hotend and heats it. However, unlike M104, it waits for the hotend to reach the target temperature.
After the hotend reaches the target temperature, the host continues executing G-Code commands. It takes all the same parameters the M104 command takes.
However, it adds one extra. That one is:
- [R< temp (°C)>]: This parameter sets the target temperature to heat or cool the hotend to. Unlike the S command, it waits until the printer heats or cools the nozzle to this temperature.
The S command waits on heating but not on cooling.
For example, if you want the nozzle to cool down to 120°C from a higher temp, the command is M109 R120.
Marlin M112 Shutdown
The M112 is an emergency stop G-Code command. Once the host sends the command, it immediately stops all the printer’s heaters and motors.
Any move or print in progress is also immediately stopped. After activating this command, you will have to reset your printer to resume printing your model.
In Marlin firmware, the command may get stuck in the queue and take a while before executing. To avoid this, you can enable the EMERGENCY_PARSER flag to execute the command immediately after it is sent to the printer.
You can enable this by going to your advanced printer configuration file (Marlin/Configuration_adh.v) then remove some text from it as follows:
// Enable an emergency-command parser to intercept certain commands as they // enter the serial receive buffer, so they cannot be blocked. // Currently handles M108, M112, M410 // Does not work on boards using AT90USB (USBCON) processors! //#define EMERGENCY_PARSER
You’ll need to remove the // before the #define EMERGENCY_PARSER and recompile the sources.
You can learn more about updating Marlin firmware in the video below.
Marlin M125
The M125 command pauses the print and parks the printhead in a pre-configured parking location. It also saves the nozzle’s current position to memory before parking.
There’s usually a preconfigured parking position set in the printer’s firmware. You can park the nozzle at this position by using the M125 command alone.
However, you can change it using one or more of these parameters.
- [L<length>]: This retracts a set length of filament from the nozzle after parking
- [X<pos>], [Y<pos>], [Z <pos>]: You can combine one or more of these coordinate parameters to set a new parking position for the printhead.
If you want to park the nozzle at the origin and retract 9mm of filament, the command is M125 X0 Y0 Z0 L9.
Marlin M140
The M140 command sets a target temperature for the bed and continues executing other G-Code lines immediately. It doesn’t wait for the bed to reach the target temperature before returning control to the host.
The bed continues heating up in the background while the printer executes other lines of G-Code. It takes one parameter, which is:
- [S< temp (°C)>]: This parameter sets the target temperature for the bed in Celsius.
For example, to heat the bed up to 80°C, the command is M140 S80.
Marlin M190
The M190 command sets a target temperature for the bed and waits until the bed reaches it. It doesn’t return control to the host or execute any other G-Code till the bed reaches that temperature.
Note: If you set the target temperature with the S parameter, it only waits while heating the bed UP to the set temperature. However, if the bed has to cool down to reach that temperature, the host doesn’t wait.
For the command to wait while heating and cooling, you must set the target temperature with the R parameter. For example, to cool the bed down to 50°C and wait until it reaches that temperature, the command is M190 S50.
Marlin M400
The M400 command pauses the G-Code processing queue until all the current moves in the buffer are complete. The processing queue waits in a loop till all the commands are completed.
After completing all the moves, the printer continues executing the G-code after that line. For example, look at the G-Code below:
M400;
M81;
Line 1 pauses processing until all the current moves are done, and then line 2 shuts down the 3D printer using the M81 power off G-Code.
Marlin M420
The M420 command retrieves or sets the 3D printer’s bed leveling state. This command only works with printers that have automatic bed leveling systems.
After leveling, these printers create a mesh from the print bed and save it to the EEPROM. The M420 command can help retrieve this mesh data from the EEPROM.
It can also enable or disable the printer from using this mesh data for printing. It can take several parameters, which include:
- [S<0|1>]: It enables (1) or disables (0) bed leveling. The printer must have a valid mesh in the EEPROM to enable bed leveling.
- [l<index>]: It specifies the bed leveling mesh you want to use if there are multiple files. It takes in the index of the mesh data as its value.
- [V<0|1>]: It prints out the mesh data if it is set to 1. The default value is 0
- [T<0|1|4>]: It sets the format in which the retrieved mesh data will be printed. 0 prints it in human-readable form, 1 prints it in CSV, and 4 prints it in compact form.
- Z<linear>]: It sets a Z fade height for the printer. After this height, the printer stops using the mesh compensation.
For example, let’s say you want to print out the second mesh data in the EEPROM in CSV format. The right command to use is: M420 V1 I1 T1
Marlin M420 S1
The M420 S1 is a subset of the M420 command. It enables bed leveling on the printer using a valid mesh it retrieves from the EEPROM.
If there is no valid mesh in the EEPROM, it will not do anything. It is usually found after the G28 homing command.
Marlin G0
The Marlin G0 is the rapid move command. It moves the nozzle from one position to another on the build plates through the shortest possible distance (straight line).
It doesn’t lay down any filament while moving, which enables it to move faster than the G1 command. Here are the parameters it takes:
- [X<pos>], [Y <pos>], [Z<pos>]: These parameters set the new position to move to on the X, Y, and Z axes.
- [F<mm/s>]: The feed rate or speed of the printhead. The printer will automatically use the feed rate from the last G1 command if left out.
So, if you want to move the printhead rapidly to the origin at 100mm/s, the command is G0 X0 Y0 Z0 F100.
Marlin G1
The G1 command moves the printer from one point to another on the build plate in a linear path. It’s known as the linear move command because it extrudes filament while moving between points.
This differentiates it from the rapid move (G0), which doesn’t lay down filament while moving. It takes several parameters, including:
- [X<pos>], [Y <pos>], [Z<pos>]: These parameters set the new position to move to on the X, Y, and Z axes.
- [E<pos>]: This sets the amount of filament to extrude while moving to the new point.
- [F<mm/s>]: The feed rate or speed of the printhead. The printer will automatically use the feed rate from the last G1 command if left out.
For example, to lay filament down in a straight line between two points at a rate of 50mm/s, the right command is G1 X32 Y04 F50 E10.
Marlin G4
The G4 command pauses the machine for a set period. The command queue is paused during this time, so it doesn’t execute any new G-Code command.
During the pause, the machine still maintains its state. All heaters retain their current temperatures, and the motors are still on.
It takes two parameters, which are:
- [P<time(ms)>]: This specifies the pause time in milliseconds
- [S<time(s)>]: This sets the pause time in seconds. If both parameters are set, the S takes precedence.
To pause the machine for 10 seconds, you can use the command G4 S10.
Marlin G12
The G12 command activates the printer’s nozzle cleaning procedure. First, it moves the nozzle to a preset location on the printer where a brush is mounted.
Next, it moves the printhead aggressively across the brush to clean up any filament stuck on it. Here are some of the parameters it can take.
- [P<0|1|2>]: This parameter lets you choose the cleaning pattern you want for the nozzle. 0 is straight back and forth, 1 is a zigzag pattern, and 2 is a circular pattern.
- [S<count>]: The number of times you want the cleaning pattern to repeat itself.
- [R<radius>]: The radius of the cleaning circle if you choose pattern 2.
- [T<count>]: This specifies the number of triangles in the zig-zag pattern.
If you want to clean your nozzle on the brush in a back-and-forth pattern, the right command is G12 P0.
Cura provides a way to use this command in its experimental settings. You can read more about the wipe nozzle command in this article I wrote on How to Use Experimental Settings in Cura.
Marlin G20
The G20 command sets the printer’s firmware to interpret all units as inches. So, all the extrusion, movement, print, and even acceleration values will be interpreted in inches.
So, the printer will have inches for linear motion, inches/second for speed, and inches/second2 for acceleration.
Marlin G21
The G21 command sets the printer’s firmware to interpret all units as millimeters. So, linear movements, rates, and acceleration will be in mm, mm/s, and mm/s2, respectively.
Marlin G27
The G27 command parks the nozzle at a pre-defined position on the build plates. It waits until all movements in the queue are completed, then it parks the nozzle.
This is very helpful when you want to pause printing to make adjustments to the print. You can park the nozzle to avoid hovering over the print and melting it.
It takes one parameter, which is:
- [P<0|1|2>]: This determines the Z-park location. If you choose 0, the firmware will raise the nozzle to the Z-park location only if the nozzle’s initial height is lower than the Z-park location.
Choosing one parks the nozzle at the Z park location no matter its initial height. Choosing 2 raises the nozzle by the Z-park amount but limits its Z Height to lower than the Z max.
If you use the G27 command without any parameters, it defaults to P0.
Marlin G28
The G28 command homes the printer to establish a known location at the origin. Homing is the process by which the printer finds the origin (coordinate [0,0,0]) of the printer.
It does this by moving each axis of the printer until they hit their respective limit switches. Where each axis triggers its limit switch is its origin.
Here are some of its parameters:
- [X], [Y], [Z]: You can add any of these parameters to restrict homing to these axes. For example, G28 X Y homes only the X and Y axes.
- [L]: It restores the bed leveling state after homing.
- [0]: This parameter skips homing if the printhead’s position is already trusted.
For example, if you want to home only the X and Z axes, the right command is G28 X Z. To home all the axes, you can use the G28 command alone.
Marlin G29
The G29 is the automatic bed leveling command. It deploys the automatic or semi-automatic bed leveling system installed on your machine to level the bed.
Depending on the printer’s brand, you can have one of five complex bed leveling systems in your firmware. They include:
- Mesh bed leveling
- Auto bed leveling
- Unified bed leveling
- Auto bed leveling (linear)
- Auto bed leveling (3-point)
Each one has specific parameters to work with the printer’s hardware.
Marlin G30
The G30 command probes the build plate at a specific point with the probe of an automatic bed leveling system. It does this to determine that point’s Z height (the distance from the nozzle to the bed).
After getting the height, it sets the nozzle to the proper distance above the build plate. It takes some parameters, which include:
- [C<0|1>]: Setting this parameter at one enables temperature compensation since most materials expand while heated.
- [X<pos>], [Y<pos>]: These parameters specify the coordinates where you want to probe.
To probe the bed at the nozzle’s current position, you can use the command without any parameters. To probe it at a specific location like [100, 67], the right command is G30 X100 Y67.
Marlin M76
The M76 command pauses the print job timer.
Marlin G90
The G90 command sets the printer to absolute positioning mode. This means that all coordinates in the G-Code are interpreted as positions in the XYZ plane relative to the printer’s origin.
It also sets the extruder to absolute mode unless the M83 command overrides it. It doesn’t take any parameters.
Marlin G92/G92 E0
The G92 command sets the nozzle’s current position to the specified coordinates. You can use it to exclude certain areas of your print bed and also set offsets for your printer.
The G92 command takes in several coordinate parameters. They include:
- [X<pos>], [Y<pos>], [Z<pos>]: These parameters take in the coordinates for the printhead’s new position.
- [E<pos>]: This parameter takes in a value and sets it as the extruder’s position. You can use the E0 command to reset the extruder’s origin if it’s in relative or absolute mode.
For example, let’s say you want the center of your bed to be the new origin. First, make sure your nozzle is in the middle of the bed.
Next, send the G92 X0 Y0 command to your printer.
Note: The G92 command maintains the physical boundaries set by the end-stops. You can’t use G92 to move outside the X limit switch or below the print bed.
So, that’s it! The G-Codes above represent a small but essential part of the G-Code library every 3D print enthusiast should know.
As you print more models, you might run into more G-Code commands you can add to your library.
Good luck and Happy Printing!