Controlling Arduinos with Sensors
The packets in this section show you how to use a variety of sensors
as inputs to an Arduino, in order to control one or more output devices.
The first two sensors introduced in this section, a potentiometer
and a light sensor, are both “analog” sensors.
That is instead of producing values which are either
LOW (0V) or
like the reading from a switch, they can produce values over the entire
range from 0V to 5V. The input from these “analog” devices cannot be read
on the digital I/O pins of the Arduino. Instead, they must be used on one
of the analog I/O pins labelled
Your sketch will read the value of the voltage produced by one of these
sensors using the arduino function
analogRead(), for example:
int PIN = 0; this will be interpreted as `A0` by analogRead() int vIn = analogRead(PIN);
analogRead() will output a value between 0 and 1023.
A packet Passive InfraRed Sensors describes how these devices are used
to detect motion. These devices produce digital output, i.e., their
signal pin goes
HIGH when motion is detected; they are therefore
digital, not analog sensors. Typically, PIR sensors detect motion within
about 20 feet, but they can be adjusted to be more or less sensitive
Packet ultrasonic-sensor shows how to use an ultrasonic sensor
to detection the distance to the object in front of it. This sensor
not an analog sensor, nor is it a digital sensor like the PIR. Instead,
this sensor produces a pulse (a transition from
by a subsequent transition back from
LOW) whose duration
is used to infer the target’s distance. Using this sensor requires the
introduction of a new, special-purpose arduino function,
Using the Sensors to Control Something!
In most of the examples in this section, the value from a sensor is simply used to
control the brightness or blink rate of an LED. More complicated applications will
be presented in the sections on Sound, and Motors.