Implementing PID with DC motor and 48 CPR Encoder

I am going to begin working on speed control with PID using 34:1 Metal Gearmotor 25Dx52L mm MP 12V with 48 CPR Encoder. I am driving my motors with MAX14870 Single Brushed DC Motor Driver Carriers.

My primary question as of now is whether or not I have some flexibility on which pins I can use for the built in quadature encoders. I’ve noticed that some example PID programs that I have seen use timer 1 for the encoders. However I cannot use timer 1 because my radio communication does. With my driver/motor setup might there be any problems with conflict?

Thanks,
Eric

Hello, Eric.

Since Timer 1 is already used for radio communication, any code implementation that tries to use Timer 1 will create a conflict within your program. Depending on your microcontroller, you could redefine the timer used in the PID examples so both devices are not using the same timer.

Alternatively, you might consider looking at the Reading Rotary Encoders page on Arduino’s website, which contains several helpful examples and some libraries that might be more appropriate for your setup.

- Amanda

OK thank you. It seems like the most common way to read encoder counts is through interrupts. Since my Arduino uno only has two interrupt pins and I am using two DC motors, will I only be able to use encoder A on each motor?

My main application is speed control so I may be able to get away with not being able to read encoder B.

Also is there an equation were I can get the desired motor speed as a function of input PWM? Would it just be max rpm of the motor time the duty cycle?

-Eric

All of the digital inputs on the Arduino Uno support pin change interrupts, and you can use them to detect the changes of an encoder channel for each motor. For more information on the differences between external interrupts and pin change interrupts, see the “Interrupts” page on the Arduino website.

The equation is not that simple (if it was, you would not need to use encoders). If you are trying to run your motors at a fixed speed, you could just implement a simple feedback loop that compares the measured speed using your encoders to the desired speed and change the PWM duty cycle accordingly. The PID algorithm is much more complex and requires a lot of fine-tuning. It might be helpful for you to look at this Wikipedia article on PID to get a better understanding of how the PID algorithm works.

- Amanda

Sorry I wasn’t very clear with my message. I am mainly trying to derive RPM based on my counts on the encoder, 16 counts per revolution and a gear ratio of 34.

I tried something along these lines using the number of counts in a 100 ms loop.

Thanks

Your equation looks fine, assuming that “LOOPTIME” is in milliseconds.

- Amanda

The PID control system is now working with the motors. Thank you for the help Amanda.

-Eric