Read Digital Input with Pushbutton
🔰 Introduction
In this project, you’ll learn how to use a pushbutton to read a digital input on Arduino UNO or ESP32. This project helps beginners understand how microcontrollers detect HIGH or LOW signals from components like buttons or switches.
🔧 Components Needed
| Component | Quantity | Notes |
|---|---|---|
| Arduino UNO or ESP32 | 1 | Any compatible board |
| Pushbutton | 1 | Momentary type |
| Resistor | 1 | 10kΩ for pull-down resistor |
| Breadboard | 1 | Optional but helpful |
| Jumper Wires | 4 |
📷 Circuit Diagram

🧩 Schematic Diagram

🛠️ Wiring Instructions
- Place the pushbutton on the breadboard.
- Connect one leg of the pushbutton to digital pin 2.
- Connect the other leg of the pushbutton to GND.
- Place a 10kΩ pull-down resistor between digital pin 2 and GND.
- Plug the Arduino or ESP32 into your PC via USB.
🔍 What is digitalRead()?
digitalRead() is a built-in Arduino function used to read the digital signal from a specified pin. It detects whether the pin is receiving a HIGH or LOW voltage.
➤ Syntax
Syntax
int state = digitalRead(pin);
pin: The digital pin number to read- Returns:
HIGH(1) orLOW(0)
🧾 Code
digital-read-pushbutton.ino
#define BUTTON (2)
void setup() {
pinMode(BUTTON, INPUT); // Set pin 2 as input
Serial.begin(9600); // Start serial communication
}
void loop() {
int buttonState = digitalRead(BUTTON); // Read pin 2 state
Serial.println(buttonState); // Print state to Serial Monitor
delay(100); // Delay for stability
}
🧠 Code Breakdown
void setup()
Runs once when the microcontroller starts.
Configures pin 2 as an **input**
pinMode(2, INPUT);
— Configures pin 2 as an input.
Serial.begin(9600);— Starts serial communication at 9600 baud to print messages to the Serial Monitor.
void loop()
Runs continuously.
int buttonState = digitalRead(2);— Reads the state of pin 2 (HIGHorLOW) and stores it inbuttonState.Serial.println(buttonState);— Prints the state to the Serial Monitor (1for HIGH,0for LOW).delay(100);— Waits for 100ms before reading again (adds stability and acts as basic debounce).
💡 Applications of Digital Read
Reading digital input is widely used in real-world applications such as:
- 🕹️ Game controllers – detect button presses
- 🚪 Security systems – monitor door or motion sensors
- 🤖 Robotics – read limit switches or triggers
- 📟 User interfaces – control via keypads or pushbuttons
- 🛠️ Industrial automation – detect machine states
- 🧠 IoT sensors – handle smart triggers
If you need to detect ON/OFF, pressed/released, or open/closed states, digitalRead() is your go-to function.
No Comments yet.