The pinMode() function sets a pin on the Arduino as either an input or an output. This tells the board how to use that pin.
pinMode() function behavior in Arduino
Start learning this pattern below
Jump into concepts and practice - no test required
pinMode(pin, mode);
pin is the number of the pin you want to set.
mode can be INPUT, OUTPUT, or INPUT_PULLUP.
pinMode(13, OUTPUT);pinMode(7, INPUT);pinMode(2, INPUT_PULLUP);This program sets pin 13 as an output to control an LED and pin 7 as an input to read a button. When the button is pressed (HIGH), the LED turns on. When the button is not pressed, the LED turns off.
void setup() {
pinMode(13, OUTPUT); // Set pin 13 as output
pinMode(7, INPUT); // Set pin 7 as input
}
void loop() {
int buttonState = digitalRead(7); // Read the button state
if (buttonState == HIGH) {
digitalWrite(13, HIGH); // Turn LED on
} else {
digitalWrite(13, LOW); // Turn LED off
}
}Always call pinMode() in the setup() function before using the pin.
Using INPUT_PULLUP activates an internal resistor to keep the input stable without extra parts.
If you forget to set the pin mode, the pin may not work as expected.
pinMode() tells the Arduino how to use a pin: input or output.
Use INPUT to read signals, OUTPUT to send signals, and INPUT_PULLUP for buttons without extra resistors.
Always set pin modes in setup() before using pins in your program.
Practice
pinMode() function do in an Arduino sketch?Solution
Step 1: Understand the purpose of pinMode()
ThepinMode()function tells the Arduino whether a pin will be used to read signals (input) or send signals (output).Step 2: Differentiate from other functions
Reading values is done bydigitalRead(), writing bydigitalWrite(), and resetting is unrelated topinMode().Final Answer:
It sets a pin as input or output to control how it behaves. -> Option AQuick Check:
pinMode() sets pin direction = C [OK]
- Confusing pinMode() with digitalRead() or digitalWrite()
- Thinking pinMode() reads or writes values
- Assuming pinMode() resets the board
Solution
Step 1: Recall pinMode() syntax
The correct syntax ispinMode(pinNumber, mode);where mode is a constant like OUTPUT without quotes.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.Final Answer:
pinMode(7, OUTPUT); -> Option AQuick Check:
pinMode(pin, mode) correct order = B [OK]
- Swapping parameters order
- Using quotes around OUTPUT
- Omitting the mode parameter
void setup() {
pinMode(3, INPUT_PULLUP);
Serial.begin(9600);
Serial.println(digitalRead(3));
}
void loop() {}Solution
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.Step 2: digitalRead on pin 3
Since nothing else is connected, digitalRead(3) returns 1, which is printed to the serial monitor.Final Answer:
1 -> Option DQuick Check:
INPUT_PULLUP reads HIGH = 1 [OK]
- Expecting 0 instead of 1 for INPUT_PULLUP
- Thinking pinMode causes error
- Assuming no output without loop code
void setup() {
pinMode(13, "OUTPUT");
digitalWrite(13, HIGH);
}
void loop() {}Solution
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.Step 2: Verify other parts
Pin 13 is valid. digitalWrite() can be used in setup(). Serial.begin() is not required here.Final Answer:
The mode parameter in pinMode() should not be in quotes. -> Option CQuick Check:
pinMode mode no quotes = D [OK]
- Putting mode in quotes
- Thinking pin 13 is invalid
- Believing digitalWrite() can't be in setup()
pinMode() setting should you use to ensure the pin reads HIGH when the button is not pressed?Solution
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.Step 2: Choose correct pinMode()
UsingINPUT_PULLUPactivates the internal pull-up resistor.INPUTalone leaves the pin floating.OUTPUTis wrong for reading.INPUT_PULLDOWNis not standard on Arduino.Final Answer:
pinMode(2, INPUT_PULLUP); -> Option BQuick Check:
Use INPUT_PULLUP for internal resistor = A [OK]
- Using INPUT without pull-up resistor causes floating pin
- Trying OUTPUT mode to read button
- Assuming INPUT_PULLDOWN exists on all Arduino boards
