Go Down

Topic: Debounce a Potentiometer? (Read 11457 times) previous topic - next topic


Even when I'm not touching the pot, analogRead returns a range of values, typically +/- 3.

Even mapping the 10bits to a range of 50-241 will still flicker +/- 1

How do you ignore the noise?  I have a 7 segment display showing the pot value and it jumps all over the place.

This method sort of works yet really sucks as I can't change the value by 1.

Code: [Select]

  if(abs(prevPotValMap - potValMap)>1){
    Serial.print("potValMap ");
    prevPotValMap = potValMap;


Try putting a 100nF cap from the analog input pin to ground.

You have a 100nF cap from Aref to ground as well, yes? (is included on most Arduino boards)
Designing & building electrical circuits for over 25 years.  Screw Shield for Mega/Due/Uno,  Bobuino with ATMega1284P, & other '328P & '1284P creations & offerings at  my website.


You can filter/decouple in analog (as crossroads said) or in digital as well. Do averaging of the values (ie sum 32 pot measurements and divide by 32) or implement a low pass filter:
where A+B=1, and the smaller the B (ie. B=0.2) the bigger the filtering effect.


I've been struggling with this for a while.  Perhaps a plain old 10k pot is too erratic for what I need, which is:

Dial in BPM (Beats per Minute) and convert to the microSeconds for the display.

I only want a range of 50-240 BPMs out of thing, but it has to sit there rock steady and musical.

I wanted to dial in the integer portion only with the pot, and have inc+/dec- buttons for the 1/10s of a BPM.  A coarse/fine arrangement.

To eliminate a lot of things:
- I grabbed a spare Arduino (Freeduino)
- did the most basic analogRead() tut
- tried the smoothing tut with numReadings up to 40!
- tried the .1 uF cap over the middle and ground pins of the pot
- tried different pots
- did the software LPF with assorted values
- various averaging algo's

I hear of people getting stable +/- 1, what am I doing wrong?  Do I have to go discreet steppers like switches?  Hope not,  I don't need 6 switches for coarse, medium, fine +/-  that would be unusable in low light conditions -or- by anyone that's ingested intoxicants. 

Geez, I'd have this wrapped up in minutes if it were a software UI.  I feel like such a stoopid boob.

Happy Halloween everybuddy!



Nov 01, 2011, 07:12 am Last Edit: Nov 01, 2011, 07:19 am by carlmal Reason: 1
Here's the potentiometer spazziness I'm talking about:

Looks like it is contained to 20mV, what is the period of the wave and what could it be tied to?

How do folks deal with this?



I had a similar problem with an Arduino app that used an external 78l05 voltage regulator.  The regulator had a low level oscillation at about 20 Mhz on the output.  The problem went away when I added a 0.1 ufd cap across the regulator input.
He who dies with the most software wins.


the potentiometer spazziness

While you might not think that word is offensive, remember this is a world forum and in the UK this is a very offensive word to use, and I don't mean potentiometer.

You will always get a reading +/- 1 out of any A/D converter, that is the nature of converters. You need to do one or more of the things you have been told about:-
1) Add some capacitors to smooth the output.
2) Add some software to take the average and thus smooth the output.
3) Implement a threshold so that you only consider a number to have changed when it is say 3 or 4 units away from the previous reading. When you detect this distance then increment (or decrement) the number and look again.


My regrets for using the "s" word.  Here we use it to describe what someone being swarmed by mosquitoes looks like.  

I've been doing more reading on the AREF as crossroads eluded to earlier, read your blog about decoupling, some forum archives here and on bildr.org, LPF on wiki.  One thing I could improve upon is move the cap away from the pot and nearer the input pin on the Arduino.  

I will hook the scope up and watch the changes I make to the circuit and see if I'm improving things.  I was previoulsly trying to draw conclusions from the serial output.

From looking at the wave, it appears to be 1.75MHz  Just wonder what else clocks at that frequency...


Hmm...  ten cycles in 0.55 microseconds.. Looks more like 18 Mhz to me. 

Take a 0.1 or 0.01 ufd capacitor, ground one end then poke around the circuit with the other end and see if you find a spot that removes the oscillation.
He who dies with the most software wins.


SDI-12 USB Adapter, custom designs
Routinely doing USB host and device stuff


Hadn't seen that one, thanks!

I am making some progress.  Advice to put the coupling cap as close as possible to the Arduino pins is not to be ignored.  I had the cap over the pins of the pot, moving it near the Arduino made a big difference.  I will still need some additional smoothing from what I'm seeing so will try this averaging approach on the weekend.  I can see light at the end of the tunnel.


Wow, adding a 100nf capacitor between ground and the analog pin really works for this sort of problem. Kind of amazing.


You measured 18 mHz?, the crystal runs @ 16 mHz?

--> WA7EMS <--
"The solution of every problem is another problem." -Johann Wolfgang von Goethe
I do answer technical questions PM'd to me with whatever is in my clipboard

Go Up