Building a Sonar System
SCREENSHOT 1. This is a look at
a LV-MaxSonar-EZ0 ranging pulse
that is emitted from the ultrasonic
rangefinder’s PW pin. This pulse
happens to be 9562.3 μS wide.
With 147 μS representing one
inch, this pulse equates to
65.0496 inches to the target,
which happens to be the coffee
tabletop to ceiling distance.
is ready to process input on its
RX pin. Recall that if the RX pin
is left open or forced logically
high, the ranging process will
begin and continue until the RX
pin is pulled logically low. With
that, let’s run a scenario with
the RX pin open at power-up
plus 250 ms.
With the RX pin open or
forced logically high following
the setup time, the first ranging operation will be a 49 ms
calibration cycle. The next ranging operation will be the first
ranging operation that will report ranging data to the I/O
interface. Thus, the very first ranging data will appear at
the EZ0’s data portal 98 ms past the 250 ms power-up
setup period. All subsequent ranging operations will
consume 49 ms each. What this all means is that the
LV-MaxSonar-EZ0 can perform a ranging operation every
49 ms. If your application requires control of the ranging
process, the EZ0 will scan the RX line at the end of every
ranging cycle. This allows you to force the RX I/O pin
logically low and take control of the scheduling of the
subsequent ranging cycles.
Each ranging cycle is initiated by a logical high level at
the RX I/O pin. Thirteen 42 kHz waves are transmitted at
the beginning of a ranging cycle. After the 13 waves have
been sent, the PW I/O pin is pulled to a logically high
level. The PW pin will go logically low when a target object
is detected. As you would expect, the maximum length
of a PW pulse is 37. 5 ms, or the equivalent of just over
255 inches. The maximum ranging distance of the
LV-MaxSonar-EZ0 is 254 inches.
The 37. 5 ms pulse width will occur when no target
objects are detected. Assuming we didn’t detect a target
object, we still have 11. 5 ms of time left in the ranging
cycle; 6. 8 ms of the remaining ranging cycle time is used
to adjust the analog voltage that will appear on the AN
pin to the correct level. We still have 4. 7 ms left. At this
point, the EZ0 has presented its pulse width ranging data
and its analog ranging data to the I/O interface. The
RS-232 ranging data is all that’s left to present. The
serial ranging data is sent during the final 4. 7 ms of the
ranging cycle.
To guarantee successful ranging operations, all we have
to do is make sure that there are no targets closer than
seven inches to the ultrasonic rangefinder during its
calibration time. Also, the EZ0 is not an outdoor cat. So,
we must be sure to keep it out of harm’s way as far as
weather goes. We now have enough information to begin
writing some interface code. I’m going to write the EZ0
driver in C using HI-TECH PICC- 18 and I’ll target the
PIC18F2620. Before we start writing code and assembling
hardware, we can use my CleverScope to check out the
LV-MaxSonar-EZ0’s pulse ranging data mechanism.
Ranging with a CleverScope
Let’s use our human eyes to interpret the EZ0 ranging
pulse I captured in Screenshot 1. The pulse width as
measured by the CleverScope extents is 9562.3 μs. A bit
of simple math will yield the distance from my coffee
tabletop to the ceiling:
9562.3 μs/147 μs per inch = 65.0496 inches
This is an easy way to receive instant gratification from
a ultrasonic rangefinder. However, to make the ranging
information work for us, we must employ the resources of
a microcontroller. My rangefinder support hardware is
visually obtainable in Schematic 2. As you can see, I’ve tied
the LV-MaxSonar ultrasonic rangefinder’s PW output pin to
the PIC18F2620’s CCP1 capture pin.
Ranging with a PIC18F2620
We need to electronically measure the pulse width
presented to the PIC18F2620’s CCP1 capture input. The
algorithm is simple and so is the code. We must set up the
PIC18F2620 capture engine to trigger an interrupt on the
rising edge of the PW ranging signal. Meanwhile, TIMER1 is
running free with a period of 1 μs. Thus, a count is supplied
to the CCP1 holding registers every microsecond. The
1 μs TIMER1 period is a direct result of us running the
PIC18F2620 system clock at 4 MHz. I programmatically
SERVO 06.2008 45