Digital Input With Arduino (Push Button)

by STEAM-DIY in Circuits > Arduino

172 Views, 1 Favorites, 0 Comments

Digital Input With Arduino (Push Button)

1.PNG

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:

  1. Arduino Nano/Uno
  2. Push button
  3. 10kΩ resistor (for pull-up or pull-down)
  4. 1kΩ resistor (for current limiting to the LED)
  5. LED
  6. Jumper wires (male-to-male)
  7. Breadboard

Build the Circuit

2.PNG
3.PNG
  1. Push Button Setup:
  2. Place the push button on the breadboard.
  3. Connect one side of the push button to digital pin D2 on the Arduino.
  4. 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.
  5. Connect the other leg of the push button to 5V for pull-down or GND for pull-up.
  6. LED Setup:
  7. Connect the longer leg (anode) of the LED to digital pin D3.
  8. 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:

  1. pinMode(2, INPUT); defines the D2 pin as the input (connected to the push button).
  2. pinMode(3, OUTPUT); defines the D3 pin as the output (connected to the LED).
  3. The digitalRead(2) checks the state of the button (either HIGH or LOW).
  4. When the button is pressed, the LED will turn on, otherwise it will turn off.

Upload the Code and Test


  1. Connect the Arduino to your computer using a USB cable.
  2. Open the Arduino IDE and select the correct board (Arduino Nano/Uno) and COM port.
  3. Paste the code and upload it to the Arduino.
  4. 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!