Digital Input With Arduino (Push Button)
by STEAM-DIY in Circuits > Arduino
172 Views, 1 Favorites, 0 Comments
Digital Input With Arduino (Push Button)
Introduction: In this tutorial, we’ll show you how to use a push button as a digital input with Arduino. You’ll learn how to control an LED using the push button and how to monitor its state via the Arduino IDE's serial monitor. This project uses a simple pull-up or pull-down resistor configuration.
Supplies
Gather Your Components
You’ll need the following components for this project:
- Arduino Nano/Uno
- Push button
- 10kΩ resistor (for pull-up or pull-down)
- 1kΩ resistor (for current limiting to the LED)
- LED
- Jumper wires (male-to-male)
- Breadboard
Build the Circuit
- Push Button Setup:
- Place the push button on the breadboard.
- Connect one side of the push button to digital pin D2 on the Arduino.
- Connect a 10kΩ resistor between the button pin and ground (for pull-down configuration). For pull-up, connect it between the button pin and 5V.
- Connect the other leg of the push button to 5V for pull-down or GND for pull-up.
- LED Setup:
- Connect the longer leg (anode) of the LED to digital pin D3.
- Connect the shorter leg (cathode) of the LED to ground through a 1kΩ resistor.
Code the Arduino
void setup() {
pinMode(2, INPUT); // Push button pin
pinMode(3, OUTPUT); // LED pin
}
void loop() {
bool value = digitalRead(2); // Read button state
if (value == 0) { // If button is pressed
digitalWrite(3, HIGH); // Turn on LED
} else { // If button is not pressed
digitalWrite(3, LOW); // Turn off LED
}
}
Explanation:
- pinMode(2, INPUT); defines the D2 pin as the input (connected to the push button).
- pinMode(3, OUTPUT); defines the D3 pin as the output (connected to the LED).
- The digitalRead(2) checks the state of the button (either HIGH or LOW).
- When the button is pressed, the LED will turn on, otherwise it will turn off.
Upload the Code and Test
- Connect the Arduino to your computer using a USB cable.
- Open the Arduino IDE and select the correct board (Arduino Nano/Uno) and COM port.
- Paste the code and upload it to the Arduino.
- Press the push button and observe the LED turning on and off. You can also view the push button state in the serial monitor.
Conclusion
This project shows you how to read digital input using a push button and control an LED using an Arduino. It’s a simple but fundamental project that introduces key concepts like digital input/output and pull-up/pull-down resistor configurations.
Stay tuned for more tutorials, and happy building!