Hardware Reference
In-Depth Information
Infrared Distance Ranger Example
The Sharp GP2 xx series of infrared-rang-
ing sensors give a decent measurement
of short-range distance by bouncing an
infrared light signal off the target, and
then measuring the returned brightness.
They're very simple to use. Figure 8-2
shows a circuit for a Sharp GP2Y0A21
IR ranger, which can detect an object in
front of it within about 10cm to 80cm. The
sensor requires 5V power, and it outputs
an analog voltage from 0 to 5V, depending
on the distance to the nearest object in its
sensing area.
MATERIALS
» 1 Arduino module
» 1 Sharp GP2y0A21 IR ranger
» 1 10µF capacitor
» 3 male header pins
The Sharp sensors' outputs are not linear, so if you want
to get a linear range, you need to make a graph of the
voltage over distance, and do some math. Fortunately, the
good folks at Acroname Robotics have done the math for
you. For the details, see www.acroname.com/robotics/
info/articles/irlinear/irlinear.html . The sensor's datasheet
at http://www.sharpsma.com/webfm_send/1208
includes a graph of voltage over the inverse of the
distance. It shows a pretty linear relationship between the
two from about 10 to 80cm, and the slope of that line is
about 27V*cm. You can get a decent approximation of the
distance using that.
This sketch reads the sensor
and converts the results to a voltage.
Then, it uses the result explained above
to convert the voltage to a distance
measured in centimeters.
8
/*
Sharp GP2xx IR ranger reader
Context: Arduino
*/
void setup() {
// initialize serial communications at 9600 bps:
Serial.begin(9600);
}
The conversion formula gives only
an approximation, but it's accurate
enough for general purposes.
void loop() {
int sensorValue = analogRead(A0);
// convert to a voltage:
float voltage = map(sensorValue, 0, 5, 0, 1023);
For many applications, though, you
don't need the absolute distance, but
the relative distance. Is the person
nearer or farther away? Has she
passed a threshold that should trigger
some other interaction? For such
applications, you won't need this con-
version. You can just use the output
of the analogRead() command and
choose a value for your threshold by
experimentation.
// the sensor actually gives results that aren't linear.
// This formula is derived from the datasheet's graph
// of voltage over 1/distance. The slope of that line
// is approximately 27:
float distance = 27.0 /voltage;
// print the sensor value
Serial.print(distance);
Serial.println(" cm");
// wait 10 milliseconds before the next reading
delay(10);
}
Search WWH ::




Custom Search