Language: EN

leer-un-pulsador-con-arduino

Read a button with Arduino

In the tutorial digital inputs in Arduino we saw how to read a sensor that provides a digital signal with two levels of voltage LOW and HIGH. We still need to take advantage of the digital inputs to read the state of a switch or button with Arduino.

To do this we will need to correctly make a specific assembly and the help of two new friends the Pull Down and Pull Up resistors. Although both cases are very similar, the assembly and the type of resistance to use will depend on whether we want to read a LOW or HIGH value when the button or switch is pressed.

To understand the operation of both assemblies, we will present the logical reasoning by which we arrive at the same, for which we will use two incorrect assemblies that will help us understand the role of each element in the final assembly.

Therefore, we start with the first attempt to make a reading of the button state.

Another more advanced way to read a button is to use interrupts and apply a debounce to filter the input, as we see in this post

First attempt, direct connection

Our first idea to read a button could be to directly connect an Arduino digital PIN to a reference voltage value, whether this is 0V or 5V. When the button is closed, the voltage on the PIN would be the reference value (0V or 5V depending on the assembly) and we could make the reading as in any digital input.

arduino-pulsador-1

What’s the problem? Well, this will work when the switch is closed. But, What happens when the switch is open? In this case we are leaving the PIN totally disconnected from any voltage, something we will call a high impedance state.

What value does an automaton register if we make the measurement in a high impedance state? Well, it depends on several factors, such as the internal construction of the automaton or the last state it has been connected to. But, in summary, the input is in an undetermined state, that is, it can assume any value. Therefore, it is necessary to avoid this situation in our designs.

How can we resolve this indeterminate state? Well, this leads us directly to our second attempt.

Improving our solution, double connection

The next thing we could think of is to connect the PIN to two voltage references, alternated depending on the state of the switch:

  • To measure a LOW value when the switch is pressed, we can connect the PIN fixedly to 5V, and to 0V through the switch. With the switch open we would read HIGH, and when the switch is closed, 0V would be forced on the PIN, so we would read LOW.
  • To measure a HIGH value when the switch is pressed, we can connect the PIN fixedly to 0V, and to 5V through the switch. With the switch open we would read LOW, and when the switch is closed, 5V would be forced on the PIN, so we would read HIGH.

arduino-pulsador-2

What is the problem with this assembly? Well, when pressing the switch we are directly connecting the values of 0V and 5V, which means that we are causing a short circuit. This would cause a high current to flow and a rapid heating of components and conductors.

How to avoid this short circuit? Well, we are close. We will see this next in the final assembly.

Short circuits are dangerous faults. In addition to damaging a component, you can start a fire. Be careful.

The assembly would also not work because we would be connecting the PIN simultaneously to 0V and 5V, so we would have an indeterminate value again, and the actual measurement would depend on the resistance of the conductors to both voltage levels.

Correct assembly, Pull-Down or Pull-Up resistors

As we had anticipated, to correctly resolve the assembly we will need the presence of two new friends, Pull Down and Pull Up resistors. These two resistors are a basic mechanism, very common in the world of electronics and automation.

arduino-pulsador-3

The Pull-Down and Pull-Up resistors are connected between the digital PIN and one of the reference voltages (0V or 5V) and “force” (hence their name) the voltage value to LOW or HIGH, respectively.

  • The Pull-Up resistor forces HIGH when the button is open. When it is closed, the PIN is set to LOW, the current that flows is limited by this resistor
  • The Pull-Down resistor forces LOW when the button is open. When it is closed, the PIN is set to HIGH, and the current that flows is limited by this resistor

This is how the final assembly would look in schematic view (the connection can be made using any of the digital PINs).

arduino-pulsador-4

And this is the wiring on a prototyping board.

arduino-pulsador-5

Finally, the reading of the PIN state is done normally, as we saw in the tutorial digital inputs in Arduino.

const int inputPin = 2;

int value = 0;
 
void setup() {
  Serial.begin(9600);
  pinMode(inputPin, INPUT);
}
 
void loop(){
  value = digitalRead(inputPin);  //digital pin reading
 
  //send message to serial port based on the read value
  if (value == HIGH) {
      Serial.println("On");
  }
  else {
      Serial.println("Off");
  }
  delay(1000);
}

Try it online

Arduino has internal Pull-Up resistors of 30k but they are not usually used for two reasons.

  • They have little authority (too high resistance value)
  • If we configure it incorrectly from the program, we can generate a short circuit, so it is preferable to connect it physically to verify that we have not left it behind.

What resistance value to choose?

The resistance value is conditioned by the current that passes when the switch is pressed, and by a concept called “Pull Down/up authority” which is related to the noise in the measurement.

  • A very small resistance will have a lot of authority, but will allow a greater current to pass, which means greater consumption and greater heating.
  • A very large resistance will allow little current to pass, but will have little authority, so it will be more susceptible to incorrect measurements due to noise.

Download the code

All the code for this post is available for download on Github. github-full