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