Bird
Raised Fist0
Embedded Cprogramming~20 mins

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

Choose your learning style10 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.

Practice

(1/5)
1. Which function is used to set a pin as an output before writing HIGH or LOW to it?
easy
A. setPinMode(pin, HIGH)
B. digitalWrite(pin, OUTPUT)
C. pinMode(pin, OUTPUT)
D. writePin(pin, OUTPUT)

Solution

  1. Step 1: Understand pin configuration

    Before writing HIGH or LOW, the pin must be set as an output to control voltage.
  2. Step 2: Identify the correct function

    The function to set pin mode is pinMode(pin, OUTPUT), which configures the pin for output.
  3. Final Answer:

    pinMode(pin, OUTPUT) -> Option C
  4. Quick Check:

    Set pin as output = pinMode(pin, OUTPUT) [OK]
Hint: Set pin mode with pinMode before writing HIGH or LOW [OK]
Common Mistakes:
  • Using digitalWrite to set pin mode
  • Confusing HIGH/LOW with OUTPUT/INPUT
  • Forgetting to set pin mode before writing
2. Which of the following is the correct syntax to turn pin 5 ON (HIGH) in embedded C?
easy
A. digitalWrite(5, 1);
B. digitalWrite(5, ON);
C. digitalWrite(5, OUTPUT);
D. digitalWrite(5, HIGH);

Solution

  1. Step 1: Identify the function to write pin state

    The function digitalWrite(pin, value) sets the pin voltage HIGH or LOW.
  2. Step 2: Use correct constants

    Use HIGH to turn the pin ON. The correct syntax is digitalWrite(5, HIGH);.
  3. Final Answer:

    digitalWrite(5, HIGH); -> Option D
  4. Quick Check:

    Turn pin ON = digitalWrite(pin, HIGH) [OK]
Hint: Use HIGH constant, not ON or numbers, for clarity [OK]
Common Mistakes:
  • Using ON instead of HIGH
  • Using OUTPUT instead of HIGH
  • Using numeric 1 instead of HIGH constant
3. What will be the output state of pin 3 after running this code?
pinMode(3, OUTPUT);
digitalWrite(3, LOW);
digitalWrite(3, HIGH);
medium
A. Pin 3 will be LOW
B. Pin 3 will be HIGH
C. Pin 3 will be in input mode
D. Pin 3 will be undefined

Solution

  1. Step 1: Set pin 3 as output

    The code sets pin 3 as output with pinMode(3, OUTPUT);.
  2. Step 2: Write LOW then HIGH to pin 3

    First, pin 3 is set LOW, then immediately set HIGH, so final state is HIGH.
  3. Final Answer:

    Pin 3 will be HIGH -> Option B
  4. Quick Check:

    Last digitalWrite sets pin state = HIGH [OK]
Hint: Last digitalWrite sets the pin state [OK]
Common Mistakes:
  • Assuming pin stays LOW after writing HIGH
  • Forgetting pinMode sets output mode
  • Thinking pin state is undefined without delay
4. Identify the error in this code snippet:
pinMode(7, OUTPUT);
digitalWrite(7, HIGH)
digitalWrite(7, LOW);
medium
A. Missing semicolon after digitalWrite(7, HIGH)
B. pinMode should be called after digitalWrite
C. HIGH and LOW are not valid constants
D. Pin number 7 cannot be used

Solution

  1. Step 1: Check syntax line by line

    The second line digitalWrite(7, HIGH) is missing a semicolon at the end.
  2. Step 2: Confirm other lines are correct

    pinMode(7, OUTPUT); and digitalWrite(7, LOW); are correct syntax.
  3. Final Answer:

    Missing semicolon after digitalWrite(7, HIGH) -> Option A
  4. Quick Check:

    Every statement must end with semicolon [OK]
Hint: Check each line ends with a semicolon [OK]
Common Mistakes:
  • Forgetting semicolon after function calls
  • Calling pinMode after digitalWrite
  • Misusing HIGH/LOW constants
5. You want to toggle pin 10 ON and OFF every second in a loop. Which code snippet correctly sets pin 10 HIGH then LOW with a 1-second delay between states?
hard
A. pinMode(10, OUTPUT); digitalWrite(10, HIGH); delay(1000); digitalWrite(10, LOW); delay(1000);
B. digitalWrite(10, HIGH); delay(1000); digitalWrite(10, LOW); delay(1000);
C. pinMode(10, INPUT); digitalWrite(10, HIGH); delay(1000);
D. pinMode(10, OUTPUT); digitalWrite(10, LOW); digitalWrite(10, HIGH);

Solution

  1. Step 1: Set pin 10 as output

    Use pinMode(10, OUTPUT); to configure pin 10 for output.
  2. Step 2: Write HIGH, wait, then LOW, wait

    Use digitalWrite(10, HIGH); then delay(1000); to wait 1 second, then digitalWrite(10, LOW); and delay again.
  3. Final Answer:

    pinMode(10, OUTPUT); digitalWrite(10, HIGH); delay(1000); digitalWrite(10, LOW); delay(1000); -> Option A
  4. Quick Check:

    Set output, write states with delay between [OK]
Hint: Always set pinMode before toggling with delays [OK]
Common Mistakes:
  • Forgetting to set pinMode as OUTPUT
  • Missing delay between HIGH and LOW
  • Setting pinMode to INPUT instead of OUTPUT
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