Bird
Raised Fist0
Arduinoprogramming~20 mins

digitalRead() for input reading in Arduino - Practice Problems & Coding Challenges

Choose your learning style10 modes available

Start learning this pattern below

Jump into concepts and practice - no test required

or
Recommended
Test this pattern10 questions across easy, medium, and hard to know if this pattern is strong
Challenge - 5 Problems
🎖️
DigitalRead Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2:00remaining
What is the output of this Arduino code snippet?

Consider the following Arduino code that reads a button state connected to pin 7 and prints a message:

void setup() {
  pinMode(7, INPUT);
  Serial.begin(9600);
}

void loop() {
  int buttonState = digitalRead(7);
  if (buttonState == HIGH) {
    Serial.println("Button pressed");
  } else {
    Serial.println("Button not pressed");
  }
  delay(1000);
}

Assuming the button is pressed (pin 7 reads HIGH), what will be printed to the Serial Monitor?

Arduino
void setup() {
  pinMode(7, INPUT);
  Serial.begin(9600);
}

void loop() {
  int buttonState = digitalRead(7);
  if (buttonState == HIGH) {
    Serial.println("Button pressed");
  } else {
    Serial.println("Button not pressed");
  }
  delay(1000);
}
AButton not pressed
BHIGH
CButton pressed
DLOW
Attempts:
2 left
💡 Hint

digitalRead() returns HIGH or LOW depending on the voltage at the pin.

Predict Output
intermediate
1:30remaining
What will be the value of buttonState after this code runs?

Given this Arduino code snippet:

pinMode(5, INPUT_PULLUP);
int buttonState = digitalRead(5);

If the button connected to pin 5 is not pressed, what is the value of buttonState?

Arduino
pinMode(5, INPUT_PULLUP);
int buttonState = digitalRead(5);
AHIGH
BLOW
C1
D0
Attempts:
2 left
💡 Hint

INPUT_PULLUP mode sets the pin HIGH by default when the button is not pressed.

Predict Output
advanced
2:00remaining
What error does this Arduino code produce?

Examine this Arduino code snippet:

void setup() {
  pinMode(8, INPUT);
  Serial.begin(9600);
}

void loop() {
  int state = digitalRead(8)
  if (state == HIGH) {
    Serial.println("High");
  }
}

What error will the Arduino IDE show when compiling?

Arduino
void setup() {
  pinMode(8, INPUT);
  Serial.begin(9600);
}

void loop() {
  int state = digitalRead(8)
  if (state == HIGH) {
    Serial.println("High");
  }
}
ARuntimeError: pin not initialized
BSyntaxError: missing semicolon
CNo error, code compiles
DTypeError: digitalRead returns void
Attempts:
2 left
💡 Hint

Check the end of the line where digitalRead is called.

🧠 Conceptual
advanced
1:30remaining
Which option correctly explains why digitalRead() might return LOW even if the button is not pressed?

Consider a button connected to an Arduino input pin. Sometimes digitalRead() returns LOW even when the button is not pressed. Why could this happen?

AThe input pin is floating because it lacks a pull-up or pull-down resistor
BThe button is broken and always connects the pin to ground
CdigitalRead() only returns HIGH or LOW randomly
DThe Arduino board is powered off
Attempts:
2 left
💡 Hint

Think about what happens to an input pin voltage if it is not connected to anything.

Predict Output
expert
2:30remaining
What is the output of this Arduino code with multiple digitalRead calls?

Analyze this Arduino code snippet:

void setup() {
  pinMode(2, INPUT_PULLUP);
  pinMode(3, INPUT);
  Serial.begin(9600);
}

void loop() {
  int val2 = digitalRead(2);
  int val3 = digitalRead(3);
  if (val2 == LOW && val3 == HIGH) {
    Serial.println("Case 1");
  } else if (val2 == HIGH && val3 == LOW) {
    Serial.println("Case 2");
  } else {
    Serial.println("Other case");
  }
  delay(1000);
}

Assuming pin 2 button is pressed (connected to ground) and pin 3 is floating (not connected), what will be printed?

Arduino
void setup() {
  pinMode(2, INPUT_PULLUP);
  pinMode(3, INPUT);
  Serial.begin(9600);
}

void loop() {
  int val2 = digitalRead(2);
  int val3 = digitalRead(3);
  if (val2 == LOW && val3 == HIGH) {
    Serial.println("Case 1");
  } else if (val2 == HIGH && val3 == LOW) {
    Serial.println("Case 2");
  } else {
    Serial.println("Other case");
  }
  delay(1000);
}
ANo output, code hangs
BCase 2
CCase 1
DOther case
Attempts:
2 left
💡 Hint

Remember that a floating input pin can read either HIGH or LOW unpredictably.

Practice

(1/5)
1. What does the digitalRead() function do in Arduino?
easy
A. Sets a digital pin to HIGH or LOW
B. Reads the voltage level (HIGH or LOW) from a digital input pin
C. Configures a pin as an output
D. Reads analog voltage from a pin

Solution

  1. Step 1: Understand the purpose of digitalRead()

    The function digitalRead() checks the voltage level on a digital pin and returns HIGH or LOW.
  2. Step 2: Differentiate from other functions

    Unlike digitalWrite() which sets pin output, digitalRead() only reads input state.
  3. Final Answer:

    Reads the voltage level (HIGH or LOW) from a digital input pin -> Option B
  4. Quick Check:

    digitalRead() reads pin state [OK]
Hint: digitalRead() always reads input pin state [OK]
Common Mistakes:
  • Confusing digitalRead() with digitalWrite()
  • Thinking digitalRead() sets pin output
  • Mixing digitalRead() with analogRead()
2. Which of the following is the correct syntax to read a digital pin 7 in Arduino?
easy
A. digitalReadPin(7);
B. digitalRead = 7;
C. digitalRead(7);
D. readDigital(7);

Solution

  1. Step 1: Recall the correct function syntax

    The correct function call is digitalRead(pinNumber); where pinNumber is the pin to read.
  2. Step 2: Check each option

    Only digitalRead(7); matches the correct syntax digitalRead(7); others are invalid function calls or assignments.
  3. Final Answer:

    digitalRead(7); -> Option C
  4. Quick Check:

    Correct function call [OK]
Hint: digitalRead(pinNumber) reads pin state correctly [OK]
Common Mistakes:
  • Using assignment instead of function call
  • Wrong function name like readDigital()
  • Adding extra words like digitalReadPin()
3. What will be the output on the Serial Monitor if the button connected to pin 2 is pressed (assuming HIGH when pressed)?
void setup() {
  pinMode(2, INPUT);
  Serial.begin(9600);
}

void loop() {
  int buttonState = digitalRead(2);
  Serial.println(buttonState);
  delay(500);
}
medium
A. 1
B. Error: pinMode missing
C. 0
D. Random values

Solution

  1. Step 1: Analyze pinMode and digitalRead usage

    Pin 2 is set as INPUT, so digitalRead(2) reads the button state correctly.
  2. Step 2: Understand button press state

    When the button is pressed, the pin reads HIGH which is 1, so Serial.println prints 1 repeatedly.
  3. Final Answer:

    1 -> Option A
  4. Quick Check:

    Pressed button = HIGH = 1 [OK]
Hint: Pressed button reads HIGH (1) on digitalRead() [OK]
Common Mistakes:
  • Assuming pressed button reads 0 instead of 1
  • Forgetting to set pinMode to INPUT
  • Confusing analogRead with digitalRead
4. Identify the error in this code snippet that reads a button state on pin 4:
void setup() {
  Serial.begin(9600);
}

void loop() {
  int state = digitalRead(4);
  Serial.println(state);
  delay(1000);
}
medium
A. delay() cannot be used with digitalRead()
B. digitalRead() cannot be used in loop()
C. Serial.begin() should be in loop()
D. Missing pinMode(4, INPUT) in setup()

Solution

  1. Step 1: Check pin configuration

    The code does not set pin 4 as INPUT using pinMode(), which is required before reading.
  2. Step 2: Verify other parts

    digitalRead() can be used in loop(), Serial.begin() must be in setup(), and delay() is allowed.
  3. Final Answer:

    Missing pinMode(4, INPUT) in setup() -> Option D
  4. Quick Check:

    Always set pinMode before digitalRead() [OK]
Hint: Always set pinMode(INPUT) before digitalRead() [OK]
Common Mistakes:
  • Forgetting pinMode() setup
  • Moving Serial.begin() to loop() incorrectly
  • Thinking delay() breaks digitalRead()
5. You want to detect if a switch connected to pin 3 is pressed, but the switch is wired so it connects the pin to GND when pressed (active LOW). Which code snippet correctly reads the switch state and prints "Pressed" or "Not Pressed" accordingly? A)
pinMode(3, INPUT);
if (digitalRead(3) == HIGH) {
  Serial.println("Pressed");
} else {
  Serial.println("Not Pressed");
}
B)
pinMode(3, INPUT);
if (digitalRead(3) == LOW) {
  Serial.println("Pressed");
} else {
  Serial.println("Not Pressed");
}
C)
pinMode(3, OUTPUT);
if (digitalRead(3) == LOW) {
  Serial.println("Pressed");
} else {
  Serial.println("Not Pressed");
}
D)
pinMode(3, INPUT_PULLUP);
if (digitalRead(3) == HIGH) {
  Serial.println("Pressed");
} else {
  Serial.println("Not Pressed");
}
hard
A. Code snippet B
B. Code snippet A
C. Code snippet C
D. Code snippet D

Solution

  1. Step 1: Understand active LOW switch wiring

    The switch connects pin 3 to GND when pressed, so digitalRead(3) returns LOW when pressed.
  2. Step 2: Check pinMode and condition

    pinMode must be INPUT (not OUTPUT). The condition to detect press is digitalRead(3) == LOW.
  3. Final Answer:

    Code snippet B -> Option A
  4. Quick Check:

    Active LOW switch reads LOW when pressed [OK]
Hint: Active LOW means pressed when digitalRead() returns LOW [OK]
Common Mistakes:
  • Using OUTPUT mode instead of INPUT
  • Checking for HIGH instead of LOW for active LOW switch
  • Using INPUT_PULLUP but checking wrong logic