PID tuning for 3D printers: Marlin M303 and Klipper steps

Advanced PID tuning for FDM printers: calibrating hotend and heated bed for temperature stability

PID tuning is the process of finding the three gain values (proportional, integral and derivative) that tell your printer’s firmware how hard to drive a heater for a given temperature error. On Marlin you run M303 and save the result with M500; on Klipper you run PID_CALIBRATE and then SAVE_CONFIG. Do the hotend and the heated bed as two separate runs, each at a temperature you actually print at.

You only need this when the temperature visibly wanders around its target, or after you change something that alters how the heater behaves. A new silicone sock, heater cartridge or thermistor counts; swapping filament does not. The commands and parameters below come from the Marlin and Klipper documentation and Prusa’s PID article, linked at the end. Where a value is a starting point rather than a published figure, it is labelled as one.

Affiliate · Featured hardware Snapmaker U1 5X More Speed. 5X Less Waste.

The consumer toolchanger 3D printer: the SnapSwap 4-toolhead system swaps heads in 5 seconds for true multi-color and multi-material printing, with smart calibration and auto filament loading.

  • 4 toolheads · 5 s swap
  • Up to 500 mm/s · 20,000 mm/s²
  • 270 mm cube build volume
  • Multi-color & multi-material
See the Snapmaker U1 →
Snapmaker U1

PID tuning in 60 seconds

  1. Heat-soak the printer in its normal state: enclosure closed if you print that way, no draft, part fan off unless you want to tune for it.
  2. Marlin hotend: M303 E0 C8 S210 U1 (use your own temperature), then M500.
  3. Marlin bed: M303 E-1 C8 S60 U1, then M500.
  4. Klipper: PID_CALIBRATE HEATER=extruder TARGET=210, then SAVE_CONFIG. Repeat with HEATER=heater_bed TARGET=60.
  5. Hold the target and watch the temperature graph for a few minutes. Small ripples are expected; if it still hunts, check the thermistor and fan before tuning again.

When to tune and when something else is wrong

Prusa’s PID article draws a useful line: tuning is for small fluctuations, around ±5 °C on the nozzle. Larger swings point to a problem with the fan or with the thermistor wire or connector, and no set of gains will fix that. The same logic applies to any printer.

What you seeLikely causeDo this
Temperature drifts a few degrees above and below the target, steadilyGains not matched to this heaterRun PID tuning
Swings start after fitting a new heater cartridge, thermistor, heater block or silicone sockThe thermal behaviour changedRun PID tuning again (and check the thermistor type in the firmware)
Large swings, jumps, or readings that spikeLoose thermistor wire or connector, failing sensorFix the wiring first; tune afterwards
Temperature dips whenever the part fan switches on or offFan airflow hits the heater blockTune with the fan in its typical state, or use fan scaling or MPC (see below); check the duct aim
“Heating failed” or “Thermal runaway” stops the printerThermal protection is working, usually on a wiring or sensor faultDo not retune: see the thermal runaway protection guide

Changing filament does not change the heater, so it does not call for a new tune. Prusa says the same: one run improves stability at all temperatures, so you do not need to repeat it per material. Tune at a temperature you print at often. If you are still dialling in the temperature itself, a temperature tower comes first.

What P, I and D do

The firmware compares the measured temperature with the target and sets heater power from three terms. You never need to calculate them by hand, but knowing what each one does helps when you read the result or tune manually.

TermReacts toToo highToo low
P (proportional)The current errorOvershoot and oscillationSlow, weak response
I (integral)Error accumulated over timeSlow overshoot, huntingTemperature settles off target
D (derivative)How fast the error is changingJittery power from sensor noiseMore overshoot when approaching the target

Marlin: M303, M301, M304 and M500

Marlin’s M303 heats and cools the chosen heater for several cycles and works out the gains. Send it from a terminal such as OctoPrint, Pronterface or the printer’s own console. The parameters from the Marlin documentation:

ParameterMeaning
E<index>Hotend number; E-1 is the heated bed. Default is 0.
S<temp>Target temperature for the test (no default).
C<count>Number of cycles. Minimum 3, default 5.
U<bool>With U1 the result is applied; without it the values are only printed.

Hotend

  1. Make sure the hotend fan runs and the thermistor reads sensibly at room temperature. Stay with the printer for the whole run.
  2. Send M303 E0 C8 S210 U1. Replace 210 with a temperature you use; Marlin’s own example is 210 °C with 8 cycles. The documented minimum is 3 cycles and the default is 5; more cycles take longer.
  3. When the run ends, the printer prints the resulting P, I and D values to the terminal. Write them down.
  4. If you ran without U1, apply them yourself with M301. Marlin’s example is M301 P21.01 I1.73 D63.67 (those numbers are the documentation’s example, not a recommendation).
  5. Save with M500. It writes the active settings to EEPROM, provided the firmware was built with EEPROM settings enabled.
  6. Send M503 to print the active settings and confirm the new values are there.

Without M500, the values live in RAM only, and the printer returns to the compiled-in defaults at the next power cycle. M501 reloads saved values and M502 resets to the firmware defaults. If your printer has no EEPROM support, copy the values into Configuration.h (DEFAULT_KP, DEFAULT_KI, DEFAULT_KD) and rebuild; see flashing Marlin. Marlin’s configuration notes say the sample values in the file will not suit most machines, so tuned values are the norm.

If the firmware is built with extrusion or fan scaling, M301 also accepts C and L (extrusion scaling, needs PID_EXTRUSION_SCALING) and F (fan scaling, needs PID_FAN_SCALING). M303 does not set those; they are advanced options you can leave alone.

Heated bed

The bed uses the same command with E-1: M303 E-1 C8 S60 U1 (Marlin’s example is 60 °C with 8 cycles). Apply manually with M304 P… I… D…, then M500 and M503. A bed has far more thermal mass than a hotend, so the run takes noticeably longer; leave time for it.

Bed PID only exists if the firmware has PIDTEMPBED enabled (M304 needs it), and Marlin’s configuration file warns not to enable it without confirming the hardware can handle PWM. A mechanical relay switching on and off constantly is the case to avoid. Without bed PID the firmware falls back to simple on/off control.

Klipper: PID_CALIBRATE and SAVE_CONFIG

Klipper’s documentation says PID settings copied from other firmware or from the example configs often work poorly, so each printer should be calibrated. The command is:

PID_CALIBRATE HEATER=extruder TARGET=210
SAVE_CONFIG
PID_CALIBRATE HEATER=heater_bed TARGET=60
SAVE_CONFIG
  1. Run PID_CALIBRATE HEATER=extruder TARGET=<temp> in the console. Klipper’s docs use 170 °C as the example target; use your own printing temperature instead (that is a recommendation, not a Klipper requirement). The heater warms to the target, then cycles on and off several times.
  2. When it finishes, run SAVE_CONFIG. Per the docs, this writes the new PID settings into printer.cfg and restarts the host software.
  3. Repeat for the bed with HEATER=heater_bed. The docs’ example target is 60.
  4. Check the results in printer.cfg: the heater section holds pid_Kp, pid_Ki and pid_Kd. They must be present for any heater with control: pid.

The optional WRITE_FILE=1 argument writes a sample log to /tmp/heattest.txt if you want to graph the run. The Klipper docs also note that PID on a bed is recommended only when the heater is driven by PWM, since PID can switch it about ten times a second (the default pwm_cycle_time is 0.100 s). Mechanical relays do not suit that. If you are new to Klipper, see migrating from Marlin to Klipper.

Prusa and other closed firmware

On the Prusa MK2.5/S, MK3/S/+ family (firmware 2.0.12 or newer) the tune is a menu item: LCD Menu, Calibration, PID calibration. You set the temperature you use most, and the printer heats the nozzle over 5 cycles. Prusa recommends a dry spot with stable room temperature above 20 °C and no drafts. Prusa’s staff reply on the MINI, MK4 and XL says their PID values are already set for the stock heaters and normally need no change, though M301, M303 and M304 exist for those who want them. Other manufacturers’ closed firmware may not expose PID at all; check the maker’s documentation before sending G-code you have not seen documented for your model.

Marlin’s MPC as an alternative

Marlin also supports Model Predictive Control for the hotend. Instead of reacting to the sensor reading, it simulates the hotend and plans heater power from that model. The docs say it accounts for heat lost to the part fan and to the filament directly, so it does not need fan or extrusion scaling. To use it you disable PIDTEMP, enable MPCTEMP, set MPC_HEATER_POWER and MPC_TUNING_POS, rebuild, then run M306 T to autotune and M500 to save.

The autotune homes the printer, moves the nozzle close to the bed, heats past 200 °C and measures heat loss, so Marlin warns to look out for cooled blobs on the nozzle. It also needs a firmware rebuild, which makes it a project, not a quick fix. For most people on stock Marlin, PID is the simpler route.

Mistakes that ruin a PID tune

  • Forgetting to save. On Marlin the result is gone after a power cycle without M500 (or a firmware rebuild with the values). On Klipper the tune is not stored until SAVE_CONFIG.
  • Tuning with a wrong sensor setup. If the thermistor type in the firmware does not match the part, the printer will regulate the wrong temperature accurately. Confirm the reading first.
  • Tuning in a different state from printing. A closed enclosure, an open door or a fan duct that blows on the heater block changes the heat loss. Tune the way you print.
  • Walking away. The heater cycles hard during the run. Stay near the printer, and follow the fire-safety advice in the 3D printer fire safety guide.
  • Using tuning to cover a hardware fault. Spiking readings or swings beyond Prusa’s ±5 °C guide point to wiring, a failing thermistor or a loose sensor, not to the gains.

Frequently asked questions

How long does PID tuning take?

It depends on the heater and the number of cycles, and the docs give no fixed time. Expect a hotend run to finish much faster than a bed run, because the bed has far more thermal mass. Stay with the printer until it ends.

Do I need to retune for each filament?

No. Prusa states that one run improves stability across all temperatures, and the heater does not change when you change filament. Retune after hardware changes: a new heater cartridge, thermistor, block or silicone sock, or a different part-fan duct aimed at the block.

Why did my PID values disappear after a restart?

On Marlin, M303 and M301 change values in memory only; M500 writes them to EEPROM, and only if EEPROM settings are enabled in the firmware. On Klipper the values are not written to printer.cfg until you run SAVE_CONFIG.

Can PID tuning fix a thermal runaway error?

No. Thermal protection exists to stop the printer when the heating does not behave as expected, which usually means a wiring, sensor or heater fault. Fix the hardware cause first, then tune.

Sources