Digital I/O Functions
Digital I/O functions on Arduino UNO are
• pinMode()
• digitalRead()
• digitalWrite()
pinMode():-
• Function: Configures a pin to be an INPUT or OUTPUT.
• Syntax: pinMode(pin, mode);
• pin – The pin could be a number or variable with a value ranging from 0 to 13 or A0 to A5
• mode - Specifies whether the pin is an INPUT, OUTPUT, or INPUT_PULLUP
• Returns: None
• Example: pinMode(13, OUTPUT); configures pin 13 as an output.
Swarnalatha G.L, Assistant Professor, CSE dept, SSIT 52
Digital I/O Functions (contd.)
digitalRead():-
• Function: With a digital pin configured as an INPUT, it is used to read the state of that
pin.
• Syntax: digitalRead(pin);
• pin - The number of the digital pin (0 – 13) to read
• Returns: HIGH if the pin is at the higher voltage level, LOW if at the lower voltage
level.
• Example: int value = digitalRead(2); reads the value from digital pin 2 and stores it in
the variable value.
digitalWrite():-
• Function: Once a digital pin is established as an OUTPUT, it is used to write a
specified value to that pin.
• Syntax: digitalWrite(pin, value);
• pin - The number of the digital pin (0 – 13) to write to
• value – The value specifies HIGH or LOW
• Returns: None
• Example: digitalWrite(13, HIGH); sets digital pin 13 to a HIGH state, turning ON an
LED connected to it.
Swarnalatha G.L, Assistant Professor, CSE dept, SSIT 53
Digital I/O Functions (contd.)
Example code: Reads a pushbutton connected to a digital input and turns ON/OFF an LED connected to a
digital output with the value read from pushbutton.
Components
• Arduino UNO R3 board and USB cable – 1
• Bread board – 1
• LED – 1
• Pushbutton – 1
• Resistor 220Ω - 2
• Jumper wires
#define led 13 // connect LED to pin 13
#define pin 7 // connect pushbutton to pin 7
int value = 0; // variable to store the read value
void setup() {
pinMode(led, OUTPUT); // set pin 13 as output
pinMode(pin, INPUT); // set pin 7 as input
}
void loop() {
value = digitalRead(pin); // reads the value from pin 7 and stores it in the variable value.
digitalWrite(led, value); // set LED to the pushbutton value
}
Swarnalatha G.L, Assistant Professor, CSE dept, SSIT 54
Digital I/O Functions (contd.)
Swarnalatha G.L, Assistant Professor, CSE dept, SSIT 55