Hardware Reference
In-Depth Information
Examples of digital inputs are switches, like push buttons or contact switches.
They are either on or off; there are no values in between.
pinMode()
Before using a pin as a digital input or output, you must i rst coni gure the pin,
which is done with
pinMode()
.
pinMode()
uses two parameters:
pin
and
mode
.
pinMode(pin, mode)
The pin parameter is simply the digital pin number you want to set. The mode
parameter is one of three constants:
INPUT
,
OUTPUT
, or
INPUT_PULLUP
. The
INPUT
and
OUTPUT
constants set the pin to be a digital input or output, respectively.
The
INPUT_PULLUP
constant sets the selected pin to become a digital input but
also connects an internal resistor to keep the input level at a logical one if there
is no input value.
By default, all digital pins are coni gured as
INPUT
, but it's considered best
practice to explicitly declare the
pinMode()
.
INPUT
Pins coni gured as
INPUT
can read voltage applied to them. It takes only a small
amount of current to change an
INPUT
pin's state. The drawback to this is that
pins coni gured as
INPUT
with nothing connected to them are more likely to
change state due to electronic interference like static discharges. It is useful to
use a pull-down resistor (going to ground) when connecting a switch to a pin
coni gured as
INPUT
. Ten kilohm is a good resistor value for this.
INPUT
pins are good at reading logical inputs but cannot be used to input,
or
sink
, any current. For example, you cannot use an
INPUT
pin to sink current
from an LED.
OUPUT
Pins coni gured as
OUTPUT
are capable of delivering power to circuits, up to
40 mA. This is more than enough to power an LED but is not enough to power
motors. Output pins cannot read sensors. Connecting output pins directly to 5
volts or 0 volts can damage the pin.
INPUT_PULLUP
Pins coni gured as
INPUT_PULLUP
are coni gured as output, but with an internal
pull-up resistor connected. On most Arduino boards this internal resistor is at
least 20 kilohms. This has the effect of setting the input value to HIGH if it is
pulled to ground, and LOW if voltage is applied.
digitalRead()
In order to read the state of a digital pin, you must use
digitalRead()
:
Search WWH ::
Custom Search