This is only a preview of the December 2016 issue of Silicon Chip. You can view 45 of the 104 pages in the full issue, including the advertisments. For full access, purchase the issue for $10.00 or subscribe for access to the latest issues. Items relevant to "Automotive Sensor Modifier":
Items relevant to "Arduino-Based Digital Theremin":
Items relevant to "Voltage/Current Reference With Touchscreen, Pt.2":
Articles in this series:
Articles in this series:
Purchase a printed copy of this issue for $10.00. |
U s in g
Ch e a
A sianp
El e c t
Moduronic
P a r t le s
2
The HC-SR04
Ultrasonic Distance
Sensor Module
In the second article on cheap pre-built electronics modules, we’re
focusing on the HC-SR04 ultrasonic distance sensor module. We
describe how the module works and show how it can be used as a
hallway monitor or door sentry.
I
F THE HC-SR04 module shown in
the picture looks familiar, that’s because it has already been used in Geoff
Graham’s Ultrasonic Garage Parking
Assistant, published in the March
2016 issue. But this module doesn’t
have to be used with a microprocessor module like a Micromite or an Arduino, it can also be used with much
simpler circuitry, as we’ll see later.
Before we get to how it works, we
should note these ultrasonic sensor
modules have been around for about
six years, beginning life as an add-on
By JIM ROWE
“shield” for the Arduino. Since then,
they have gone through a number of iterations, all bearing the same HC-SR04
label but with various minor circuit
and component changes. We suspect
this has been due to various manufacturers working out ways of reducing
costs, rather than seeking to achieve
better performance.
The bottom line is that although
some of these slightly different HC-
SR04 modules are still being sold,
they all seem to function and perform
much the same. So don’t worry if the
module you buy looks a little different from that shown in the photos. The
odds are that if your module carries
the label HC-SR04, it will work just
like any other HC-SR04.
Current HC-SR04 modules are based
on a PCB measuring 45 x 20mm. On the
top side of the PCB are a pair of small
(16mm diameter) ultrasonic transducers with a 4MHz crystal between them.
All the components on the other
≈
Fig.1: one ultrasonic burst is sent out from the transmitter
transducer. The receiver transducer will detect this burst
if it is reflected off an object in front of the module. Once
detected by the receiver, an output pulse is produced with
a width in microseconds of (distance in cm) ÷ 0.01725.
82 Silicon Chip
Fig.2: there must be a delay of 60ms between trigger
pulses to prevent late echoes from affecting successive
readings.
siliconchip.com.au
Fig.3: complete circuit diagram for the HC-SR04 ultrasonic sensor module. When IC1 detects a TRIG pulse at pin 1, a
40kHz burst signal of eight pulses is generated at pins 13 and 14 of IC1. This is taken to pins 10 and 11 of IC3 respectively
and output at pins 7 and 14 connecting to the transmit transducer.
side of the PCB are surface-mount
types, apart from the 4-pin right-angle
header at bottom centre. Fig.1 shows
how it’s used. It sends out a burst of
ultrasonic energy from the transmitter transducer (the one marked T, on
the left) and then listens via the other
receiver transducer (marked R, on the
right) for any echo that may be reflected back from an object in front of the
module (see Fig.1).
If it detects this ultrasonic echo, it
produces an output pulse with a width
approximately proportional to the distance between the module’s sensors
and the object producing the echo.
The ultrasonic frequency used is
very close to 40kHz, roughly double
the highest frequency that can be heard
by human ears. The burst of transmitted energy consists of eight pulses at
40kHz, so the transmitted burst lasts
for only 200µs, as shown in Fig.2.
Since the speed of sound in air at
25°C and 100kPa (ie, 1 bar) is close to
345m/s (= 0.0345cm/µs) and the distance travelled by the ultrasonic burst
siliconchip.com.au
energy corresponds to double the distance between the transducers and the
reflecting object, we can calculate the
distance from the delay as follows:
distance in cm
µs)
= 0.0345 x echo pulse width (µ
2
= 0.01725 x echo pulse width (µ
µs)
As shown in Fig.2, each measurement cycle begins when a positive
trigger pulse of at least 10µs duration
is applied to the HC-SR04 module’s
trigger input pin. When the echo has
been detected, it then produces a pulse
at the echo output pin. Note that there
should be at least 60ms between trigger pulses, to prevent late echoes from
one cycle from causing false readings
on the next. So in practice, it’s a good
idea to limit the trigger pulse frequency to no more than 16Hz.
Circuit details
The full circuit for the HC-SR04
module is shown in Fig.3. It is based on
an EM78P153S microcontroller (IC1),
a low power 8-bit CMOS device made
by Elan Microelectronics in Hsinchu,
Taiwan. This device has a 1024 x 13
bits one-time programmable (OTP)
ROM plus 32 bytes of on-chip SRAM,
and comes in a 14-pin SOIC package.
It runs here with a 4MHz crystal between pins 5 and 6.
When a TRIG pulse arrives at pin 1
of IC1 (from pin 3 of CON1), the controller generates a 40kHz burst signal
of eight pulses at pins 13 and 14, with
one pin 180° out of phase with the
other. These go to pins 10 and 11 of
IC3, a bus driver IC very similar to the
MAX232. The outputs from IC3 (pins
7 and 14) connect across the transmitter transducer, effectively driving it in
bridge mode to emit the bursts of ultrasonic energy.
Echoes picked up by the receive
transducer pass through the four sections of IC2, an LM324 quad op amp.
These provide amplification, bandpass filtering and phase detection,
with the result that a received echo
December 2016 83
Fig.4: complete circuit for an ultrasonic intruder alarm using an HC-SR04 module. IC1a generates 60µs-wide trigger
pulses at 12Hz, which are fed to pin 3 of CON1. The echo pulses trigger monostable multivibrator IC2 and IC3a then
compares the width of the resulting pulse to the echo pulse. If these differ, LED1 lights and the piezo buzzer sounds.
pulse is fed back to pin 10 of IC1.
The micro then compares the timing
of the leading edge of this received
echo pulse with the leading edge of
the transmitted burst fed to IC3 and
the transmit transducer, and produces an echo output pulse at pin 2 with
its width equal to the time difference.
This echo output pulse appears at pin
2 of CON1.
How it’s used
If you want to use the HC-SR04
module to actually measure the distance to an object or wall in front of
it, the best way to do it is to hook it
up to a microprocessor module like
an Arduino, a Micromite or a Raspberry Pi. The micro’s program generates the trigger pulse to the HC-SR04,
then measures the length of the echo
pulse and calculates the corresponding distance.
There’s no need to worry about writing a program to do these tasks for
you, because many people have already produced programs to do this. A
quick search on the Arduino website
(www.arduino.cc) or by using Google will find a sample program for the
micro you’re using in short order.
If you want to use the HC-SR04 with
a Micromite, Geoff Graham has already
built a DISTANCE function into his
MMBasic programming language for
the Micromite family to make it really easy.
All you have to do to get the Micro84 Silicon Chip
mite to trigger the HC-SR04 and then
calculate the object distance from the
echo pulse is use this one-line function call:
d = DISTANCE(trig, echo)
Where “d” is the distance in centimetres, “trig” is the Micromite’s I/O
pin connected to the HC-SR04’s trigger input pin and “echo” is the I/O
pin connected to the HC-SR04’s echo
output pin.
The only extra step is to connect the
HC-SR04’s +5V and GND pins to the
corresponding pins of your Micromite.
If you want to display the result “d”
on an alphanumeric LCD, you can do
this using commands like:
LCD INIT ...
LCD 1, 2, “Distance = “
LCD 2, 6, STR$(d)
and so on.
You can get a good idea of what’s
involved in using the HC-SR04 with
a Micromite from Geoff Graham’s article describing the Ultrasonic Garage
Parking Assistant, in the March 2016
issue of Silicon Chip.
But say you want to use this module without a microcontroller at all.
That’s fairly straightforward, as we’ll
now demonstrate.
A simple intruder alarm
For example, to use it as an ultra-
sonic intruder alarm, have a look at
the circuit shown in Fig.4. It uses three
low cost CMOS ICs, a 2N7000 Mosfet,
three diodes, one LED, a piezo buzzer
and some passive components. This
circuit and the HC-SR04 operate from
a common 5V DC power supply, which
can be from a USB plugpack or USB
power bank.
IC1 is a hex Schmitt trigger inverter package and we’re using just two
sections of it, IC1a & IC1b. IC1a at
upper left is connected as a relaxation oscillator, to generate a stream
of 60µs-wide pulses at a frequency of
about 12Hz, ie, with a pulse spacing
of about 83ms. These form the trigger
pulses which are fed to the HC-SR04
via pin 3 of CON1.
The rest of the circuit monitors the
width of the echo pulses sent back
from the HC-SR04 via pin 2 of CON1.
If this varies significantly (indicating
that something has moved between
the sensor and the nearest object, like
This tiny active piezo transducer
module from Jaycar can be used in
the intruder alarm instead of the
piezo buzzer.
siliconchip.com.au
the opposite wall of your entry hall),
it sounds the alarm by switching on
LED1 and the piezo buzzer connected across it.
This section is a little more complex. First, the incoming echo pulse
passes through inverter IC1d, so that
its leading edge is negative-going. The
1nF capacitor and 100kΩ resistor then
form a differentiator circuit, which develops a narrow negative-going pulse
from the negative-going leading edge
of the inverted pulse.
This is then used to trigger IC2, a
7555 CMOS timer chip connected as
a one-shot multivibrator. When IC2
is triggered, its output pin 3 switches
high for a short time, determined by
the 2.2µF capacitor connected from
pins 6 and 7 to ground and the resistance connected between the same two
pins and the +5V line.
As shown, this resistance is the series combination of a 10kΩ resistor
and VR1, a 100kΩ pot. So by varying VR1, we can vary the width of the
pulse generated each time the one-shot
is triggered.
The output of IC2 is connected to
pin 2 of IC3a, one section of a 4070B
quad XOR (exclusive-OR) gate. The
echo pulses from the HC-SR04 are fed
to pin 1 of IC3, the second input of the
same XOR gate. Since the output of
an XOR gate is high only when one of
its inputs is high and the other low, it
forms a pulse width comparator.
Consider the situation where the
HC-SR04 sensor is facing a wall say
1.5m or 150cm away. The echo pulses
fed back from the sensor will be very
close to 8.7ms wide and these are fed
to input pin 1 of IC3a.
If we adjust VR1 so that IC2 also
produces 8.7ms wide pulses, since
they start at virtually the same instant
as the start of the echo pulse, both inputs of XOR gate IC3a will rise and
fall at the same time. As a result, the
output of IC3a (pin 3) will remain low
at all times.
But if someone moves in front of
the HC-SR04, this will cause the echo
pulses to shorten, because the ultrasonic energy reflected back by the person or object will be travelling over a
smaller distance. So the echo pulse
width will drop briefly to say 5-6ms,
and as a result the inputs of IC3a will
no longer be synchronised.
Although the pulses fed to pin 2
will still be high for 8.7ms, the echo
pulses being fed to pin 1 will drop
low after 5-6ms, so the output of IC3a
will switch high for the remaining 2.73.7ms. These positive-going pulses
will very quickly charge up the 1µF
capacitor in the gate circuit of Mosfet
Q1, via diode D3 and the 10kΩ series
resistor, and this will turn on Q1, causing LED1 to light and the piezo buzzer
to sound the alarm.
Then when the intruding person or
object moves away again and the echo
pulses return to their original width
of 8.7ms, the pulses fed to the two inputs of IC3a will be again be synchronised. There will be no more output
pulses from IC3a and the 1µF capacitor will be discharged by the 1MΩ resistor connected across it. So within
a couple of seconds, the buzzer and
LED will switch off.
The circuit is quite easy to set up,
too. All you need to do is wire it up
and connect it to the HC-SR04 module using a suitable length of 4-conductor cable. Then mount the sensor module on one side of the hall or
doorway to want to monitor, facing
either a wall or a large fixed object
such as a dresser, a chest of drawers
or a filing cabinet.
Next, set pot VR1 to its fully anticlockwise (ie, minimum resistance)
position and turn on the 5V power
Parts List
1 HC-SR04 ultrasonic sensor
(Jaycar XC4442)
1 active piezo transducer module
(Jaycar XC4424) OR
1 piezo buzzer
1 100kΩ trimpot (VR1)
Semiconductors
1 1N5819 diode (D1)
2 1N4148 diodes (D2)
1 LED, any colour (LED1)
1 2N7000 mosfet (Q1)
1 40106B or 74HC14 CMOS IC
(IC1)
1 LM7555 CMOS timer IC (IC2)
1 4070B quad XOR gate IC (IC3)
Capacitors (16V)
1 2.2µF
1 1µF
2 100nF
1 1nF
Resistors (0.25W, 5%)
1 2.2MΩ 1 1MΩ
2 10kΩ
1 1.5kΩ
1 100Ω
1 100kΩ
1 470Ω
supply. You’ll find that LED1 will
immediately light, and if you have a
piezo buzzer connected as well, it will
sound. That’s because the pulses being
generated by the one-shot IC2 will be
shorter than the echo pulses coming
from the HC-SR04.
Now slowly turn pot VR1 clockwise
until LED1 turns off and the piezo
buzzer goes silent. Your intruder
alarm will then be set up and ready
to detect the presence of a “foreign
body” in the space between the sensor and its reflecting wall. So we’ve
done all this without a microprocessor – apart from the EM78P153S micro inside the HC-SR04 sensor modSC
ule itself, of course.
Are Your S ILICON C HIP Issues
Getting Dog-Eared?
REAL
VALUE
AT
$16.95
*
PLUS P
&
P
Keep them safe, secure & always available
with these handy binders
Order now from www.siliconchip.com.au/Shop/4 or call (02) 9939 3295
and quote your credit card number. *See website for overseas prices.
siliconchip.com.au
December 2016 85
|