In this chapter, we explore the Digital I/O functions in Arduino — essential tools to interact with external devices like LEDs, buttons, or sensors. By the end, you’ll know how to configure pins as inputs or outputs, control devices using digitalWrite(), and read signals with digitalRead().
Syntax Table: Digital I/O Functions in Arduino
Topic | Syntax | Simple Example |
pinMode() | pinMode(pin, mode); | pinMode(13, OUTPUT); |
digitalWrite() | digitalWrite(pin, value); | digitalWrite(13, HIGH); |
digitalRead() | int state = digitalRead(pin); | int buttonState = digitalRead(7); |
pinMode() (Set Pin Mode) in Arduino
The pinMode() function in Arduino is used to configure a specific pin to behave either as an input or output. This is an essential function when interfacing with external devices like LEDs, buttons, or sensors, as it defines how the pin will interact with those devices. Without setting the mode of a pin, it will not operate correctly as an input or output.
Use purpose
The pinMode() function is used for:
- Setting pins as inputs: When reading data from sensors, buttons, or switches.
- Setting pins as outputs: When controlling devices such as LEDs, motors, or relays.
- Controlling input/output behavior: Ensures that the pin behaves correctly based on the intended interaction with external components.
Arduino Syntax use
pinMode(pin, mode);
Arduino Syntax Explanation
- pin: The number of the pin you want to configure.
- mode: The mode in which you want the pin to operate. It can be:
- INPUT: Configures the pin to read data (e.g., from a sensor or button).
- OUTPUT: Configures the pin to send data (e.g., to an LED or motor).
- INPUT_PULLUP: Configures the pin as an input with an internal pull-up resistor, which is useful for button inputs.
Arduino code Example
int ledPin = 13; // Pin connected to an LED
int buttonPin = 7; // Pin connected to a button
void setup() {
// Set pin modes
pinMode(ledPin, OUTPUT); // Set pin 13 as output for the LED
pinMode(buttonPin, INPUT); // Set pin 7 as input for the button
}
void loop() {
// Example logic to control the LED based on the button press
if (digitalRead(buttonPin) == HIGH) {
digitalWrite(ledPin, HIGH); // Turn the LED on
} else {
digitalWrite(ledPin, LOW); // Turn the LED off
}
}
Code Explanation
- Pin 13 is configured as an output using pinMode(ledPin, OUTPUT), which means it can send signals (e.g., turn on or off an LED).
- Pin 7 is configured as an input using pinMode(buttonPin, INPUT), allowing it to read the state of a button (whether it is pressed or not).
- In the loop() function, digitalRead(buttonPin) checks the state of the button. If the button is pressed (HIGH), the LED on pin 13 is turned on (HIGH). If not, the LED is turned off (LOW).
Notes
- Default state: If you don’t set a pin mode using pinMode(), the default is usually INPUT, but it’s a good practice to always specify it.
- INPUT_PULLUP: You can use the internal pull-up resistor by setting the mode to INPUT_PULLUP, which eliminates the need for an external pull-up resistor when reading inputs like buttons.
- Modes must be set in setup(): Pin modes should be configured in the setup() function to ensure they are correctly set before the loop starts.
digitalWrite() (Write Digital Value to Pin) in Arduino
The digitalWrite() function in Arduino is used to set a digital pin either HIGH or LOW, which corresponds to sending a voltage (HIGH = 5V or 3.3V, depending on the board) or grounding the pin (LOW = 0V). It is commonly used to control external components like LEDs, motors, or relays by turning them on or off. This function is essential for interacting with devices connected to Arduino’s digital I/O pins.
Use purpose
The digitalWrite() function is used for:
- Controlling output devices: Used to turn devices like LEDs, motors, or buzzers on or off by sending a digital signal.
- Triggering digital components: Allows you to send high or low signals to actuators, relays, and other hardware components.
- Manipulating pin states: Easily set a digital pin’s state for use in a variety of applications, such as toggling lights or controlling logic gates.
Arduino Syntax use
digitalWrite(pin, value);
Arduino Syntax Explanation
- pin: The number of the pin you want to set.
- value: The digital value to write to the pin, which can be either:
- HIGH: Sets the pin to 5V (or 3.3V on some boards).
- LOW: Sets the pin to 0V (ground).
Arduino code Example
int ledPin = 13; // Pin connected to an LED
void setup() {
pinMode(ledPin, OUTPUT); // Set pin 13 as an output
}
void loop() {
digitalWrite(ledPin, HIGH); // Turn the LED on
delay(1000); // Wait for 1 second
digitalWrite(ledPin, LOW); // Turn the LED off
delay(1000); // Wait for 1 second
}
Code Explanation
- The pinMode() function is used in setup() to set pin 13 as an output for the LED.
- In the loop() function, digitalWrite(ledPin, HIGH) turns the LED on by applying 5V to pin 13, and digitalWrite(ledPin, LOW) turns it off by grounding the pin.
- The delay(1000) function is used to create a 1-second delay between turning the LED on and off, resulting in a blinking effect.
Notes
- Must set pin mode: Always configure the pin as an output using pinMode(pin, OUTPUT) before using digitalWrite(). Failing to do so may result in unexpected behavior.
- Use for digital signals only: digitalWrite() can only be used for digital pins and sends either a HIGH or LOW signal (no intermediate values).
- Slow on some boards: On some older boards, digitalWrite() can be slow for time-sensitive applications. In such cases, consider using direct port manipulation for faster control, though this is more complex.
digitalRead() (Read Digital Value from Pin) in Arduino
The digitalRead() function in Arduino is used to read the digital state (HIGH or LOW) of a specified digital pin. This function allows the Arduino to detect whether a pin is receiving a HIGH signal (5V or 3.3V, depending on the board) or a LOW signal (0V or ground). It is commonly used to read inputs from devices such as buttons, switches, or sensors.
Use purpose
The digitalRead() function is used for:
- Reading input signals: To detect the state of a digital input device such as buttons, switches, or sensors.
- Controlling logic based on pin state: Trigger specific actions or behaviors in your code depending on the HIGH or LOW state of an input pin.
- Interfacing with digital components: It helps in gathering real-time data from connected components and making decisions based on this data.
Arduino Syntax use
int state = digitalRead(pin);
Arduino Syntax Explanation
- pin: The digital pin number from which you want to read the state.
- digitalRead() returns:
- HIGH: If the pin is receiving a high signal (5V or 3.3V).
- LOW: If the pin is connected to ground (0V).
Arduino code Example
int buttonPin = 7; // Pin connected to a button
int ledPin = 13; // Pin connected to an LED
void setup() {
pinMode(buttonPin, INPUT); // Set pin 7 as an input
pinMode(ledPin, OUTPUT); // Set pin 13 as an output
}
void loop() {
int buttonState = digitalRead(buttonPin); // Read the state of the button
if (buttonState == HIGH) {
digitalWrite(ledPin, HIGH); // Turn the LED on if the button is pressed
} else {
digitalWrite(ledPin, LOW); // Turn the LED off if the button is not pressed
}
}
Code Explanation
- Pin 7 is configured as an input using pinMode(buttonPin, INPUT) to read the state of the button.
- Pin 13 is set as an output to control the LED using pinMode(ledPin, OUTPUT).
- The digitalRead(buttonPin) function checks the state of the button (whether it is pressed or not). If the button is pressed (HIGH), the LED is turned on using digitalWrite(ledPin, HIGH). Otherwise, the LED is turned off.
Notes
- Pull-up/pull-down resistors: If you are reading a button or switch, you often need a pull-up or pull-down resistor to ensure the pin doesn’t “float” and produce unreliable readings. Alternatively, you can use INPUT_PULLUP in pinMode() to enable the internal pull-up resistor.
- Must set pin mode: The pin you want to read must be configured as an input using pinMode(pin, INPUT) or pinMode(pin, INPUT_PULLUP) before using digitalRead().
- Only for digital pins: digitalRead() works on pins that are configured to handle digital input. For analog signals, use analogRead().
Common Problems and Solutions
- Pin Mode Not Set
Problem: Forgetting to set the pin mode with pinMode() results in unexpected behavior.
Solution: Always configure pin modes in the setup() function. - Floating Input Pins
Problem: Floating pins can produce unreliable readings.
Solution: Use INPUT_PULLUP mode for stable input readings. - Slow Performance
Problem: digitalWrite() can be slow on some older boards.
Solution: Use direct port manipulation for faster performance.
Chapter Summary
- pinMode() configures a pin as input, output, or INPUT_PULLUP for working with LEDs, motors, or sensors.
- digitalWrite() sends a digital signal (either HIGH or LOW) to control devices.
- digitalRead() checks the state of a pin (HIGH or LOW) for reading inputs like buttons or switches.
- These functions are the backbone of controlling external devices in Arduino.
FAQ
- What does pinMode() do in Arduino?
pinMode() sets a pin as either input or output, allowing interaction with external devices. - How does digitalWrite() work?
digitalWrite() sends either a HIGH or LOW signal to a pin to turn devices on or off. - How is digitalRead() used in Arduino?
digitalRead() checks whether a pin is receiving a HIGH or LOW signal.
Simple MCQ Questions and Answers
- What is the role of pinMode()?
a) Reads pin value
b) Configures pin as input or output
c) Toggles the pin state
Answer: b) Configures pin as input or output - What does digitalWrite() do?
a) Reads input value
b) Sends HIGH or LOW signal
c) Configures the pin mode
Answer: b) Sends HIGH or LOW signal - What does digitalRead() return?
a) Pin mode
b) Analog signal
c) HIGH or LOW signal of the pin
Answer: c) HIGH or LOW signal of the pin