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
}