Skip to content

Arduino digitalRead Returns Wrong Value

DodaTech Updated 2026-06-26 1 min read

In this tutorial, you'll learn about Arduino digitalRead Returns Wrong Value. We cover key concepts, practical examples, and best practices to help you understand and apply this topic effectively.

The Problem

digitalRead() returns unexpected HIGH or LOW values on a pin.

Quick Fix

Wrong

int val = digitalRead(7);  // Pin 7 is floating
val changes randomly based on electrical noise.
int buttonPin = 7;
void setup() {
  pinMode(buttonPin, INPUT_PULLUP);  // Enable internal pull-up
  Serial.begin(9600);
}
void loop() {
  int val = digitalRead(buttonPin);
  Serial.println(val);
  delay(100);
}
1 (when button not pressed), 0 (when button pressed to GND).

Prevention

Use INPUT_PULLUP to enable the internal pull-up resistor (20-50 kOhm on AVR). Connect the switch between the pin and GND. Read returns LOW when pressed, HIGH when released. Alternatively use INPUT_PULLDOWN on supported boards.

DodaTech engineers apply these same patterns across Doda Browser, DodaZIP, and Durga Antivirus Pro for production IoT reliability.

FAQ

### What is the difference between INPUT and INPUT_PULLUP?

INPUT leaves the pin floating — readings are unpredictable. INPUT_PULLUP connects an internal resistor to 5V/3.3V, giving a stable HIGH when nothing is connected.

Can I use external pull-down resistors?

Yes. Use pinMode(INPUT) and connect a 10 kOhm resistor from pin to GND. The switch connects the pin to VCC. Read returns HIGH when pressed.

Why does my button reading bounce?

Mechanical switches bounce for 5-20 ms. Add a 50 ms debounce delay or use a debounce library.

Built by the developers of DodaTech

Doda Browser, DodaZIP & Durga Antivirus Pro