0
0
Embedded Cprogramming~20 mins

Writing HIGH and LOW to output pins in Embedded C - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Pin Mastery Badge
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2:00remaining
What is the output state of the pin after this code?
Consider this embedded C code snippet that sets a pin high and then low. What is the final state of the pin?
Embedded C
#define PIN 5
#define HIGH 1
#define LOW 0

int pin_state = LOW;

void digitalWrite(int pin, int state) {
    if (pin == PIN) {
        pin_state = state;
    }
}

int main() {
    digitalWrite(PIN, HIGH);
    digitalWrite(PIN, LOW);
    return pin_state;
}
A1
B0
C-1
DCompilation error
Attempts:
2 left
💡 Hint
Think about the last command that sets the pin state.
🧠 Conceptual
intermediate
1:30remaining
Which statement correctly sets a pin HIGH in embedded C?
You want to set a digital output pin to HIGH. Which of the following code lines correctly does this?
AdigitalWrite(pin, HIGH);
BdigitalWrite(pin);
CdigitalWrite(pin, 2);
DdigitalWrite(HIGH, pin);
Attempts:
2 left
💡 Hint
The function takes the pin number first, then the state.
🔧 Debug
advanced
2:30remaining
Why does this code fail to set the pin LOW?
This code is intended to set the pin LOW but it does not work as expected. What is the error?
Embedded C
#define PIN 3
#define HIGH 1
#define LOW 0

int pin_state = HIGH;

void digitalWrite(int pin, int state) {
    if (pin = PIN) {
        pin_state = state;
    }
}

int main() {
    digitalWrite(PIN, LOW);
    return pin_state;
}
ALOW is not defined properly.
BThe pin_state variable is not declared correctly.
CThe if condition uses assignment '=' instead of comparison '==', causing always true.
DdigitalWrite function is missing a return statement.
Attempts:
2 left
💡 Hint
Check the if condition inside digitalWrite carefully.
Predict Output
advanced
2:00remaining
What is the output of this pin toggle code?
This code toggles a pin state starting from LOW. What is the final pin state after running main?
Embedded C
#define PIN 2
#define HIGH 1
#define LOW 0

int pin_state = LOW;

void digitalWrite(int pin, int state) {
    if (pin == PIN) {
        pin_state = state;
    }
}

void togglePin(int pin) {
    if (pin_state == LOW) {
        digitalWrite(pin, HIGH);
    } else {
        digitalWrite(pin, LOW);
    }
}

int main() {
    togglePin(PIN);
    togglePin(PIN);
    togglePin(PIN);
    return pin_state;
}
A2
B0
CCompilation error
D1
Attempts:
2 left
💡 Hint
Count how many times the pin toggles starting from LOW.
🧠 Conceptual
expert
1:30remaining
Which option causes a compilation error when writing HIGH or LOW to a pin?
Which of these code snippets will cause a compilation error when trying to write HIGH or LOW to a pin?
AdigitalWrite(PIN, "HIGH");
BdigitalWrite(PIN, HIGH);
CdigitalWrite(PIN, 1);
DdigitalWrite(PIN, true);
Attempts:
2 left
💡 Hint
Consider the data types expected by digitalWrite.