digitalRead()

Description

Reads the value from a specified digital pin, either

HIGH
or
LOW
.

Syntax

Use the following function to read the value of a digital pin:

digitalRead(pin)

Parameters

The function admits the following parameter:

pin
: the Arduino pin number you want to read.

Returns

The function returns the boolean state of the read pin as

HIGH
or
LOW
.

Example Code

Control the Arduino built-in LED on pin 13 (output) by assigning the same value of a push button connected to pin 7 (input).

1int ledPin = 13; // LED connected to digital pin 13
2int inPin = 7; // pushbutton connected to digital pin 7
3int val = 0; // variable to store the read value
4
5void setup() {
6 pinMode(ledPin, OUTPUT); // sets the digital pin 13 as output
7 pinMode(inPin, INPUT); // sets the digital pin 7 as input
8}
9
10void loop() {
11 val = digitalRead(inPin); // read the input pin
12 digitalWrite(ledPin, val); // sets the LED to the button's value
13}

Notes and Warnings

If the pin isn’t connected to anything,

digitalRead()
can return either
HIGH
or
LOW
(and this can change randomly).

The analog input pins can be used as digital pins, referred to as A0, A1, etc. The exception is the Arduino Nano, Arduino Pro Mini, and Arduino Mini’s A6 and A7 pins, which can only be used as analog inputs.

See also

Suggest changes

The content on docs.arduino.cc is facilitated through a public GitHub repository. If you see anything wrong, you can edit this page here.

License

The Arduino documentation is licensed under the Creative Commons Attribution-Share Alike 4.0 license.