Pages: [1]   Go Down
Author Topic: Debounce a Potentiometer?  (Read 2735 times)
0 Members and 1 Guest are viewing this topic.
Northern Canada
Offline Offline
Newbie
*
Karma: 0
Posts: 24
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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:
  if(abs(prevPotValMap - potValMap)>1){
    Serial.print("potValMap ");
    Serial.println(potValMap);
    prevPotValMap = potValMap;
  }
Logged

Global Moderator
Boston area, metrowest
Online Online
Brattain Member
*****
Karma: 538
Posts: 27117
Author of "Arduino for Teens". Available for Design & Build services. Now with Unlimited Eagle board sizes!
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

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)
Logged

Designing & building electrical circuits for over 25 years. Check out the ATMega1284P based Bobuino and other '328P & '1284P creations & offerings at  www.crossroadsfencing.com/BobuinoRev17.
Arduino for Teens available at Amazon.com.

Rapa Nui
Offline Offline
Edison Member
*
Karma: 60
Posts: 2073
Pukao hats cleaning services
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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:
new_value=A*old_value+B*new_value
where A+B=1, and the smaller the B (ie. B=0.2) the bigger the filtering effect.
P.
Logged

Northern Canada
Offline Offline
Newbie
*
Karma: 0
Posts: 24
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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!

Carl
Logged

Northern Canada
Offline Offline
Newbie
*
Karma: 0
Posts: 24
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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?

Thanks,
Carl
« Last Edit: November 01, 2011, 01:19:20 am by carlmal » Logged

Chicago
Offline Offline
Newbie
*
Karma: 0
Posts: 9
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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.
Logged

He who dies with the most software wins.

Manchester (England England)
Online Online
Brattain Member
*****
Karma: 627
Posts: 34233
Solder is electric glue
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
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.
Logged

Northern Canada
Offline Offline
Newbie
*
Karma: 0
Posts: 24
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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...
Logged

Chicago
Offline Offline
Newbie
*
Karma: 0
Posts: 9
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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.
Logged

He who dies with the most software wins.

Central MN, USA
Offline Offline
Tesla Member
***
Karma: 73
Posts: 7198
Phi_prompt, phi_interfaces, phi-2 shields, phi-panels
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

Have you tried the running average code?

http://arduino.cc/playground/Main/RunningAverage

Logged


Northern Canada
Offline Offline
Newbie
*
Karma: 0
Posts: 24
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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.
Logged

0
Offline Offline
Jr. Member
**
Karma: 0
Posts: 65
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

Anaheim CA.
Offline Offline
Faraday Member
**
Karma: 47
Posts: 2892
...
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

Doc
Logged

--> 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

Pages: [1]   Go Up
Jump to: