0
0
Embedded Cprogramming~10 mins

Reading digital input pin state in Embedded C - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - Reading digital input pin state
Start
Configure pin as input
Read pin state
Check if HIGH or LOW
If HIGH
Do something
If LOW
Do something else
End or Repeat
The program sets a pin as input, reads its state (HIGH or LOW), then acts based on that state.
Execution Sample
Embedded C
pinMode(2, INPUT);
int state = digitalRead(2);
if(state == HIGH) {
  // do something
} else {
  // do something else
}
This code sets pin 2 as input, reads its state, and runs code depending on whether the pin is HIGH or LOW.
Execution Table
StepActionPin ModePin State ReadConditionBranch TakenOutput/Effect
1Set pin 2 mode to INPUTINPUTN/AN/AN/APin 2 ready for input
2Read digital state of pin 2INPUTHIGHstate == HIGH?YesExecute HIGH branch
3Execute HIGH branchINPUTHIGHN/AN/ADo something (e.g., turn LED on)
4End or repeatINPUTHIGHN/AN/AProgram waits or loops
5If pin state was LOWINPUTLOWstate == HIGH?NoExecute LOW branch
6Execute LOW branchINPUTLOWN/AN/ADo something else (e.g., turn LED off)
7End or repeatINPUTLOWN/AN/AProgram waits or loops
💡 Program stops or repeats after reading and acting on pin state
Variable Tracker
VariableStartAfter Step 2After Step 5Final
pin 2 modeUndefinedINPUTINPUTINPUT
stateUndefinedHIGHLOWDepends on pin reading
Key Moments - 3 Insights
Why do we need to set the pin mode to INPUT before reading?
Because the microcontroller needs to know the pin is for input, not output. Step 1 in the execution_table shows setting pinMode to INPUT before reading the state.
What does digitalRead return if the pin is not connected?
It may return LOW or unpredictable values. The execution_table rows 2 and 5 show reading HIGH or LOW, but if floating, the value can be unstable.
Why do we check if state == HIGH instead of just using the value directly?
Because digitalRead returns either HIGH or LOW constants. Step 2 and 5 in the table show the condition check to decide which branch to take.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table, what is the pin state read at Step 2?
ALOW
BINPUT
CHIGH
DUndefined
💡 Hint
Check the 'Pin State Read' column at Step 2 in the execution_table.
At which step does the program decide to execute the LOW branch?
AStep 5
BStep 2
CStep 3
DStep 1
💡 Hint
Look at the 'Branch Taken' column where condition 'state == HIGH?' is No.
If we forget to set pinMode to INPUT, what would likely happen?
AdigitalRead will always return HIGH
BdigitalRead may return unpredictable values
CPin will automatically be input
DProgram will not compile
💡 Hint
Refer to key_moments about why setting pinMode is important.
Concept Snapshot
Set pin mode to INPUT before reading.
Use digitalRead(pin) to get HIGH or LOW.
Check if state == HIGH to decide action.
Without setting INPUT, readings may be wrong.
Repeat reading as needed in loop.
Full Transcript
This program starts by setting a pin as input so the microcontroller knows to listen to signals on that pin. Then it reads the pin's digital state, which can be HIGH or LOW. Based on the reading, it decides which code branch to run, for example turning an LED on or off. The execution table shows each step: setting pin mode, reading state, checking condition, and acting accordingly. Variables like pinMode and state change as the program runs. Beginners often wonder why setting pin mode is needed and what happens if it's skipped. The quiz questions help check understanding of pin state reading and decision making.