Marlin Firmware G29 (Bed Leveling) G-Code – Explained

We can consider bed leveling compensation to be one of the most vital features of Marlin Firmware for a successful print, as an imperfect print bed that’s not compensated for, whether due to incorrect tramming or warping, will practically make it impossible to print a high-quality first layer without problems.

In this guide, we will explain the function of the G29 (Bed Leveling) G-code command in Marlin Firmware, take you through the different mesh-based bed leveling systems that Marlin Firmware can utilize when you run the G29 G-code, and take a look at the commonly asked question of whether it would be necessary to run M420 S1 after G29.

What Is the G29 (Bed Leveling) G-Code in Marlin Firmware?

Even though its exact function and the parameters it accepts depend on the particular bed leveling system that’s activated through the configuration file, the G29 G-code command, in essence, prompts your 3D printer to gather the necessary data for the activation of the bed leveling compensation feature, whether with manual assistance from you, or through the usage of an automatic bed leveling sensor that probes the bed automatically.

marlin g29 example in octoprint


While Marlin can either generate a mesh or a matrix by using this data, depending on the bed leveling method selected in the firmware configuration, we will be focusing on mesh-based leveling for the rest of this article as it’s the go-to method for practically all the widely used home 3D printers due to it compensating for both tilt and warp, unlike matrix-based leveling that assumes the bed is perfectly flat without imperfections and only compensates for tilt.

So, provided that you have selected a mesh-based leveling system, the way the mesh building process initiated by G29 works is to divide the print area into a particular number of pieces on both the X and Y axes (denoted in the firmware configuration), measure the distance between the nozzle and the print bed for each corner of each piece, choose a reference point (zero point), and calculate the Z-axis distance from each of these points to the reference point, which effectively creates a map of imperfections caused by factors such as tilt and warping across the entirety of the print area.

As an example, if you run G29 with both GRID_MAX_POINTS_X and GRID_MAX_POINTS_Y set to 3, you will find that the print area is split into four evenly-sized squares, which will create a mesh that has 9 different readings in total once your 3D printer gathers the height data from each corner of each of the squares, such as the example mesh below that has the center of the build area as its reference point, and 8 other points with varying distances to the center.

ender 3 g29 unified bed leveling mesh example


Once your 3D printer has access to a mesh that shows precisely how much higher or lower the print bed is at each of the XY positions relative to the reference point, it becomes able to adjust the Z-axis position accordingly throughout the printing process, whether it’s raising the nozzle for areas that are higher, or lowering the nozzle for areas that are lower, effectively ensuring that the nozzle maintains the same Z distance to the print bed throughout the entirety of each layer and preventing issues such as the nozzle scraping into the build surface or printing in the air, which essentially is how bed leveling compensation works.

Following up from the example image from earlier, a value of 0.07 for the front-left corner would effectively mean that your 3D printer will need to raise the nozzle by an extra 0.07 mm compared to the center when printing around this area of the print surface, as the mesh tells us that the front-left corner of the print bed is 0.07 mm higher than the center point, which acts as the reference in this case.

Marlin Firmware Mesh-Based Leveling Systems (UBL, ABL, Manual)

When configuring Marlin Firmware, you will find that you have access to three different mesh-based leveling systems (and two matrix-based leveling systems that we won’t be discussing in this article), which are mesh bed leveling (manual mesh), automatic bed leveling (ABL), and unified bed leveling (UBL).

Mesh Bed Leveling (Manual Mesh)

Manual mesh bed leveling is a bed leveling system that allows you to generate the bed leveling mesh through manual measurements across the print area without the need for an automatic bed leveling probe, which makes it the go-to choice in cases where you would like to utilize mesh bed leveling but don’t have an ABL probe installed on your 3D printer.

marlin config mesh bed leveling explanation


To activate manual mesh bed leveling in Marlin and get your 3D printer to initiate this process when you send the G29 G-code command, you will need to ensure that you define the MESH_BED_LEVELING (and LCD_BED_LEVELING if your 3D printer has an LCD controller, which will make things more convenient) parameter in the Configuration.h file before flashing the firmware, as there is no other way to switch between bed leveling systems.

enabling manual mesh leveling in marlin config


When generating the bed leveling mesh with the manual mesh system, the first step you need to take is to send the G28 G-code command, which will home the axes of your 3D printer.

Next, you will need to send the G29 S1 G-code command to your 3D printer, which will initiate the bed leveling process and automatically bring the nozzle over to the first point of the mesh.

Once the nozzle is positioned correctly, you can use the paper method (the same one used for optimizing the Z offset for those familiar) to find the correct Z-axis value, where you slide a piece of paper under the nozzle, bring the nozzle down (you can use the G0 G-code command) until the nozzle grips the paper tightly enough to make harder to slide it away but not so tight that you would end up ripping it, which effectively tells you that the gap between the nozzle and the print bed is optimal.

Finally, you will need to send the G29 S2 G-code command to save the Z value you have found & bring the nozzle to the next point of the mesh, and keep repeating the process of finding the optimal Z value with the paper method and sending the G29 S2 G-code command until you go through all of the necessary points and complete building the mesh.

Alternatively, if your 3D printer has an LCD controller, and you have enabled LCD_BED_LEVELING in the firmware configuration, you can initiate manual mesh bed leveling through the LCD controller by using the Level Bed option in the bed leveling menu instead of sending the G29 G-code command, which will bring up a wizard that will take you through the process of manually probing each point interactively.


Regardless of whether you used the G-code or the LCD controller method, the last step you will need to take is to save the mesh to the EEPROM with the M500 G-code command (or the Store Settings / Store Memory option on the LCD controller), which will tell your 3D printer to preserve the mesh and make it possible to use the same mesh with M420 S1 after powering your 3D printer off and back on.

Automatic (Bilinear) Bed Leveling (ABL)

Automatic (bilinear) bed leveling is a bed leveling system that utilizes an ABL sensor, such as a BLTouch or a CR Touch, to generate the bed leveling mesh automatically, where the sensor gathers the necessary data by probing a fixed number of points throughout the print area without the need for any manual intervention.

marlin configuration ABL bilinear explanation


To activate automatic (bilinear) bed leveling in Marlin and get your 3D printer to probe the bed automatically by using this bed leveling system, you will need to define the AUTO_BED_LEVELING_BILINEAR parameter in the Configuration.h file before flashing the firmware to your 3D printer, which will modify the behavior of the G29 G-code command accordingly.

enabling bilinear leveling in marlin config


To generate a mesh with the bilinear bed leveling system, the first step you will once again need to take is to home the axes of your 3D printer with G28, as this is the only way for the firmware to know the position of the bed leveling sensor while building the mesh.

Next, you will need to send the G29 G-code command to your 3D printer, which will prompt the automatic bed leveling sensor to automatically probe the number of points specified in the firmware configuration, save the generated mesh into the temporary storage of your 3D printer, and activate bed leveling compensation for your prints without any further input required from you.

Unlike the manual mesh method, where it’s practically a necessity to save the mesh to the EEPROM for later usage, as it would be highly inconvenient to manually build the bed leveling mesh every time you power cycle your 3D printer, you can choose whether you would like to save the mesh or not when using the bilinear bed leveling system, as the mesh generation process barely requires any effort or time in this case.

The primary benefit of regenerating the bed leveling mesh for each print, as you may predict, is that the mesh is always up-to-date with the current status of the print bed, which practically ensures that it becomes impossible issues regarding the bed’s tilt or warp to reduce the quality of your prints.

On the other hand, saving the mesh (M500) and reusing it (M420 S1) for further prints comes with the benefit of reducing wear and tear on the automatic bed leveling sensor, and considering that the bed’s tilt and warp shouldn’t see considerable changes after a single print, our recommendation would be to go with this method instead.

That being said, as the mesh you have generated won’t stay optimal forever, our recommendation would be to rebuild the mesh before long prints (to be on the safe side), after a change to the bed’s tilt (such as tramming the bed), when you notice issues regarding bed leveling, and possibly after a number of prints (once per 15-20 prints should do) as a preventative measure.

Unified Bed Leveling (UBL)

Unified bed leveling is a bed leveling system that also utilizes an automatic bed leveling sensor to generate the bed leveling mesh automatically in a similar but more precise way compared to the bilinear bed leveling system with the higher number of points it uses to create the mesh, and comes with some extra features that make it possible to modify and validate the mesh after generating it.

marlin configuration UBL explanation


To activate unified bed leveling in Marlin and make it the system that your 3D printer will use once you send the G29 G-code command, the parameter you need to define in the Configuration.h file before flashing the firmware to your 3D printer is AUTO_BED_LEVELING_UBL.

enabling unified leveling in marlin config


Similar to the other bed leveling systems we have talked about so far, the first step you will need to take to generate a bed leveling mesh with the unified bed leveling system is once again to home the axes of your 3D printer with the G28 G-code command to make the position data available to the firmware.

Next, you will need to send the G29 P1 G-code command (G29 alone won’t have any effect), which will initiate the automatic bed leveling process where the sensor starts probing each of the points necessary to create the bed leveling mesh and activate the bed leveling compensation afterward.

While the mesh generation process in the case of unified bed leveling is also fully automatic, the fact that it can go up to 225 individual points and that it comes with tools that make it possible to correct parts of the mesh after generating it makes saving the mesh into the EEPROM and reusing for further prints the correct way of doing things, which, in this case, requires you to send the G29 S<index> G-code command, such as G29 S0 to save your mesh to the first slot.

Finally, to load a saved mesh and activate bed leveling compensation when using UBL, the commands you will need to send to your 3D printer are G29 L<index>, such as G29 L0, which will load the mesh you have saved into the first slot, and then G29 A (or M420 S1), which will activate the unified bed leveling system for bed leveling compensation.

Is It Necessary to Run M420 S1 After G29?

Since running the G29 G-code command already prompts the 3D printer to activate bed leveling compensation after generating the mesh, it’s not necessary to run M420 S1 after G29, as running M420 S1 would practically have no effect in a scenario where bed leveling compensation is already active.

That being said, even though your bed leveling mesh will persist through power cycles, as you can save it into the EEPROM for later usage, the bed leveling state won’t, which will make it necessary to use the M420 S1 G-code command to activate bed leveling in any scenario where you would like to use the bed leveling mesh that you have saved into the EEPROM earlier and shut your 3D printer off afterward.

Additionally, one more thing that’s worth mentioning regarding the bed leveling state is that homing the axes of your 3D printer with the G28 G-code command deactivates bed leveling compensation (unless RESTORE_LEVELING_AFTER_G28 or ENABLE_LEVELING_AFTER_G28 is active), which makes it critical to ensure that you always run G29, G29 L<index>, or M420 S1 after G28.

So, to round things up, we can practically say that while M420 S1 won’t be necessary in cases where you have run G29 and haven’t shut off your 3D printer afterward, as the bed leveling state will already be active from G29, you will need to enable bed leveling compensation with M420 S1 after G28 in scenarios where you have a mesh saved into the EEPROM from an earlier G29 that took place before you turned your 3D printer on at this time.

Conclusion

While G29 is one of the more “loaded” G-code commands you can find in Marlin Firmware due to it possibly behaving differently based on the active bed leveling system, which can make the process of leveling your bed a bit challenging at first, it’s also one that we highly recommend familiarizing yourself with to ensure that you have no issues with bed leveling.

As the bed leveling system you’re using is also a vital part of ensuring that you’re getting the most out of bed leveling, we also highly recommend familiarizing yourself with each of the systems and picking the one that would work the best for you, based on factors such as your hardware and your usage, as opposed to using what’s already active on your 3D printer’s firmware by default.