Using PID to control Pololu DC Geared Motor

Hello Members,
I have a Pololu geared motor - 131:1 ration. I am trying to use the PID library to drive this motor.
I recognize this post is somewhat repetitive to my other posts.
I still have a problem getting one part of my program to work correctly.
I am driving the DC geared motor, using Setpoint,Input, and Output from the PID
object. The input value comes from an encoder on the motor shaft. I use pin change
interrupt to increase (or decrease) the count ==> the Input. When I run the PID in the
DIRECT mode, the input speed is detected and adjusted more or less “close enough”, the “count” is incremented, and I.E. a Setpoint of 40, gives and input of 39 or 40, the PWM is around 39.
BUT if it reverse the direction of the motor (switching HIGH to LOW on Arduino Pin 2, and LOW to HIGH - Arduino pin 4) the motor reverses, the encoder interrupt decrements for a few counts and the motor stops, which means the encoder has no output.

I have made some changes in the code I am using, and now when I reverse the motor from the initial direction (in this case clockwise) - the PWM goes to MAX (255), the motor runs at full speed (approx 100 rpm), and the encoder decrements, but the PID object does not respond - i.e. does not try to maintain setpoint of 40,

I have tried using myPid.SetControllerDirection(REVERSE), etc to no avail.

Any guidance here? I am missing something. I will attach the code I am using.
I also have code here (commented out) that performs a manual error calculation using Kp and Kd,
and this code functions correctly - I can reverse direction of motor (using “getparam” func) and the motor
will reverse and maintain the setpoint.

My end goal for all this is to be able to use a DC motor (not a stepper), and perform a “one step” with
a combination of PID and count - I want to rotate motor 1 “click” pause for a given time, and move one more “click”, etc.

Thanks again,

FullMotorControl.ino (16.1 KB)

Hello, ewholz.

If you are getting it to work in one direction but not the other, I suspect you might be mishandling a negative sign somewhere. I briefly looked at your code, and it looks like you are missing a semicolon in line 162. Does the sketch you uploaded compile?

I recommend writing a simple program that turns the motor in the reverse direction using PID control. Once you get that working, you can then integrate it to your code.

- Jeremy

Hello Jeremy,

thanks for the input. I will check line 162 - the code does compile,
so that line may be commented out. I will continue to see if I can
find where the + and - values are important.