digitalRead()

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

Library Documentation

uint32 digitalRead(uint8 pin)

Read a digital value from a pin.

The pin must have its mode set to one of INPUT, INPUT_PULLUP, and INPUT_PULLDOWN.

Parameters:
  • pin -

    Pin to read from.

Return:

LOW or HIGH.

See:

pinMode()

Discussion

If the pin isn’t connected to anything, digitalRead() can return either HIGH or LOW (and this will change in a way that seems random).

Example

The following example turns the LED on or off when the button is pressed:

void setup() {
  pinMode(BOARD_LED_PIN, OUTPUT);
  pinMode(BOARD_BUTTON_PIN, INPUT);
}

void loop() {
  int val = digitalRead(BOARD_BUTTON_PIN);   // reads the input pin
  togglePin(BOARD_LED_PIN, val);
}

Arduino Compatibility

The Maple version of digitalRead() is compatible with Arduino.

See Also

License and Attribution

Portions of this page were adapted from the Arduino Reference Documentation, which is released under a Creative Commons Attribution-ShareAlike 3.0 License.