Practical Answers Hub · Learn Arduino UNO · Lessons 5–8

Digital Inputs, Floating Pins and Tactile Switch

Investigate raw digital readings, floating pins and reliable pull resistors before wiring and checking a tactile button. Finish by counting presses.

Lesson 5 · Read a digital input in Serial Monitor

Start with pin D2 configured as INPUT and no switch connected. Print the reading at 9600 baud.

const int inputPin = 2;
void setup() {
  pinMode(inputPin, INPUT);
  Serial.begin(9600);
}
void loop() {
  Serial.println(digitalRead(inputPin));
  delay(200);
}
Temporary test connection
1UNO D2 is the input being measured
2Touch a jumper from D2 to GND: expect 0 (LOW)
3Move the jumper to 5 V: expect 1 (HIGH)
PHOTO TO ADDClose view of the test jumper at D2 and the labelled GND/5 V sockets.

Lesson 6 · Why a disconnected pin floats

Remove the test jumper completely and watch Serial Monitor. A disconnected INPUT has no defined voltage, so it may show changing or apparently stable values. Neither is a reliable button reading.

External pull-down

Defined LOW until the button is pressed
1D2 → 10 kΩ resistor → GND
2D2 → switch → 5 V
3Released: LOW; pressed: HIGH

Built-in pull-up

The UNO can hold the input HIGH internally, saving the external resistor. Now the switch goes between D2 and GND, and the meanings reverse.

void setup() {
  pinMode(2, INPUT_PULLUP);
  Serial.begin(9600);
}
void loop() {
  Serial.println(digitalRead(2));
  delay(200);
}
INPUT_PULLUP wiring
1D2 → one side of switch
2Other side of switch → GND
3Released: 1 (HIGH); pressed: 0 (LOW)
PHOTO TO ADDSide-by-side physical pull-down resistor and INPUT_PULLUP wiring, with pin labels.

Lesson 7 · Place and test the tactile switch

On a common four-leg tactile switch, the two legs on each side are permanently connected. Pressing joins the sides. Check your own switch with a continuity tester if its pin layout is unclear. Put it across the breadboard centre trench so the two sides sit on different sets of connected rows.

Top view of switch across centre gap
1Pair A: two legs already connected inside the switch
2Centre breadboard gap separates A from pair B
3Pair B: two legs already connected; pressing joins A to B
Four-leg tactile switch: the two permanent pairs
Pair A● ━━━ ●Already joined internally
Press bridges
A ↔ B
Pair B● ━━━ ●Already joined internally
PHOTO TO ADDTop view of the four switch legs straddling the centre gap, with the internally joined pairs marked.

Run the INPUT_PULLUP sketch above. Released should repeatedly print 1; pressed should print 0. If it always reads 0, inspect the breadboard rows and switch orientation.

Lesson 8 · Count presses in Serial Monitor

Once the input works, count new HIGH-to-LOW changes instead of repeatedly counting the entire time the switch is held.

const int buttonPin = 2;
int count = 0;
int previous = HIGH;
void setup() {
  pinMode(buttonPin, INPUT_PULLUP);
  Serial.begin(9600);
}
void loop() {
  int current = digitalRead(buttonPin);
  if (current == LOW && previous == HIGH) {
    delay(30); // brief bounce check
    if (digitalRead(buttonPin) == LOW) {
      count++;
      Serial.println(count);
    }
  }
  previous = current;
}
VIDEO TO ADDRecord the floating reading, both defined input methods, switch internals and a successful press count.