Hi all, I have been trying to tune a PID controller connected to a mini 12v low current computer fan with an in built hall effect sensor. I am just looking to control the rpm. When I connect the fan directly to 5v I can view the rpm using an interrupt which is great. The trouble is when the PID is used. The fan will oscillate up and down between an output value of either 0 or 255 every second. I feel like I have tried every Kp and Ki but still no luck.
Was seeing if I have missed anything?
double Setpoint, Input, Output;
double Kp = 1, Ki = 0, Kd = 0;
unsigned long last=0;
int pulseCount,RPM;
PID myPID(&Input, &Output, &Setpoint, Kp, Ki, Kd, DIRECT);
void setup() {
Serial.begin(9600);
pinMode(2,INPUT);
attachInterrupt(0,encoderPulseInterrupt,FALLING);
pulseCount=0;
Setpoint = 4000;
myPID.SetMode(AUTOMATIC);
myPID.SetTunings(Kp, Ki, Kd);
myPID.SetSampleTime(1);
}
void loop() {
if ((millis()-last)>=1000)
{
last=millis();
noInterrupts();
RPM=pulseCount*60;
Input = RPM;
myPID.Compute();
analogWrite(5, Output);
Serial.println(RPM);
interrupts();
pulseCount=0;
}
}
void encoderPulseInterrupt()
{
pulseCount++;
}
Alex.