What Does the G29 G-Code Do in Marlin Firmware? (Detailed)

While 3D printing is an easy hobby to get into, it’s undeniably a hard one to master. Since the knowledge and the experience you require increase as the prints become more complex, there is always something new to learn.

G-Codes are especially handy to have a good grasp of, as they allow you to control your 3D printer to its fullest extent. Considering that all the actions a 3D printer is able to perform can be denoted with a G-Code, having experience with them is a significantly valuable trait.

While we possibly can’t cover all of them in a single article, our topic will be one of the most vital G-Codes in the Marlin firmware today, which is the G29 G-Code.

So, what does the G29 G-Code do in the Marlin firmware?

The G29 G-code in the Marlin firmware executes the Bed Leveling function, provided that you have enabled it in the configuration file.

As there are five separate bed leveling systems available, the G29 G-code invokes the one you have enabled in the configuration file before building the firmware.

While the main functionality of the G29 G-code is to level the bed, the strategy it uses entirely depends on the configuration, with some of them performing better than the others depending on the scenario.

What Does the G29 G-Code Do in Marlin Firmware?

The G29 G-code is one of the most loaded G-codes in the Marlin firmware due to it having five different modes you need to pick from depending on the bed of your 3D printer.

In a nutshell, the G29 G-code in the Marlin firmware starts the Bed Leveling process with the strategy you have enabled in the configuration (Configuration.h) file.

As the G29 G-code requires you to enable one of the five strategies before you build and flash the firmware to the 3D printer, it will not function at all if you haven’t.

While enabling the functionality of the G29 G-code is a straightforward task, the process can feel slightly foreign if you have never modified the configuration file before.

Next up, we will be sharing the steps you will need to take to enable the G29 G-code by selecting the strategy you want to use, which should come in handy, especially for beginners.

How to Enable the G29 (Bed Leveling) G-Code in Marlin Firmware?

To enable the functionality of the G29 G-code, you will need to edit the “Configuration.h” file and uncomment (remove the // at the start of the line) the line which corresponds to the strategy you want to enable before you build the firmware.

Here are the five strategies and their definitions in the config file:

  • Manual Mesh Bed Leveling – #define MESH_BED_LEVELING
  • Unified Bed Leveling – #define AUTO_BED_LEVELING_UBL
  • Bilinear Auto Bed Leveling – #define AUTO_BED_LEVELING_BILINEAR
  • Linear Auto Bed Leveling – #define AUTO_BED_LEVELING_LINEAR
  • 3-Point Auto Bed Leveling #define AUTO_BED_LEVELING_3POINT

As you are only allowed to choose a single strategy for the G29 G-code, do not uncomment more than one of these definitions.

Moving on, we will be analyzing each of these strategies in detail and help you to find out which one works best for the type of bed you have.

Marlin G29 G-Code Bed Leveling Strategies

The bed leveling strategies in the Marlin firmware are separated into two categories, which are mesh-based leveling and matrix-based leveling.

Mesh-Based Leveling

Mesh-based leveling strategies generate a 3D mesh of the printer bed’s warped areas by measuring the height of the printer bed at different points.

The printer uses this mesh to lower or raise the nozzle by an extra margin during print, which allows it to compensate for the warped areas of the bed.

Manual Mesh Bed Leveling

Manual mesh bed leveling is a strategy that can create a mesh without a bed probe with some manual work on your side.

Here’s how to use manual mesh bed leveling:

  1. Run G29 S1 to start probing the mesh points.
  2. Adjust the Z-axis of the nozzle so that there is only enough space for a piece of paper to fit between it and the printer bed.
  3. Run G29 S2 to save the value and repeat until the process is complete.
  4. (Optional) Save the configuration to EEPROM with M500.

If you have saved your mesh to the EEPROM, you can load it with M420 S1 the next time you print.

You can activate manual mesh bed leveling with the MESH_BED_LEVELING option in the configuration file.

Bilinear Auto Bed Leveling

Bilinear auto bed leveling creates a mesh without needing manual work but requires a probe to do so.

Here’s how to use bilinear auto bed leveling:

  1. Run G28 (Auto Home).
  2. Run G29.
  3. Run M420 S1 to turn leveling back on, as G28 disables it.
  4. (Optional) Save the configuration to EEPROM with M500.

You can activate manual mesh bed leveling with the AUTO_BED_LEVELING_BILINEAR option in the configuration file.

Unified Bed Leveling

Unified bed leveling is a strategy that you can either use through automatic or manual probing of the mesh points and usually produces the best results.

Here’s how to use unified bed leveling:

  1. Run G28 (Auto Home).
  2. Run G29 P1 (auto-probing) or G29 P2 B T (manual probing) depending on whether your printer has a probe or not.
  3. Run G29 P3 T to fill the rest of the mesh.
  4. Run G29 S1 to save the mesh points to EEPROM.
  5. Run G29 F 10.0 to set the fade height to 10.0 mm.
  6. Run G29 A to activate Unified Bed Leveling.
  7. (Optional) Run M500 to save the configuration to EEPROM.

You can activate manual mesh bed leveling with the AUTO_BED_LEVELING_UBL option in the configuration file.

Matrix-Based Leveling

Matrix-based leveling strategies use three points or a grid to measure the height of the bed and create a rotation matrix with these data points.

The printer uses this matrix to perform rotations on the object relative to the print bed, meaning that if the print bed is not entirely flat, the printer may print your object in a tilted position.

As a result, matrix-based leveling strategies are most suitable for completely flat printer beds.

Linear Auto Bed Leveling

The linear auto bed leveling strategy allows the printer to probe the bed in a grid to form the rotation matrix it will use for compensation.

While manual probing is possible, we recommend using automatic probing for this strategy.

Here is how you can use linear auto bed leveling:

  1. Run G28 (Auto Home).
  2. Run G29.
  3. Run M420 S1 to turn leveling back on, as G28 disables it.
  4. (Optional) Save the configuration to EEPROM with M500.

You can activate manual mesh bed leveling with the AUTO_BED_LEVELING_LINEAR option in the configuration file.

3-Point Auto Bed Leveling

The 3-point auto bed leveling strategy allows the printer to probe the bed in 3 different points to form the rotation matrix it will use to compensate for bed tilt.

While manual probing is possible, we recommend using automatic probing for this strategy.

Here is how you can use 3-point auto bed leveling:

  1. Run G28 (Auto Home).
  2. Run G29.
  3. Run M420 S1 to turn leveling back on, as G28 disables it.
  4. (Optional) Save the configuration to EEPROM with M500.

You can activate manual mesh bed leveling with the AUTO_BED_LEVELING_3POINT option in the configuration file.

Wrapping Up

While it’s certainly not easy to be accustomed to all the G-codes, we highly recommend getting yourself familiar with the G29 code and the strategies you can use with it to take your print quality to the next level.

As bed leveling is a vital part of 3D printing, using the correct bed leveling strategy can easily make or break how your prints turn out, which is where extensive knowledge of the G29 code comes into play.

We will be doing detailed explanations of more G-codes that we think are vital in the future, so stay tuned if you have enjoyed this article.

To get more information about the G29 G-Code and its parameters, we highly recommend checking the Marlin G-code docs, which is the source we have used to compile this article.