Weird Analog input behavior... Help greatly appreciated

Hi,

I'm trying to hook multiple buttons into one analog input in order to minimize the IO pin usage of my project.

I've setup the circuit as shown in the JPG; hooking a 220Ohm resistor to between Ground and the Analog pin #5; then I've setup 5 buttons with different resistors between 5V and the same analog pin in order to get different value depending on which button I was going to press.

The code is very simple:

void setup()
{
  pinMode(A5, INPUT);
  Serial.begin(9600);
}
void loop()
{
  int val = analogRead(A5);
  Serial.println(val);
  delay(10);
}

However, the reading are all weird....

| | | Read from A5 | | Comment |
| - | - | - |
| No button pressed | | ~0 | | Expected. |
| S1 pressed | | ~530 | | A bit low... but seems fine. |
| S2 pressed | | ~20 | | What???. It's only a 1K resistor |
| S3 pressed | | ~100 | | Now it's really weird; a 2K resistor reads a value between the 220 and the 1K resistors???. |
| S4 pressed | | ~40 | | Hu???. |
| S5 pressed | | ~960 | | Now I'm completely lost. |

In case, the JPG wasn't clear:
5V -> S1 -> 220 Ohm -> A5 input
5V -> S2 -> 1K Ohm -> A5 input
5V -> S3 -> 2K Ohm -> A5 input
5V -> S4 -> 5K Ohm -> A5 input
5V -> S5 -> 10K Ohm -> A5 input

What I've tried to understand or make sense of any of this:

  • Grab a multimeter and read each resistor to make sure I wasn't mistaken on their value. (resistor values confirmed)
  • I switched from A5 to A4 to see if the reading would be different; same reading.
  • I swapped some push button in case some had defect causing them to have some significant internal resistors... nothing change.
  • I swap cables, position on the breadboard to see if they would influence the output... nothing.

I'm out of idea. I don't quite understand these results.
What am I missing???

Thanks for helping :slight_smile:

Ok... I just realized the "grounded" resistor may have been the one causing these interferences.
To test my theory, I've changed it to 100K Ohm.

It does indeed change the read values; but it's still not quite that:

S1: 1021
S2: 930 ? ? ?
S3: 1004
S4: 970
S5: 1023 ? ? ? ?

kandjar:
Thanks for helping :slight_smile:

Hi,
A schematic is mandatory.
Regards

vffgaston:
Hi,
A schematic is mandatory.
Regards

I did attach a schematic of the circuit.

Something like the enclosed would work.

Allan

s5.pdf (16.6 KB)

allanhurst:
Something like the enclosed would work.

Allan

Unless I'm mistaken, considering the change i made in my first reply, changing the 220 grounded resistor to 100K, the schematic looks similar, no?

As in: U1 in your schematic would be the same a S2 on mine.
U2 as S3, and U5 as S4.

Yet, I'm reading the following value with the 100K Ohm
S2: 930
S3: 1004
S4: 970

Which I can't quite explain.

Hi,
Your circuit is a bit primitive, but it should work, at least as an approach.
Revise all the connections, may be something makes a loose contact.
Modify your software to accept an input only if it repeats two consecutive inputs.
Regards.

Your circuit is a bit primitive, but it should work, at least as an approach.

Yeah, I agree; it's very primitive, the circuit doesn't even have proper debounce system. I'm very new to this,
trying to build a Yahtzee game for my daughter :slight_smile: Hence the 5 buttons.

Revise all the connections, may be something makes a loose contact

Yeah, I guess I should redo the whole button circuit, one button at a time.
I wish I had pins for the multimeter to plug straight in the breadboard. That would seriously help the debugging.

Modify your software to accept an input only if it repeats two consecutive inputs.

Will do.

I think you've mixed up some of your switches, and one of them has a 12 ohm, not 1k resistor?(*)
The values you should see are 500, 200, 100, 40, 20, and you are missing 200

  • the codes could be mirror images for this combination:
    1k = brown black red brown
    12R = brown red blank brown

MarkT:
I think you've mixed up some of your switches, and one of them has a 12 ohm, not 1k resistor?(*)
The values you should see are 500, 200, 100, 40, 20, and you are missing 200

  • the codes could be mirror images for this combination:
    1k = brown black red brown
    12R = brown red blank brown

This is very unlikely as:

  • I don't have 12 ohm resistors; the smallest one I got right now is 100 Ohm.
  • I measured all resistors twice with a multimeter; I wanted to make sure I did not accidentally swap them; as it would have been an obvious reason for this results.

I did grab brand new resistors for this; I verified their value before using them.

That said, that was my first thought as well: wrong resistors.

I'm however not sure what you mean by: "I think you've mixed up some of your switches"?
Unless you're thinking that I mixed up the resistors order with the switches; which I tested against it.

Test again, because I don't believe you got those values in that order if your circuit is accurate,
I simply don't.

kandjar:
I'm trying to hook multiple buttons into one analog input in order to minimize the IO pin usage of my project.

A5-voltage vs SX Calculation:

Switch Close A5-voltage
S1 ------------------------> (50.220)/0.440 = 2.500V
S2 -------------------------> (5
.220)/1.22 = 0.901V
S3 -------------------------> (50.220)/2.22 = 0.495V
S4 -------------------------> (5
0.220)/5.22 = 0.210V
S5 -------------------------> (5*0.220)/10.22 = 0.107V

Re-think about your resistor-network so that you can rationally use VREF = 1.1 V (INTERNAL) for all the voltages of the above chart. You will get good quality measurements with VREF = INTERNAL rather than VREF = DEFAULT (5V). For example: with VREF = 5V, S5 will be seen as count 21; with VREF = 1.1V, S5 will be seen as count 99.

The arduino 5v out is used to reference the a/d, so the absolute voltage value doesn't matter if you also use it to power the resistor network string - the ratios remain unchanged.

And your odd results can only be through a wiring error or a faulty analog input - try another one

Allan

Found one of the issue; some of the "breadboard wires" I was using were faulty....
After changing the wires; and redoing the whole circuit, thing worked as expected.

Thanks for the help.

Few last questions:

I've noticed that changing the "ground" resistor affect the range of value I'm getting; in my first post, the ground resistor was 220Ohm, and the S1 resistor was also 220, I was getting a reading of ~530.
Changing the ground resistor to 100K Ohm change the reading to ~1020.

For my circuit, I found that 2K was the best choice since it gave me a decent range (> 100) between each button to compensate for error reading.

Q1: Is it because the Arduino has a internal resistor on each Analog pin which causes the current to be split between ground and the arduino pin?

Q2: If so, do we know what is the internal resistor value? Is there a good way to pick a ground resistor other than try/error?

Q3: Should I consider putting a capacitor to "debounce" the button? and if so, how would I wire it and choose its capacitance?

1/ no

2/ n/a, and yes - by calculating using ohm's law.

3/ yes - try 100nF from analog input to ground.

Allan