I am trying to position control a DC motor using PID controller using Arduino and a L298N driver. Motor connections is complete and I can send PWM values from 0 to 250 to the motor and it is working fine. The motor does not spin when the PWM value is smaller that around 130 which is expected. I wrote my PID code by calculating PID value and adding it to 130 to generate voltage when there is any position errors. Here is the issue:
If the motor is at complete stop, it starts spinning only with PWM values larger than 130 (it is at stop at 130). However, while the motor is spinning let's say with PWM value of 150, if I switch the PWM value from 150 to 60 it still continue spinning. In other words, while it is spinning, commanding lower PWM values (as low as 50 or 60) can keep it rotating. This makes sense to some extend because of friction and inertia.
Here is my question:
For PID to work, it should generate PWM values of around 130 when there is no position error. However, in real application when the motor spinning toward the target position, it won't stop by commanding the PWM values around 130 even when the error is very small. So, it keeps spinning and this makes the motor to oscillate back and forth around the target position. If I lower the PWM value corresponding to the stop position of the motor, let's say to 50 or 60 (instead of 130), then the issue is that the motor won't start rotating until the error is very big and the generated PWM value is more than 130.
Is there a solution for this? I appreciate your opinion.