Using PID Lib (brettbeauregard) to control a 4-20mA Output

Hello friends.

Until my new circuit board arrvives i am dealing with the PID lib.

When my project is finished i should be able to control a pump, that has an 4-20 mA output via a PID control.

As an leading example i use the basic example from the PID lib from brettbeauregard.

/********************************************************
 * PID Adaptive Tuning Example
 * One of the benefits of the PID library is that you can
 * change the tuning parameters at any time.  this can be
 * helpful if we want the controller to be agressive at some
 * times, and conservative at others.   in the example below
 * we set the controller to use Conservative Tuning Parameters
 * when we're near setpoint and more agressive Tuning
 * Parameters when we're farther away.
 ********************************************************/

#include <PID_v1.h>

#define PIN_INPUT 0
#define PIN_OUTPUT 3

//Define Variables we'll be connecting to
double Setpoint, Input, Output;

//Define the aggressive and conservative Tuning Parameters
double aggKp=4, aggKi=0.2, aggKd=1;
double consKp=1, consKi=0.05, consKd=0.25;

//Specify the links and initial tuning parameters
PID myPID(&Input, &Output, &Setpoint, consKp, consKi, consKd, DIRECT);

void setup()
{
  //initialize the variables we're linked to
  Input = analogRead(PIN_INPUT);
  Setpoint = 100;

  //turn the PID on
  myPID.SetMode(AUTOMATIC);
}

void loop()
{
  Input = analogRead(PIN_INPUT);

  double gap = abs(Setpoint-Input); //distance away from setpoint
  if (gap < 10)
  {  //we're close to setpoint, use conservative tuning parameters
    myPID.SetTunings(consKp, consKi, consKd);
  }
  else
  {
     //we're far from setpoint, use aggressive tuning parameters
     myPID.SetTunings(aggKp, aggKi, aggKd);
  }

  myPID.Compute();
  analogWrite(PIN_OUTPUT, Output);
}

So as far as i understood, the output in the libary gives me per default a value between 0-255 (PWM). Since i will have a 4-20mA output signal to control the pump i have to change the output range of the PID.

I found this funcation in the lib: Arduino Playground - PIDLibrarySetOutputLimits

So do i get it right that i just have to add SetOutputLimits(4, 20) in my SETUP part and the PID will vary its outputvalue in this range?

Thank you

No. analogWrite does PWM which controls the duty cycle on the chosen pin. It provides 0V or whatever voltage your board uses (likely 5V or 3.3). The current drawn will depend on your pump controller.

Assuming you're not actually powering the pump from the Arduino, you may be able to get some control of speed using analogWrite. It's possible though, that you'll need some external components to condition your signal.

I suggest that you forget about PID until you have sorted out basic control of the pump.

I suggest that you forget about PID until you have sorted out basic control of the pump.

Yes. Generating a 4-20 ma current control loop from an arduino is not trivial.

You may want to use a 0-5v to 4-20 ma converter module with the input voltage generated with a low pass filter from Arduino pwm.