pinMode()

Library Documentation

void pinMode(uint8 pin, WiringPinMode mode)

Configure behavior of a GPIO pin.

Parameters

  • pin -

    Pin to configure. One of: 0-38 (pin numbers as labeled on silkscreen), or D0-D38 (symbols for same)

  • mode -

    Mode corresponding to desired pin behavior.

See
WiringPinMode

WiringPinMode enum

Specifies a GPIO pin behavior.

Each of the GPIO pins on a Maple board may be configured using pinMode() to behave in a number of ways: as a digital output pin, or as an analog input pin, etc., depending on the particular pin.

This enum specifies the complete set of possible configurations; not every pin can have all of these modes. For example, on the Maple, pin 15 may be configured as INPUT_ANALOG, but not as PWM. See your device’s silkscreen and and the GPIO documentation for more information.

See
pinMode()

Values:

  • OUTPUT -

    Basic digital output: when the pin is HIGH, the voltage is held at +3.3v (Vcc) and when it is LOW, it is pulled down to ground.

  • OUTPUT_OPEN_DRAIN -

    In open drain mode, the pin indicates “low” by accepting current flow to ground and “high” by providing increased impedance.

    An example use would be to connect a pin to a bus line (which is pulled up to a positive voltage by a separate supply through a large resistor). When the pin is high, not much current flows through to ground and the line stays at positive voltage; when the pin is low the bus “drains” to ground with a small amount of current constantly flowing through the large resistor from the external supply. In this mode no current is ever actually /sourced/ from the pin.

  • INPUT -

    Basic digital input.

    The pin voltage is sampled; when it is closer to 3.3v (Vcc) the pin status is high, and when it is closer to 0v (ground) it is low. If no external circuit is pulling the pin voltage to high or low, it will tend to randomly oscillate and be very sensitive to noise (e.g., a breath of air across the pin might cause the state to flip).

  • INPUT_ANALOG -

    This is a special mode for when the pin will be used for analog (not digital) reads.

    Enables ADC conversion to be performed on the voltage at the pin.

  • INPUT_PULLUP -

    The state of the pin in this mode is reported the same way as with INPUT, but the pin voltage is gently “pulled up” towards +3.3v.

    This means the state will be high unless an external device is specifically pulling the pin down to ground, in which case the “gentle” pull up will not affect the state of the input.

  • INPUT_PULLDOWN -

    The state of the pin in this mode is reported the same way as with INPUT, but the pin voltage is gently “pulled down” towards 0v.

    This means the state will be low unless an external device is specifically pulling the pin up to 3.3v, in which case the “gentle” pull down will not affect the state of the input.

  • INPUT_FLOATING -

    Synonym for INPUT.

  • PWM -

    This is a special mode for when the pin will be used for PWM output (a special case of digital output).

  • PWM_OPEN_DRAIN -

    Like PWM, except that instead of alternating cycles of LOW and HIGH, the voltage on the pin consists of alternating cycles of LOW and floating (disconnected).

Discussion

pinMode() is usually called within setup() in order to configure a pin for a certain usage (although it may be called anywhere).

Example

int ledPin = 13;                 // LED connected to digital pin 13

void setup() {
  pinMode(ledPin, OUTPUT);      // sets the digital pin as output
}

void loop() {
  digitalWrite(ledPin, HIGH);   // sets the LED on
  delay(1000);                  // waits for a second
  digitalWrite(ledPin, LOW);    // sets the LED off
  delay(1000);                  // waits for a second
}

Arduino Compatibility

The libmaple implementation of pinMode() supports the INPUT and OUTPUT modes with semantics identical to that of the Arduino function (however, be advised that the Maple, as a 3.3V device, will only drive 3.3V to an OUTPUT pin that has been set HIGH).

INPUT_ANALOG and PWM modes were added because the Maple does not distinguish between analog and digital pins the same way the Arduino does. Unlike the Arduino, you must call pinMode() to set up a pin for these purposes before a call to, e.g., analogRead(). In practice, this should only add a few lines to your setup() function.

See also

License and Attribution

This documentation page was adapted from the Arduino Reference Documentation, which is released under a Creative Commons Attribution-ShareAlike 3.0 License.