Hardware Reference
In-Depth Information
Figure 4-4.
The pulldown resistor in a digital input circuit ensures that
there's a connection to ground when the connection between 5 volts and
the input pin is broken.
Example 4-1. Basic digital input sketch
int
switchInputPin
=
2
;
void
setup
()
{
pinMode
(
switchInputPin
,
INPUT
);
//
Serial
.
begin
(
9600
);
}
void
loop
()
{
int
switchState
=
digitalRead
(
switchInputPin
);
//
if
(
switchState
==
HIGH
)
{
//
Serial
.
println
(
"The switch is on!"
);
//
}
else
{
//
Serial
.
println
(
"The switch is off!"
);
//
}
delay
(
500
);
//
}
Set the
switchInputPin
(pin 2) as an input.
Read the state of
switchInputPin
and store it in the variable
switchState
.