Marlin G-Code: The Ultimate Guide (2023)

Replicating rapid prototypers, popularly known as 3D printers, rely on computer numerical control (CNC) to print the parts. 

CNC is the language spoken by computers controlling a machine, communicating all commands for movement and other actions. All of this is done through a standard language called G-code that works on different types of machinery. 

Marlin is an open-source G-code derivative developed for 3D printers specifically. Its popularity is owed to Marlin’s straightforward, reliable, and adaptable nature that works with most 3D printer brands. This firmware is also economical for hobbyists and vendors alike, and the program can adapt to added components.

What Is Marlin G-Code?

Marlin G-code is a type of firmware used by several respected 3D printer brands, including Creality, Prusa, LulzBot, and Ultimaker. 

Its role is to act as a communicator between the slicer software and the printer, helping transform an STL file into an actual physical object. 

While most 3D printers use Marlin G-code, the slicers typically generate G-codes automatically with no input required from users. 

When starting the printing process, the printer’s computer reads the commands received via G-code after slicing, moving the printer’s arm, and adjusting extrusion accordingly.

This automated process makes it easy for everyone to 3D print, regardless of the individual’s programming skills. Yet, understanding G-code basics and commands can help you unlock the next level of 3D printing. 

How Does Marlin Work?

Marlin is a derivative of G-code whose role is to manage all activities of the printer in real time. 

It runs on the printer’s main board and coordinates the machine’s heaters, buttons, sensors, lights, steppers, and all other components involved in the 3D printing process. 

Starting out with 3D printing and want to AVOID rookie MISTAKES? New: Filament Printing 101 Course
Starting out with 3D printing and want to AVOID rookie MISTAKES?
  • Our new Filament Printing 101 Course is just for you! Lean how to create perfect professional prints without all the hassle.
  • Don't let common mistakes hold you back, click the link to learn more and get ahead now!

It also communicates commands from the slicer to the printer, implementing the additive manufacturing process called fused deposition modeling (FDM). 

FDM basically refers to the process through which the printer’s motor pushes filament through a nozzle moved under computer control.

In a simplified version, this is how Marlin works: 

  1. A slicer software reads the STL file prepared with a CAD program. 
  2. The slicer prepares a solid 3D model from the STL file and slices it into thin layers. In the process, it generates the G-code that tells the printer how to reproduce that model.
  3. The sliced file is then transferred to a host, which can be a customized version of Cura or Repetier installed on the 3D printer board or another host, such as a desktop system, Raspberry Pi, or Android device. The host controls Marlin during printing.

Learning the various commands and functions of G-codes can help you edit the file and even make changes to the model without going through all of the steps above.


Popular G-Code Command and Functions

There are hundreds of G-code commands, including geometry and miscellaneous (machine) codes. All are coded alphanumerically, G standing for geometry and M for miscellaneous or machine. 

G-codes control the print head’s motion, path, or position. M-codes are responsible for turning the machine on and off, setting the cooling fan speed, etc.

Regardless of their type, the 20 codes below can help you customize printing settings on the go.

1. G0-G1: Linear Movement

Both commands move the arm from point A to point B in a linear movement. By convention, the G0 command moves the arm without extruding material. G1 command is used for movements that include extrusion. 

All G0 and G1 commands must include the final positions for the X, Y, and Z coordinates. G1 must also include the amount of extrusion to be performed during the move. The commands can also include the feed rate. 

Example: G1 X45 Y45 Z1 F1000 E1 

This command will move the arm in a straight line to the final coordinates X = 45mm, Y = 45mm, Z = 1mm at a feed rate of 1,000mm/min while extruding 1mm of material.

Tip: If you want to extrude material in a single layer, omit the Z-axis from the command line. 

2. G00: Move Fast

This command speeds up the arm movement at the beginning or end of a program. It is also useful to reset the head mid-way through printing.

The G0 code refers to movement without extrusion, while the second 0 adds speed. 

To perform this command, you have to input the current position and end position and let the firmware take care of the rest.

3. G92: Set Current Position

Sometimes, you may want to change or offset the location of your axes. This is where the G92 command comes in handy, enabling you to overwrite the position of any axis. 

One of the most popular uses of this command is overwriting the current filament position so that all future commands will become relative to this new value.

See also  Marlin Auto Bed Leveling Not Working? (Issue Resolved)

Typically, this command is given right before a prime or retraction command or at the start of each new layer. 

For example, the G92 E0 command sets the current filament position to 0. When followed by G1 F800 E10, the printer extrudes 10mm of filament at a feed rate of 800mm/minute, starting from the position set by the G92 command.

4. G90: Absolute Positioning

The G90 code is used at the beginning of a command queue to tell the printer to move to an exact XYZ coordinate. This command can be used on its own or together with M82, which sets the extruder axis (E) to absolute positioning.

Slicing software uses G90 positioning by default. 

For example: 

  • G90
  • G1 X20 Y20 F1200

This command sets all axes to absolute and moves X and Y to a 20mm position on the bed at a feed rate of 1,200mm/min.

5. G91: Relative Positioning

G91 command is similar to G90, but it refers to relative positioning. This means that it tells your printer head how far to travel from the current location. 

For instance, the G91 command followed by G1 X20 F1200 tells the printer to move the arm 10mm to the right of the current location at a feed rate of 1,200mm/min.

Using relative positioning is useful when you want to move the head a certain distance along an axis or if you don’t know the previous position of the nozzle. 

6. G02-G03: Circular Interpolation 

Also called arc or circle move, the circular interpolation adds a clockwise (G02) or counterclockwise (G03) arc move to the planner. 

By default, the arc move starts at the current position and ends at the given coordinates (XYZ), pivoting around a center point given by an X offset (I), Y offset (J), or radius offset (R). 

Users can choose which form of the command to use between axis (I and J) offset or radius offset. Mixing I or J with R will throw an error. 

To draw an arc using the I or J form, users must specify at least one of the I or J parameters and the X or Y position to which the head should move. If X and Y are omitted, the head will do a complete circle, returning to the original position.

Otherwise, the arc will end based on the angle of the destination.

Using the R form of the command won’t allow you to draw a full circle. In this form, omitting both X and Y will throw an error. You can input either X or Y, which must be different from the current XY position.

7. G28: Homing Routine

In its basic understanding, the G28 homing command is used to send the head back to where it all began. However, this command’s role is actually vital.

Most users tend to switch off the 3D printer right after a print is done without bothering to move the arm back to its original position.

When starting up the printer again, the machine doesn’t know where the head is positioned, so the firmware must trigger a homing procedure to establish a known position.

To do that, Marlin has to move each axis toward the end of the track until it triggers an endstop. Endstops are switches with a predetermined and known position, and once all endstops are triggered, Marlin can tell the printer where the arm is.

In its default form without other parameters, G28 homes all axes. However, users can input specific commands, such as G28 X or G28 Y, to only home the X or Y axis, respectively.  

Homing is required before leveling the bed through the G29 command, and it can also be required before other commands. 

Tip: If you’ve turned off the steppers on your 3D printer and the machine has been idle for some time, running a G28 can reconfigure the lost position.

8. G29: Bed Leveling 

Leveling the build plate is crucial to ensure the quality of your prints. After running a homing command, you can use G29 to level the bed in one of the following ways: 

  • 3-point: This automatic leveling type probes the bed at three points to reveal leveling issues. It then enables compensation and uses a rotating matrix to compensate for bed tilt.
  • Linear: Similar to the 3-point, this automatic procedure enables compensation to adjust bed tilt. However, Marlin probes the bed in a grid rather than only three points. 
  • Bilinear: Another automatic procedure; Marlin probes the bed in a fixed number of points and generates a mesh that represents bed imperfections. 
  • Unified: This comprehensive procedure uses a set of resources to optimize bed leveling. Automated probing allows users to compensate the entire bed and fine-tune the system, modifying the mesh based on print results. This enhances adhesion for the first layer and prevents problems like the elephant foot, but the command can be complicated to use.
  • Manual: Perhaps the most popular bed leveling method, this procedure enables users to measure Z height with a piece of paper or feeler gauge, then move Z coordinates to compensate for variations in bed height.
See also  Cura Z Hop: Boosting Accuracy & Precision In 3D Printing

9. G20-G21: Units Selection

One of the simplest but most useful G-code commands, the G20 and G21, enable users to switch from millimeters to inches. G21 sets the units in millimeters, whereas G20 calculates them in inches. 

This can come in handy if you have trouble designing your parts in the metric system and would rather use the US standard instead.

10. G17-G18-G19: Workspace Planes

With these three commands, 3D printing enthusiasts can set the planes in which they want the machine to execute the commands. 

G17 sets the XY plane (horizontal and vertical) and is used by default in most machines. The two other options allow you to switch to XZ (G18) or YZ (G19) planes. 

This command doesn’t require any other parameters and is generally inserted among the first ones in the queue, usually after homing and bed leveling.

11. G42: Move to Mesh Coordinate

If you want to move the nozzle to a specific position in the bed leveling mesh, the G42 command will allow you to do so.

This command requires you to set column and row coordinates, as well as a feed rate. The feed rate implemented through this command will then be used for all subsequent movements that don’t include a feed rate coordinate. 

An example G42 command should look like: G42 F800 I20 J20. This command would tell the nozzle to move to column 20 (I) and row twenty (J) of the mesh coordinate at a feed rate of 800mm/min.

Tip: By knowing the mesh size, you can use the various points of the mesh to move the nozzle in a specific position. For example, on a 9×9 mesh, you can use G42 I0 J0 to move the nozzle to the front-end corner. 

12. G60: Save Current Position

The G60 command comes in handy whenever you want the printer to memorize the position of all axes for later recall. 

To do that, Marlin uses slots that users can input manually. All slots are initially set to 0,0,0, and if this is the position that you want to save, you can omit inputting anything (just use the G60 command on its own). 

13. G61: Return to Saved Position

This command is used to recall the position saved with the G60 command. 

If you have omitted the slot, you can omit to input it in the G61 command too. Otherwise, the command could be given as G61 if you want to restore the previous position of all axes or follow it by the letter of the axis you want to restore. 

For example, G61 XY S0 restores to slot 0 position the axes X and Y without moving axes Z and E. 

14. G80: Cancel Current Motion Mode

The most common motion modes in Marlin are set by G-codes G0 and G1. However, the commands G2, G3, G5, and G38.X can also set motion modes. 

No matter which of these commands is used, the G80 command can be used to cancel it.

This command can also be used to switch from one motion mode to another. For instance: 

G0

X10 Y10

G80

G1 

X20 Y20

G80 

This command performs a non-extrusion movement of the X and Y coordinates to 10mm. G80 cancels the non-extrusion motion mode. G1 enables extrusion and moves the X and Y axes to 20mm. G80 at the end of the command disables extrusion movement.

15. G76: Probe Temperature Calibration

When leveling the bed, you may also want to calibrate its temperature. The G76 command allows you to do just that. 

This command enables the probe to measure the temperature of the bed by heating up as it gets closer to the heated bed, then cooling off as it moves away from it. At the same time, the procedure calibrates the temperature of the probe. 

You can calibrate the bed temperature or bed temperature only by adding a B for bed or P for probe after the command.

Note: This command can’t be used to calibrate the temperature of the hotend. 

16. M106-M107: Fan Control

When printing with a filament that requires cooling, the M106 commands can help you set the fan speed and the number of fans you want to turn on if the printer has more than one. The M107 command allows you to turn off the fan.

The M106 command requires you to input parameters for speed and fan number. 

S stands for speed; this number can range from 0 (off) to 255 (full speed). So, to set the fan at 50%, for instance, you must input 127.5.

If the printer has more than one fan, you can input a P index that can be 0, 1, 2, etc., depending on the number of fans you have.

See also  Cura Z Offset: The Ultimate Tool For Accurate 3D Printing

17. M25: Stop SD Print

Another one of the simplest but most useful commands, the M25, stops the print progress.

This command requires SD support, and you can also use it to park the nozzle if the Park Head on Pause mode is enabled.

18. M48: Probe Repeatability Test

A 3D printer’s probe is an essential tool for calibration. If it fails to work properly, it won’t calibrate the bed and other parameters, with a negative impact on your prints. 

The M48 command enables you to test the probe for precision by performing a series of repeated actions. 

Using obtained data, Marlin produces a standard deviation that can help you calibrate this component if needed.

19. M109: Wait for Bed Temperature 

When setting up the machine to print an object, you might have observed that the printer waits for the hotend to reach the temperature before enabling extrusion.

This is achieved through the M109 command. However, this command can also be handy if you want to switch from one type of filament to another or change the extrusion temperature mid-cycle.

An M109 command followed by S and temperature waits for the nozzle before extrusion only when heating. If followed by R and temperature, the printer will wait for the nozzle to reach the set temp regardless of whether it’s heating or cooling. 

Thus, if you want to switch from a higher temperature to a lower one, M109 R<temp> can help you do that.

20. M114: Get Current Position

The M114 will tell you the current position of the active tool, including the position of the steppers. 


Commonly Encountered Marlin G-Code Errors

G-codes make it so that the slicer can communicate with the printer. However, errors can sometimes occur. Let’s see how to fix the most common ones.

Communication Errors

Communication errors are the most frequent error types resulting from the manual editing of the G-code. They often occur due to typing errors or parameters that the user forgot to input. 

An easy way to fix this is by consulting the firmware’s log for details. Marlin generally highlights the error or line that needs your attention. 

Check the specific G-code in the Marlin manual, and pay attention to syntax when typing in or correcting the command.

Sometimes, communication errors can also happen due to a broken SD card or other external host problems. 

If you checked the code and all is fine, check the host, including connection cables and function, to make sure they work properly. 

A broken main board on the 3D printer can also be the culprit.

Incorrect Print Results

Marlin can affect print results by failing to move the nozzle correctly or by setting an incorrect bed or nozzle temperature. 

Once again, these issues are mostly caused by human error. 

You should check the commands, including their syntax, and make sure everything is set correctly.

If all commands are correct, make sure the bed and nozzle heat up correctly and that the arm moves properly. A broken thermistor or issues with the arm cables could be the culprits. 

Filament Runout

Filament runout is a command that moves the filament away from the print and lets the nozzle cool down until you replace the spool. 

With Marlin, you can set filament runout status and distance through the M412 command. 

Errors usually happen due to incorrect syntax or the absence of a filament runout sensor. The firmware may also fail to take action for filament runout if filament runout detection is disabled.

To fix the issue, make sure filament runout detection is enabled and that you have input the command properly.

Heating Failed

Marlin heating failed can happen when the bed or nozzle isn’t able to heat up for some reason or due to a thermal runaway error. 

Both errors can be triggered by a variety of factors, such as a low room temperature, a defective thermistor, setpoints for thermal protection, and so on. 

You should investigate each scenario to find out the cause and fix it.


Final Thoughts

Marlin is an open-source firmware commonly used by 3D printer brands to help translate STL files into commands machines can understand. 

Slicer software usually creates G-code commands automatically, but users can edit them to change the arm movement, temperature, or coordinates. 

Learning the most common G-code commands and how to troubleshoot errors can help you alter a model without changing the original STL file and fine-tune the printing process.

Recent Posts