Project 3: Push Button LED Control

Control Arduino Built-in LED through Push Button

10/23/20264 min read

Introduction

Have you ever pressed a button to turn on a light? Buttons are used in many everyday devices, including doorbells, keyboards, remote controls, and electronic toys.

In this project, you will use a push button to control the built-in LED on your Arduino Uno. When you press the button, the LED will turn on. When you release it, the LED will turn off.

This project introduces you to digital inputs and teaches you how Arduino can respond to physical actions.

What You Will Learn:

  • How a push button works in an electronic circuit.

  • How to read a digital input using digitalRead().

  • How to configure a pin using INPUT_PULLUP.

  • How to use if and else statements.

  • How to control an LED based on a button's state.

  • How Arduino responds to user input.

1. Components Required

You will need the following components from your Arduino robotics kit:

  • 1 Arduino Uno

  • 1 push button

  • Jumper wires

  • 1 breadboard

  • 1 USB cable to connect and power the Arduino

This version of the project uses the built-in LED on the Arduino Uno, so you do not need an external LED or an additional resistor.

2. Understanding the Push Button

A push button is a simple electronic component that allows you to open or close an electrical connection when you press it. When you press a button, its internal contacts connect. When you release it, the contacts return to their original state.

Push buttons are used in many applications, such as:

  • Doorbells

  • Computer keyboards

  • Game controllers

  • Electronic toys

  • Control panels

  • Robotics projects

In this project, the button will act as an input for the Arduino.

The Arduino will continuously check whether the button is pressed. If it detects a press, it will turn on the built-in LED. When the button is released, the LED will turn off.

3. Understanding INPUT_PULLUP

Before connecting the circuit, let's understand an important part of our code:

Arduino's Built-in LED

The INPUT_PULLUP setting enables an internal pull-up resistor inside the Arduino. This helps keep the input in a stable state when the button is not pressed.

With this configuration, the button behaves in a slightly different way from what you might expect:

  • When the button is released, the input reads HIGH.

  • When the button is pressed, the input reads LOW.


This happens because pressing the button connects the input pin directly to GND.

You do not need to add an external pull-up resistor for this circuit.

4. Connecting the Push Button to Arduino

Let's connect the button to your Arduino Uno.

Step 1: Identify the Button Terminals

Most four-legged tactile push buttons have two pairs of internally connected terminals. Pressing the button connects the two pairs together.

If you are using a four-legged button, position it across the center gap of the breadboard so that the button's two sides are not accidentally connected to the same breadboard strip.

Step 2: Connect the Button to Pin 2

Connect one side of the push button to Arduino digital pin 2.

Step 3: Connect the Button to GND

Connect the opposite side of the button to an Arduino GND pin.

Your connections should be:

  • Push button terminal 1 → Arduino digital pin 2

  • Push button terminal 2 → Arduino GND

The built-in LED does not need any additional wiring because it is already connected inside the Arduino Uno.


See the circuit diagram below:

Important: Make sure the button connects pin 2 to GND when pressed. Do not connect the button directly between 5V and GND, as that could create a short circuit depending on the wiring.

5. Writing the Arduino Code

Now let's program the Arduino to control the built-in LED using the push button.

Open the Arduino IDE using the setup you completed in Project 1.

Copy and paste the following code into your sketch:

Upload the code to your Arduino Uno.

Now press the button and observe the built-in LED.

  • When you press and hold the button, the LED should turn on.

  • When you release the button, the LED should turn off.

6. Understanding the Code

Let's break the program into simple parts so you can understand how it works.

A. Defining the Button Pin

LED_BUILTIN is a predefined constant that represents the built-in LED pin on the Arduino board.

On a standard Arduino Uno, this is digital pin 13.

Using LED_BUILTIN makes the code easier to reuse on compatible Arduino boards without manually changing the LED pin number.

C. Setting Up the Pins

This line assigns the name buttonPin to digital pin 2.

The Arduino will use this pin to read the button's state.

B. Defining the LED Pin

The setup() function runs once when the Arduino starts or resets.

Inside this function:

  • pinMode(buttonPin, INPUT_PULLUP) configures pin 2 to read the button and enables the internal pull-up resistor.

  • pinMode(ledPin, OUTPUT) configures the built-in LED pin as an output.

  • digitalWrite(ledPin, LOW) starts the program with the LED turned off.

D. Reading the Button State

The digitalRead() function reads the electrical state of the button pin.

The result is stored in the variable buttonState.

Because we are using INPUT_PULLUP, the reading will normally be:

  • HIGH when the button is released.

  • LOW when the button is pressed.

Remember that LOW means pressed in this particular circuit.

E. Using an IF Statement

The if statement checks whether the button is pressed.

If buttonState equals LOW, the Arduino executes the instruction inside the curly brackets and turns on the LED.

The double equals sign == compares two values. It checks whether they are equal.

F. Using an ELSE Statement

The else statement runs when the condition in the if statement is false.

In our project, this happens when the button is released.

The Arduino then turns off the LED.

G. Repeating the Process

The loop() function runs repeatedly while the Arduino is powered.

The Arduino keeps checking the button's state and updating the LED. This makes the LED respond to the button without needing to restart the program.

Congratulations! You have built a push-button-controlled LED system using your Arduino Uno.

Through this project, you learned how to:

  • Connect a push button to an Arduino.

  • Read digital inputs using digitalRead().

  • Use INPUT_PULLUP to simplify button wiring.

  • Understand why the pressed state reads LOW.

  • Use if and else statements to make decisions.

  • Control an LED based on a physical action.

These concepts are important in robotics because robots often use buttons, switches, and sensors to receive information from their surroundings.