PID controlled Pump

Hello,
Whilst waiting for my Arduino board to arrive, I have been working on some code to impliment a PID controlled pump.
To give an idea of what the project entails, I will have the Arduino switching a MOSFET that will provide power to a DC motor. The motor is connected to a small pump and I have a pressure sensor that has a range of 0 to 2600mbar.

So far the code is quite simple, as I dont want to spend too long coding before I can test it, but what I would like to be able to do is to increase the pressure over a defined period of time, and then maintain the target pressure with as little oscillation as possible.
The rate of pressure increase will be limitted by the flow rate of my pump, but also the volume of the air that will be compressed, and whether there are any small air leaks etc. hence the use of PID.

If anyone has any ideas how to implement a 'pressure controlled' ramp into the code that I have so far, it would be much appreciated.

Regards,
Oliver

#include <PID_v1.h>

//Define Variables 
double Setpoint, Input, Output;
int pressure;

//Define pins
int pressure_pin = A0;              // aka pin 14
int motor_pin = 3;

//Define Initial PID Terms
double Kp = 2;
double Ki = 5;
double Kd = 1;

//Specify the initial tuning parameters
PID myPID(&Input, &Output, &Setpoint,Kp,Ki,Kd, DIRECT);

void setup()
{
  pinMode(motor_pin,OUTPUT);
  
  pressure = analogRead(pressure_pin);
  Input = map (pressure,0,1023,0,2600);  //0 to 1023 --> 0 to 2600mbar
  Setpoint = 1013;                                     // desired pressure in mbar
 
  myPID.SetSampleTime(200);               // Initialise sample rate in ms (200ms default)
  myPID.SetOutputLimits(0, 255);         // Initialise Output range - 0 to 255 suitable for AnalogWrite
  myPID.SetMode(AUTOMATIC);            // Turn the PID on
}

void loop()
{
  pressure = analogRead(pressure_pin);   // Read Pressure Sensor
  Input = map (pressure,0,1023,0,2600);  // Convert to mbar
  myPID.Compute();                                    // Compute output using PID terms
  analogWrite(motor_pin, Output);          // PWM output
}

Hi, just for some input, if you are going to speed control your pump.
If you have it occasionally pulsing to keep the pressure to set point, I would advise you monitor the pump motor temperature as short ON/OFF or slow RPM will cause overheating.
The motor may not get up to speed to ventilate itself. So a ventilation fan may be necessary.

You will need to program in a minimum speed for your motor so that it will not stall, and will start when commanded to.

Hope this helps.

Tom........ :slight_smile:

OliverWestby:
If anyone has any ideas how to implement a 'pressure controlled' ramp into the code that I have so far, it would be much appreciated.

The target pressure is defined by the Setpoint variable. At the moment this is initialised to 1013 and then left at that constant value.

If you want the set point to change over time then you need to add some code to alter the value of that variable over time. There are endless different ways to implement a ramp, but for example you could store your ultimate target value in a variable and periodically compare the current set point with the target; if it's not equal, adjust is slightly towards the target. The speed of the ramp will be determined by how often you do this check, and how big your adjustments are.

The blink without delay example sketch demonstrates how to execute code at regular intervals.

I want to ask.

how you define Initial PID Terms?

double Kp = 2;
double Ki = 5;
double Kd = 1;

thanks