# How to smooth potentiometer values

I have a linear actuator that has a pot in it. I am reading the value of the pot on my uC and I am noticing that the values are fluctuating wildly when the pot is changing (both up and down). The values don't ever seem to stabilize. I use the moving average value to stabilize the value in software however this leads to errors. When I attach a normal pot to the uC I do not get any value fluctuations. All the systems are connected to the same power supply.

Question: Is there a better way in electronics obtain a stable reading from the pot in a noisy environment?

• Do you have a schematic? That might explain why the pot isn't stable. You can also try to test the pot when it's disconnected, so that you're sure it isn't just that the pot's broken. – user17592 Apr 6 '13 at 16:29
• 1. The pot's contact surface is probably either scraped off in parts, or covered by an oxide layer, thus causing the jumping values. Replace the pot. 2. A code-based method to address the problem is to reject outliers, i.e. any values more than a set amount away from the average of the previous n values. Would you want me to formulate this into an answer? – Anindo Ghosh Apr 6 '13 at 16:36
• Ack, the entire system is rather complex so I can't really produce a schematic (sorry). There are steppers in the system as well but everything has decoupling caps. 2nd Its a new pot so I don't think the wiper is damaged as yet. Also I don't get noise when the motor is not moving (well very little anyways) – user3045 Apr 6 '13 at 17:35
• This may be related: electronics.stackexchange.com/questions/51474/… – jippie Apr 6 '13 at 18:01
• Please add a datasheet and circuit diagram to your question. – jippie Apr 6 '13 at 18:02

## 3 Answers

Are you digitizing a variable voltage produced by an analog potentiometer?

I have used gear like that and seen the jitter and flakiness in the values being read from the pots. In my experience it could be improved substantially by cleaning the potentiometers with compressed air and alcohol.

I suspect what the problem is that DC is naively being sent across the potentiometer, and then the divided voltage is sampled.

Audio designers know that significant DC currents flowing across potentiometers (which also carry audio) will add lots of scratchy, crackling noises when those potentiometers are moved (and maybe even when they aren't).

If I were designing a circuit which digitizes the position of an analog potentiometer (rather than the obvious approach of using a rotary or linear encoder), I would drop strictly an AC signal (from an oscillator) across the pot, with no DC component. This would then be rectified, peak-detected and digitized.

Another consideration is that cheap carbon pots are more noisy than ceramic, conductive plastic or wire wound units.

A very simple software technique to smooth values is exponentially-weighted smoothing. This is a beautiful trick which allows the smoothed value to depend on all prior values, without retaining a history of prior samples. (Familiar analogy: the charging of a capacitor is similarly exponential, and the current value depends on all the past currents. However, the capacitor has no memory, per se.) Looks like there is a Wikipedia page about it.

To implement exponential smoothing, simply keep track of the estimated value $s$ of the potentiometer (or whatever parameter $x$ you're sampling). The estimator is initialized with the first sample:

$$s_0 = x_0$$

Therafter, when a new sample is obtained, we replace the estimator with a linear blend of the existing and new value, where $\alpha$ is the blending factor (called "smoothing factor" in this situation).

$$s_t = \alpha x_{t-1} + (1 - \alpha) s_{t-1}$$

This can be conveniently rewritten like this:

$$s_t = (s_{t-1} - s_{t-1})^{\rightarrow 0} + \alpha x_{t-1} + (1 - \alpha) s_{t-1}$$

$$s_t = s_{t-1} - s_{t-1} + s_{t-1} + \alpha x_{t-1} - \alpha s_{t-1}$$

$$s_t = s_{t-1} + \alpha x_{t-1} - \alpha s_{t-1}$$

$$s_t = s_{t-1} + \alpha (x_{t-1} - s_{t-1})$$

In other words, the new estimate is the old estimate, plus a fraction of the difference between the new sample value and the old estimate.

If all the quantities are integers and $\alpha$ is some power of two constant, this is very easy to implement in code. For instance, suppose we take $\alpha$ to be $1/4$, so that we blend 25% new value, 75% estimator:

   /* C pseudo-code */
s += (sample - s)/4;  /* optimizes to s += (sample - s) >> 2; */

/* s returned to caller as the value */


Exponentially-weighted smoothing implemented as one line: subtract, shift right, accumulate.

• So, a moving average then? – user3045 Apr 6 '13 at 20:53
• A moving average is related but somewhat different. You keep a history of N samples and take their mean (or some weighted average). Samples older than N have no effect on the value. – Kaz Apr 7 '13 at 1:12
• I prefer this answer because of the suggestion of the AC component. – user3045 Apr 7 '13 at 3:24
• This worked amazingly well for me in smoothing out a noisy pot: s += (sample - s)/4; – Chris McCormick Dec 20 '16 at 13:31

Have you tried just putting a 100nF capacitor across the potentiometer wiper? That might just do the trick for you. This should have a "smoothing" effect, and bigger value capacitors will have a more pronounced smoothing effect.

• Yea, I tried that; it failed. – user3045 Apr 6 '13 at 17:31
• @kurtnelle do you have an oscilloscope you can use to have a look at how the signal varies for real? – vicatcu Apr 6 '13 at 20:15
• nope no oscilloscope (would really like one of those) – user3045 Apr 6 '13 at 20:44

It sounds like electrical noise from your motor is being coupled back to the potentiometer. If this is a DC motor linear actuator, try the following best practices as a first step:

• Use twisted shielded cable for the potiometer excitation, wiper, and ground wires.
• Use two pair twisted cable for the potentiometer - one pair for excitation and ground and the second pair for wiper and ground, with the grounds connected at the potentiometer. Use a differential amplifier at your board to reject common mode noise.
• If practical, use twisted pair cable for the motor power leads
• Use ~0.1uF ceramic capacitors across the motor leads (at the motor) and from each motor lead to the motor housing to mitigate some of the motor commutation-induced noise.
• Avoid long parallel runs of motor power and potentiometer signal cabling to reduce crosstalk.

A better way to do loop closure in noisy environments is to use a quadrature encoder, as the signals between the linear actuator and your controller are digital and thus more noise immune. For very noisy environments, even digital encoder signals are handled differentially.

Encoders are available in many form factors, including ones that are interchangeable with potentiometers:

• The ceramic capacitors are implemented in the linear actuator from the manufacturer. Twisted pair for the pot? Yikes but sounds fair. The differential amplifier sounds like a good idea. – user3045 Apr 6 '13 at 17:33
• How is the potentiometer excitation voltage generated? Is it well regulated and clean? Use a scope to look at the excitation leaving your board and the excitation at the potentiometer (try both with and without the motor running). Since the pot wiper is single-ended at the moment (and inherently ratiometric), noise on the pot excitation will be coupled into its output. – HikeOnPast Apr 6 '13 at 18:20
• ask so that means it generates noise as it moves. So the only way to get the true value is to wait till it stops!? – user3045 Apr 6 '13 at 20:45
• I wont be able to use a quadrature encoder here but I wish I could. – user3045 Apr 7 '13 at 3:25