Arduino Connecting Buttons
Arduino Connecting Buttons
hu/miszak/en/arduino-corner/
Arduino is a development platform, if you have a board, you typically use it for many different tasks.
You are likely to change the code and the external components repeatedly. During code
development and testing, you may make mistakes, configure the hardware inadequately for a short
time. Changing the external hardware before changing the code accordingly may be one of the
reasons.
Should you worry about destroying your Arduino just by an unsuitable code? Let’s see a simple
example, the reading of the state of a button or switch.
Figure 1 shows the typical solutions on how buttons and switches can be connected to the Arduino
board. The corresponding pin of the microcontroller must be programmed as digital input. When the
button is off, a resistor pulls it down to keep it logic low [1] or pulls it up to stay in logic high.
A B C
VCC VCC VCC
VCC VCC
RP
RPU L RPU H RPU H
Arduino
Arduino
Arduino
RP
Figure 1. Usual ways to connect buttons and switches to the Arduino board in positive (A) or
negative logic (B and C). The pin must be configured as digital input by turning off the internal
MOSFET switches of the microcontroller. If the internal pull-up resistor RPU is switched on, no
external pull-up resistor is needed (C).
The typical value of the pull-up and pull-down resistors RP falls in the range from 1k to 10k. In
addition, an internal pull-up resistor RPU can be switched on or off, its value is between 20k and 50k
[2]. The pull-down version (A) implements positive logic, the digitalRead function returns HIGH if the
button is on. Note that if the internal pull-up resistor is switched on, it forms a voltage divider with
RP, while the button is off. Therefore, to generate valid logic LOW level (less than 0.3·VCC), RP < 5k is
recommended.
The negative logic used in the other examples (B and C) is more practical. Both work properly,
regardless of the presence and value of the internal pull-up resistance. You can also use the internal
pull-up resistor alone (C), in this case only a button is needed. The recommended solution is shown in
figure 1, B.
This study was funded by the Content Pedagogy Research Program of the Hungarian Academy of Sciences.
What happens if you accidentally program the pin as output? What happens if it was an output in
your previous code and you power your board to download the new code?
If the button or switch is on, the pin is wired to VCC or GND, so if the pin is programmed as output, a
short circuit can be created, see figure 2.
A B C
VCC VCC VCC
VCC VCC
Arduino
Arduino
RP
Figure 2. If then pin is programmed as output and the button is on, you may create a short
circuit. The red curves show the corresponding current paths.
The current can be so high, that it can damage your board’s microcontroller [2,3].
What can you do to prevent this? Insert a series current limiting resistor between the button and the
pin as shown in figure 3.
A B C
VCC VCC VCC
VCC VCC
Arduino
Arduino
RP
Figure 3. Series resistors do not disturb normal operation and protect the microcontroller from
overcurrent if the pin mode is misconfigured.
A good choice for the value of RS is 1k. It keeps the current below 5mA in any case and allows to drive
the input reliably.
References
1. Arduino tutorials, Button, https://www.arduino.cc/en/Tutorial/Button
2. ATmega328P datasheet, https://www.microchip.com/wwwproducts/en/ATmega328P
3. Z Gingl, R Mingesz, G Makan and J Mellar, Driving with Arduino? Keep the lane!,
http://www.inf.u-szeged.hu/miszak/en/arduino-corner/driving-with-arduino-keep-the-lane/