Reading a button

On the same line of experiments, let’s now learn about logic states, as well as how to work with data input devices.

!Important Information

Up to this point, our experience as been too one-sided. Blinking LEDs, making sound with the Buzzer, they are all considered output. Let’s change a little bit the approach and read an input signal.

The inputs are signals or external values sent to a system. Some of the common input components are buttons or switches. The keys on a keyboard, for instance, are an input for your computer, because they send data to this system.

!Identifying the Components

The figure below shows the buttons on the Interface Nanoshield. They are connected to pin A0 on Arduino (further details on the next projects).

Interface Button

!Code Components

Let’s introduce a new function, besides an addendum to if.

Digital Input: digitalRead([pin])

The digitalRead() function is used to read the logic state at a pin. It is capable to tell wether the voltage at this pin is high (~ 5V) or low (~ 0V) or, in other words, if the pin is at logic state 1 or 0 (or HIGH/LOW). Notice that the digitalRead() function does not effectively measures the voltage at the pin. It only tells us if the logic level is 1 or 0, which is sufficient when we work with digital systems.

There is only one parameter on digitalRead() - the number of the pin you want to read like, for example, digitalRead(A0) in order to read the logic state of the pin A0. The digitalRead() works with all Arduino pins from D2 to D13 and from A0 to A5 (the only exceptions are the pins A6 and A7).

On Arduino, by default, all the pins are already pre-configured as input. This way, it is not necessary to configure it again to use the function digitalRead().

The digitalRead() function is different from the other ones, because it returns a value, which is the logic state of the pin. So, instead of only writing the call to the function and jump to the next line, we really need to use this function along with other pieces of code.

The digitalRead() function will return one of the two values: high or low. You can use this return value as part of a conditional test inside an if-statement. For instance:

if (digitalRead(pin) == LOW)
{
    // The button is pressed, do something!
}

Otherwise, you can store the value of digitalRead() inside a variable: variable_name = digitalRead(2);

If/Else:if( [condition] ){ [consequence if true] } else { [consequence if false] }

Let’s use the extension of the if-statement declaration, the else. if / else lets us take an action if the condition is true and another action if it is false.

Here is the structure of an if / else:

 
if ( [condition] )
{
    // The condition is true, do something.
}
else
{
    // Oops, the condition is false, do something else!
}

if / else is really useful when you are dealing with digital outputs, which can only be in one of two states. If a button is pressed, do something and, if it is not, do another thing.

!Running on Arduino

Run the code below on your Arduino. After uploading the code, you can press the RIGHT button on the Interface Nanoshield to turn on one of the LEDs. On the code below you can check out the comments to understand better how it works.

//Pin connected to push-button
int buttonPin = A0;
int led2 = A2;
//Variable to check the state
int buttonState;

void setup()
{
  
  // Defining the button pin as input
  pinMode(buttonPin, INPUT);
  // Defines the green LED as output
  pinMode(led2, OUTPUT);
}

void loop() 
{
  // Checks if the button state has change
  buttonState = digitalRead(buttonPin);
  if (buttonState == LOW) 
  {
   // Turns the LED on    
    digitalWrite(led2, HIGH);  
  } 
  else 
  {
    // Turns the LED off
    digitalWrite(led2, LOW); 
  }
}

!Your turn!

Try to modify the code above so that the yellow LED will be always on and, when the button is pressed, make the buzzer play a sound.

Answer

//Pin connected to push-button
int buttonPin = A0;
int led1 = A1;
int led2 = A2;
//Variable to check the state
int buttonState;
//Pin connected to buzzer
int buzzer = 5;

void setup()
{
  // Defines the button pin as input
  pinMode(buttonPin, INPUT);
  // Defines the Yellow LED pin as Output
  pinMode(led1, OUTPUT);
  // Defines the Green LED pin as Output
  pinMode(led2, OUTPUT);
  // Defines the Buzzer pin as Output 
  pinMode(buzzer,OUTPUT);  
  // Turns the Yellow LED on
  digitalWrite(led1, HIGH);
}

void loop() 
{
  // Checks if the button state has changed
  buttonState = digitalRead(buttonPin);
  if (buttonState == LOW) 
  {
   // Turns the green LED on   
    digitalWrite(led2, HIGH);
    // Makes the buzzer play a sound equivalent to the note G, in Hz
    tone(buzzer,392);    
  } 
  else 
  {
    // Turns the green LED off
    digitalWrite(led2, LOW);
    noTone(buzzer);  
  }
}