Bird
Raised Fist0
Embedded Cprogramming~20 mins

Reading digital input pin state in Embedded C - Practice Problems & Coding Challenges

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
Digital Input 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 reading a digital input pin?
Consider the following embedded C code snippet that reads a digital input pin connected to a button. What value will be printed if the button is pressed (assuming active HIGH)?
Embedded C
#include <stdio.h>
#define BUTTON_PIN 2

int digitalRead(int pin) {
    // Simulated hardware input: returns 1 if pressed, 0 if not
    if (pin == BUTTON_PIN) return 1;
    return 0;
}

int main() {
    int buttonState = digitalRead(BUTTON_PIN);
    printf("Button state: %d\n", buttonState);
    return 0;
}
AButton state: 1
BButton state: 0
CButton state: -1
DCompilation error
Attempts:
2 left
💡 Hint
Think about what digitalRead returns when the button is pressed.
❓ Predict Output
intermediate
2:00remaining
What happens if you read an unconfigured input pin?
Given this code snippet, what output will it produce if the input pin is not configured and floating?
Embedded C
#include <stdio.h>
#define INPUT_PIN 5

int digitalRead(int pin) {
    // Simulate floating pin returns random 0 or 1
    if (pin == INPUT_PIN) return 0; // Simulated as LOW
    return 1;
}

int main() {
    int state = digitalRead(INPUT_PIN);
    printf("Pin state: %d\n", state);
    return 0;
}
APin state: 1
BRuntime error
CPin state: 0
DPin state: -1
Attempts:
2 left
💡 Hint
Floating pins often read as LOW or unpredictable, here simulated as LOW.
🔧 Debug
advanced
2:30remaining
Why does this code always print 0 for the input pin?
Find the bug in this code that causes the input pin state to always print 0, even when the button is pressed.
Embedded C
#include <stdio.h>
#define BUTTON_PIN 3

int digitalRead(int pin) {
    if (pin == BUTTON_PIN) return 1;
    return 0;
}

int main() {
    int buttonState = 0;
    if (BUTTON_PIN = 3) {
        buttonState = digitalRead(BUTTON_PIN);
    }
    printf("Button state: %d\n", buttonState);
    return 0;
}
AThe condition uses assignment '=' instead of comparison '==', so it always evaluates true but BUTTON_PIN is changed.
BdigitalRead always returns 0 for BUTTON_PIN 3.
CbuttonState is never assigned any value.
DMissing semicolon after digitalRead call.
Attempts:
2 left
💡 Hint
Check the if condition syntax carefully.
🧠 Conceptual
advanced
1:30remaining
What is the effect of enabling pull-up resistor on a digital input pin?
In embedded systems, enabling an internal pull-up resistor on a digital input pin means:
AThe pin is connected internally to ground, so it reads LOW when not pressed.
BThe pin is connected internally to a high voltage, so it reads HIGH when not pressed.
CThe pin is disconnected internally, so it floats and reads random values.
DThe pin output is driven HIGH constantly.
Attempts:
2 left
💡 Hint
Pull-up resistors pull the pin voltage up to a high level when no external signal is applied.
❓ Predict Output
expert
3:00remaining
What is the output of this code reading multiple digital input pins?
Given the following code that reads three digital input pins and prints their states, what is the output?
Embedded C
#include <stdio.h>

int digitalRead(int pin) {
    switch(pin) {
        case 1: return 0;
        case 2: return 1;
        case 3: return 1;
        default: return -1;
    }
}

int main() {
    int states[3];
    for (int i = 0; i < 3; i++) {
        states[i] = digitalRead(i + 1);
    }
    printf("States: %d %d %d\n", states[0], states[1], states[2]);
    return 0;
}
AStates: 1 0 1
BStates: -1 -1 -1
CStates: 1 1 0
DStates: 0 1 1
Attempts:
2 left
💡 Hint
Check the digitalRead return values for each pin number.

Practice

(1/5)
1. What does the function digitalRead(pin) do in embedded C programming?
easy
A. Configures the pin as an analog input.
B. Sets the digital pin to output mode.
C. Reads the current state (HIGH or LOW) of a digital input pin.
D. Writes a HIGH signal to a digital pin.

Solution

  1. Step 1: Understand the purpose of digitalRead

    The function digitalRead(pin) is used to check the current electrical state of a digital input pin, which can be either HIGH (on) or LOW (off).
  2. Step 2: Differentiate from other pin functions

    Unlike functions that set pin modes or write signals, digitalRead only reads the pin's state without changing it.
  3. Final Answer:

    Reads the current state (HIGH or LOW) of a digital input pin. -> Option C
  4. Quick Check:

    digitalRead reads pin state = D [OK]
Hint: digitalRead always reads input pin state, not sets it [OK]
Common Mistakes:
  • Confusing digitalRead with pinMode or digitalWrite
  • Thinking digitalRead sets pin mode
  • Assuming digitalRead writes output signals
2. Which of the following is the correct way to set a pin as input before reading its state?
easy
A. pinMode(pin, OUTPUT);
B. pinMode(pin, INPUT);
C. digitalRead(pin);
D. digitalWrite(pin, HIGH);

Solution

  1. Step 1: Identify the function to set pin mode

    pinMode(pin, mode) is used to configure a pin as INPUT or OUTPUT.
  2. Step 2: Choose the correct mode for reading input

    To read a digital input pin, it must be set as INPUT using pinMode(pin, INPUT).
  3. Final Answer:

    pinMode(pin, INPUT); -> Option B
  4. Quick Check:

    Set pin as INPUT before reading = A [OK]
Hint: Use pinMode(pin, INPUT) before digitalRead [OK]
Common Mistakes:
  • Setting pin as OUTPUT before reading input
  • Calling digitalRead before setting pin mode
  • Using digitalWrite to set pin mode
3. What will be the output of the following code snippet if the button connected to pin 7 is pressed (assuming pressed means HIGH)?
pinMode(7, INPUT);
int buttonState = digitalRead(7);
if (buttonState == HIGH) {
    printf("Button is pressed\n");
} else {
    printf("Button is not pressed\n");
}
medium
A. Button is pressed
B. Button is not pressed
C. Compilation error
D. No output

Solution

  1. Step 1: Analyze pin mode and reading

    The pin 7 is set as INPUT, so digitalRead(7) correctly reads the pin state.
  2. Step 2: Check condition for pressed button

    If the button is pressed, the pin reads HIGH, so the if condition is true and prints "Button is pressed".
  3. Final Answer:

    Button is pressed -> Option A
  4. Quick Check:

    Pressed button = HIGH = prints pressed [OK]
Hint: Pressed button means digitalRead returns HIGH [OK]
Common Mistakes:
  • Forgetting to set pinMode before reading
  • Confusing HIGH and LOW states
  • Assuming digitalRead returns boolean true/false
4. Identify the error in the following code that reads a digital input pin state:
int pin = 5;
int state;
state = digitalRead();
if(state == HIGH) {
    // do something
}
medium
A. Pin should be set as OUTPUT before reading.
B. The if condition syntax is incorrect.
C. digitalRead returns a float, not int.
D. Missing pin number argument in digitalRead function.

Solution

  1. Step 1: Check digitalRead usage

    digitalRead requires the pin number as an argument, but here it is called without any argument.
  2. Step 2: Verify other parts of code

    Pin mode setting is missing but not the main error asked; the if condition syntax is correct.
  3. Final Answer:

    Missing pin number argument in digitalRead function. -> Option D
  4. Quick Check:

    digitalRead needs pin argument = B [OK]
Hint: digitalRead always needs pin number inside parentheses [OK]
Common Mistakes:
  • Calling digitalRead without pin argument
  • Setting pin as OUTPUT before reading input
  • Misunderstanding return type of digitalRead
5. You want to detect if a switch connected to pin 3 is ON and turn on an LED connected to pin 13 accordingly. Which code snippet correctly reads the switch state and controls the LED?
hard
A. pinMode(3, INPUT); pinMode(13, OUTPUT); if (digitalRead(3) == HIGH) { digitalWrite(13, HIGH); } else { digitalWrite(13, LOW); }
B. pinMode(3, OUTPUT); pinMode(13, INPUT); if (digitalRead(13) == HIGH) { digitalWrite(3, HIGH); }
C. pinMode(3, INPUT); if (digitalRead(13) == HIGH) { digitalWrite(3, HIGH); }
D. pinMode(13, OUTPUT); digitalWrite(3, HIGH); if (digitalRead(3) == HIGH) { digitalWrite(13, HIGH); }

Solution

  1. Step 1: Set correct pin modes

    Pin 3 is input for the switch, so pinMode(3, INPUT). Pin 13 is output for the LED, so pinMode(13, OUTPUT).
  2. Step 2: Read switch and control LED

    If digitalRead(3) is HIGH (switch ON), turn LED on with digitalWrite(13, HIGH), else turn it off with digitalWrite(13, LOW).
  3. Final Answer:

    pinMode(3, INPUT); pinMode(13, OUTPUT); if (digitalRead(3) == HIGH) { digitalWrite(13, HIGH); } else { digitalWrite(13, LOW); } -> Option A
  4. Quick Check:

    Input pin read controls output pin write = A [OK]
Hint: Input pin set INPUT, output pin set OUTPUT, read then write [OK]
Common Mistakes:
  • Setting input pin as OUTPUT or vice versa
  • Reading wrong pin for switch state
  • Not controlling LED pin properly
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard