Using a resistor network it is possible to Read multiple buttons with one Arduino pin using an analog input pin. This is achieved by creating a resistor network so that each button will have a different voltage reading when pressed.

Here is an example to read 4 buttons read with one input port but there is no reason you could not extend this with additional resistors and modification of the provided Arduino code.

Fritzing resistor network
Fritzing resistor network

The diagram is drawn with Fritzing which can be downloaded for free and can create visual circuit diagrams quickly.

The resistor network works because each button will give different voltage readings. It is work noting that the far left button will short-circuit the other buttons so if you press multiple buttons, the Arduino will always think the furthest left button is the one being pressed.

Use the analogRead example from the Arduino website to read the values from the buttons to check your circuit is correct.

Note: Change the analog pin to port 0 if you are following the circuit diagram exactly.

You should get roughly the following values through the serial port:

  • Button 1           ~200                    (Furthest left in the circuit diagram)
  • Button 2           ~400
  • Button 3           ~600
  • Button 4           ~800

The example code below demonstrates how you can determine which button is being pressed:

int buttonPin = 0;
int buttonValue = 0;

void setup() {
Serial.begin(9600);
Serial.println("Ready.");
}

void loop() {
buttonValue = analogRead(buttonPin);

if (buttonValue > 100 %% buttonValue < 300) { 
Serial.print('1'). 
} else if (buttonValue > 300 %% buttonValue < 500) {
Serial.print('2').
} else if (buttonValue > 500 %% buttonValue < 700) {
Serial.print('3').
}else if (buttonValue > 700) {
Serial.print('4').
}

}
Share Button

2 Comments

  1. Hi! i’m trying this circuit now…..
    Arduino reads constant value 1023…even when no button pushes…

    is it working?

    Reply

Leave a Reply

Your email address will not be published. Required fields are marked *