I have written some code to test buttons that I want to use

const int buttonPin1 = 1;
const int buttonPin2 = 2;
const int buttonPin3 = 3;
const int buttonPin4 = 4;
const int buttonPin5 = 5;
const int buttonPin6 = 6;

int buttonState1 = 0;
int buttonState2 = 0;
int buttonState3 = 0;
int buttonState4 = 0;
int buttonState5 = 0;
int buttonState6 = 0;

void setup() {
  pinMode(buttonPin1, INPUT_PULLUP);
  pinMode(buttonPin2, INPUT_PULLUP);
  pinMode(buttonPin3, INPUT_PULLUP);
  pinMode(buttonPin4, INPUT_PULLUP);
  pinMode(buttonPin5, INPUT_PULLUP);
  pinMode(buttonPin6, INPUT_PULLUP);

void loop() {
  buttonState1 = digitalRead(buttonPin1);
  buttonState2 = digitalRead(buttonPin2);
  buttonState3 = digitalRead(buttonPin3);
  buttonState4 = digitalRead(buttonPin4);
  buttonState5 = digitalRead(buttonPin5);
  buttonState6 = digitalRead(buttonPin6);

  if (buttonState1 == HIGH) {
    Serial.println("button1 press");
  if (buttonState2 == HIGH){
    Serial.println("button2 press");
  else if (buttonState3 == HIGH){
    Serial.println("button3 press");
  else if (buttonState4 == HIGH){
    Serial.println("button4 press");
  else if (buttonState5 == HIGH){
    Serial.println("button5 press");
  else if (buttonState6 == HIGH){
    Serial.println("button6 press");


I have tried with the buttonPins being A0-A5 as well, and whenever I try to run the code, it always spams out that button 5 and button 2 are being pressed, despite me not pressing them, as well as when I remove the buttons entirely, and pull out all the wires from it, it still prints it out. Even when I re-run the code. How can I fix this?

  • 1
    Yes? That's to be expected. The clue is in the pin mode.... INPUT_PULLUP - and you check to see if it's HIGH. See the correlation? – Majenko Nov 9 '18 at 15:35
  • how did you wire the buttons? – Juraj Nov 9 '18 at 15:41
  • @Majenko oh, I see, so it's when I release it? That explains it. – Ola Kveli Nov 9 '18 at 15:46

You are setting button pins with INPUT_PULLUP mode, which activates internal resistor connected to supply voltage. In this way, your input pins are in state HIGH if disconnected or if button is not pressed.

To fix this, you need to compare your button state with LOW. Also, make sure that one side of your buttons is connected to ground. Then, pressing the button will connect the button to ground and will bring your button pin to LOW.


The answer is in the way you configured the buttons. Because you used INPUT_PULLUP, the Arduino will read the pin as HIGH when it is connected to ground.

To utilize this in your setup, the buttons must be connected to ground, not 5V. This way, when the button is pressed, the input pin gets connected to ground, causing it to read HIGH.

Another way to fix the problem is to leave the buttons connected to 5V and just set the button pins to normal input: pinMode(buttonPin, INPUT);

In summary, there are two fixes: either connect your buttons between the input pins and ground, not 5V OR use pinMode(buttonPin, INPUT);.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.