Auto Tuning Pid Arduinotreecost

Going from a finished print to a great print takes a lot of calibration steps. Besides having the heatbed leveled correctly, and the extruder perfectly calibrated to melt just enough filament, the temperature of the hotend and the heatbed is just as important.

Today I am going to show you how to perform a PID Tuning to have constant and accurate temperatures during your prints.

Note when tuning the P Mode, the PV will line out below the SP. To tune P Mode Step Description 1 Set P to 30% of SP; I and D Modes to 0. Wait until PV is close to SP and as stable as it can get. 2 Set Control Mode to Manual 3 Set P to ½ of current setting 4 Set Control Mode to Auto. Use the command M303 E0 S225 C10 to start the P.I.D. The command you just entered will tune your hot-end to 225 degrees and it will do the tuning cycle 10 times. Pronterface terminal will show a few lines of code for the tuning while running but will show “PID Autotune finished!” when complete. Note when tuning the P Mode, the PV will line out below the SP. To tune P Mode Step Description 1 Set P to 30% of SP; I and D Modes to 0. Wait until PV is close to SP and as stable as it can get. 2 Set Control Mode to Manual 3 Set P to ½ of current setting 4 Set Control Mode to Auto. PID tuning involves the entire process. The device used to switch the heater is not particularly relevant. Every system is different and will in general require individual parameter sets. Google 'PID tuning' for lots of tutorials.

What is PID Tuning?

Before starting with the guide on how to do a PID tuning, we fist need to understand the concept. In just a few words, PID is an algorithm that makes sure the heaters for both hotend and heatbed supply just enough heat in order to have the difference between the highest and lowest temperature as small as possible.

If you are interested in learning more about what is PID, you can check this Wikipedia article where a PID controller is described

Prerequisites for 3D Printer PID Calibration

In order to perform a successful 3D printer PID tuning, you need to have the 3D printer connected to your computer via USB.

Next, you need to access the terminal for your printer firmware. Today I will use Pronterface but anything will do, as long as you are able to send G-Code commands to the printer.

Hotend PID Tuning

Now that we have the 3D Printer connected to the computer, we can start the Hotend PID Calibration.

  1. Get the current PID settings using the M503 command. Your printer will return the current PID settings.
  2. Run the M106 S255 command in order to set your cooling fan to 100%
  3. Run the M303 E0 S215 C8command and wait for the process to finish.

The message “PID Autotune start” will appear in the terminal. Your hotend will start to gradually heat and get new readings.

While the hotend PID Calibration is underway, let’s understand the command we ran.

M303 – This command initiates a process of heating and cooling to determine the proper PID values for the specified hotend or the heated bed.
E0 – This argument selects the extruder we want to calibrate. I have only one extruder, so I will set it to 0.
S215 – This argument sets the temperature for the extruder PID Calibration to 215C.
C8 – This argument sets the number of cycles we want to run. I selected 8 because it’s the recommended value in Marlin firmware, but any value from 3 to 10 is great.

When the message “PID Autotune Finished” is displayed in the terminal window, the hotend PID Tuning is complete.

You will also see new Kp, Ki and Kd constants that need to be saved so let’s do that now. The previous values were so we need to adapt the command with the new values and save them.

  1. Run the M301 P24.36 I1.39 D106.76 command to add the new values
  2. Run M500 to save the values.
  3. Run M503 to check your current values. These should be the same as the values we just saved.

Heatbed PID Tuning

If you managed to perform the hotend calibration, then the heatbed PID Calibration will be much easier.

  1. Get the current PID settings using the M503 command. Your printer will return the current PID settings for the heatbed.
  2. Run the M303 E-1 S60 C8command and wait for the process to finish.

The message “PID Autotune start” will appear in the terminal. Your heatbed will start to gradually heat and get new readings. While the heatbed PID calibration is underway, let’s understand the command we ran.

M303 – This command initiates a process of heating and cooling to determine the proper PID values for the specified hotend or the heated bed.
E-1 – This argument selects the heatbed we want to calibrate. I have only one heatbed, so I will set it to 1.
S60 – This argument sets the temperature for the heatbed PID Calibration to 60C.
C8 – This argument sets the number of cycles we want to run. I selected 8 because it’s the recommended value in Marlin firmware, but any value from 3 to 10 is great.
When the message “PID Autotune Finished” is displayed in the terminal window, the hotend PID Tuning is complete.

You will also see new Kp, Ki and Kd constants that need to be saved so let’s do that now. The previous values were so we need to adapt the command with the new values and save them.

  1. Run the M304 P824.78 I154.89 D1097.99 command to add the new values
  2. Run M500 to save the values.
  3. Run M503 to check your current values. These should be the same as the values we just saved.

More information about PID Tuning can be found on the RepRap wiki

The facilities described here are available in RepRepFirmware-dc42 and DuetWiFiFirmware version 1.15 and later.

How the heating control operates

Each heating controller in RepRapFirmware 1.15 and later can work either in bang-bang mode or in PID mode. When operating in PID mode, it can get its PID parameters in either of two ways:

  • Use two sets of PID parameters computed from a first-order + delay time (FOPDT) model of the heating system. The FOPDT model parameters may be derived using auto-tuning, or configured manually using M307.

Pid Arduino Code

  • Use one set of legacy PID parameters configured manually using M301. This provides backwards-compatibility with previous versions, and provides a fallback if the behaviour of your heating system cannot be approximated well enough by a FOPDT model.

After power up or reset, the bed heater controller is in bang-bang mode and the extruder heaters use the model-derived PID parameters. If you use the M301 command to set the legacy PID parameters, the controller for that heater switches over to use them. If you run a successful auto tune or you set the model parameters manually using the M307, the controller switches back to using model-derived PID parameters.

To see which set of parameters is being used, run M307 H# where H is the heater number, and that will tell you either that the model is in use (so model-based PID parameters are being used for that heater), or not in use (so legacy PID parameters are being used).

Each heating controller performs temperature monitoring to try to detect fault conditions such as a heater of thermistor falling out of an extruder heating block. The temperature monitor relies on the model parameters to decide what is reasonable behaviour. Therefore, even if you decide to use legacy PID parameters or bang-bang control, you should still define an approximate model of the heater to provide good heater monitoring without false alarms.

Setting the model parameters by auto tuning

Auto Tuning Pid Arduinotreecost

Auto tuning is initiated by the M303 command:

Firmware 1.18 and later

The S parameter is the temperature to heat up to. The default value depends on which heater you are tuning. During auto tuning, the heater will be run at the specified power until this temperature is reached and then it will be switched off; so the temperature will overshoot the target somewhat

There is also an optional P parameter, which is the PWM value to use. Unless your heater is greatly over-powered, use the default PWM value of 1.0.

Firmware 1.17 and earlier

The S parameter is the maximum allowed temperature. Auto tuning will be abandoned if the temperature exceeds this value.

The P parameter is the PWM fraction to use. You should choose it such that when the heater is run at this PWM for an extended period, a typical printing temperature will be reached but the maximum specified by the S parameter will not be exceeded. The default value of 0.5 is satisfactory for a heater of modest power. If you have a particularly powerful hot end heater, reduce it as necessary to avoid excessive temperature rise. Conversely, when tuning a weak bed heater that can barely reach 100C, increase it.

All firmware versions

Only one heater may be auto tuned at a time. Before commencing auto tuning, the heater to be tuned must be at or near room temperature and its temperature reading should be stable.

Send M303 with an H parameter (and optionally P and S parameters) to start the auto tune process. A message will be generated when auto tuning is completed or abandoned. You can also run M303 with no parameters to see the current status of auto tuning.

Warning! During auto tuning there is minimal protection against heating faults. Therefore you should not leave the printer unattended during auto tuning.

If auto tuning is successful, new model parameters are set and the PID parameters computed from them are used. You can see these parameters by running the M307 H# command, where # is the heater number M307 will also indicate that the model is in use, meaning that the PID parameters displayed by M307 are used, not the PID parameters displayed by M301.

Tuning a hot end heater typically takes between five and ten minutes. Tuning a bed heater may take more than half an hour, depending on the thermal capacity of the bed. You can cancel tuning by sending M0.

After you have run auto tuning and checked that the heater control is working well, run M500 to save the heater parameters in config-override.g (this is supported in firmware 1.17 and later). Alternatively, construct a M307 command with the model parameters and add it to config.g. For example, if after tuning you run M307 H0 and see this:

then you should add the following to config.g to preserve this model:

The B0 parameter tells the system to use PID (the default on power up is bang-bang for the bed heater). If you need to limit the maximum PWM of the heater, add an S parameter to the M307 command, for example S0.8 will limit the PWM to 80%.

Make sure there is no M301 command for the same heater after the M307 command, otherwise the model will not be used.

RRF 3.x - Error: Heater 0 fault: temperature rising much more slowly than the expected 1.7°C/sec

In RRF3 there is no longer a default heater model assumption used for the bed. Therefore, you must perform a PID auto tuning to give the firmware a model for bed heater performance. Failure to do so will result in an error message that the heater temperature is not rising fast enough and therefore triggering thermal protection. Running the PID auto tune for the bed and saving the results should resolve the error.

Troubleshooting auto tuning

  • If you are using firmware 1.17 or earlier and auto tuning fails because the maximum temperature you specified in the M301 command (or the default) is exceeded, you will be prompted to use a higher maximum temperature (S parameter) or lower PWM (P parameter). You should wait until the hot end is within 10C of room temperature before trying again. The higher your hot end heater power, the lower the P value you need. The default of 0.5 works well for a 30W heater on a typical hot end (E3Dv6) run at its nominal voltage. A 40W heater might need P=0.4, and a 60W heater P=0.25. If your power supply voltage is higher than the nominal heater voltage then you should reduce P further. For example if you are using a 12V heater with a 13.2V supply, you should reduce P by 20% to account for the 10% higher voltage.
  • If auto tuning fails with a message that the temperature is not rising fast enough, this indicates that either you are using too low a P value in the M303 command, or the dead time in the existing model is much too low for your heater. You can increase the dead time using the M307 command. For example, sending M307 H0 D30 will increase the dead time to 30 seconds. The actual dead time will be known after a successful auto tuning.

Setting the model parameters manually

Model parameters can be changed and reported using the M307 command. Examples:

The H parameter specifies the heater.

The A parameter is the model gain, which is the ultimate temperature rise divided by the PWM fraction. For example, a gain of 350 means that at a constant 50% PWM, the temperature would eventually reach ambient temperature plus 350 * 0.5 degC.

The D parameter is the dead time, which is the delay between a change in PWM and an appreciable effect on the rate of temperature change.

Arduino pid autotune

The C parameter is the model time constant. This may be thought of as the time taken for the temperature rise to reach 63.2% of its ultimate value after the heater is switched on from cold at constant PWM, less the dead time.

You can limit the PWM by adding a suitable S parameter, for example S0.8 will limit the PWM to 80%.

To use bang-bang mode instead of PID, change B0 to B1. In bang-bang mode, the S parameter is still used to limit the PWM when the heater is turned on.

Measuring the gain (M307 A parameter)

  • If necessary, use M307 to set some parameters that let you achieve a steady temperature. The default ones may work.
  • Set the heater to a temperature you typically use and wait until the temperature is stable. It doesn't matter if it is still creeping up slowly, but it must not be oscillating (if it oscillates, increase the M307 D parameter).
  • Send M573 P# (where # is the heater number) to report the average PWM.
  • The gain is: (actual_heater_temperature - ambient_temperature)/average_pwm.

Measuring the cooling time constant (M307 C parameter)

Pid

With the heater at a steady temperature Tstart, calculate the following target temperature:

Ttarget = Tstart * 0.37 + Tambient * 0.63

Turn the heater off and time how many seconds it takes for the temperature to drop to Ttarget. That is the time constant.

Manual adjustments to the heater model parameters

If necessary you can make manual adjustments to the M307 model parameters, as follows:

  • If during initial heating the temperature undershoots the target, reduce the A parameter. If it overshoots the target, increase the A parameter. Try increasing/decreasing it by 5% or 10%.
  • If the response to temperature changes caused externally (e.g. the print cooling fan turning on) is too slow, reduce the D parameter. If the temperature is not stable but oscillates around the target temperature even when the print head is stationary, increase the D parameter. Try increasing/decreasing it by 30%.

Setting legacy PID parameters

This mode is intended as a backup, for use if model-based tuning is not working well enough. The parameters are configured using the M301 command. Example:

The H parameter is the heater number. Usually, 0 is the bed heater, 1 is the extruder 0 heater, and so on.

P, I and D are the standard proportional. integral and differential coefficients, scaled by 255 for compatibility with older firmware. A negative P value means use bang-bang control.

Previous firmwares also had B, S, T and W parameters in the M301 command. These are no longer used.

Pid Auto Tuning Algorithm

Temperature faults

If the firmware detects a temperature anomaly, it will turn off the corresponding heater and put it in the Fault state. This can be seen in DuetWebControl, which will show the status of that heater as 'fault' instead of 'active', 'standby' or 'off'.

You can reset a heater that is in the fault state using the command M562 P# where # is the heater number.

When a heater goes into the fault state, an error message is generated giving the reason. You can view the message in the Console page of DuetWebControl or PanelDue. The most common reasons are:

  • Temperature reading was too low for several consecutive readings, suggesting that the thermistor has become disconnected
  • Temperature reading was above the limit set by M143 for several consecutive readings
  • A thermocouple or PT100 interface chip reported an error for several consecutive readings
  • During initial heating, the temperature was not rising as fast as expected. This can occur if you have a slow or low-powered heater and you have not tuned the heater model, so that your heater has a greater dead time or a lower gain than the default model
  • The heater reached the target temperature, but the temperature subsequently varied by more than +/-10C. This can occur if you are using unsuitable PID parameters, or if you turn on a very strong print cooling fan that cools the nozzle excessively.

Using multiple temp sensors for a single heater

There are three ways of using two thermistors to control one heater:

  1. Connect them in series. The temperature readings will be averaged with a bias in favor of the colder one. If any wire breaks, you will get the usual -273C reading and the heater will fault.
  2. Connect them in parallel. The temperature readings will be averaged with a bias in favor of the hotter one. If one of the thermistor wires breaks, the temperature will under-read, so you will end up heating too much.
  3. Use one to control the temperature, and the other as an over-temperature cutout, configured using M143.

Pid Loop Tuning

Note: if connecting 2 in series, the resistance of the thermistors needs to be combined in config.g. So if each thermistor is 100k at room temperature, they need to be set as 200k when declaring the sensor with M308. And if connecting in parallel, the resistances need to be half their combined average (or just half of one if both are the same).

However, when connecting thermistors in series or in parallel, although the B value remains the same, the effective C value changes. So it's easier to adjust the R parameter instead. For example, on a Duet 3 the normal R value is 2200. If you connect two identical thermistors in parallel, you can specify the usual T, B and C parameters for the thermistors, but double R to 4400. Likewise, if connecting two thermistors in series, halve the R value.