Units on call Brett Beauregard's PID


I am trying to maintain a 30 millibar pressure in a tank with a control by PID and an Arduino Nano.
The sensor sends back a 0-5V voltage on a 10bits analog input (0-1023). A simple coefficient on this input gives millibars (analogRead*Coefficient = millibars) where the result is between 0 and 150 millibars. An output of the Arduino, driven in 0-255 steps, controls a pump in PWM by calling analogWrite.

Because of all these range of units are different, how should I modify the below standard example? : Should I SetPoint=30 & (Input*=Coefficient before calling Compute()), or SetPoint=30*100/150 (percentage), or … ?

double Setpoint, Input, Output;
PID myPID(&Input, &Output, &Setpoint, 2, 5, 1, DIRECT);
void setup() {
Setpoint = 100;
void loop() {
Input = analogRead(0);

Thanks for your help.

The only rule you need to know is “Input and Setpoint are in the same units.”

Input is the current pressure measured by your sensor. Since your target is in millibars I recommend you convert the raw reading to millibars and set Setpoint in millibars (30, right?).

The Output is in arbitrary units. Since you are using a PWM output, the default range of 0 to 255 is correct for you.

The DIRECT/REVERSE parameter tells the PID if increasing Output makes the Input go UP (DIRECT) or DOWN (REVERSE). If it’s a pressure pump, DIRECT. If it’s a vacuum pump, REVERSE.

There is no need to worry about units, although you can do the conversions if you want to.

The setpoint can be made equal to the sensor reading when the pressure is at the target value.

When correctly selected, the K values take care of scaling the output.

I like this approach because the debugging will be more readable in millibars.
void setup() {
Setpoint = 30; //30 millibars
void loop() {
Input = Coefficient*analogRead(0); //Where the result is "double" in millibars unit

I'll try it and I'll keep you informed.
Thanks a lot.

31/08/2021 :wink:
It's rock thanks.

This topic was automatically closed 120 days after the last reply. New replies are no longer allowed.