Skip to main content

Arduino Push Button LED: Control an LED with a Push Button

Learn how to use a push button as a digital input and control an LED with an Arduino Uno R3.
So far, we have used Arduino to control an LED in several different ways.

In the previous three projects, we: 
  • turned an LED ON and OFF.
  • used PWM to control its brightness.
  • changed the speed at which the LED blinked.
Now, in this fourth project, we are going to introduce something new: an input.

We will use a push button to tell the Arduino when to turn an LED ON or OFF.

This project introduces the digitalRead() function, which allows the Arduino to read the state of a digital input. Arduino's digitalRead() function returns either HIGH or LOW.

What You Will Learn

After completing this project, you will understand:
  • What a push button does in an electronic circuit.
  • The difference between an input and an output.
  • How to connect a push button to an Arduino.
  • Why a pull-down resistor is used.
  • How digitalRead() reads a digital input.
  • How if and else statements control program behavior.
  • How an Arduino can respond to a user's physical input.

Supplies

You will need:
  • Arduino Uno R3
  • 1 × LED
  • 1 × Push button
  • 1 × 220 Ω resistor
  • 1 × 10 kΩ resistor
  • Solderless breadboard
  • Jumper wires
  • USB cable
  • Computer with the Arduino IDE

Understanding the Push Button

A push button is a type of mechanical switch.
When the button is pressed, it connects two points in the circuit.
When the button is released, the connection is opened.
In this project, the Arduino will use the button as a digital input.

The input can have two states:
  • HIGH → Button pressed
  • LOW  → Button released
The Arduino can determine the state of the button using:

digitalRead()

The digitalRead() function reads a digital input and returns either HIGH or LOW.

Setting Up Your Hardware

Your circuit contains two separate parts:
  • LED circuit - controlled by the Arduino.
  • Push-button circuit - provides an input to the Arduino.

Connections

LED

  • LED anode (+) - D3 through the 220Ω resistor
  • LED cathode (-) - GND

Push Button

  • Push button - D9
  • 10 kΩ resistor - Button input and GND
  • Other side of button - 5V
The important idea is that D9 reads the button's state.

Why Do We Need the 10 kΩ Resistor?

This is an important part of the circuit.

A digital input should have a defined electrical state when the button is not being pressed.

Without an appropriate pull-up or pull-down arrangement, the input can be floating, meaning the Arduino may read unpredictable HIGH or LOW values. Arduino's documentation specifically warns that an unconnected digital input can fluctuate between HIGH and LOW.

The 10 kΩ resistor in this circuit acts as a pull-down resistor.

Therefore:

Button Released

Input → 10 kΩ resistor → GND

Arduino reads:
LOW

Button Pressed

Input → 5V

Arduino reads:
HIGH

This gives the Arduino a stable digital signal to read.

Understanding the Two Resistors

There are two resistors in this project, and they have different purposes.

220 Ω resistor

The 220 Ω resistor is connected in series with the LED to limit the current flowing through the LED.

10 kΩ resistor

The 10 kΩ resistor is used as a pull-down resistor to give the button input a defined LOW state when the button is not pressed.

So:

220 Ω → protects the LED

10 kΩ → stabilizes the button input

This distinction is important to understand.

Coding

The goal of our program is simple:
Button pressed
↓
Arduino detects HIGH
↓
LED turns ON


Button released
↓
Arduino detects LOW
↓
LED turns OFF

Complete Code

const int ledPin = 3;
const int buttonPin = 9;

int buttonState = LOW;

void setup() {
pinMode(ledPin, OUTPUT);
pinMode(buttonPin, INPUT);
}

void loop() {
buttonState = digitalRead(buttonPin);

if (buttonState == HIGH) {
digitalWrite(ledPin, HIGH);
} else {
digitalWrite(ledPin, LOW);
}
}

Understanding the Code

1. Define the LED Pin

const int ledPin = 3;

This tells the Arduino that the LED is connected to digital pin 3.

2. Define the Button Pin

const int buttonPin = 9;

This tells the Arduino that the push button is connected to digital pin 9.
Unlike the previous Fade project, D9 does not need to be a PWM pin here. We are using it as a digital input.
The Arduino Uno's digital pins can be configured as either inputs or outputs using pinMode().

3. Store the Button State

int buttonState = LOW;

The variable buttonState stores the current state of the push button.
It can contain:
  • HIGH
  • LOW

4. Configure the Pins

void setup() {
pinMode(ledPin, OUTPUT);
pinMode(buttonPin, INPUT);
}

The LED pin is configured as an OUTPUT because the Arduino controls the LED.
The button pin is configured as an INPUT because the Arduino reads the button's state.
Arduino's pinMode() function supports INPUT, OUTPUT, and INPUT_PULLUP modes.

5. Read the Button

buttonState = digitalRead(buttonPin);

This is the most important new function in this project.
digitalRead() checks the electrical state of D9 and returns either:
 
HIGH

or:
 
LOW

The result is stored in buttonState.

6. Check Whether the Button Is Pressed

if (buttonState == HIGH) {
digitalWrite(ledPin, HIGH);
}

This means:

If the button state is HIGH, turn the LED ON.

7. Turn the LED OFF Otherwise

else { 
digitalWrite(ledPin, LOW); 
}

The else statement handles the other possibility.
If the button is not pressed and the input is LOW, the Arduino turns the LED OFF.

Therefore:
IF button == HIGH 
LED = ON 

ELSE 
LED = OFF

This is a simple example of conditional logic controlling physical hardware.

Uploading the Program

After entering the code into the Arduino IDE:
  1. Connect the Arduino Uno R3 to your computer.
  2. Select the correct board.
  3. Select the correct port.
  4. Click Verify.
  5. If the code compiles successfully, click Upload.
  6. Wait for the upload to finish.
  7. Press the push button and observe the LED.

Expected Result

When the push button is pressed:

LED → ON

When the push button is released:

LED → OFF

The LED therefore responds directly to the state of the push button.

Troubleshooting

The LED does not turn on

Check:
  • LED polarity.
  • 220 Ω resistor connection.
  • LED connection to D3.
  • LED cathode connection to GND.
  • Button wiring.
  • 10 kΩ resistor connection.
  • USB connection.
  • Correct board and port selection.

The LED stays ON even when the button is released

Check the 10 kΩ pull-down resistor.

The input pin should be connected to GND through the resistor when the button is released.

The LED behaves randomly

A floating input can cause unpredictable HIGH/LOW readings. Check that the button input has a proper pull-down connection. Arduino's documentation notes that an unconnected digital input can fluctuate between HIGH and LOW.

Experiment 1: Reverse the Behavior

Can you modify the program so that:

Button pressed → LED OFF

Button released → LED ON

Think about which digitalWrite() values need to be changed.

Experiment 2: Use the Built-In LED

Try controlling the Arduino's built-in LED instead of the external LED.
On an Uno R3, the board provides an onboard LED, and the digital pins can be configured as inputs or outputs.
Can you modify the program so that pressing the button controls the built-in LED?

Experiment 3: Make the Button Control a Different Output

The button doesn't have to control only an LED.
Once you understand digitalRead(), you can use the button as an input for other projects involving:
  • Buzzers
  • Motors
  • Servos
  • Displays
  • Relays
  • Sensors
The basic programming pattern remains:
Read input 
↓ 
Check condition 
↓ 
Control output

This pattern is fundamental to interactive Arduino projects.

What You Learned

In this project, you learned how to use a push button as a digital input.

You learned:
  • What a mechanical push button does.
  • The difference between an input and an output.
  • How pinMode() configures Arduino pins.
  • How digitalRead() reads a digital input.
  • How digitalWrite() controls an output.
  • Why a pull-down resistor is needed in this circuit.
  • How if and else statements allow the Arduino to make decisions.
  • How physical input can be used to control an electronic output.

From Output to Input

This project represents an important step in your Arduino journey.
In the first three projects, we primarily told the Arduino what to do.
Now, we are allowing the Arduino to receive information from the physical world.

You can think of the process as:

Input → Processing → Output

In this project:

Push button → Arduino → LED

This simple pattern forms the foundation of many interactive electronic systems.

Conclusion

In this project, we learned how to use a push button to control an LED.

The Arduino continuously reads the state of the button using digitalRead(). When the button is pressed, the Arduino detects a HIGH input and turns the LED ON. When the button is released, the input becomes LOW and the Arduino turns the LED OFF.

Although this is a simple project, it introduces one of the most important concepts in physical computing:

An Arduino can read the physical world through inputs and use that information to control outputs.

We have now moved from simply controlling an LED to making the Arduino respond to user input.





Comments

Popular posts from this blog

Arduino Serial Communication Beginner Guide (UART Explained)

Serial communication is the backbone of most Arduino projects. It allows your Arduino to talk to your computer, Bluetooth modules, Wi-Fi modules, sensors, and even other Arduinos. If you have ever used the Serial Monitor, you have already used serial communication - now you will understand how it really works. By the end of this guide, you will learn: What serial communication is What UART means How Arduino communicates with devices How to send and receive data How baud rate works Common mistakes beginners make What is Serial Communication? Serial communication is a method of sending data one bit at a time over a single wire. Instead of sending many bits simultaneously (parallel communication), serial sends bits sequentially: Example: The letter A in binary = 01000001 These bits are sent one after another very quickly. This method is: Simple Reliable Perfect for microcontrollers What is UART? UART stands for: Universal Asynchronous Receiver Transmitter It is the hardware ins...

How to Connect HC-06 Bluetooth Module with Arduino (Step-by-Step Beginner Guide)

Bluetooth communication is one of the easiest ways to make Arduino projects wireless. Once you understand how to use a Bluetooth module, you can build robots, home automation systems, and smartphone-controlled devices. In this tutorial, you will learn how to interface the HC-06 Bluetooth module with Arduino and send data wirelessly using your phone.    By the end of this guide, you will be able to: Understand how the HC-06 Bluetooth module works Configure the module using AT commands Safely connect it to an Arduino Send and receive serial data over Bluetooth Build the foundation for wireless Arduino projects Watch the Project Overview What is the HC-06 Bluetooth Module? The HC-06 is a Bluetooth-to-Serial module designed to add wireless communication to microcontrollers such as Arduino. It uses Bluetooth 2.0 (SPP - Serial Port Protocol), which means it behaves like a wireless serial cable. Whatever is sent from the phone appears in Arduino’s Serial Monitor and vice versa. ...

How to Use SoftwareSerial on Arduino (Connect Multiple Serial Devices)

In the previous tutorial, you learned how Arduino uses hardware serial communication through pins 0 (RX) and 1 (TX). That built-in serial port is mainly used to communicate with your computer through the USB cable and Serial Monitor . But here is the problem beginners quickly face: Many popular modules use serial communication, such as: Bluetooth modules ( HC-05 / HC-06 ) GPS modules GSM modules Wi-Fi modules ( ESP8266 ) Arduino Uno has only one hardware serial port. So how can we connect these modules while still using the Serial Monitor? The solution is SoftwareSerial . In this tutorial you will learn how to create additional serial ports using normal digital pins. By the end, your Arduino will be able to talk to multiple serial devices. What is SoftwareSerial? SoftwareSerial is a special Arduino library that allows digital pins to behave like RX and TX pins. This means Arduino can create extra serial ports using software instead of hardware. Hardware Serial vs Software Seri...