top of page

Arduino Push Button: A Comprehensive Beginners Guide

Are you a newcomer to the exciting world of Arduino, eager to explore the wonders of push buttons in your projects? Well, search no more! In this extensive guide, we will embark on a journey through the realm of integrating push buttons with Arduino. From wiring a push button into a new or existing circuit to programming your Arduino to detect click events. By the time you reach the end of this article, you'll possess a strong foundation in incorporating push buttons into your project.




How Does an Arduino and a Push Button Work Together?

A push button serves as a fundamental input device, comprised of two distinct terminals in its resting state. When the button is pressed, these terminals connect, forming a complete circuit and enabling the unimpeded flow of electrons. This phenomenon becomes even more remarkable when introducing an Arduino into the equation. Equipped with digital IO pins, the Arduino can sense whether a pin is at a HIGH or LOW state (+5V or 0V). By pressing a button and changing the state at an IO pin, we now gain the power to orchestrate the flow of our program from external events.



Wiring a Push Button to an Arduino

To wire the Arduino push button correctly, you'll need the materials listed below and a way to upload a sketch to your Arduino board.


Hardware:

  • Arduino

  • Push Button

  • LED

  • 220Ω resistor

  • Breadboard

  • Jumper wires


Step 1

First, place your LED into the breadboard and connect a 220Ω resistor in series to the cathode (negative side).

Wiring diagram of an LED connected to an Arduino


Step 2

Next, connect digital pin 7 of the Arduino to the LED's Anode (positive end).

Wiring diagram of an LED connected to an Arduino


Step 3

Then, place your push button across the divot in the middle of the breadboard. Connect one end of the push button to digital pin 6 of the Arduino and the other side of the push button to the ground rail of the breadboard.

Wiring diagram of an LED and push button connected to an Arduino


Step 4

Finally, connect the ground rail to any ground pin on the Arduino. Your completed circuit should look like this:

Wiring diagram of an LED and push button connected to an Arduino


Programming Arduino to Detect Click Events

Now that the circuit has been wired to the Arduino, we can start writing some code to detect when the button has been pressed and then turn the LED on/off depending on the button's current state.



Creating Constants and Variables

First, let's create a few constants and variables which we'll use throughout the program.

The first two constants define which pins we use to control the LED and read the state of the button. Remember, we connected the button to pin 6 on the Arduino and the LED to pin 7.


Then we create a variable of type int, which will store the button's current state. I gave it a default value of HIGH because, initially, the LED should be off when the button is not pressed (at +5V). You'll see why this is the case in the next section.



Setting Pins as Outputs or Inputs

Before using the digital pins connected to our circuit, we must set them up within the setup function. To do this, we'll use the "pinMode" function. This function expects two arguments, the pin number and one of the following Arduino constants: INPUT, OUTPUT, INPUT_PULLUP. In our case, we'll use INPUT_PULLUP for the button pin and OUTPUT for the LED pin.

Why do we use INPUT_PULLUP instead of INPUT for the BTN_PIN? When we look at the definitions of both constants, we find that they allow us to read the state of a digital pin. However, the difference is that using INPUT_PULLUP internally adds a pull-up resistor to the circuit. On the other hand, if we were to use INPUT, we would need to include an external pull-up resistor in the circuit we already have set up.


To understand the purpose of a pull-up resistor, it helps to know that it is a passive electronic component commonly used in digital circuits. Its role is to ensure that an input signal, such as one from a switch or button, remains stable when it is not actively driven by an external source, like when we press the button. Without one, the pin can randomly change from LOW to HIGH or vice versa without pressing the button.


In our specific case, imagine an additional resistor connected from pin 6 of the Arduino to the +5V supply. With this setup, the pin will read a HIGH state when the button is idle and not pressed. However, when the button is pressed, the electrons find a path to GND, causing the pin to read a LOW state.


By utilizing the INPUT_PULLUP constant, we can conveniently activate the internal pull-up resistor of the Arduino. This stabilizes our digital pin and simplifies our circuit by eliminating the need for an external pull-up resistor.



Reading State and Toggling Pins HIGH and LOW

With all of our pins setup, we can now read the state of our button (pressed or not pressed) and turn the LED on/off depending on that state. Remember, since we attached a pullup resistor to the button pin, the default state (when the button is not pressed) will be HIGH or +5V. When the button is pressed, the state of pin 6 will drop to a value of LOW or 0V.


Below is the code that utilizes the "digitalRead" and "digitalWrite" functions to read the state of the button and turn the LED on/off accordingly.

Here's an in-depth breakdown of the above code:

  1. The loop() function is the central part of the program that runs while the Arduino has power. It is where you put the instructions you want the Arduino to execute continuously.

  2. The line "buttonState = digitalRead(BTN_PIN);" reads the state of the connected button, BTN_PIN. The digitalRead() function returns the state of the pin, which can be either HIGH or LOW. The state is then stored in the variable "buttonState."

  3. The next part is an if statement that checks if the "buttonState" variable is LOW, meaning the button is pressed. If the condition is true, it executes the code within the curly braces.

  4. Inside the if statement, the line "digitalWrite(LED_PIN, HIGH);" sets the state of the LED connected to digital pin 7, LED_PIN, to HIGH. This turns the LED on.

  5. If the button state is not LOW (meaning it is HIGH or not pressed), the else block is executed.

  6. Inside the else block, we have the line "digitalWrite(LED_PIN, LOW);" which sets the state of the LED pin to LOW. This turns the LED off.




Here's all of the code put together:




FAQs - Working with Push Buttons

What is the purpose of a pull-up resistor when using a push button?

When using a push button with an Arduino, a pull-up resistor is necessary to ensure that the input pin has a defined voltage level when the push button is idle. Attaching this resistor prevents the pin from floating and producing unpredictable results. Depending on the desired configuration, the resistor is connected between the input pin and the 5V power supply for a pull-up resistor.

How can I determine the state of a push button with an Arduino?

Can I use multiple push buttons in a single project?

How can I debounce an push button with an Arduino?

Can I use alternative input devices instead of a push button?


207 views0 comments
bottom of page