by Jon Williams
Putting the Spotlight on BASIC Stamp Projects, Hints, and Tips
Measuring Up — Up to 80 Centimeters, That Is
Add a bit of intelligence to
your Halloween displays with
IR distance measuring.
Read Volts, Get Distance
The night is drawing closer ... my favorite night of
the whole year: Halloween. I love Halloween — the
costumes, haunted houses, parties, and friendly
exchanges with trick-or-treaters; Halloween is the best.
When I have the chance, something I like to do is build
Halloween-oriented props and decorations and you can
bet that many of those props get some sort of automation
via the BASIC Stamp microcontroller.
Good Halloween props add an element of surprise,
which, of course, intensifies the fright — and that’s the
most fun thing about Halloween, right? The only problem
is that, as a society, we are far more sophisticated than we
were in the past (especially the teenagers). We can easily
see through a cheesy effect and find the trigger, which
ruins the effect for those who immediately follow.
Instead of using a fixed-point trigger for an automated
prop, what if we used a distance measuring device so that
we could select a random trigger point? That would keep
‘em guessing, wouldn’t it? You bet! We’ve used sonic
measuring devices in the past (SRF-04 and SRF-08); this
time, we’ll do it with infrared. The device we’re going to
use is the low cost Sharp GP2D12.
There is no great mystery to using the GP2D12: We
simply connect it to an appropriate analog-to-digital
converter and read the output voltage. The voltage is then
converted to distance.
The first part is very easy. For this project, we’ll use
the ADC0831 analog-to-digital converter — a part we’ve
used before and should have no trouble with. In order to
simplify the project code, we’ll connect the wiper of a
mutli-turn pot to the Vref input of the ADC0831 and set
this to 2.55 volts. What this does for us is set each output
count to be equal to 0.01 volts (255 [max count] divided
by 2.55 [Vref] = 0.01 volts/count). Figure 1 shows the
schematic for the project.
Let’s have a look at the code that reads the voltage
from the ADC0831:
SHIFTIN AdcDta, AdcClk, MSBPOST, [result\9]
NUTS & VOLTS
This code is straightforward, but — if you haven’t
used the ADC0831 before — you may be wondering
why we need nine clocks for an eight-bit value. As
always, you should download the documentation for
any part you’re working with and, when you look at the
ADC0831 timing chart, you’ll see that the
ADC conversion is started by bringing the
CS (chip select) line low, then putting a
pulse on the clock line. Here’s where we get
the extra clock pulse. The value bits are
clocked out, MSB to LSB, with the following
eight clock pulses. Each ADC bit is valid
after the falling edge of the clock, so we use
MSBPOST to read the bits. Once all the bits
are clocked in, the device is deselected by
bringing the CS line back high.
Okay, that’s done, but what we’re
likely to run into is a bit of jitter in actual
application. An easy way to smooth this jitter
is to take the average of multiple readings.
Let’s do it:
Figure 1. ADC0831/GP2D12 connections.