How to connect set of buttons to an ADC with Voltage Divider

Post Reply
User avatar
Shane
Captain
Captain
Posts: 226
Joined: Sun Jul 19, 2009 9:59 pm
Location: Jönköping, Sweden

How to connect set of buttons to an ADC with Voltage Divider

Post by Shane » Thu Jun 23, 2011 4:27 pm

This tutorial uses the same principal of the voltage divider that the potentiometer and analogue sensors are based on. Each of the switches pass through a resistor of a different value to ground, creating a different analogue voltage on input A0. Multiple switches simultaneously pressed creates additional variations in the voltage that appears on pin A0 (1/Rt = 1/R1 + 1/R2 +...+ 1/Rn). This enables the Arduino to determine exactly which switch or combinations of switches are pressed at any one time, creating 15 possible switch combinations. In this example, we used a 1K Ohm resistor on switch 1, a 2.2K Ohm resistor on switch 2, a 3K Ohm resistor on switch 3, and a 3.9K Ohm resistor on switch 4 (these exact resistances must be used for proper functionality). To complete the voltage divider, a 2.2K Ohm resistor connected to the other leg of each switch and the pin A0 on the arduino, with its other leg connected to +5VDC. Resistors of different values can be used - in order to determine the analogue value on pin A0 with each switch and combination of switches, you can use the Serial.print command to see the value in the Arduino serial terminal window.
2.jpg
2.jpg (141.62 KiB) Viewed 2954 times
1.jpg
1.jpg (203.67 KiB) Viewed 2954 times

Code: Select all

// This program reads the analogue value of the resistor paths when the buttons are // pressed. Each button and combination of buttons generates a unique analogue 
 // voltage reading on pin A0. This analogue voltage is then used to identify which // button or combination of buttons are pressed at any given moment. The state of // the buttons is printed into the serial window.

int analogButtonPIN = 0; //pin the buttons and resistors are connected to

int ButtonValue = 0; //value for reading analogue value on pin A0

void setup()
 {
   Serial.begin(9600); //start serial and set baud rate
 }

void loop()
 {
   ButtonValue = analogRead (analogButtonPIN); //read value on poteniometer
   if (ButtonValue >700 && ButtonValue <706){
     Serial.println ("switch one is pressed");
   }
   else if (ButtonValue >511 && ButtonValue <517){
     Serial.println ("switch two is pressed");
   }
   else if (ButtonValue >428 && ButtonValue <434){
     Serial.println ("switch three is pressed");
   }
   else if (ButtonValue >367 && ButtonValue <373){
    Serial.println ("switch four is pressed");
   }
   else if (ButtonValue >778 && ButtonValue <784){
     Serial.println ("switches one and two are pressed");
   }
   else if (ButtonValue >760 && ButtonValue <766){
     Serial.println ("switches one and three are pressed");
   }
   else if (ButtonValue >749 && ButtonValue <755){
     Serial.println ("switches one and four are pressed");
   }
   else if (ButtonValue >647 && ButtonValue <653){
     Serial.println ("switches two and three are pressed");
   }
   else if (ButtonValue >623 && ButtonValue <629){
     Serial.println ("switches two and four are pressed");
   }
   else if (ButtonValue >575 && ButtonValue <581){
     Serial.println ("switches three and four are pressed");
   }
   else if (ButtonValue >814 && ButtonValue <820){
     Serial.println ("switches one, two and three are pressed");
   }
   else if (ButtonValue >807 && ButtonValue <813){
     Serial.println ("switches one, two and four are pressed");
   }
   else if (ButtonValue >793 && ButtonValue <799){
     Serial.println ("switches one, three and four are pressed");
   }
   else if (ButtonValue >712 && ButtonValue <718){
     Serial.println ("switches two, three and four are pressed");
   }
   else if (ButtonValue >835 && ButtonValue <841){
     Serial.println ("all switches are pressed");
   }
   delay (200);
   //slow down the refresh of analog input reading and print of switch state

 }
Courtecy of UMASS AMHERST
Post Reply

Return to “Arduino”