Arduino Starter’s Guide  (3/7): Push Button (Switch) Exercises
Arduino Starter’s Guide (3/7): Push Button (Switch) Exercises

Arduino Starter’s Guide (3/7): Push Button (Switch) Exercises

01 Overview

Up to this point we have focused entirely on outputs, time to get our Arduino to listen, watch and feel. We’ll start with a simple pushbutton. Wiring up the pushbutton is simple. There is one component, the pull up resistor, that might seem out of place. This is included because an Arduino doesn’t sense the same way we do (ie button pressed, button unpressed). Instead it looks at the voltage on the pin and decides whether it is HIGH or LOW. The button is set up to pull the Arduino’s pin LOW when it is pressed, however, when the button is unpressed the voltage of the pin will float (causing occasional errors). To get the Arduino to reliably read the pin as HIGH when the button is unpressed, we add the pull up resistor.

02 Hardware and Software Components

Gather everything below before you start. This is your checklist — names, models, and versions only.

Hardware Components

Component Description
Pushbuttons connects two terminals only while pressed, then opens again when released
LED emits light when current flows through it
2 – 10k Resistor passive two-terminal electrical component that implements electrical resistance in a circuit
1 – 100 ohm Resistors passive two-terminal electrical component that implements electrical resistance in a circuit
1 – Arduino Uno a small microcontroller board that acts as the “brain” of electronic projects

Software Tools

Software Version / Details
Arduino IDE official software used to write, compile, and upload code to Arduino boards and other microcontrollers

03 Application Discussion

Here is what each component does and why it is part of this project.

Pushbuttons

A push button is a simple switch used to trigger an action in a device or process. Commonly found in homes and workplaces, it is often referred to as a pushbutton or push switch. There are two common types of pushbuttons.

A momentary pushbutton only maintains contact while it is being pressed, while a latching push button remains in its current state even if it is no longer pressed (image from pushbuttonsswitch.com). We will be using a momentary push button for this activity.

LED

An LED, or light‑emitting diode, is an electronic component that produces light when current flows through it. As a semiconductor device, it emits light when electrons interact with the material inside. LEDs are available in many shapes, colors, and sizes (image from engineerfix.com). Unlike resistors, LEDs only work on one direction so make sure to connect each pin to its assigned terminal. The longer end is usually the anode which or the VCC pin, while the shorter end is the cathode or the GND pin.

Resistors

A resistor is a passive device that restricts how easily electricity flows through a circuit. By adding resistance, it helps manage voltage levels, prevents excess current from damaging parts, and ensures each component receives the right amount of power. Each resistor is rated in ohms (Ω), and its value is usually shown with colored stripes printed on its body (image from ra-electronics.com).

04 Code

Read the Code Breakdown section to understand what each part does.

Arduino / C++

/*
  Button

  Turns on and off a light emitting diode(LED) connected to digital pin 13,
  when pressing a pushbutton attached to pin 2.

  The circuit:
  - LED attached from pin 13 to ground
  - pushbutton attached to pin 2 from +5V
  - 10K resistor attached to pin 2 from ground

  - Note: on most Arduinos there is already an LED on the board
    attached to pin 13.

  created 2005
  by DojoDave 
  modified 30 Aug 2011
  by Tom Igoe

  This example code is in the public domain.

  http://www.arduino.cc/en/Tutorial/Button
*/

// constants won't change. They're used here to set pin numbers:
const int buttonPin = 2;     // the number of the pushbutton pin
const int ledPin =  13;      // the number of the LED pin

// variables will change:
int buttonState = 0;         // variable for reading the pushbutton status

void setup() {
  // initialize the LED pin as an output:
  pinMode(ledPin, OUTPUT);
  // initialize the pushbutton pin as an input:
  pinMode(buttonPin, INPUT);
}

void loop() {
  // read the state of the pushbutton value:
  buttonState = digitalRead(buttonPin);

  // check if the pushbutton is pressed. If it is, the buttonState is HIGH:
  if (buttonState == HIGH) {
    // turn LED on:
    digitalWrite(ledPin, HIGH);
  } else {
    // turn LED off:
    digitalWrite(ledPin, LOW);
  }
}
      

05 Code Breakdown

Here is what each part of the code does. Read this after uploading.

Key Functions

Variable Declaration

Arduino / C++

const int buttonPin = 2;
const int ledPin = 13;
      

The difference of int and const int is that the int will change because it is a variable while the other one is a constant. We are taught by our math teachers about constant and variables. Constant are fixed no matter what you will do and Variables has an infinite number outcomes its value will depend on the equation.

if (buttonState == HIGH)

The if else allows greater control over the flow of code than the basic if statement, by allowing multiple tests to be grouped together. An else clause (if at all exists) will be executed if the condition in the if statement results in false. The else can proceed another if test, so that multiple, mutually exclusive tests can be run at the same time.

Each test will proceed to the next one until a true test is encountered. When a true test is found, its associated block of code is run, and the program then skips to the line following the entire if/else construction. If no test proves to be true, the default else block is executed, if one is present, and sets the default behavior.

Note that an else if block may be used with or without a terminating else block and vice versa. An unlimited number of such else if branches is allowed.

pinMode(ledPin, OUTPUT); and pinMode(buttonPin, INPUT);

This were explained in the Blinking LED.

Try More!

On button off button:

The initial example may be a little underwhelming, so let’s make it a little more complicated. One button will turn the LED on and the other will the LED off. Change the code to:

Arduino / C++

int ledPin = 13;
int buttonPin = 3;
int buttonPin2 = 2;

void setup() {
  pinMpde(ledPin, OUTPUT);
  pinMode(buttonPin, INPUT);
  pinMode(buttonPin2, INPUT);
}
void loop() {
  if (digitalRead(buttonPin) == LOW) {
     digitalWrite(ledPin, LOW);
  }  else if (digitalRead(buttonPin2) == LOW) {
     digitalWrite(ledPin, HIGH);
}
      

And upload the program to your board, and start toggling the LED on and off.

Fade up and down:

Lets use the buttons to control an analog signal. To do this you will need to change the wire connecting the LED from pin 13 to pin 9, also change this code.

Add this code above the void setup() :

Arduino / C++

int ledPin = 13; -----> int ledPin = 9;
      

Add this code above the void setup() :

Arduino / C++

const int buttonPin2 = 3;
int value = 0;
      

Add this code inside the void set up():

Arduino / C++

pinMode(buttonPin2, INPUT);
      

Remove all codes from void loop() and replace with this:

Arduino / C++

else if (digitalRead(buttonPin2) == LOW) { value++; }
value = constrain(value, 0, 255);
analogWrite(ledPin, value);
delay(10);
      

06 Conclusion

This project centers on exercises with momentary push buttons, guiding beginners through the process of wiring, coding, and experimenting with switches that directly control LEDs on the Arduino platform. By completing these activities, you gain hands‑on experience with how button presses translate into immediate visual feedback, learn to manage button states, and explore different ways to toggle or blink LEDs in response to user input.

07 References

  • https://custommapposter.com/article/a-complete-guide-to-push-button-switches/1811#toc-0
  • https://www.pushbuttonswitch.com/momentary-push-button-switch-guide/
  • https://engineerfix.com/a-complete-guide-to-leds/
  • https://ra-electronics.com/introduction-to-resistor-beginners-guide/

08 Project Authors

  • Kevin Montenegro

Quality Checked By:

  • Lemuel Jess Azaria
  • Kyla Jade Entorum
Arduino Starter’s Guide (3/7): Push Button (Switch) Exercises – CreateLabz
Arduino, Buttons, Knowledgebase, Led, Push button, Pushbuttons, Starter kit, Switch

Leave a comment

All comments are moderated before being published