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.
Right
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
← Previous
Arduino Internal Pull-Up Resistor Not Working
Next →
Arduino Pin Does Not Toggle Correctly
Built by the developers of DodaTech
Doda Browser, DodaZIP & Durga Antivirus Pro