Bird
Raised Fist0
Arduinoprogramming~10 mins

pinMode() function behavior in Arduino - Step-by-Step Execution

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
Concept Flow - pinMode() function behavior
Start
Call pinMode(pin, mode)
Check mode value
INPUT
Set pin as input
Apply internal pull-up if INPUT_PULLUP
pinMode set
End
This flow shows how pinMode sets a pin as input or output, including pull-up input mode.
Execution Sample
Arduino
pinMode(13, OUTPUT);
digitalWrite(13, HIGH);
pinMode(7, INPUT_PULLUP);
Sets pin 13 as output and pin 7 as input with internal pull-up resistor.
Execution Table
StepFunction CallPinModeAction TakenPin State After
1pinMode(13, OUTPUT)13OUTPUTSet pin 13 as outputPin 13 mode=OUTPUT
2digitalWrite(13, HIGH)13OUTPUTSet pin 13 voltage HIGHPin 13 output=HIGH
3pinMode(7, INPUT_PULLUP)7INPUT_PULLUPSet pin 7 as input with pull-up resistorPin 7 mode=INPUT_PULLUP with pull-up enabled
4End----
💡 All pinMode and digitalWrite calls completed, pins configured as requested.
Variable Tracker
VariableStartAfter Step 1After Step 2After Step 3Final
Pin 13 modeUNDEFINEDOUTPUTOUTPUTOUTPUTOUTPUT
Pin 13 outputLOW (default)LOWHIGHHIGHHIGH
Pin 7 modeUNDEFINEDUNDEFINEDUNDEFINEDINPUT_PULLUPINPUT_PULLUP
Pin 7 pull-upDISABLEDDISABLEDDISABLEDENABLEDENABLED
Key Moments - 3 Insights
Why does pin 7 have pull-up enabled after pinMode(INPUT_PULLUP)?
Because pinMode with INPUT_PULLUP sets the pin as input and activates the internal pull-up resistor, as shown in step 3 of the execution_table.
What happens if you call digitalWrite on a pin set as OUTPUT?
digitalWrite sets the voltage level on the pin (HIGH or LOW), as seen in step 2 where pin 13 output changes to HIGH.
Does pinMode change the voltage level immediately?
No, pinMode only sets the pin mode; voltage level changes happen with digitalWrite, as shown between steps 1 and 2.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table, what is the mode of pin 13 after step 1?
AOUTPUT
BINPUT
CINPUT_PULLUP
DUNDEFINED
💡 Hint
Check the 'Pin State After' column in row for step 1.
At which step is the internal pull-up resistor enabled for pin 7?
AStep 2
BStep 3
CStep 1
DStep 4
💡 Hint
Look for 'pull-up enabled' in the 'Pin State After' column.
If we changed pinMode(13, OUTPUT) to pinMode(13, INPUT), what would happen at step 1?
APin 13 set as OUTPUT
BPin 13 set as INPUT_PULLUP
CPin 13 set as INPUT
DNo change
💡 Hint
pinMode sets the pin mode directly as per the second argument.
Concept Snapshot
pinMode(pin, mode) sets Arduino pin mode.
Modes: INPUT, OUTPUT, INPUT_PULLUP.
INPUT_PULLUP enables internal pull-up resistor.
digitalWrite controls output voltage on OUTPUT pins.
pinMode does not change voltage level directly.
Full Transcript
The pinMode() function in Arduino sets a pin as input or output. When called with OUTPUT, the pin is set to output mode, allowing voltage control with digitalWrite. When called with INPUT_PULLUP, the pin is set as input and the internal pull-up resistor is enabled, which helps read stable HIGH signals without external resistors. The execution steps show pin 13 set as output and pin 7 as input with pull-up. Variables track pin modes and output states. Key moments clarify pull-up behavior and voltage control. The quiz tests understanding of pin modes and effects of pinMode calls.

Practice

(1/5)
1. What does the pinMode() function do in an Arduino sketch?
easy
A. It sets a pin as input or output to control how it behaves.
B. It reads the value from a pin.
C. It writes a value to a pin.
D. It resets the Arduino board.

Solution

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

    The pinMode() function tells the Arduino whether a pin will be used to read signals (input) or send signals (output).
  2. Step 2: Differentiate from other functions

    Reading values is done by digitalRead(), writing by digitalWrite(), and resetting is unrelated to pinMode().
  3. Final Answer:

    It sets a pin as input or output to control how it behaves. -> Option A
  4. Quick Check:

    pinMode() sets pin direction = C [OK]
Hint: pinMode() sets pin direction: input or output [OK]
Common Mistakes:
  • Confusing pinMode() with digitalRead() or digitalWrite()
  • Thinking pinMode() reads or writes values
  • Assuming pinMode() resets the board
2. Which of the following is the correct syntax to set pin 7 as an output pin?
easy
A. pinMode(7, OUTPUT);
B. pinMode(OUTPUT, 7);
C. pinMode(7, "OUTPUT");
D. pinMode(7);

Solution

  1. Step 1: Recall pinMode() syntax

    The correct syntax is pinMode(pinNumber, mode); where mode is a constant like OUTPUT without quotes.
  2. Step 2: Check each option

    pinMode(7, OUTPUT); matches the correct syntax. pinMode(OUTPUT, 7); reverses parameters. pinMode(7, "OUTPUT"); uses quotes incorrectly. pinMode(7); misses the mode parameter.
  3. Final Answer:

    pinMode(7, OUTPUT); -> Option A
  4. Quick Check:

    pinMode(pin, mode) correct order = B [OK]
Hint: pinMode(pin, mode) with mode as constant, no quotes [OK]
Common Mistakes:
  • Swapping parameters order
  • Using quotes around OUTPUT
  • Omitting the mode parameter
3. What will be the output on the serial monitor after running this code?
void setup() {
  pinMode(3, INPUT_PULLUP);
  Serial.begin(9600);
  Serial.println(digitalRead(3));
}
void loop() {}
medium
A. No output
B. 0
C. Error: pinMode not set correctly
D. 1

Solution

  1. Step 1: Understand INPUT_PULLUP behavior

    Setting pin 3 as INPUT_PULLUP activates an internal pull-up resistor, so the pin reads HIGH (1) if not connected to ground.
  2. Step 2: digitalRead on pin 3

    Since nothing else is connected, digitalRead(3) returns 1, which is printed to the serial monitor.
  3. Final Answer:

    1 -> Option D
  4. Quick Check:

    INPUT_PULLUP reads HIGH = 1 [OK]
Hint: INPUT_PULLUP makes pin read HIGH if unconnected [OK]
Common Mistakes:
  • Expecting 0 instead of 1 for INPUT_PULLUP
  • Thinking pinMode causes error
  • Assuming no output without loop code
4. Identify the error in this code snippet:
void setup() {
  pinMode(13, "OUTPUT");
  digitalWrite(13, HIGH);
}
void loop() {}
medium
A. Pin number 13 is invalid.
B. Missing Serial.begin() in setup().
C. The mode parameter in pinMode() should not be in quotes.
D. digitalWrite() cannot be used in setup().

Solution

  1. Step 1: Check pinMode() parameter types

    The mode parameter must be a constant like OUTPUT without quotes. Using quotes makes it a string, causing a compile error.
  2. Step 2: Verify other parts

    Pin 13 is valid. digitalWrite() can be used in setup(). Serial.begin() is not required here.
  3. Final Answer:

    The mode parameter in pinMode() should not be in quotes. -> Option C
  4. Quick Check:

    pinMode mode no quotes = D [OK]
Hint: Use OUTPUT without quotes in pinMode() [OK]
Common Mistakes:
  • Putting mode in quotes
  • Thinking pin 13 is invalid
  • Believing digitalWrite() can't be in setup()
5. You want to connect a push button to pin 2 and read its state without an external resistor. Which pinMode() setting should you use to ensure the pin reads HIGH when the button is not pressed?
hard
A. pinMode(2, OUTPUT);
B. pinMode(2, INPUT_PULLUP);
C. pinMode(2, INPUT);
D. pinMode(2, INPUT_PULLDOWN);

Solution

  1. Step 1: Understand button wiring without external resistor

    Without an external resistor, the internal pull-up resistor must be enabled to keep the pin HIGH when the button is not pressed.
  2. Step 2: Choose correct pinMode()

    Using INPUT_PULLUP activates the internal pull-up resistor. INPUT alone leaves the pin floating. OUTPUT is wrong for reading. INPUT_PULLDOWN is not standard on Arduino.
  3. Final Answer:

    pinMode(2, INPUT_PULLUP); -> Option B
  4. Quick Check:

    Use INPUT_PULLUP for internal resistor = A [OK]
Hint: Use INPUT_PULLUP to avoid external resistor on button pin [OK]
Common Mistakes:
  • Using INPUT without pull-up resistor causes floating pin
  • Trying OUTPUT mode to read button
  • Assuming INPUT_PULLDOWN exists on all Arduino boards