There is no denying that G-codes can be confusing compared to using a graphical interface that presents everything in a more user-friendly manner, but they don’t have to be.
With sufficient explanation and examples, most G-codes aren’t too cryptic to understand, and in fact, it’s sometimes easier to execute them directly than using the slicer software.
Today we will be talking about a G-code closely related to bed leveling, which is a vital concept in 3D printing for prints to turn out as intended, as an unlevel bed can easily cause the object to come out all wrong.
So, what does the M420 S1 G-code do?
M420 is the G-code for setting or getting the bed leveling state in Marlin firmware.
Executing this G-code with the S1 parameter (M420 S1) enables bed leveling if a valid mesh exists.
Without a valid mesh for bed leveling, this command will do nothing.
As the M420 is an extensive command that accepts many parameters, we will be taking a deeper look into it moving forward, analyze these parameters in detail, and go over examples of scenarios.
What Does the M420 S1 G-Code Do?
To start, let’s clarify that M420 is the G-code, and S1 is a parameter that it accepts.
So, let’s start with M420.
The M420 G-code allows you to either get or set the bed leveling state in Marlin firmware, considering the existence of a valid mesh the printer can use for bed leveling.
Now, let’s move on to the explanation of S1.
Setting the bed leveling state requires the usage of the S flag, which accepts a boolean value. As 1 refers to true, and 0 refers to false in boolean, the S1 parameter in M420 S1 enables the bed leveling functionality.
Similarly, you can use M420 S0 to disable the bed leveling state.
The S flag isn’t the only one that the M420 command accepts, so let’s take a quick look at the other parameters you can use with it.
- C<bool> – Setting the C flag to true (C1) calculates a new center point within the mesh by taking the mean of the highest and lowest points.
- L<int> – Passing the L flag alongside the index of the mesh data (such as L2) in the EEPROM loads the specified mesh to be used for bed leveling.
- T<0|1|4> – Passing the T flag modifies the format of the mesh data that will be printed. Use the value 0 for human-readable, 1 for CSV, and 4 for compact format. Example -> M420 T4
- V<bool> – Setting the V (verbose) flag to true (V1) causes the printer to display the stored mesh data.
- Z<linear> – The Z flag allows you to set the Z fade height, which is a parameter that reduces the effect of bed leveling correction as the printhead gets closer to the set height.
What Is the Difference Between M420 S and M420 S1?
A widely asked question in the community is the difference between using M420 S and M420 S1.
There is absolutely no difference between M420 S and M420 S1. As it’s possible to use either of these commands to enable bed leveling, you can feel free to use the one you wish to.
Now, let’s get down to the explanation for those who are interested.
As you can see, in the case of M420 S, there isn’t a value that we pass to the S flag.
On the other hand, when we look at M420 S1, we notice that the S flag has the boolean value of 1, which refers to true.
In most programming languages, not passing a value to a flag means that the value of the flag is undefined, making it a falsy value by proxy.
On the other hand, the developers of Marlin firmware decided to go the opposite way, and as a result, the Marlin firmware interprets an undefined value as truthy.
In this case, the value of the S flag in M420 S is undefined, and the firmware interprets it as truthy, making S essentially equivalent to S1.
As a result, M420 S is entirely the same as M420 S1, with both enabling bed leveling.
M420 S1 vs. G29
G29 is another G-code closely related to bed leveling, bringing up many questions about how the M420 S1 G-code differs from it.
While running the G29 command allows you to generate the parameters the printer requires (such as mesh readings) for bed leveling, M420 S1 enables the bed leveling feature itself.
As the bed leveling feature requires the information from running G29, M420 S1 can’t enable bed leveling without it.
By running the M420 T0 command, you can display a human-readable output of the mesh data the G29 command has generated for bed leveling.
Is Running M420 S1 After G29 Necessary?
Another widely asked question is whether you need to run M420 S1 after G29 or not.
While the G29 command enables bed leveling by default, some bed leveling methods (such as 3-point or linear bed leveling) require you to run the G28 (Auto-Home) command first, which disables bed leveling.
As a result, while not always necessary, it’s a good practice to run M420 S1 after G29 to ensure that the bed leveling is active at all times.
Since running M420 S1 while bed leveling is already on won’t have any adverse effects, you can feel free to run it without needing to check the state first if you suspect that bed leveling is off.
As bed leveling is vital for achieving a high print quality, the M420 S1 command is certainly something you want to keep in mind to ensure that the feature is active at all times.
A trick you can use to make your life easier is to add this G-code to the starting script (ensure that it comes after G28) of your slicer, which will cause the slicer to enable bed leveling automatically before every print.
If you have been enjoying our series of detailed explanations for vital G-codes, stay tuned for more in the future!