Marlin Firmware M301 / M303 / M304 G-Code Commands Explained

Whenever you set up a 3D printer for the first time or install new firmware to it, PID tuning is one of the vital calibration steps that you will need to take to ensure that your 3D printer can operate as intended, as correct PID values are an absolute necessity for both the hotend and the bed temperatures to remain stable at the target values.

In this guide, we will explain the functionalities and the usage of the M303, M301, and M304 G-codes in Marlin Firmware, which make up the commands that allow you to tune the PID values for both the hotend and the bed of your 3D printer.

Marlin Firmware M303 G-Code (PID Autotune)

Running the M303 G-code in Marlin Firmware initializes the PID autotuning procedure for the specified target temperature, whether it’s for the hotend or the heated bed, which practically allows the firmware to automatically find and set the optimal PID values without requiring any further action from you.

Technically speaking, the only non-optional parameter you will need to pass when using the M303 G-code command is S, which is responsible for letting the firmware know the target temperature for which the PID tuning process will take place, such as M303 S180 for PID tuning the hotend by using a target temperature of 180 degrees Celsius.

m303 s180 example


The target temperature, in this case, will need to be the hotend temperature you generally use when printing, which is why we highly recommend running the PID autotuning process whenever you switch to a new filament that requires the usage of a hotend temperature value that’s different than what your 3D printer’s PID values are currently tuned for.

While the S parameter covers the most basic form of usage for the M303 G-code command, as it will allow you to PID tune the hotend according to your needs, you will find that this command accepts four more parameters to give you further control over its behavior, which are C, E, and U, and D.

The C parameter (which defaults to 5 in cases where you don’t pass the parameter at all and can’t have a value that’s less than 3), in this case, allows you to control the number of “cycles” the PID autotuning process will use, with a higher C value improving the accuracy of the results, but also increasing the amount of time that the PID autotuning process takes.

While there isn’t a universally accepted optimal C value, meaning that you’re free to run as many cycles as you wish and conduct your tests to find the C value that produces the best results in the least amount of time, the consensus is that there isn’t much of a need to over 10 cycles due to the improvements becoming minuscule, which is what we would recommend going for if you’re looking for a quick answer on setting the C value, such as an example of M303 C10 S200 for 10 cycles at a target temperature of 200 degrees Celsius.

m303 s180 c10 gcode command example


The E parameter (defaults to 0 if you omit the parameter, which refers to the first hotend; use -1 for the heated bed) of the M303 G-code command allows you to choose the hotend (or heated bed) you will be performing the PID autotuning process for, which will become necessary if your 3D printer has multiple hotends or a heated bed, as all of these components will require individual PID tuning for best results.

For instance, if you would like to autotune the PID values of the heated bed with a target temperature of 60 degrees Celsius, the command you need to send to your 3D printer would be M303 S60 P-1, or if you would like to autotune the PID values for the second hotend with a target temperature of 200 degrees Celsius, the command you would need to use would be M303 S200 P1.

m303 s60 p-1 gcode command example


Next up is the U parameter (defaults to 1, can only take values of 0 and 1), which allows you to specify whether the firmware should automatically activate the results generated by the PID autotuning process (if set to 1) or only to print the results out for you to read in the terminal without taking them into effect (if set to 0).

As an example, if you would like to run the PID autotuning process for a target temperature of 220 for the first hotend and see the results in the terminal without putting them into use, the command you would need to send would be M303 S220 P0 U0.

m303 s220 p0 u0 example


Finally, the last parameter that the M303 G-code command accepts is D (doesn’t accept any value), which fulfills the purpose of activating or deactivating the debug output generated by the PID autotuning process, provided that the PID_DEBUG variable is active in the Configuration.h file for the firmware you’re using.

Unlike the rest of the parameters we have gone through earlier, you will need to pass the D parameter alone to the M303 G-code command without any other parameters included, which will only result in the debug output toggle taking place instead of the standard PID autotuning function of the command.

m303 d command example


Marlin Firmware M301 G-Code (Set Hotend PID)

The M301 G-code command in Marlin Firmware makes it possible to explicitly set the PID values of the hotend manually, unlike the PID autotuning process that causes the firmware to find the optimal PID values and configure them automatically, which can come in handy in cases where you already have access to optimized PID values from an earlier autotune and would like to save time.

You can use the M503 (Report Settings) G-code command to find the current PID values of your 3D printer and note them down for later usage with M301.

As you may predict, the main parameters that you will need to pass when using the M301 G-code command are P (Proportional), I (Integral), and D (Derivative), together with their values, which will allow you to directly configure the PID values of your 3D printer without any further steps or explanation required.

For instance, in a case where you noted the optimized values of 20, 1.5, and 85 for P, I, and D, respectively, you can send the command M301 P20 I1.5 D85 to quickly set the PID values on your 3D printer without having to go through the autotuning process again.

m301 pid tuning example


Alongside the P, I, and D parameters necessary for the command to run, you will also find that M301 accepts the optional parameters of E, C, F, and L, allowing you to customize the command’s behavior in various ways.

The E parameter of the M301 G-code command allows you to specify the index of the hotend for which the PID values will be applied, which will only be necessary if your 3D printer has multiple hotends, as it will default to 0 automatically if you don’t include it, referring to the first hotend.

m301 e parameter example marlin


Please note that the parameters below are for advanced use only and can lead to undesired results if misconfigured.

The C parameter of the M301 G-code command (defaults to 100), which requires the PID_EXTRUSION_SCALING variable to be active in the Configuration_adv.h file, makes it possible to add another value to the PID algorithm that also accounts for the extrusion speed, which effectively is an optimization to ensure that your 3D printer always uses the correct amount of power to melt the necessary filament based on the extrusion, and not exceed it.

On the other hand, the L parameter (defaults to 50), which also requires the PID_EXTRUSION_SCALING variable to be active, allows you to set the extrusion scaling queue length.


Finally, the F parameter, which requires the PID_FAN_SCALING variable to be active in the Configuration_adv.h file, adds another value to the PID algorithm in a similar way to the C parameter, but this time, to account for the power loss from the cooling fan instead, once again offering another way to optimize the heating in a way that makes it possible for your 3D printer to use the correct amount of power to melt the filament, even at different fan speeds.

m301 f parameter explained marlin


Marlin Firmware M304 G-Code (Set Bed PID)

Similar to the M301 G-code command, the M304 G-code command also allows you to configure the PID values manually, but for the print bed instead of the hotend, as it’s possible for the print bed to also come with a heater, which is the case for many 3D printers available on the market nowadays.

Unlike M301, which comes with four additional parameters, M304 only accepts the mandatory parameters of P, I, and D, together with their values, allowing you to set the PID values for the heated bed quickly and conveniently in cases where you already have the optimized values at hand from an earlier autotuning process.

As an example, with P, I, and D values of 800, 154, and 1000 at hand from an earlier autotuning process, you can send the command M304 P800 I154 D1000 to set the PID values for the heated bed quickly and save time by not having to go through the autotuning process again.

m304 pid tuning example


Conclusion

While Marlin Firmware offers a few different ways to set the PID values for both the hotend and the heated bed, using the PID autotune feature (M303) is the standard way of getting your 3D printer’s PID values dialed in, as it’s both the most reliable and also convenient way of discovering the optimal PID values for the target temperatures you have specified.

As the usage of optimal PID values is vital for both the hotend and the bed to be able to hold the target temperatures, ensure that you always autotune the PID after switching to a new firmware, and feel free to run the M303 G-code command whenever you come across a scenario where the hotend or bed temperature ends up fluctuating during prints.