M420 S1 G-Code (Marlin Firmware) – Explanation & Usage

While an automatic bed leveling probe is one of the best additions to a 3D printer, both for the convenience and the reliability it brings to bed leveling, getting auto bed leveling to work as intended can prove to be a bit of a challenge at first glance due to the G-code knowledge required to set things up.

In this guide, we will explain the purpose of the M420 S1 G-code command in Marlin firmware, take you through how you can correctly use it, and clarify the differences between the M420 S1 and G29 G-code commands that are often confused with one another.

What Is the Purpose of the M420 S1 G-Code?

The M420 S1 G-code command, which is the combination of the M420 G-code (Bed Leveling State), the S (set) parameter, and a value of 1 (true) for the S parameter, sets the state of bed leveling to active, prompting your 3D printer to use the stored bed leveling mesh to level the bed before each print.

running m420 s1 on octoprint


The one pre-requisite for the M420 S1 command to work as intended and enable bed leveling is for a valid bed leveling mesh to be present, as your 3D printer won’t automatically generate a mesh if it can’t find one in this case, but ignore the command and proceed to run the following commands with the bed leveling state inactive instead.

bed leveling mesh example on ender 3 v2


To generate a mesh that your 3D printer can use when bed leveling is activated, you will need to utilize the G29 (Bed Leveling) G-code command first, which will behave differently based on which of the five modes are selected in the firmware configuration.

While going through all the different bed leveling systems is out of this article’s scope, you can learn more about their syntaxes and how they work by visiting the Marlin firmware documentation on the G29 G-code.

bed leveling systems in marlin explained


Out of all the available G29 modes, we can consider the most commonly used one to be the Bed Leveling (Bilinear) system used in conjunction with an automatic bed leveling probe such as the BLTouch or CR Touch, which automatically generates the bed leveling mesh by getting the probe to take readings at a few different fixed points on the bed.

enabling bilinear auto bed leveling in marlin config


In this case, the default way of generating the bed leveling mesh consists of running the G29 command without any extra parameters first, and once the probe takes the necessary readings and generates the mesh, running the M500 command to store the mesh in the EEPROM permanently.

running g29 and m500 on octoprint


On the other hand, for a different bed leveling system, such as Unified Bed Leveling, which is also utilized in conjunction with an automatic bed leveling probe, the default way of generating and saving a mesh would be to use the G29 P1 (automatic probing) and G29 S1 (save mesh to slot 1) commands instead, as running the G29 command without any parameters won’t do anything in this case.

So, to put everything together, once you find out which bed leveling system the firmware of your 3D printer is using (you can use the Configuration.h file for this), generate the bed leveling mesh accordingly with G29, and save the mesh to the EEPROM, you will then be able to use the M420 S1 command to activate bed leveling, which will tell your 3D printer to use this same mesh you have generated with G29 for all of your prints as long as bed leveling is active.

How to Use the M420 S1 G-Code?

The most common and reliable way of using the M420 S1 G-code is to get your 3D printer to run the command automatically before each print, which will take the human factor out of the equation and ensure a scenario where bed leveling is not active for a print (which would almost certainly lead to print failure) never takes place.

To achieve this effect in the most straightforward way possible, you can append the M420 S1 G-code command to the Start G-code section in your slicer (this is also what the official Marlin firmware documentation on auto bed leveling recommends), which will prompt your slicer to inject this line at the start of every G-code file it produces and get your 3D printer to run the command before each print as a result.

As an example, let’s go through the process of accessing the Start G-code section and adding the M420 S1 G-code command to it in Cura.

To start, navigate to the Manage Printers section of Cura by clicking the Settings button on the menu bar and hovering on the Printer option in the dropdown menu.

Cura manage printers


Then, select the 3D printer you will be modifying from the list on the left, and click on Machine Settings.

Cura machine settings


If you can’t see the Machine Settings button once you select your 3D printer, make sure to click the Activate button in the menu that you can access by clicking the “three lines” icon on the top right.

Cura activate printer


Once the Machine Settings window comes up, you will see the Start G-code section at the bottom-left corner, which should already have some commands written in it.

For instance, in the below screenshot, you can see the default Start G-code block for an Ender 3, which already includes some commands, such as G28 (Auto Home), that should already be running automatically before each print.

cura default ender 3 start gcode


Finally, the last step is to add M420 S1 right below G28, which is a critical part of the process, as the auto-homing process disables bed leveling (which makes it mandatory to activate bed leveling after auto-homing), and as bed leveling needs to be active before any extrusion (such as the test lines in the Start G-code section) takes place.

cura ender 3 start gcode with m420 s1 added


With M420 S1 now added to its appropriate place, you won’t have to worry about activating bed leveling manually anymore, as the slicer will handle it for you from now on.

While there are ways around the requirement of activating bed leveling after auto-homing, such as defining the RESTORE_LEVELING_AFTER_G28 (brings the bed leveling state to what it was before G28 disables it) or ENABLE_LEVELING_AFTER_G28 (automatically enables bed leveling after G28) parameters in Marlin firmware configuration, the fact that these parameters are usually inactive in most forks of Marlin firmware makes it good practice to ensure that M420 S1 always comes right after G28.

marlin enable leveling after g28 and restore leveling after g28 parameters


M420 S1 vs. G29 – What Is the Difference?

Even though both M420 S1 and G29 are capable of activating bed leveling, which is why they often get mixed up with one another, these two G-code commands fulfill two entirely different purposes.

M420 S1, as we have been talking about so far, has a single purpose, which is to set the bed leveling state to active.

If a valid bed leveling mesh is already stored on your 3D printer, this mesh will be used to compensate for the imperfections of the print bed’s positioning when you activate bed leveling with M420 S1.

On the other hand, if there is no valid bed leveling mesh present, M420 S1 won’t do anything at all and be skipped over by the 3D printer, as bed leveling cannot be activated without a mesh to use.

As M420 S1 is only responsible for activating bed leveling, you will never see a scenario where the bed leveling probe starts working after you run the command, which is the intended behavior.

The G29 G-code command is the one responsible for creating the mesh (or matrix) that will be used for bed leveling, whether it’s manually or with an automatic bed leveling probe, with the most famous example being the BLTouch.

Additionally, when you run G29 to generate the bed leveling mesh, your 3D printer will also set the bed leveling state to active, meaning that you won’t have to run the M420 S1 G-code command after G29.

Finally, to be able to use the mesh you have created with G29 by running the M420 S1 G-code command after a power cycle, you will need to make sure that you have saved the mesh to the EEPROM, as failing to do so will cause the mesh to be lost once you turn the 3D printer off.

Glossary of M420 Parameters

As the S parameter (from M420 S1) isn’t the only one that the M420 G-code command accepts, you may stumble upon other forms of M420 in use, which will have different functions.

Here is a list of all the parameters that the M420 G-code command supports, along with some quick explanations for each parameter:

  • C (0 or 1) – Centers the mesh on the mean of the lowest and highest points.
  • L (integer) – Loads the bed leveling mesh from the selected EEPROM index. This parameter only works when the bed leveling mode is set to Unified Bed Leveling.
  • S (0 or 1) – Sets the bed leveling state.
  • V (0 or 1) – Sets the state of verbose mode, where the stored mesh or matrix data gets printed on the screen.
  • T (0, 1, or 4) – Determines the format of the printed mesh data. 0 for human readable, 1 for CSV, and 4 for compact.
  • Z (decimal) – Sets the Z fade height value, which determines the Z-axis position where bed leveling correction will no longer be applied. Additionally, bed leveling correction will be gradually reduced as the Z-axis position gets closer to the fade height value.

Conclusion

Now that you know what the M420 S1 G-code command does and how you can utilize it, you will also have the option of using a pre-generated mesh to level your 3D printer’s bed whenever necessary alongside the standard method of using G29.

That being said, since using M420 S1 is the preferred method of enabling bed leveling, as opposed to running G29 to generate a new mesh before every single print, both due to it saving time and reducing machine wear, it’s definitely a command you will want to add to your 3D printer’s workflow.