Capactiors and multiple buttons

Hello everybody. I'm trying to monitor 5 push buttons over analog pin, but readings aren't very stable. They very around a certain number depending on which button is pressed. Is it possible to somehow stable that reading using capacitors or on some other way?

Is this using an R-2R network or something like that?

(ie show us the circuit if you want meaningful comments!)

Hello everybody. I'm trying to monitor 5 push buttons over analog pin, but readings aren't very stable. They very around a certain number depending on which button is pressed. Is it possible to somehow stable that reading using capacitors or on some other way?

Where are you getting the voltage for the analog part? Try a capacitor to see what happens. What are your resistor values? It helps a lot if you show code and drawings/pictures to us.

Analog pin is set as INPUT_PULLUP.

Try adding a 2.2k resistor to A5 with the other end going to +5Volts

LarryD: Try adding a 2.2k resistor to A5 with the other end going to +5Volts

Tried it. Even tried 100k ohms, but it still varies.

Hi, if you are trying to get an exact constant value for each switch, I’d say you are going to have a hard time.
What are you using for press button?
I have a LCD shield that has press buttons on it arranged to give a similar output, each button is a different analog value, the sketch for it? I will go looking. (Yes I will, the force is strong it is…)
It does not look for an exact number of each button, but a range say 2 or 5% above and below the expected value.
You have contact resistance to work against, and if the buttins are the soft touch tactile, the ON resistance can vary form 10 Ohms to 200 Ohms from one press to the next.

Tom… :slight_smile:

Hi, i found this one that uses if statements in an order so that they look for values lower than expected.

// include the library code:
#include <LiquidCrystal.h>

// initialize the library with the numbers of the interface pins
LiquidCrystal lcd(8, 9, 4, 5, 6, 7);

void setup() {
  // set up the LCD's number of columns and rows:
  lcd.begin(16, 2);
  // Print a message to the LCD.
  lcd.setCursor(0,0);
  lcd.print("LCD Key Shield");
  lcd.setCursor(0,1);
  lcd.print("Press Key:");
}

void loop() {
  int x;
  x = analogRead (0);
  lcd.setCursor(10,1);
  if (x < 100) {
    lcd.print ("Right ");
  }
  else if (x < 200) {
    lcd.print ("Up    ");
  }
  else if (x < 400){
    lcd.print ("Down  ");
  }
  else if (x < 600){
    lcd.print ("Left  ");
  }
  else if (x < 800){
    lcd.print ("Select");
  }
}

Hope this helps…

Tom… :slight_smile:

Thanks Tom and may the force be with you. I'm using push buttons. Values vary by plus/minus 3. So if I press say third button reading goes between 171 and 173. If I can't get exact value I'll then have to set in code what is the range for each button, but that leaves room for errors since analog value is constantly varying because of temperature and what not.

I'm really starting to hate Fritzing. I prefer a schematic over a pictorial, much easier to read. Also, can you crop and shrink? It is difficult to follow when I have to keep scrolling up and down and left and right.

I don't see any connection to 5V. The Analog inputs do not measure resistance, they only measure voltage. Since you are getting stable readings (varying by +-1 bit is stable), that leads me to believe you have this wired a bit differently than what you show here.

Bypassing is de rigueur. The Analog input pins by default use Vcc as a reference. So any noise is picked up as unstable readings. Especially put a bypass capacitor on the Aref pin. 0.1uF to ground would be good.

Hi, fritzing, ditto. A circuit DIAGRAM is certainly better, and a picture of the project so we can see the layout( which in some cases is important). If you are a noob, fritzing fair enough, but migrate to schematics as quickly as possible. Just drawing the diagram with pencil is usually conducive to finding any bugs as you place and connect components.

The problem with using analog method of detecting 5 buttons is that unless you use R-2R network in the switching, you will get errors when pressing more than one button at a time.

So keep going logitech, we all learn as we go.

Tom.. :)

TomGeorge: It does not look for an exact number of each button, but a range say 2 or 5% above and below the expected value.

Tom... :)

Hi, a bit off topic but I am looking for a function just like the one you described - setting an error range of +/-5% of values - is there such a function or anything similar? I have tried with mapping, min, max, constrain but none of these work for me. Regards