Bird
Raised Fist0
Arduinoprogramming~20 mins

digitalWrite() for output control 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
🎖️
digitalWrite Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
1:30remaining
What is the output state of the LED after running this code?

Consider the following Arduino code snippet controlling an LED connected to pin 13.

void setup() {
  pinMode(13, OUTPUT);
  digitalWrite(13, HIGH);
}

void loop() {
  // empty loop
}

What will be the state of the LED after setup() finishes?

Arduino
void setup() {
  pinMode(13, OUTPUT);
  digitalWrite(13, HIGH);
}

void loop() {
  // empty loop
}
AThe LED will be ON (pin 13 set to HIGH).
BThe LED state is undefined because loop() is empty.
CThe LED will blink rapidly.
DThe LED will be OFF (pin 13 set to LOW).
Attempts:
2 left
💡 Hint

Remember that digitalWrite(pin, HIGH) sets the pin voltage to 5V (or 3.3V), turning the LED ON if connected correctly.

Predict Output
intermediate
1:30remaining
What will be the output on pin 8 after this code runs?

Analyze this Arduino code snippet:

void setup() {
  pinMode(8, OUTPUT);
  digitalWrite(8, LOW);
  digitalWrite(8, HIGH);
  digitalWrite(8, LOW);
}

void loop() {}

What is the final state of pin 8 after setup() completes?

Arduino
void setup() {
  pinMode(8, OUTPUT);
  digitalWrite(8, LOW);
  digitalWrite(8, HIGH);
  digitalWrite(8, LOW);
}

void loop() {}
APin 8 will be in INPUT mode.
BPin 8 will be HIGH (LED ON).
CPin 8 will toggle rapidly between HIGH and LOW.
DPin 8 will be LOW (LED OFF).
Attempts:
2 left
💡 Hint

The last digitalWrite call sets the pin state.

Predict Output
advanced
1:30remaining
What error occurs when using digitalWrite() on a pin not set as OUTPUT?

Consider this Arduino code:

void setup() {
  digitalWrite(7, HIGH);
}

void loop() {}

Pin 7 was never set as OUTPUT. What happens when this code runs?

Arduino
void setup() {
  digitalWrite(7, HIGH);
}

void loop() {}
ACompilation error: pinMode must be set before digitalWrite.
BPin 7 will be set HIGH but remain INPUT internally (enabling pull-up resistor).
CPin 7 will be set HIGH as if it were OUTPUT.
DRuntime error: digitalWrite on INPUT pin causes crash.
Attempts:
2 left
💡 Hint

Think about how Arduino handles digitalWrite on pins configured as INPUT.

🧠 Conceptual
advanced
1:30remaining
How does digitalWrite() affect power consumption when controlling outputs?

When using digitalWrite() to control an LED connected to an Arduino output pin, which statement about power consumption is correct?

APower consumption depends on the LED and resistor, not just pin state.
BSetting the pin LOW always consumes more power than HIGH.
CdigitalWrite() does not affect power consumption at all.
DSetting the pin HIGH always consumes more power than LOW.
Attempts:
2 left
💡 Hint

Consider the whole circuit, not just the microcontroller pin.

Predict Output
expert
2:00remaining
What is the output of this Arduino code controlling multiple pins?

Examine this Arduino code snippet:

void setup() {
  for (int pin = 2; pin <= 4; pin++) {
    pinMode(pin, OUTPUT);
    digitalWrite(pin, pin % 2 == 0 ? HIGH : LOW);
  }
}

void loop() {}

Which pins are HIGH after setup() runs?

Arduino
void setup() {
  for (int pin = 2; pin <= 4; pin++) {
    pinMode(pin, OUTPUT);
    digitalWrite(pin, pin % 2 == 0 ? HIGH : LOW);
  }
}

void loop() {}
APins 3 and 4 are HIGH; pin 2 is LOW.
BPins 2 and 3 are HIGH; pin 4 is LOW.
CPins 2 and 4 are HIGH; pin 3 is LOW.
DAll pins 2, 3, and 4 are LOW.
Attempts:
2 left
💡 Hint

Check which pins are even and which are odd.

Practice

(1/5)
1. What does the digitalWrite() function do in Arduino programming?
easy
A. It sets the analog value of a pin.
B. It reads the voltage from a digital pin.
C. It initializes the serial communication.
D. It sets a digital pin to HIGH or LOW voltage.

Solution

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

    The function digitalWrite() is used to control the voltage level on a digital pin, setting it either HIGH (on) or LOW (off). It does not read values (that's digitalRead()), nor does it set analog values or initialize serial communication.
  2. Final Answer:

    It sets a digital pin to HIGH or LOW voltage. -> Option D
  3. Quick Check:

    digitalWrite() controls pin voltage = D [OK]
Hint: digitalWrite sets pin ON or OFF voltage [OK]
Common Mistakes:
  • Confusing digitalWrite() with digitalRead()
  • Thinking digitalWrite() reads pin values
  • Mixing digitalWrite() with analogWrite()
2. Which of the following is the correct syntax to turn on an LED connected to pin 13 using digitalWrite()?
easy
A. digitalWrite(13, HIGH);
B. digitalWrite(13, LOW);
C. digitalWrite(13, ON);
D. digitalWrite(13, TRUE);

Solution

  1. Step 1: Recall correct constants and check options

    digitalWrite() uses HIGH and LOW constants. To turn on, use HIGH. A uses ON (not valid), B uses LOW (turns off), D uses TRUE (not valid). Only C: digitalWrite(13, HIGH); is correct.
  2. Final Answer:

    digitalWrite(13, HIGH); -> Option A
  3. Quick Check:

    Use HIGH or LOW with digitalWrite() = C [OK]
Hint: Use HIGH or LOW, not ON or TRUE [OK]
Common Mistakes:
  • Using ON or TRUE instead of HIGH
  • Using LOW instead of HIGH
  • Forgetting to set pinMode to OUTPUT first
3. What will be the output on pin 8 after running this code snippet?
pinMode(8, OUTPUT);
digitalWrite(8, LOW);
digitalWrite(8, HIGH);
digitalWrite(8, LOW);
medium
A. Pin 8 will toggle rapidly.
B. Pin 8 will be HIGH (on).
C. Pin 8 will be LOW (off).
D. Pin 8 will cause a syntax error.

Solution

  1. Step 1: Trace the digitalWrite() calls

    pinMode sets OUTPUT, then LOW, HIGH, LOW. Final LOW so pin off.
  2. Final Answer:

    Pin 8 will be LOW (off). -> Option C
  3. Quick Check:

    Last digitalWrite sets pin LOW = A [OK]
Hint: Last digitalWrite() sets pin state [OK]
Common Mistakes:
  • Assuming pin toggles automatically
  • Confusing initial and final pin states
  • Thinking multiple digitalWrite calls cause errors
4. Identify the error in this Arduino code snippet:
digitalWrite(12, HIGH);
pinMode(12, OUTPUT);
medium
A. No error, code runs fine.
B. pinMode() must be called before digitalWrite().
C. digitalWrite() cannot use pin 12.
D. HIGH is not a valid value for digitalWrite().

Solution

  1. Step 1: Check order of pinMode() and digitalWrite()

    pinMode() must set OUTPUT before digitalWrite(), otherwise unexpected behavior. Here digitalWrite first.
  2. Final Answer:

    pinMode() must be called before digitalWrite(). -> Option B
  3. Quick Check:

    Set pinMode before digitalWrite = A [OK]
Hint: Always set pinMode(OUTPUT) before digitalWrite() [OK]
Common Mistakes:
  • Calling digitalWrite before pinMode
  • Assuming pinMode defaults to OUTPUT
  • Using invalid pin numbers
5. You want to blink an LED connected to pin 9 on and off every second. Which code snippet correctly uses digitalWrite() inside the loop() function to achieve this?
hard
A. digitalWrite(9, HIGH); delay(1000); digitalWrite(9, LOW); delay(1000);
B. digitalWrite(9, HIGH); digitalWrite(9, LOW); delay(1000);
C. pinMode(9, OUTPUT); digitalWrite(9, HIGH); delay(1000);
D. digitalWrite(9, HIGH); delay(500); digitalWrite(9, HIGH); delay(500);

Solution

  1. Step 1: Understand blinking and analyze options

    Blink: HIGH, delay(1000), LOW, delay(1000). A: HIGH immediate LOW, no on time. C: pinMode in loop inefficient, no LOW. D: HIGH twice, always on. Only B correct.
  2. Final Answer:

    digitalWrite(9, HIGH); delay(1000); digitalWrite(9, LOW); delay(1000); -> Option A
  3. Quick Check:

    Turn ON, delay, turn OFF, delay = B [OK]
Hint: Turn ON, delay, turn OFF, delay for blinking [OK]
Common Mistakes:
  • Missing delay between ON and OFF
  • Not turning LED OFF after ON
  • Setting pinMode inside loop repeatedly