Bird
Raised Fist0
Embedded Cprogramming~10 mins

Writing HIGH and LOW to output pins in Embedded C - Step-by-Step Execution

Choose your learning style10 modes available
Concept Flow - Writing HIGH and LOW to output pins
Setup pin as OUTPUT
↓
Write HIGH to pin
→Pin voltage set to HIGH
↓
Write LOW to pin
→Pin voltage set to LOW
↓
End
First, configure the pin as an output. Then write HIGH or LOW to set the pin voltage accordingly.
Execution Sample
Embedded C
pinMode(13, OUTPUT);
digitalWrite(13, HIGH);
delay(1000);
digitalWrite(13, LOW);
Set pin 13 as output, turn it ON (HIGH), wait 1 second, then turn it OFF (LOW).
Execution Table
StepActionPin ModePin StateOutput Voltage
1pinMode(13, OUTPUT)OUTPUTLOW (default)0V (LOW)
2digitalWrite(13, HIGH)OUTPUTHIGH5V (HIGH)
3delay(1000)OUTPUTHIGH5V (HIGH)
4digitalWrite(13, LOW)OUTPUTLOW0V (LOW)
5End of codeOUTPUTLOW0V (LOW)
💡 Code ends after setting pin 13 LOW.
Variable Tracker
VariableStartAfter Step 1After Step 2After Step 3After Step 4Final
Pin 13 ModeINPUT (default)OUTPUTOUTPUTOUTPUTOUTPUTOUTPUT
Pin 13 StateLOW (default)LOWHIGHHIGHLOWLOW
Pin 13 Voltage0V (LOW)0V (LOW)5V (HIGH)5V (HIGH)0V (LOW)0V (LOW)
Key Moments - 3 Insights
Why do we need to set the pin mode to OUTPUT before writing HIGH or LOW?
Because the pin must be configured as OUTPUT to control voltage levels; otherwise, writing HIGH or LOW won't change the pin voltage (see Step 1 in execution_table).
Does writing HIGH immediately change the pin voltage?
Yes, writing HIGH sets the pin voltage to 5V right away (Step 2 shows pin state changes to HIGH and voltage to 5V).
What happens if we write LOW after writing HIGH?
The pin voltage changes back to 0V (LOW), turning the output off (Step 4 shows this change).
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table, what is the pin state after Step 2?
ALOW
BHIGH
CINPUT
DUNKNOWN
💡 Hint
Check the 'Pin State' column at Step 2 in the execution_table.
At which step does the pin voltage become 0V after being HIGH?
AStep 1
BStep 2
CStep 4
DStep 3
💡 Hint
Look at the 'Pin 13 Voltage' in variable_tracker after Step 4.
If we skip setting pinMode to OUTPUT, what will happen when writing HIGH?
APin voltage stays LOW
BPin voltage changes to HIGH
CPin mode automatically changes to OUTPUT
DProgram crashes
💡 Hint
Refer to key_moments about why pinMode must be set before writing.
Concept Snapshot
pinMode(pin, OUTPUT) sets pin as output
Use digitalWrite(pin, HIGH) to set voltage HIGH (5V)
Use digitalWrite(pin, LOW) to set voltage LOW (0V)
Must set pinMode before writing
Writing changes pin voltage immediately
Delay can keep state before changing
Full Transcript
This example shows how to control an output pin in embedded C. First, the pin is set as OUTPUT using pinMode. Then digitalWrite sets the pin voltage HIGH or LOW. The execution table tracks each step: setting mode, writing HIGH, waiting, then writing LOW. Variables track pin mode, state, and voltage changes. Key moments clarify why setting pin mode is necessary and how writing changes voltage immediately. The visual quiz tests understanding of pin state and voltage changes at each step. The snapshot summarizes the commands and rules for writing HIGH and LOW to output pins.

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