[sdiy] Digital filtering of pot input

Matthias Puech matthias.puech at gmail.com
Fri Dec 2 18:21:28 CET 2016

All right, got it! Thanks a lot Richie.
What you describe is:

  if (in - out > H) 
    out = in - H
  else if (in - out < -H) 
    out = in + H

It works like a charm, and is super simple. Indeed, I do a bit of boxcar filtering before, and a bit of one pole IIR after, to smoothen the angles.

Thanks again for the nice advice.

> Le 2 déc. 2016 à 15:28, Richie Burnett <rburnett at richieburnett.co.uk> a écrit :
> You're on the right track, but you haven;t implemented it correctly.  You need to introduce a dead-band around the current output value in which the ADC result is allowed room to wiggle around.  For example, you might choose to add a dead-band of +/- 4 counts....
> So if the current stored output value is 100, the lower edge of the dead-band is 96 and the upper edge is 104.  If the ADC result wiggles around between 96 and 104 you do nothing.
> If it steps below 96, you add 4 to its value, store the new output value and update the upper and lower edges of the dead-band.
> If it steps above 104, you subtract 4 from its value, store the new output value and update the upper and lower edges of the dead-band.
> If you choose the dead-band to be just a bit larger than the amount of noise your ADC is producing you won't get any spurious changes in the output value unless *you* physically turn the pot up or down.
> If you choose an ADC with more bits than you need, then you can implement the dead-band and still achieve a full range of outputs without stepping when you reduce the bit depth down to what you need for the actual output. For instance you might use a 10-bit ADC for what will ultimately be 7-bit MIDI CCs.
> When you are testing your implementation you can temporarily increase the dead-band to something large that is clearly noticeable when turning the pot.  Then you can feel a noticeable dead-band: As you turn the pot clockwise the output value will increase, but if you stop and turn it back anticlockwise, you have to go a little way back before it starts counting back down.  This proves you've implemented it correctly.  It's kind of like backlash in gears.  The ideal amount of dead-band is an amount that swallows up the ADC noise but is imperceptible to the end user.
> As an aside, if you're putting the product into an electrically noisy (EMI) environment or using cheap pots that might degrade, then add a bit more dead-band than you think you need, just to be on the safe side.  Most users don't even notice the "backlash" effect when they turn a pot, but they will notice control values that jump around when they're not touching the pot!!!
> -Richie,
> PS. Roman's suggestion of averaging multiple samples is also a very wise suggestion.  Do this as well if you can.  A trick I used to use was to read ADC inputs repeatedly for 100ms then take the average.  Since 100ms is a whole number of cycles at both 50Hz and 60Hz, any contribution from mains "hum" cancels out in the average, and doesn't effect the measurement result.
> -----Original Message----- From: Matthias Puech
> Sent: Friday, December 02, 2016 12:51 PM
> To: synth-diy at synth-diy.org
> Subject: [sdiy] Digital filtering of pot input
> Hi DIYists,
> I have a potentially simple question on filtering. I get my pot reading from a very noisy ADC (on-board STM32F4), but I absolutely need the value to be constant when I'm not touching the pot. It's ok for it to be a bit wobbly while I am turning the pot. Now I'm using hysteresis:
> if (fabs(reading - output) < 0.01)
> output = reading;
> which gives me a stable value when I'm not turning the pot, but I am annoyed by the staircase effect while I'm turning it. Is there a simple filter that will let signal through when there is consistent movement but hold it when not?
> Thanks in advance,
> -m
> _______________________________________________
> Synth-diy mailing list
> Synth-diy at synth-diy.org
> http://synth-diy.org/mailman/listinfo/synth-diy
> -----
> No virus found in this message.
> Checked by AVG - www.avg.com
> Version: 2016.0.7924 / Virus Database: 4728/13523 - Release Date: 12/02/16 

More information about the Synth-diy mailing list