The pinMode() function in Arduino is a fundamental function used for configuring the behavior of digital pins on an Arduino board. It is an essential part of setting up and controlling the input and output capabilities of the pins, allowing you to interact with various external devices and components.
Here's a concise explanation of the pinMode() function:
Syntax: The pinMode() function is called with two parameters:
The first parameter specifies the pin number you want to configure.
The second parameter specifies the mode in which you want to set the pin, which can be either INPUT or OUTPUT.
Purpose:
When you set a pin to INPUT, it is configured to read digital signals or voltage levels. This is typically used when connecting sensors, buttons, or other devices that provide information to the Arduino.
When you set a pin to OUTPUT, it is configured to send digital signals, either high (5V) or low (0V). This is used when you want to control LEDs, motors, relays, or other output devices.
Example:
pinMode(13, OUTPUT); // Configures digital pin 13 as an OUTPUT
pinMode(2, INPUT); // Configures digital pin 2 as an INPUT
Usage:
You usually call pinMode() in the setup() function to configure the pin modes before your main Arduino program starts running.
Once you've set the pin mode, you can use digitalWrite() to write values to output pins (HIGH or LOW) and digitalRead() to read values from input pins (HIGH or LOW).
In summary, pinMode() is a crucial function in Arduino programming that defines how each digital pin should behave, whether it's for input or output, enabling you to interact with a wide range of electronic components and sensors in your Arduino projects.