Working with digital pins is one of the most fundamental skills when learning Arduino. In this tutorial, we will explore how to configure digital pin 7 as an input, read its state, and display the result on the Serial Monitor. This is a great starting point for beginners who want to understand how buttons, switches, or sensors interact with Arduino.
What You’ll Need
·
Arduino Uno
·
USB cable
·
Push button or switch
·
Breadboard and jumper wires
·
Resistor (10kΩ recommended
for pull-down setup)
Understanding Digital Input
Digital pins on Arduino can be configured as either INPUT
or OUTPUT.
·
INPUT mode allows
the pin to detect voltage levels (HIGH or LOW).
·
A HIGH signal means the pin
is receiving ~5V (on most Arduino boards).
·
A LOW signal means the pin
is at 0V (ground).
We’ll use pin 7 as an input to detect whether a
button is pressed or not.
Circuit Setup
·
Connect one side of the
push button or any switch to pin 7.
·
Connect the other side of
the button/switch to 5V.
·
Add a 10kΩ resistor
between pin 7 and GND (this acts as a pull-down resistor,
ensuring the pin reads LOW when the button isn’t pressed).
This way:
·
Button pressed → pin 7
reads HIGH.
·
Button released → pin 7
reads LOW.
Arduino Code
// Digital Input Example using Pin 7
const int buttonPin = 7;
// Pin 7 as input
int buttonState = 0;
// Variable to store state
void setup() {
// Initialize pin 7 as
input
pinMode(buttonPin,
INPUT);
// Start Serial
communication at 9600 baud
Serial.begin(9600);
}
void loop() {
// Read the state of
pin 7 using digitalRead() function
buttonState =
digitalRead(buttonPin);
// Print the state to
Serial Monitor
if (buttonState ==
HIGH) {
Serial.println("Button is PRESSED");
} else {
Serial.println("Button is RELEASED");
}
delay(500); // Small
delay for readability
}
Viewing Results on Serial Monitor
1. Upload the code to your Arduino board.
2. Open the Serial Monitor (Tools → Serial Monitor or press
Ctrl+Shift+M).
3. Set the baud rate to 9600.
4. Press and release the button — you’ll see messages like:
·
Button is PRESSED: When button
is pressed
·
Button is RELEASED: When
button is released
Applications
Once you understand digital inputs and its working, you can
expand this concept to:
·
Detect motion using PIR
sensors
·
Read toggle switches for
controlling devices
·
Build interactive projects
with multiple buttons
·
And so on…
Conclusion
Using digital pins as inputs is a cornerstone of Arduino
programming. With just a button, a resistor, and a few lines of code, you can
start building interactive projects.
Pin 7 is just one example — but the same logic can be
applied to any digital pin.
