I've got a simple control problem that I've tried to solve with a simple algorithm so far, but am wondering if there's a more sophisticated algorithm.

I have a pump hooked up to an Arduino that is feeding water into a vessel on a scale. My goal is to dose a specific amount into the vessel based on the scale feedback. So far I've got a few tolerances defined and am slowing down the motor to specific speeds as I approach the target.

This algorithm works just fine for a few pumps (I've got 4 different pumps and I'm controlling each one after another). However, some pumps require a bit more power than others in order to pump liquid at a slow rate. Rather than trying to tune my simple algorithm for any new pump I'm using, I'm wondering if there's an algorithm that will naturally slowly ramp down the power (PWM) on each motor.

I know PID is generally meant for a continuously moving target, though this use-case seems similar enough. I'm basically wondering if there's a PID-type algorithm I can use that will have no overshoot and I can cut off as soon as I've met my target weight. Do I just use a standard PID algorithm and tune to minimize overshoot?

  • 1
    \$\begingroup\$ Yes, you can use PID, but if you don't want an overshoot, you will have to have relatively low coefficients that will slow down the process and will probably give you a constant error (which you can offset by setting the setpoint higher than expected though). But for a "full-featured" PID you need a mechanism for a controlled removing the liquid as well. \$\endgroup\$
    – Eugene Sh.
    Apr 8, 2022 at 15:06
  • \$\begingroup\$ "some pumps require a bit more power than others in order to pump liquid at a slow rate" that means the feedback that you need for controlling the PIDs appropriately has to be the flow rate of each pump output. Not the liquid level. So, basically, you can do this, but not without significant changes to the mechanical part of the design. \$\endgroup\$
    – dim
    Apr 8, 2022 at 15:10
  • \$\begingroup\$ The setpoint of a PID controller can be constant or time-variable. In this case your setpoint would be a water level? If so, the PID will adjust the PWM to maintain the water level to your setpoint. In laymen's terms, the PID will slow down the motor when the water level is close to the setpoint. \$\endgroup\$
    – Ben
    Apr 8, 2022 at 15:16
  • \$\begingroup\$ PID is a second-order control system. So you get the classic outcomes of "underdamped" (reaches target quickly, has overshoot), "critically damped", "overdamped". The integral action will eliminate any constant error mentioned by Eugene in his comment, but he's completely correct that setting your gains to avoid overshoot will slow down convergence. There are many online resources explaining damping in second-order systems that you can use to conceptualize what you are trying to tune for, one nice looking one is apmonitor.com/pdc/index.php/Main/SecondOrderSystems \$\endgroup\$
    – Ben Voigt
    Apr 8, 2022 at 15:43

1 Answer 1


Do I just use a standard PID algorithm and tune to minimize overshoot?

I think no. If you set PWM you set (control) flow rate, not level. So a PID is not the right algorithm.

To reach a position (liquid level), normally two cascaded PIDs are used: one controls the position (liquid level), the other controls the speed (flow rate). Two cascaded PIDs work well to keep the level constant: even if the level drops because someone consumes the liquid, they would work together to gently refill.

If I understand well, you reduce PWM down to a value PWMmin, as the vessel gets full. Your problem is that you risk to set a PWM too low which, for some type of pump, it is not sufficient and the pump stops.

A PID algorithm would be correct to control the PWM given a flow rate. As you approach the target value (quantity of liquid) you reduce the flow rate, and the PID regulates the PWM consequently.

If you can read the flow rate, you have all you need. Perhaps you have not... but you could estimate the flow rate reading the liquid level at intervals (seconds? minutes?). May be your reading is fast and accurate enough, or maybe not, all depends on time, precision, and whether it is allowed that a pump stops for a while (because the PWM is too low to make it move), until the software notices that the liquid level stopped raising and raise PWM consequently.

So, I think there are two possible strategies:

  1. Decrease PWM as you are doing now but, if a stop of the pump is detected, raise it again

-- or --

  1. When decreasing PWM, monitor the flow rate in an effort to understand the correct value to decrease PWM in the next step. I suppose that, before reaching the critical PWMmin, you notice that the flow rate is decreasing too much in respect to decrease of PWM.

If none of the two methods are viable, then there is no other choice to read reliably the flow rate, and in this case the PID should not be too difficult to set up.

When you can reliably (more or less) control the flow rate, you can concentrate on the first problem (reach a liquid level).

What you are doing now is actually a sort of PID - no, better than a PID, because what you are doing now is an algorithm tailored to the problem. PID is a good and general algorithm, but its bigger defect is that it doesn't know anything about the system it is driving. All you can do is set three coefficients, while in many cases more "intelligence" can do better. To stay on this case: a PID can try to generate a correct PWM for the pump, but it does not natively know that there is a minimum value to respect: that is a thing that must be managed outside the PID (or the PID must be modified). But now, we are back to the first problem: if this minimum PWM is not a fixed value, the PID can well stop the pump for a while, until it recognize that there is no flow, and then the PID will increase PWM again.

  • \$\begingroup\$ Okay so this gives me an idea, if I'm aiming for 5 grams of water let's say, then I can customize and use "intelligence" to dictate what my set point (flow rate in g/s) currently is of the PID algorithm. So when I get to 1g left, I can set a slower rate, and so on at 0.1 g, ect. Did I interpret this correctly? \$\endgroup\$ Apr 8, 2022 at 16:15
  • \$\begingroup\$ Utilizing the PID to control the motor, and manually controlling the set point (desired flow rate) was very effective, and makes much more logical sense, thanks! \$\endgroup\$ Apr 8, 2022 at 23:30

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.