PID Library provides "ghost" output when Setpoint is Zero

I am controlling a dc motor by H bridge and PWM. Nothing fancy so far, the input is a pulse number coming from an encoder on the motor shaft. On and off are provided by poling a digital read on a pin and that works great too, when it goes high, the PID accelerates and settles nicely right on the set point thanks to the Ki. However when the digital read goes from HI to LOW and the setpoint is Zero, the output descends nicely but it never goes to zero. A slight analogWrite of 3.60 always remains wasting battery power and buzzing my ears out. By now the motor shaft is still and the encoder outputs 0. I even rounded the output, and it dropped to 1 from 3.60, still no solution, I expected a zero.

When my ForeVal == LOW and the setpoint is 0, only the Ki works multiplying the accumulated error by 0 when the shaft stops. Any number times 0 is 0. Where is the output of 1 coming from?

thanks a lot,
Mitch

Tracks10_DigitalSetpoint_PID_ino.ino (2.71 KB)

     if (millis() - lastmillis == 200){  //Uptade every one 5th of a second,

...is a mistake. The comparison is equal-to-or-greater-than.

   RPM_PID.SetSampleTime(100);
...
     if (millis() - lastmillis == 200){  //Uptade every one 5th of a second,

Which is it? 100 milliseconds or 200 milliseconds?

laptophead:
A slight analogWrite of 3.60 always remains wasting battery power and buzzing my ears out. By now the motor shaft is still and the encoder outputs 0.

Which means the PID algorithm is doing exactly what it should be doing. The input is zero so the output does not need to be adjusted.

You need to finish off the control strategy. The solution is simple. If the RPM drops to zero, switch the PID loop to manual and set the output to zero.

I don't know what an analogWrite of 360 represents in terms of motor power, but if this is so low that it is effectively negligible and the system is being held stationary by stiction then you could just define a dead band around the 'zero power' position which treats these very small values as zero. That's not changing anything the PID is doing, it's just applying some post-processing to the PID output before you apply it to the motor.

Yes I did, I put a If condition that when the input is 0, forget the PID and just analogWrite 0, That worked. Thanks.