07 Working with I/O Pins
pinMode()
Input/Output and Analog/Digital I/O Comparison
Pins in the Arduino controller can be configured as inputs or outputs. Additionally they can be either an analog or digital signal. The following table summarizes this variation and the commands used for each.
Configures the specified pin to behave either as an input or an output. See the description of digital pins for details on the functionality of the pins.
As of Arduino 1.0.1, it is possible to enable the internal pullup resistors with the mode INPUT_PULLUP. Additionally, the INPUT mode explicitly disables the internal pullups.
pinMode(pin, mode)
pin: the number of the pin whose mode you wish to set
mode: INPUT, OUTPUT, or INPUT_PULLUP.
Configuring Digital Pins
The pins on the Arduino can be configured as either inputs or outputs. This document explains the functioning of the pins in those modes. It is important to note that vast majority of Arduino analog pins, may be configured, and used, in exactly the same manner as digital pins.
Properties of Pins Configured as INPUT
Pins configured this way are said to be in a high-impedance state. Input pins make extremely small demands on the circuit that they are sampling, equivalent to a series resistor of 100 megohm in front of the pin.
Properties of Pins Configured as INPUT_PULLUP
Pins configured as pinMode(pin, INPUT) with nothing connected to them, or with wires connected to them that are not connected to other circuits, will report seemingly random changes in pin state, picking up electrical noise from the environment, or capacitively coupling the state of a nearby pin. For this reason it is often useful to steer an input pin to a known state if no input is present. There are 20K pullup resistors built into the Atmega chip that can be accessed from software. These built-in pullup resistors are accessed by setting the pinMode() as INPUT_PULLUP.
NOTE: Digital pin 13 is harder to use as a digital input than the other digital pins because it has the builtin LED attached. If you must use pin 13 as a digital input, set its pinMode() to INPUT and use an external pull down resistor.
Properties of Pins Configured as OUTPUT
Pins configured as OUTPUT with pinMode() are said to be in a low-impedance state. This means that they can provide a substantial amount of current to other circuits. Atmega pins can source (provide positive current) or sink (accept current) up to 40 mA (milliamps) of current to other devices/circuits. This is enough current to brightly light up an LED (don't forget the series resistor), or run many sensors, for example, but not enough current to run most relays, solenoids, or motors.
Short circuits on Arduino pins, or attempting to run high current devices from them, can damage or destroy the output transistors in the pin, or damage the entire Atmega chip.