Advertisement
read button state schematic

Read the State of a Button with Arduino – Tutorial #3

You can read the state of a button using Arduino and a few lines of code. The actual state is shown in the Serial Monitor window as 0 or 1, 0 meaning the button is not pressed and 1 that the button is pressed. You can replace the zero and one with words, for example “pressed” or “released”.

const int pinButton = 8;

void setup() {
  pinMode(pinButton, INPUT);
  Serial.begin(9600);
}

void loop() {
  int stateButton = digitalRead(pinButton);
  Serial.println(stateButton);
  delay(20);
}

Code explanation

On the first line we set a variable pinButton with an integer value of 8 (the pin number on Arduino Board where we connect the button or switch). Then in the void() function we set the pin 8 as INPUT and initialize the serial port.

In the loop() we declare variable stateButton with the value obtained using the digitalRead() function that reads the state of pin 8. Then it displays its state in the Serial Monitor window as 0 or 1. There is also the delay() that ensures that we can actually read the value. A problem with the delay might be that if you press the button exactly when the delay happens (20ms in our case) then it will display the action in the windows, that why is better to use a lower value or else you might lose some clicks.

The sketch with words instead of 1 and 0

const int pinButton = 8;

void setup() {
  pinMode(pinButton, INPUT);
  Serial.begin(9600);
}

void loop() {
  int stateButton = digitalRead(pinButton);
  if(stateButton == 1) {
     Serial.println("PRESSED"); 
  } else {
     Serial.println("RELEASED"); 
  }
  delay(20);
}

As you can see using this code Arduino will display PRESSED when the button is pressed or RELEASE otherwise. You can read the if() instruction like this “if stateButton is 1 do print the line PRESSED in the serial monitor, else print the line RELEASED”.

The Schematic

read button state schematic

You can use other pin if you want to and you can even add extra circuitry to turn ON an LED or use a relay to power up different equipments. Actually I will post an article that shows how you can add an LED and a relay.

2 Comments

Join the conversation!

Error! Please fill all fields.
  • ATILIO THOMAZ

    Olá, P. Marian
    Solicito informação como conseguir um dicionario..ou biblioteca onde encontre a tradução dessa linguagem em portugues,minha dificuldade é com a designação na escrita dos codigos,eu trabalhei em programação de CLP, com linguagem LADDER,q é bem diferente,aguardo um retorno seu,no q puder me ajudar.
    Desde já agradeço-lhe.
    A. Thomaz

  • Jack Wilborn

    Sometimes there is an advantage to a constant (pinButton), used in this manner. Declared in line 1, used in lines 4 and 9. In C it usually requires more code to manipulate a byte constant rather than to make it a declared value to be preprocessed, basically quicker and using less memory.

    Equivalent from digitalRead(pinButton) to digitalRead(8), after preprocessing. As usually during the optimizing it picks up the value right out of memory, where as a variable, the quickest way is to load a pointer to the variable, then load it or do whatever the item is via indirection. There are many ways to handle this, but these kinds of code sequences are the best way to find out. Of course being a preprocessed value, it would have a name and be easy to see and modify.

    Is there some reason that you did it this way? Many times the programmer knows of an advantage when doing this kind of coding, which is really my question. Not knowing how the compiler treats this, I believe that it is worth investigating and seeing if this would be a proper way to code all of these types of items. As it being for education, I found it wise and supported by my students to know what happens and give the a way that is consistent, functional and quick. Along with a short explanation of what’s happening.

    I, of course, would be interested in the generated code (assembler), and probably your reason. I have not set up this compiler/assembler for this device, so I can’t really check it out, at this time.

    Thanks…

    Jack Wilborn