Fork me on GitHub

Project Notes

#287 UltrasonicAlarm

Build an ultrasonic motion alarm with the HC-SR04 module and discrete logic.

Here’s a quick demo of it in action:



The HC-SR04 Ultrasonic Ranging Module is commonly used with a microcontroller for obstacle detection and other autonomous control functions.

But that doesn’t mean it can’t be used in circuits that lack a processor. Silicon Chip featured an article by Jim Rowe in the Dec-2016 edition that described Using An Ultrasonic Sensor Module As A Door Sentry.

It’s quite ingenious:

  • a free-running inverter-based oscillator provides a steady stream of trigger pulses
  • echo pulse triggers a 555 timer monostable
  • the 555 timer produces a calibration pulse, duration is set by R5 potentiometer
  • the XOR compares the echo pulse with the calibration pulse and goes high when these are different: a pulse-width comparator

How it’s used:

  • place the unit, and adjust the calibration pulse timing for a null output
  • then whenever anything increases of decreases the distance measurement, the alarm goes off

This project is my adaptation of the Silicon Chip article. It includes some simplification and component replacements.

HC-SR04 Basics

The HC-SR04 is a pretty smart and independant module, with a very simple control interface.

  • to trigger a range test, send a Trigger TTL pulse >10µs
  • the unit runs the test independantly
  • echo pulse is produced, with a duration that is proportional to the distance. Distance = (high level time × velocity of sound)/2

Note there is no explicit relationship between when the trigger pulse is sent and when the echo pulse is returned. They can be treated independantly.

The module uses TCT40-16T “air ultrasonic ceramic transducers” that operate at 40kHz.

Trigger Signal

The 74LS14 schmitt inverter is used as a free-running oscilator. The LS variant I’m using here is not great with high resistance values in an oscillator configuration - see LEAP#022 SchmittOscillator for more.

So I’ve beefed up the capacitor to 10µF and fiddled the resistors to produce an 80Hz trigger pulse with a high time just under 1ms.

  • when inverter output low, R = 1.2kΩ
  • when inverter output high, R = 890Ω (1.2kΩ in parallel to diode + 220Ω; actual measured resistance)

Here’s a scope trace of the trigger signal (CH1) and the echo (CH2)


Calibration Pulse

A 555 timer in monostable mode is used to generate the calibration pulse.

It is triggered by the echo pulse, which is first inverted in one unit of the 74LS14. The C2/R3 filter integrates the pulse to produce a sharp trigger, with D2 sinking any positive spike.

The 555 produce a pulse with a duration controlled by the R5 potentionmeter. The values I’ve selected provide for a calibration range:

In order to calibrate for different ranges, C3 (or the resistors) can be substituted as appropriate.

Pulse Width Comparator

The echo signal and the 555-generated calibration pulse are XORed with the CD4070. This results in an output that is:

  • logical 0 when the signals are in phase
  • logical 1 when the signals are out of phase. This can be one of two cases:
    • the echo pulse is shorter than the calibration pulse (meaning something has come closer to the HC-SR04)
    • the echo pulse is longer than the calibration pulse (meaning something has come moved away from the HC-SR04)

“Alarm” Circuit

The circuit includes an LED and piezzo buzzer indicator when the echo/calibration pulses are out of whack. The LED and buzzer are switched with a simple low-side n-channel MOSFET.

The output of the Pulse Width Comparator is dampened with an RC filter. In my build I replaced the more complicated arrangement from the Silicon Chip article with a simple 100nF/1kΩ circuit, with a resulting time constant of 0.1ms. This squelches incidental noise, and also provides a little tolerance with slightly wandering echo readings.

The 100Ω R8 is not particularly necessary and doesn’t alter the function of the circuit. It may provide some protection for the FET.


The circuit performs pretty well! Here are some scope traces with

  • CH1 (Yellow) - Echo pulse
  • CH2 (Blue) - 555 reset pulse
  • CH3 (Red) - 555 output / comparator
  • CH4 (Green) - output control signal

Balanced Calibration - no alarm scope-calibrated

Near object Alarm scope-triggered-near

Far object Alarm scope-triggered-far

Construction - Breadboard Prototype




Construction - Put it in a Box!

So this works quite well, so I decided to put it on protoboard in a box.

Here’s the layout and construction concept. I’ve added a 7805 5V regulator with 100µF and 100nF smoothing capacitors so I can power the circuit from a 9V battery.





Credits and References

Project Source on GitHub Project Gallery Return to the LEAP Catalog

This page is a web-friendly rendering of my project notes shared in the LEAP GitHub repository.

LEAP is just my personal collection of projects. Two main themes have emerged in recent years, sometimes combined:

  • electronics - usually involving an Arduino or other microprocessor in one way or another. Some are full-blown projects, while many are trivial breadboard experiments, intended to learn and explore something interesting
  • scale modelling - I caught the bug after deciding to build a Harrier during covid to demonstrate an electronic jet engine simulation. Let the fun begin..
To be honest, I haven't quite figured out if these two interests belong in the same GitHub repo or not. But for now - they are all here!

Projects are often inspired by things found wild on the net, or ideas from the many great electronics and scale modelling podcasts and YouTube channels. Feel free to borrow liberally, and if you spot any issues do let me know (or send a PR!). See the individual projects for credits where due.