Hardware Reference
In-Depth Information
8
This sketch is similar to the
infrared ranging sketch in Project 15.
It reads the sensor and converts the
results to a voltage, then converts that
to a distance measured in centimeters.
The conversion formula again gives
only an approximation.
/*
MaxBotix LV-EZ1 ultrasonic ranger reader
Context: Arduino
*/
void setup() {
// initialize serial communications at 9600 bps:
Serial.begin(9600);
}
The MaxBotix sensors can read only
every 50 milliseconds, so you need a
delay after each read to give the sensor
time to stabilize before the next read.
void loop() {
// read the sensor value and convert to a voltage:
int sensorValue = analogRead(A0);
float voltage = map(sensorValue, 0, 5, 0, 1023);
// the sensor's output is about 9.8mV per inch,
// so multiply by 2.54 to get it in centimeters:
float distance = voltage * 2.54 / 0.0098;
Figure 8-3
at left, opposite page
Measuring distance in two dimen-
sions using ultrasonic distance
rangers. The square in each drawing
is a 4m × 4m floor plan of a room.
In order to cover the whole of a
rectangular space, you need several
sensors placed around the sides of
the room.
// print the sensor value
Serial.print(distance);
Serial.println(" cm");
// wait 50 milliseconds before the next reading
// so the sensor can stabilize:
delay(50);
}
Figure 8-4
at right, opposite page
MaxBotix LV-EZ1 ultrasonic sensor
connected to an Arduino module.
AREF
GND
D13
D12
D11/PWM
D10/PWM
D9/PWM
D8
Reset
+3.3V
Figure 8-5
Schematic for MaxBotix
LV-EZ1 ultrasonic sensor
connected to an Arduino
module.
+5V
Gnd
Gnd
Vin
BW
PW o u t
Analo g out
RX
TX
Arduino
Module
+5V
GND
D7
D6/PWM
D5/PWM
D4
D3/PWM
D2
Digital1/TX
Digital0/RX
Analog0
A1
A2
A3
A4
A5
 
Search WWH ::




Custom Search