digitalRead()

Description

Reads the value from a specified digital pin, either HIGH or LOW.

 

Syntax

digitalRead(pin)

 

Parameters

pin: the number of the digital pin you want to read (int)

 

Returns

HIGH or LOW

 

Example

 
int val = 0;     // variable to store the read value
void setup()
{
  pinMode(RED_LED, OUTPUT);      // sets the red LED as output
  Serial.begin(9600);
}

void loop()
{
  P1OUT = ~P1OUT;
  val = digitalRead(RED_LED); // read the output pin
  Serial.print(val);
}

 

Note

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.

 

See also

 

Reference Home