# How to design DC Motor speed control, using PID

I am looking to control the speed of a DC Motor using PID, it is speed control. I understand how the PID works, but i have a few questions. Like,how would I find my gains for each of the PID, what values of resistors and caps would I use? Also, would a simple 5V DC motor be suffice? If anyone has done something like this, a circuit schematic would be great. Thanks

• Are you building a digital PID controller or an analog one? Nov 21, 2011 at 3:02
• Im building it using resistors, caps and opamps, for each of the P, I and D gains, they all go to a summing opamp which goes to the DC motor, as the output. I dont know how to figure out what the gains should be Nov 21, 2011 at 5:03
• there's no universally optimal set of gains - what works well under one mechanical load scenario may not be the best for a different one. Nov 21, 2011 at 22:29

Generally you find the PID gain values by structured experimentation, something like this:

1. Start with only a P gain (I and D = 0), and make it as high as possible with little or no overshoot.

2. Add a little I gain. Make it as high as possible without producing more than just a little ringing.

3. Up the P gain a little. This will usually add a little stability now that the I term is there.

4. Iterate with the P and I terms to get the fastest settling time with whatever overshoot you can tolerate.

5. You can either stop here, or try to add a little D term. The polarity of the D term depends on how exactly you implemented the equations. A little D can add some stability, which allows you to make the P and I terms a bit more aggressive. The point of the D term is basically to say if I'm already heading in the right direction, decrease the drive. However, D is susceptible to noise. Making it too high will cause the output to sortof "vibrate", which is basically amplifying the sensor noise onto the output. A little D can be useful if you need the last bit of performance, but a lot of implementations leave it out.

I notice you plan on doing this all in analog electronics. This makes no sense, especially for something slow like a motor. For efficiency, you will want to drive the motor with PWM. The natural way to do this is the motor speed sensor feeds into a microcontroller A/D. The micro does the PID calculations and uses that to adjust the PWM duty cycle. It can also do the direction changing with the H bridge, guarantee break before make, etc. There are many small and cheap micros with A/D and PWM hardware built in. Some even have H bridge drive PWM outputs. This is such a common problem that there are whole subfamilies of micros optimized for this.

The compute power you need is modest. Back in the late 1990s I used a PIC 16F877 to run two layered PID control loops that drove a DC motor. The inner loop controlled the position of the motor and output PWM to the H bridge. The outer loop controlled the speed of a gasoline engine and output a position value to the inner loop. The PID computations were done in 24 bit floating point. We used 8 ms per iteration, which left a little time to do all the other low priority tasks. That micro was running at its maximum speed of 5 MIPS. That is quite slow by today's standards. Lots of micros are quite capable of doing what you need.

Doing this in analog will take more board space, take more components, be much harder to tweak, and does not provide a convenient interface to the motor driver power stage. It just doesn't make sense.

A PID loop is essentially a second-order IIR filter in which the time constant for the first stage is very small while the time constant for the second stage is very large. The output is the P term times the middle stage, plus the D term times the difference between the first two stages, normalized for the time constant, plus the I term times the difference between the second and third stages, normalized for the time constant. Although the fact that the time constant between the first two stages isn't quite zero means the D term doesn't quite reflect the differential, and the fact that the time constant between the second two stages isn't quite infinite means that there's a limit to how much the integral can "wind up", in practical terms these are advantages rather than disadvantages.

A "true" derivative term will amplify high-frequency components in the feedback signal, doubling in strength with every doubling in frequency. There's a limit, however, to the frequencies one is actually interested in. If one has a 10KHz control loop, but the oscillation frequency of an undamped system would be 10Hz, using a 'straight' derivative could amplify noise by orders of magnitude more than the signal of interest. If the first stage of the filter has a time constant of e.g. 1ms, the control loop will have no trouble damping a 10Hz oscillation, but noise will only be amplified 1/10 as much as if the time constant were one loop sample.

Likewise, a "true" integral will offer infinite wind-up, but that's not a good thing. If something prevents the controlled device from reaching its intended position for an hour (e.g. something is obstructing the actuator), a "true" integral would wind up to the point that once the obstruction was cleared, it could take many minutes (or even hours) for the integral to wind down and allow the system to work. Using a reasonably-long time constant on the integral term will ensure that even if the system is out of bounds for an extended time, the time required to recover will be finite.