Using One Analog Pin to Read 4 Buttons - Arduino

3,196

40

3

Introduction: Using One Analog Pin to Read 4 Buttons - Arduino

The idea is to read the resistanse differense between them using resistors.
The buttons are connected in such way that each button add the resistors of the buttons before him to a serial out put.

I used 4 220ohm resistors and 4 buttons.

int sensorPin = A0;    
int sensorValue = 0;
void setup() {
  pinMode (sensorPin, INPUT_PULLUP);
  Serial.begin (9600);
}
void loop() {
  sensorValue = analogRead(sensorPin);    
  sensorValue =  (map (sensorValue, 18,44, 1, 5));
  if (sensorValue < 5 ) Serial.println (sensorValue); 
}

Be the First to Share

    Recommendations

    • Pi Day Speed Challenge

      Pi Day Speed Challenge
    • Trash to Treasure Contest

      Trash to Treasure Contest
    • Microcontroller Contest

      Microcontroller Contest

    3 Comments

    0
    chrisidh07
    chrisidh07

    5 years ago

    Can someone please explain how this line works?
    sensorValue = (map (sensorValue, 18,44, 1, 5));

    0
    efahrenholz
    efahrenholz

    6 years ago

    why couldn't you make a voltage divider before each button. Since this is a 10-bit adc, your 1024 steps could be chopped up into analog ranges for each button press.

    0
    ke8bg
    ke8bg

    6 years ago

    Well this takes me back.. Old VCRs used too use this method... Old top load VCRs with 30' " wired" remotes. If a switch gets worn out and develops enough resistance it will mimic the next highest resistance switch next too it.