# Getting rid of jitter when measuring voltage

• Just a quick question - maybe someone knows a better way: My custom pH-meter has a voltmeter which returns values which are jumping around a bit. Now, this may be because the whole thing currently resides on a breadboard and thus may be subject to outside influence, but still.

Calculating the mean is still jittery but the median is at least somewhat stable.

Current setup: Take 100 measurements (i.e. float) in one second, calculate the median and output the value. However, there's still some jitter and, knowing my pupils, they'll be confused if they have to decide whether to jot down 0.13 V, 0.15 V, 0.16 V or 0.18 V.

Before you say: "Simply use more data!" I'd like to point towards my use of an Arduino Nano which is a bit limited in RAM for dynamic variables. Which means that 200 float data points are pushing it.

So, any ideas on how to further stabilize the value? First instinct would be to build the average of several medians but that feels wrong somehow.

• @rhywden I've forgot the name of the approach, but you can average out the values by doing a weighted average of the previous output and the current input.

Vnext = ɑ * Vin + (1 - ɑ) * Vprev

• Maybe attach a capacitor to the voltage source and let it charge until it is in equilibrium, then measure the voltage on the capacitor.

• I've forgot the name of the approach, but you can average out the values by doing a weighted average of the previous output and the current input.

Vnext = ɑ * Vin + (1 - ɑ) * Vprev

Maybe attach a capacitor to the voltage source and let it charge until it is in equilibrium, then measure the voltage on the capacitor.

I love the fact that the first two responses said basically to do the same thing, but one of them described how to do it in software and the other one described how to do it in hardware.

• @benoni I'm not sure that one is applicable - I only have one dimension. Kalman seems to need at least two.

Of course I could create a differential from two data points. Not sure how valid that would be.

• First, if you are calculating average, us a moving average (rolling average).
Second, consider putting in a median filter first... https://embeddedgurus.com/stack-overflow/2010/10/median-filtering/

Edit, third, since you are taking readings from an ADC and using an 8-bit micro without floating point unit. The readings from the ADC would be a integer or a long before they get calculated into floating point, therefore do your averaging in integer.

• @Rhywden You have position and velocity. That's two dimensions, if you really need it. Filtering voltage is one of the simplest canonical cases for Kalman filters. This page has a reasonable explanation of how to do it with a single dimension: http://bilgin.esme.org/BitsAndBytes/KalmanFilterforDummies

• 200 float data points

floats are only precise to about 0.0000001, so depending on how precise your readings are in the first place, that might be causing the jitter. A double would bring that to around 0.0000000000000002, and with 200 of them, that's only an extra 800 bytes. If that's too much, going to 100 doubles might improve your data's stability instead of 200 floats.

• 200 float data points

floats are only precise to about 0.0000001, so depending on how precise your readings are in the first place, that might be causing the jitter. A double would bring that to around 0.0000000000000002, and with 200 of them, that's only an extra 800 bytes. If that's too much, going to 100 doubles might improve your data's stability instead of 200 floats.

Naw, I only have about 3 significant digits.

Anyway, I just tested the one-dimensional Kalman filter. It stabilizes the values nicely but takes quite some time to get there - we're talking about roughly ten to fifteen seconds (with one measurement every 10 ms). Also, for some reason my whole setup works for neutral solutions (yields -0.13 V) and acidic solutions ( > -0.8 V), but my basic solution seems to have triggered some bug and and only yields 0.0 V, whatever I do.

It doesn't help that the Arduino doesn't have a proper debugger. Maybe I'll connect the setup to an RPi - that has an I2C interface as well, after all. And using Python instead of C might reveal if it's a software or a hardware problem somehow.

• @rhywden What voltage are you expecting for the basic? Based on the other two you quoted, it should be about +0.54 volts - but at least 0 is in the right place on the continuum (higher than neutral, which is higher than acidic), so maybe it just scales weird.

• @rhywden What voltage are you expecting for the basic? Based on the other two you quoted, it should be about +0.54 volts - but at least 0 is in the right place on the continuum (higher than neutral, which is higher than acidic), so maybe it just scales weird.

It should indeed be a moderately positive number - roughly around 0.7 V. But the result is a constant 0.0000 - absolutely no jitter there (not even in the last significant digit).

• a constant 0.0000

Usually a maths issue will overflow a number and start wrapping...
Is the ADC able to measure positive voltages or run out of range? Can you post schematic of the voltage measurement end?

• an 8-bit micro without floating point unit

Fixed-point math. Fun!

• a constant 0.0000
Usually a maths issue will overflow a number and start wrapping...
Is the ADC able to measure positive voltages or run out of range? Can you post schematic of the voltage measurement end?

The ADC measures voltages in both directions just fine - that's why I'm using it in the first place. I think I'll connect my oscilloscope to various places to see what's what.

Might take some time, from today on I have two weeks of holidays

• @rhywden I've forgot the name of the approach, but you can average out the values by doing a weighted average of the previous output and the current input.

Vnext = ɑ * Vin + (1 - ɑ) * Vprev

Classic Single Pole Low Pass Filter...

VERY similar to the hardware solution of adding a capacitor, but NOT mathematically identical (hint: there is no such thing as "pure" capacitance)

• @TheCPUWizard
Another key difference I suspect is that while that code is subject to the measuring interval, a capacitor is not, allowing the capacitor to stabilise its value more quickly.

There may be benefit in combining the approaches.

• VERY similar to the hardware solution of adding a capacitor, but NOT mathematically identical (hint: there is no such thing as "pure" capacitance)

VERY similar to a normal human being's response, but ACTUALLY a pedantic dickweed response (hint: fucking unclench)

• @TheCPUWizard
Another key difference I suspect is that while that code is subject to the measuring interval, a capacitor is not, allowing the capacitor to stabilise its value more quickly.

There may be benefit in combining the approaches.

And another is that the jitter might actually be random noise from inside the ADC, in which case a capacitor would stabilize the voltage before the jitter got added -- in other words, if the jitter is from the ADC itself, you'd need a software filter to remove it. (Or a better ADC.)

• And another is that the jitter might actually be random noise from inside the ADC, in which case a capacitor would stabilize the voltage before the jitter got added -- in other words, if the jitter is from the ADC itself, you'd need a software filter to remove it. (Or a better ADC.)

@Rhywden already mentioned attacking the problem with an oscilloscope — that should be able to answer that problem.

Looks like your connection to What the Daily WTF? was lost, please wait while we try to reconnect.