Marlin Firmware G29 (Bed Leveling) G-Code - Explained - 3D Print Gorilla (2024)

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.

Table of Contents

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 Firmware G29 (Bed Leveling) G-Code - Explained - 3D Print Gorilla (1)


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.

Marlin Firmware G29 (Bed Leveling) G-Code - Explained - 3D Print Gorilla (2)


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 Firmware G29 (Bed Leveling) G-Code - Explained - 3D Print Gorilla (3)


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.

Marlin Firmware G29 (Bed Leveling) G-Code - Explained - 3D Print Gorilla (4)


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.

Marlin Firmware G29 (Bed Leveling) G-Code - Explained - 3D Print Gorilla (5)


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 Firmware G29 (Bed Leveling) G-Code - Explained - 3D Print Gorilla (6)


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.

Marlin Firmware G29 (Bed Leveling) G-Code - Explained - 3D Print Gorilla (7)


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 Firmware G29 (Bed Leveling) G-Code - Explained - 3D Print Gorilla (8)


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.

Marlin Firmware G29 (Bed Leveling) G-Code - Explained - 3D Print Gorilla (9)


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.

Marlin Firmware G29 (Bed Leveling) G-Code - Explained - 3D Print Gorilla (10)

David Taylor

As a software developer by profession, David has been particularly interested in how 3D printing works even before he picked up his first 3D printer, and completely fell in love with the hobby once he got his hands on a Prusa i3 MK2 for the first time back in 2017. Due to him enjoying both the functional and the non-functional sides of 3D printing equally, you’ll rarely see David’s 3D printer in an idle state.

Marlin Firmware G29 (Bed Leveling) G-Code - Explained - 3D Print Gorilla (2024)

FAQs

What is the Gcode for bed leveling? ›

After a G29 bed leveling is automatically enabled, but in all other situations you must use M420 S1 to enable bed leveling. It is essential to include the command M420 S1 in the “Start G-code” in your slicer settings. If you have no bed leveling, or if there is no leveling data, then this command is simply ignored.

What does G29 mean in G code? ›

CodeApplication
G29return from reference point (M,T)
G30return to alternate home position (M,T)
G31.1 - G31.4external skip function (M,T)
G33thread cutting, constant lead (T)
92 more rows

What is the G29 command in 3d print? ›

G28 & G29: Auto-Home & -Bed Leveling

The G28 command is usually performed before every print process. Another command, G29, starts the automatic bed leveling sequence. There are many different methods for leveling a bed prior to printing, as this is usually set by firmware and not by the final users.

What is the Gcode for auto bed leveling Cura? ›

Once you've set up the device, you may want to configure your slicer (e.g. Cura) to automatically level your printer's bed before every print. To do this, you can insert the G29 G-code command into your slicer's start G-code script after the G28 command, which should already be included.

Does G28 erase bed leveling? ›

By default G28 disables bed leveling. Follow with M420 S to turn leveling on. With ENABLE_LEVELING_AFTER_G28 leveling will always be enabled after G28 . With RESTORE_LEVELING_AFTER_G28 leveling is restored to whatever state it was in before G28 .

What is the code for mesh bed leveling? ›

Enter command “G29 S1” into the command line, and press enter. This will start the mesh bed levelling routine.

What is a G-code in 3D printing? ›

G-code is a language that humans use to tell a machine how to do something. With 3D printing, G-code contains commands to move parts within the printer.

What is G-code code? ›

G-code (also RS-274) is the most widely used computer numerical control (CNC) and 3D printing programming language. It is used mainly in computer-aided manufacturing to control automated machine tools, as well as for 3D-printer slicer applications. The G stands for geometry. G-code has many variants.

What does G mean code? ›

G-code (geometric code) is the most common CNC (computer numerical control) programming language used to drive computer-controlled manufacturing machines.

What is G29 in Marlin? ›

calibration Probe the bed and enable leveling compensation.

Do 3D printers use G-code or STL? ›

You have probably heard of STL and G-code files if you are into 3D printing. While G-code is the language used by most 3D printers to regulate movement, STL files are the most popular 3D printer file format.

How to convert STL to G-code for 3D printing? ›

The process of converting STL to G-code is easy. Typically, it only requires importing your STL file into the slicer, configuring the printing settings, and then let the software slice the 3D model into layers, ultimately outputting a G-code format file.

Where to put G29 code? ›

Make sure that you place the G29 command after the last G28 (homing command) in your starting script. The screenshot below shows an example profile that is properly setup for for automatic bed leveling. Please note that starting scripts may vary from one printer to another.

How to level bed with Cura? ›

  1. Step 1: Open Cura on Your Laptop. Cura is a slicing software used to generate gcode. ...
  2. Step 2: Adjust Front Left Screw. In case the nozzle is touching your print bed or is very near to it, use the 'Down' command to bring the bed down. ...
  3. Step 3: Adjust Remaining Screws. ...
  4. Step 4: Final Step.

What is M117 G-code? ›

M117: Set LCD Message. M118: Serial print. M119: Endstop States. M120: Enable Endstops. M121: Disable Endstops.

What does G92 G-code do? ›

G92 reassigns the current controlled point to the coordinates specified by the axis words (X~, Y~, Z~, and/or A~). No motion takes place. The axis words are optional, except that at least one must be used. If an axis word is not used for a given axis, the coordinate on that axis of the current point is not changed.

What is M600 G-code? ›

The M600 command initiates the filament change procedure. The basic procedure will move the print head away from the print, eject the filament, wait for new filament to be inserted and the user to confirm, load and prime the filament, and continue with the print.

What is M300 G-code? ›

The M300 code is used to call APL or Robot Custom Sequence programs. To call a specific program use the P code followed by the alias code, found in the APL custom sequence tab. M300 Pn Q0 Plays the sequence in the background to the program.

References

Top Articles
Latest Posts
Article information

Author: Horacio Brakus JD

Last Updated:

Views: 5869

Rating: 4 / 5 (51 voted)

Reviews: 82% of readers found this page helpful

Author information

Name: Horacio Brakus JD

Birthday: 1999-08-21

Address: Apt. 524 43384 Minnie Prairie, South Edda, MA 62804

Phone: +5931039998219

Job: Sales Strategist

Hobby: Sculling, Kitesurfing, Orienteering, Painting, Computer programming, Creative writing, Scuba diving

Introduction: My name is Horacio Brakus JD, I am a lively, splendid, jolly, vivacious, vast, cheerful, agreeable person who loves writing and wants to share my knowledge and understanding with you.