header
Back to Tutorials Page
  

 
 
VOLTAGE DIVIDER::

This tutorial uses the same principal of the voltage divider that the potentiometer and analog sensors are based on. Each of the switches pass through a resistor of a different value to ground, creating a different analog voltage on input A0. Multiple switches simulataneously 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 analog 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.

CODE:

// This program reads the analog value of the resistor paths when the buttons are // pressed. Each button and combination of buttons generates a unique analog
// voltage reading on pin A0. This analog 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 analog 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

}

 

4 SWITCHES USING VOLTAGE DIVIDER BREADBOARD CIRCUIT

 

VOLTAGE DIVIDER WITH RESISTORS SWITCH SCHEMATIC