PID Tuning with M303, M304, and M301 G-Codes in Marlin

While they may seem complicated at first, G-codes are something every 3D printing enthusiast should become familiar with at some point, especially considering that they aren’t as complex as they look from the outside.

As it’s possible to invoke all of your 3D printer’s individual functions through G-codes, knowing about them allows you to have complete control over your 3D printer and optimize it to the fullest extent.

Today, we will analyze a few of the vital G-codes you will need to tune your printer and prepare it for printing, the PID tuning G-codes.

So, what are the PID tuning G-codes, and how can you use them?

  • M303 (PID Autotune) – M303 is the G-code for the automatic tuning of the hotend and the heated bed.
  • M304 (Set Bed PID) – M304 is the G-code for manually setting the PID of the heated bed.
  • M301 (Set Hotend PID) – M301 is the G-code for manually configuring the PID of the hotend.

Using these G-codes requires you to send the commands to the printer, which you can either perform by directly connecting your computer to your printer or with an SD card.

While the purpose of all these G-codes is to tune the PID, their functionalities and syntaxes differ.

Next up, we will be taking a more comprehensive look at the functionality of each of these G-codes, their syntaxes, and how you can use them to tune the PID for your printer’s hotend and heated bed.

Using the M303 G-Code (PID Autotune) in Marlin

The M303 G-code is the most common way of tuning the PID, and in most cases, the only one you will need.

The M303 G-code makes the firmware find the optimal PID values automatically for the temperature you choose and display these values or set them to the printer’s memory.

Using the M303 G-code requires you to pass a few parameters to it, which we have listed below:

  • Count (C) – The number of tuning cycles. It has to be a value over 3 and defaults to 5.
  • Index (E) – Index of the hotend, with the value -1 referring to the heated bed. The parameter defaults to 0.
  • Temperature (S)* – The target temperature for the tuning of the PID values. Required.
  • Use PID (U) – The parameter to make the M303 G-code save the PID values to the memory instead of displaying it.
  • Debug (D) – The parameter for enabling the debug mode where the printer will show an output during the tuning process.

Let’s take a look at an example of how you can use the M303 G-code with some of these parameters:

M303 C8 E0 S190 U1

Now, let’s dissect this G-code to understand what it does.

C8 refers to 8 cycles of tuning, E0 refers to the first hotend, S190 refers to a target temperature of 190 degrees, and U1 activates the U parameter, causing the printer to save the PID result to memory.

An important thing to consider is that the U flag only saves the settings to the memory, meaning that you would manually have to save values to the EEPROM by using the M500 G-code if you would like this configuration to persist.

Using the M304 G-Code (Set Bed PID) in Marlin

The M304 G-code is a way of manually setting the PID for the heated bed.

Running the M304 G-code allows you to pass the desired P, I, and D numbers for the heated bed and saves these values to the memory of your 3D printer.

Using the M304 G-code is pretty straightforward, as it only supports three parameters that are all mandatory:

  • Proportional Value (P)
  • Integral Value (I)
  • Derivative Value (D)

These three parameters together make up the value we call PID.

Now, let’s take a quick look at an example:

M304 P20 I15 D50

In this example, P20 sets the proportional value to 20, I15 sets the integral value to 15, and D50 sets the derivative value to 50.

It’s vital to note that these values are saved to the memory of your 3D printer, meaning that you will also need to run the M500 G-code to save them to the EEPROM.

We highly recommend using the M303 G-code to find the correct PID values before setting them with the M304 G-code, as incorrect PID values will stop your printer from functioning.

Using the M301 G-Code (Set Hotend PID) in Marlin

The M301 G-Code allows you to set the PID values of a hotend manually.

With the M301 G-code, you can directly pass the P, I, and D values for the hotend of your choice and make the printer save these values to its memory.

Let’s take a look at the parameters of the M301 G-code:

  • Proportional Value (P)
  • Integral Value (I)
  • Derivative Value (D)
  • Extruder Index (E)
  • C Term (C)
  • F Term (F)
  • Extrusion Scaling Queue Length (L)

Among all of these parameters, we are only interested in P, I, D, and E as the others are advanced options that require enabling via the modification of the firmware.

Here is an example:

M301 E1 P22 I18 D45

In this example, E1 sets the hotend index to 1 (the second hotend, with 0 being the first), P22 sets the proportional value to 22, I18 sets the integral value to 18, and D45 sets the derivative value to 45.

Similar to the commands we have mentioned earlier, the M301 G-code will also save the values to the printer’s memory. If you want these values to persist, you need to run the M500 G-code to save to EEPROM.

We recommend refraining from using the M301 command with values you aren’t sure about, as incorrect PID values will stop the printer from functioning. You can use the M303 G-code to let the printer auto-tune the PID and use the values it produces for the best results.

Wrapping Up

PID tuning is a vital step for your printer to function as optimally as possible, and the usage of G-codes for it is the most straightforward way to get it done without a doubt.

For the most part, you should be using the M303 G-code for the printer to tune the PID automatically, as it’s the most efficient and accurate way of doing so.

While G-codes may look scary if you have no prior experience with them, we highly recommend familiarizing yourself with them at some point, as they are pretty handy in many areas of 3D printing.

We hope that this read has been informative, and we will see you later!