Project 7, Multiple Buttons on Resistor Ladder

Hello Everyone,

I've got project 7 working fine but am trying to look into it more. The book asks a question regarding what happens when you press multiple buttons.

For me I get no noticeable change in the serial monitor. It shows the same results and plays the sound of the least resistive button.

Is this correct and due to the fact the current follows the least resistance? Is there a way to configure the circuit so that pressing multiple buttons will in fact change the resistance.

Any insight would be great thanks.

I'm curious about this too. In the book they imply that you will get different analogRead() values when you push multiple buttons. This is not what I get. I also used iCircuit to build the circuit digitally. Same result where where regardless of the numbers you push, the voltage reads the same as if you had only the button pushed connected to the resistor of the lowest value. Thus, if you have the button connected to the 220 ohm resistor and the button connected to the 1Mohm resistor depressed, you would get the same voltage as if you only had the button connected to the 220ohm resistor depressed... Would love someone to answer this for us!

After some research and asking around I found a solution. The law of resistors in a parallel circuit is 1/R = 1/R1 + 1/R2. R is the total resistance and R1 and R2 are the resistances of the resistors. If you press the buttons of the 220 Ohm and the 1 MOhm resistors for example not a lot would happen because the total resistance would be 219,95 Ohm. That's almost the same as just pressing the 220 Ohm resistor. Hence pressing multiple buttons isn't not working.

The problem was solved when I used the 1k, 4,7k and 10k Ohm resistors. I just had to recalibrate the amounts of analogInput i received but pressing multiple buttons works fine! You can't combine the button without the resistor with another button however.

So now I have a keyboard which plays 7 different notes.

I hoped this helped and this is my code:

int notes[] = {262, 294, 330, 349, 392, 440, 494};
void setup() {
  Serial.begin(9600);
}
void loop() {
  int keyVal = analogRead(A0);
  Serial.println(keyVal);
  if(keyVal == 1023){
    tone(8, notes[0]);
  }
  else if(keyVal >= 925 && keyVal <= 935){
    tone(8, notes[1]);
  }
  else if(keyVal >= 690 && keyVal <= 695){
    tone(8, notes[2]);
  }
  else if(keyVal >= 510 && keyVal <= 515){
    tone(8, notes[3]);
  }
  else if(keyVal >= 940 && keyVal < 950){
    tone(8, notes[4]);
  }
  else if(keyVal >= 770 && keyVal <= 780){
    tone(8, notes[5]);
  }
  else if(keyVal >= 950 && keyVal <= 955){
    tone(8, notes[6]);
  }
  else{
    noTone(8);
  }
}

xinatron:
After some research and asking around I found a solution. The law of resistors in a parallel circuit is 1/R = 1/R1 + 1/R2. R is the total resistance and R1 and R2 are the resistances of the resistors. If you press the buttons of the 220 Ohm and the 1 MOhm resistors for example not a lot would happen because the total resistance would be 219,95 Ohm.

You've almost got it xinatron; nothing wrong in that but you've forgotten the pull-down resistor of 10K-Ohms and, not sure if you have this correct in your head or not but, the Arduino isn't measuring resistance (Ohms) or current (Amps/Amperes); the Arduino is measuring Voltage. (Specifically the Arduino is measuring the difference in voltage between the sensing pin, A0, and the Arduino's reference. More on that here, be careful and read the whole thing or you might damage your Arduino.)

So what we're dealing with in Project 7 isn't just a parallel circuit, it's what's known as a Combination Circuit or a Series Parallel Circuit. The short explination on how you calculate values for a series parallel circuit is you first simplify all parallel parts into logically-discreet parts, so that you can consider your entire circuit as a simple series circuit. You simplify the parallel parts with that formula you included:

1/R[sub]T[/sub]=1/R[sub]1[/sub]+1/R[sub]2[/sub]+...1/R[sub]n[/sub]

Now in the resistor ladder circuit we've built in Project 7 what we really want to know is how much voltage has the parallel part dropped (consumed); if we know that we will know how much voltage the Arduino will be measuring. Consult Ohm's Law and do the following:

  • Simplify the circuit.
  • Calculate the total resistance of the circuit.
  • Calculate the total current of the circuit
  • Calculate the voltage drop across you simplified parallel circuit.
  • Subtract the voltage consumed by the parallel circuit from the total. Now you know how much voltage the Arduino will measure.

Case 1:
Since our circuit has 4 switches this is a bit more complicated. There are 16 different possible parallel circuits (look at the switches as 4 bits in binary or take my word for it;). I'm only going to demonstrate 2 (well 3). Lets consider pushing just switch #2. Switch 2 will cause current to flow through a 220 Ohm resistor. We'll have a simple series circuit in this case.

+5V---|SWITCH|---|220 Ohm|---|Sensor|---|10K Ohm|---Ground

How much voltage will the Arduino measure at the Sensor? Lets find out:

  • Some rules for series circuits: V=IR; Resistance adds; Current is the same; Voltage adds to total.
  • 220 Ohm + 10K Ohm = 10,220 Ohms
  • 5V = I x 10,220
  • 5V/10,220 = I
  • I = 0.000489 == 0.489 mA == 489 uA
  • Current is the same for the whole of the series circuit, so:
  • IR1 = 0.489mA
  • VR1 = 0.428mA x 220 Ohms
  • VR1 = 0.108 V
    With R1 (the circuit's second resistor we brought into play by pressing the button) dropping 0.108V that means that the Arduino's sensor (pin A0) will see 4.892V. We can estimate what the value of analogRead(A0) will be if we know that. analogRead will return a value from 0 to 1023, that's 1024 different possibilities. It's measuring in a 5V circuit. So 5V/1024-levels = 4.88 mV per 1 level. So if A0 is seeing 4.892V/4.88mV = 1002(.46). analogRead(A0) should be about 1002.

Case 2:
How about 2 switches? OK! Lets use the second and third switches which have resistors of 220 Ohm and 10K Ohm respectively. Remember, these switches are in parallel.

+5V-\--[Switch 1|---|220 Ohm|--/-|Sensor|---Ground
[color=beige]_____[/color]\-[Switch 2|---|10K Ohm|-/[color=beige]___________________[/color]
  • Simplify the parallel part of the circuit into a logically-discreet component.

  • Some rules for parallel circuits: V=IR; 1/RT=1/R1+1/R2...1/Rn; Voltage is the same across branches; Currents add to the total (2A go into 2 branches of equal resistance? Each branch has 1A going through it.)

  • 1/RT=1/220 + 1/10,000.

  • 1/RT=0.00464545...

  • RT=215.264

  • OK! Now we have (what we can think of as) a simple series circuit again! Same as above:

  • Series' RT = 215.264 + 10,000 (Parallel + that 10K pull-down resistor)

  • RT=10215.264

  • 5V = I x 10215.264

  • 5V / 10215.264 = I

  • I = .00048946 == .48946mA == 489.46uA

  • Therefor the voltage drop against our parallel circuit is:

  • V = 489.46uA x 215.264 Ohms

  • V = 0.1054 == 105.4mV

  • Therefor the Arduino will sense 4.8946V! Turned into analogRead(A0) output it would be: 1002(.99)

And yeah, that's the same analogRead output as with just the 220 ohm resistor... I was quite unimpressed with this particular project. They hint to how powerful the resistor ladder could be but don't demonstrate it at all well. Perhaps that was on purpose... anyway, onto Case 3.

Case 3:
You push that first switch that doesn't have a resistor on it + switch 2 with a 220 Ohm resistor. This case is super simple. The two switches make up a parallel circuit, one branch has no (relavent) resistance and the other branch has 220 Ohms of resistance. How do we calculate the drop across the parallel part in this situation? EASY: The first branch has no resistance in it, we call that a SHORT! In this situation ALL (so far as we care) of the electricity will flow through the short; no electricity will bother flowing through the resistor on the other branch. Remember: Electricity will always take the EASIEST path to ground. So the 220 Ohm resistor is compleatly bypassed. Now if you're wondering about above when it was a 220 Ohm and a 10K Ohm resistor, why didn't it all flow through the 220 Ohm resistor? Because there was some resistance in both paths. MOST of the electricity went through the 220 Ohm resistor, but some went through the 10K Ohm resistor. Specifically the 220's current was 0.479mA and the 10K's current was 0.01054mA. (I used the voltage drop of the parallel part, that voltage is applied to all branches of the parallel part equally. So it was 0.1054/220=IR1 and 0.1054/10K=IR2).

So yeah, like I said, I was severely disapointed in this project in the Arduino book. It was obvious to me, and I think it was mentioned, that it would be possible to build a resistor ladder such that you could not only tell each button appart; but you could also tell all button combinations appart. So I built a circuit with that goal in mind: to be able to read all 16 possible button combinations. Now mind you I've had electronic classes before and I have parts from them, that's where these resistors came from. You should be able to acomplish this with the kit's 220, 540, 1K, & 4.7K Ohm resistors; but I haven't run the math. First thing I had to do was eliminate the short on switch 1 if I wanted to be able to read button combinations including button 1; see the previous paragraph for why. Next I gave each switch these resistors:

  • Switch 1 a 1K Ohm resistor
  • Switch 2 a 2.2K Ohm resistor
  • Switch 3 a 4.7K Ohm resistor
  • Switch 4 a 8.2K Ohm resistor All I was trying to do was double the resistance for each step in the resistor ladder. Then I did a bunch of math, exacly the same as above in case 2, and found out that YES all possible combinations resulted in more that 4.88mV of difference at the sensor than any other combination of buttons.

AND THEN ... I spent some hours writing this code, it took some help (thanks weekend!) and it's probably a bit advanced for most people reading this, but I commented it up so hopefully people can understand it. https://bpaste.net/show/01709bbb39f9 << That's the code, this post is too long if I put it in here; besides, this way you get pretty colours.

Then I went to find somebody to tell my story to. Enjoy!

Thank you Darien for pointing me on my mistakes and correcting me. You explained it very well, way better than I could. I also curse autocorrect because it messes everything up whilst writing this reply.

Thanks alot for you'r in depth answer, Darien! Learned a ton.