I'm quite new to electronics, and am right now trying to understand the theory behind this application, where a matrix keypad is using only one analog input pin by having a series of resistors to make different resistances with which the voltage measured on the analog input pin differs:
According to the example, the different button presses will result in resistances between 1,220 ohm and 4,660 ohm. What I'm really interested in is, how I can calculate which voltages the Arduino will read, when i supply 5 volt to the circuit without any other loads than the resistors themselves.
I believe it is pretty basic stuff, but i can't really get my mind around it.
kasperbruun:
What I'm really interested in is, how I can calculate which voltages the Arduino will read, when i supply 5 volt to the circuit without any other loads than the resistors themselves.
It seems you are forgetting the 10k resistor to ground.
The matrix resistor and the 10k resistor to ground make a voltage divider.
Don't calculate in voltage. Calculate in ratio.
Ratio of the button resistor and fixed 10k resistor is the same as the A/D ratio (1024 values).
Leo..
I'm quite new to electronics, and am right now trying to understand the theory behind this application, where a matrix keypad is using only one analog input pin by having a series of resistors to make different resistances with which the voltage measured on the analog input pin differs:
According to the example, the different button presses will result in resistances between 1,220 ohm and 4,660 ohm. What I'm really interested in is, how I can calculate which voltages the Arduino will read, when i supply 5 volt to the circuit without any other loads than the resistors themselves.
I believe it is pretty basic stuff, but i can't really get my mind around it.
That circuit is not very well optimized and can do better with some change of resistor values. The idea is to get the widest range in ADC counts for each button press.
To start with, the 10k would be better at around 4600 Ohms.
Second, the 220 ohm resistors do not add up to 1k when there are four in series so 250 ohms would be better.
The results are as follows and the columns are in order of:
Total resistance, voltage, voltage deviation, count, count deviation, total count.
As you can see the deviations are greater with 250 ohms and 4600 ohms instead of 220 and 10k. That means the ADC can detect the different button states better.
Note the 4600 ohm value is approximate and can probably be 4500 to 4700.
The 250 ohm resistors can be 240.
I see you are converting to voltage.
Don't understand why.
Voltage is always different, and depends on the supply.
Work with the A/D value. That is always the same.
Leo..
Wawa:
I see you are converting to voltage.
Don't understand why.
Voltage is always different, and depends on the supply.
Work with the A/D value. That is always the same.
Leo..
Hi there Leo,
Actually the voltage is there just to show the deviation as an example. If you read the header for what those columns are, you'll see there is also the ADC count, which is valid for those resistor values shown regardless of operating Vcc voltage. That's because the counts are ratiometric with the supply voltage Vcc. That means at 5v they will be the same, and at 4v the same, and at 3v the same, etc. Only the voltages themselves will change.
Also, the process of maximizing the deviation is also not specific to the operating voltage although the actual voltage deviations are. That means that no matter what the voltage supply Vcc is, the max deviation will always show up with a given set of resistor values and that set will be the same for any Vcc voltage like 5, 4, or 3v. The deviation in count will also stay the same.
The idea is to get the maximum deviation in the count for the minimum deviation, but it's related to the maximum deviation in voltage for the minimum voltage deviation so once we find one we find the other. The min deviation count is the last count deviation in each set.
To calculate a voltage with a supply other than 5v simply multiply any of those voltages shown by Vcc/5, so at 3v multiply them by 3/5 to see the actual voltage with Vcc=3v. The counts and count deviations all will stay the same however (with a 10 bit ADC of course).
+1
It seems you understand how it works.
I think spacing a 16-button keypad shouldn't be that hard.
Did once a 101 step pot (0-100%) without overlap/reading problems.
Leo..
Wawa:
+1
It seems you understand how it works.
I think spacing a 16-button keypad shouldn't be that hard.
Did once a 101 step pot (0-100%) without overlap/reading problems.
Leo..
Hi Leo,
Oh that's interesting too. Just one question, how did you know the counts were in prefect sync with the pot resistance?
If you got away with 100 steps or more then yes 16 should not present much of a problem. Just one other thing, did you use the center of the pot to feed the ADC? That would mean (if you did it that way) the top resistor would vary as well as the bottom resistor (10k in this example) so that should give a better result. The problem with the keypad setup is that as we get more and more resistance in series with that bottom resistor (10k) the count deviation gets smaller and smaller, but with 100 counts i guess that means you could easily detect a deviation of 10 counts (like 10,20,30, etc, up to 1000 or so). I guess that is reasonable too as 10 counts is sort of a lot for a 10 bit ADC. Now 1 count difference might be harder to get to be reliable.
I've used pots too for adjustment settings in circuits where the ADC reads the pot that the user sets. One such setting was for an 'on' time setting of a battery charger. As the pot was changed, the time increased. Seemed to work ok although for that i did not need perfect linearity or anything like that because if the time was not long enough the user just turns the pot up a little more
Yes, I admit I used the pot as a voltage divider, not as variable resistor with fixed pull up.
Just wanted to say that because a resistor divider is ratiometric, A/D value is independent of supply (Aref).
Because of that, steps can be very close.
Spacing just has to be enough to take the bad/varying contact resistance of those silver/painted keypads.
Example code I wrote for that 0-100% pot with some hyteresis is attached (not related to this post).
Leo..
// converts the position of a 10k lin(B) pot to 0-100%
// pot connected to A0, 5volt and ground
int rawValue;
int oldValue;
byte potPercentage;
byte oldPercentage;
void setup() {
Serial.begin(115200); // set serial monitor to this baud rate, or change the value
}
void loop() {
// read input twice
rawValue = analogRead(A0);
rawValue = analogRead(A0); // double read
// ignore bad hop-on region of a pot by removing 8 values at both extremes
rawValue = constrain(rawValue, 8, 1015);
// add some deadband
if (rawValue < (oldValue - 4) || rawValue > (oldValue + 4)) {
oldValue = rawValue;
// convert to percentage
potPercentage = map(oldValue, 8, 1008, 0, 100);
// Only print if %value changes
if (oldPercentage != potPercentage) {
Serial.print("Pot percentage is: ");
Serial.print(potPercentage);
Serial.println(" %");
oldPercentage = potPercentage;
}
}
}
Oh yeah you reminded me of my older TV set where the buttons do functions that are not assigned to them. I assume that is because the switches are old and have more resistance than they used to and so are triggering the internal uC chip to see the wrong function voltage and therefore execute the wrong function code. Pretty nutty when that happens: press the volume button, the channel changes