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:
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.
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:
The digitalRead() function reads a digital input and returns either HIGH or LOW.
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:
So:
220 Ω → protects the LED
10 kΩ → stabilizes the button input
This distinction is important to understand.
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().
The variable buttonState stores the current state of the push button.
It can contain:
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.
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.
This is a simple example of conditional logic controlling physical hardware.
LED → ON
When the push button is released:
LED → OFF
The LED therefore responds directly to the state of the push button.
The input pin should be connected to GND through the resistor when the button is released.
Button pressed → LED OFF
Button released → LED ON
Think about which digitalWrite() values need to be changed.
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?
Once you understand digitalRead(), you can use the button as an input for other projects involving:
This pattern is fundamental to interactive Arduino projects.
You learned:
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.
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.
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.
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()
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;
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 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.
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
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:- Connect the Arduino Uno R3 to your computer.
- Select the correct board.
- Select the correct port.
- Click Verify.
- If the code compiles successfully, click Upload.
- Wait for the upload to finish.
- 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
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
Post a Comment