Motivation
Long story short: I was building a “radar” solution for educational model ships.
There are a lot of ways to do this but I eventually decided to try and use ultra sound as a simpler substitute for microwaves.
I found tons of examples online and they all used integrated sensor modules like the HC-SR04.
(Please keep reading, this one is different, I promise…)
These modules perform all the necessary functions for ultrasonic ranging. The modules are triggered by an external signal and outputs a short ultrasonic pulse. This pulse bounces off a nearby object and the module in turn, outputs a pulse when the signal is received back where it came from.

By using e.g. a microcontroller to measure the time between these pulses, the distance to the reflecting object can be measured.
Most of these modules operate at 40kHz. Part of the circuit generates the transmitting pulse. Another part receives and amplifies the signal before it sent to a detector which in turn outputs a digital (on-off) signal signaling that a sound pulse has been received.
This is great if you want to know the closest thing to the device, like when you are backing up a car.
A ‘real’ radar picks up reflections from multiple targets along the path of the beam and not just the nearest one.
Since I wanted my model radar to behave like a real radar and pick up reflections from multiple targets, I needed to have access to the actual sound signal from the ultrasonic receiver rather than just the digital signal determined by the device.
Getting the ultrasonic audio
I found some schematics online of various sensor modules and noticed that the received signal could usually be tapped before the internal detector.
Here is a link to David Pilling’s excellent overview of various HC-SR04 related stuff.

My plan was to intercept this received signal and make my own detector.
Rather than building a dedicated detector circuit I thought I would try to directly sample the audio and do the detection in software.
Hardware
40KHz is just within the range of typical audio sampling devices. Many consumer devices are capable of sampling at 96kHz. At this sample rate it is theoretically possible to accurately sample signals up to 48KHz so 40 kHz. should be low enough to be usable.
I thought I needed the trigger for the transmitted pulse and audio samples to be synchronized so I opted to build my own sampling device since I thought it would be easier to achieve synchronization this way. In retrospect I could probably determine the initial pulse just by analyzing some sampled audio but that would have been a totally different journey.
I chose a PCM1808 A/D module. This device is able to sample stereo audio at 96KHz with a resolution of 24 bit.
A ESP32 microcontroller was used to handle data from the A/D converter, process the data and send the result to a laptop for visualization.


Detecting the sampled echo signal
The ESP32 was programmed to generate a trigger pulse and then sample for a short time until all echoes was assumed to have passed. This resulted in a buffer containing a few tens of milliseconds of audio.
At a sampling rate of 96kHz each cycle of the 40kHz signal is just 2.4 samples long. To accurately detect a echo pulse, potentially just a few waves long, some math trickery need to be used to determine the amplitude for each sample.
I made com code that does the following:

First the signal was bandpass filtered to remove background noise and isolate the 40kHz echo signals. I used a digital biquad filter tuned to 40KHz with a Q-value of 10.
The signal was then passed through a special Hilbert transform filter that turned it into a quadrature signal. Each sample turning into two values forming a vector whose length accurately represented the amplitude of the original sample.
Assuming that the speed of sound is 343m/s the flight time of each sample interval then represents:
343m/s * 0.5 / 96000samplesPerSecond = 1.786mm.
The 0.5 factor exists because the sound has to cover two times the distance between the sender and the object.
In short: The amplitude for each sample represents how much reflection (stuff) is at each 1.786 mm of beam.

In the above graph I have plotted the received signal vs. time.
The black object on the left contains the SR4 ultrasonic transceiver. The big peak at time=0 (sample zero) represents the transmitting pulse. This peak is obviously large since the transmitter and receiver are located right next to each other on the circuit board.
The next three peaks are reflections from the objects on the table. I tried to place them so that they gave an roughly equal echo return.
Bits rather than amplifiers
An advantage of using a 24 bit A/D converter is its extremely high dynamic range of 144dB. Rather than adding a physical amplifier to amplify faint echoes from far away objects the samples can simply be multiplied by some appropriate factor to achieve the same result. Say that you wanted to amplify the signal with e.g. 120dB, which would be extremely hard to achieve with a electrical amplifier, you would simply multiply the samples by 65000. In this case you would still be left with a signal that had the equivalent of 8 bit resolution.
As the sound waves travel further and further they disperses and the returned echoes naturally gets fainter. To compensate for this the multiplier or ‘gain value’ can be changed over time to suppress nearby strong echoes and emphasize far away weak ones.

In the above graph the purple trace illustrates this change of gain over time.
Hey, wait a minute, couldn’t we listen to that?
The sample buffer recorded in the memory of the ESP32 essentially represent a small audio clip. If played back at the original 96kHz sampling rate the result would be inaudible since the 40kHz pings are beyond the range of human hearing. If however the audio clip was played back at a lower rate the sounds could be pitched down to within the audible frequency range.
I added some more circuitry to the project so that audio could be played back as well.

The I2S peripherals on the ESP32 is capable of recording and playing audio at different sample rates. This means that audio can simply be recorded at 96kHz and played back at say 1.77kHz, a 1:54 reduction in both speed and pitch.
The result would be an echo where all the distances are perceived as 54 times larger and the frequency of the ping would be around 740Hz.
Unfortunately playing audio back this slowly result in some technical problems with the D/A converter (MAX98357) I used. At this low rate some of the internal frequencies in the DAC becomes audible and the sound greatly degraded.
Instead I opted for using a downsampling technique where a bunch of extra zeros are added to the original samples and the result low pass filtered in a special way. This way I first reduced the sample rate by 1:18 and then played this back at a much more manageable physical sample rate of 32kHz.
Das Boot
The result was really surprising and the down-pitched echo recording TOTALLY sounds like a something out of a WW2 submarine movie!
Ok this was fun!
Even though this was not the goal of this project I decided to make this into a portable device just for fun.
I 3D printed a case for the project. I thought that nothing says “something with sound” like a megaphone:

Inside I could just barely squeeze in the electronics and a small battery:

I will provide schematic and code to make such a device at the end of this post.
But now back to the “Radar” business
When playing around with this basic setup I noticed that the directivity of the ultrasonic transducers were not very narrowly focused. Looking at the datasheet for a typical transducer the directivity is quite poor.

The “opening angle” or the angle where the signal has fallen to one half or -6dB is around +- 40 degrees.
In comparison a typical maritime radar has a “beamwidth” of 0.5 to 2 degrees.
Narrowing the beamwidth
To improve this I decided to add a parabolic disc to the transducer to try and focus the sound in a single direction. For this to work I needed the transmitter and receiver to be in the same place.
I found that automotive ultrasonic back-up sensors uses a single transducer for both transmit and receive:

After finding a appropriate point in the circuit to tap off the ultrasonic audio I was able to simply drop this device in in place of the HC-SR04.
With only one transducer I was able to test a couple of reflector designs:

From a previous project I knew that a PLA 3D printed surface is a good reflector for ultrasonic waves. This allowed me to experiment with many different designs.
In the end I opted for a offset parabolic reflector that focused the beam past the transducer and so that no part of the transducer would obscure the sound path:

This design also avoided reflections from the dish being sent back to the transducer.
At this time I have not made any measurements of the beamwidth of this design but it is subjectively much-much better than the transducer itself. The larger area of the dish compared to the area of the transducer also adds some amount “gain” to the receiver since sound is now picked up from a larger area. The difference between the area of the transducer and the dish is about 1:110. This gives the system a “antenna gain” of approx. 20dB.
Putting all the parts together
At this point I pretty much had all the bits I needed to make a faux radar system. I added a small stepper motor and modified the firmware to rotate the dish and take “radar traces” at various angles.
The trace data and the directions could then be sent to a computer for visualization.
Visualization
I wrote a small JavaScript application that runs in the browser. The ESP32 runs a small web server that feeds the data to the app in in real time as JSON strings:
I set up some test objects in the lab. The range of the “radar” is designed to be greater than the size of the room so I added a slider to reduce the shown range on the scope.

Subjectively I think that there are a pretty impressive correlation between physical objects in the space and the “radar image”.
Conclusion
I have been wanting to do this experiment of sampling ultrasonic echoes and hearing how they sound when pitched down to an audible frequency range. What has previously stopped me was merely the lack of a microcontroller that was powerful enough and A/D converters that was fast enough to do this in a simple low-entry way. It has always been possible, just not easy enough that I have bothered.
Now that I regularly use the ESP32 and I2S audio interfaces for other projects it was just something that was right there to do.
I am simultaneously surprised and not-surprised by the result. On one hand, I know that ultrasonic ranging is a precise and known technology. To attach this to a motor to make a toy radar is a bit of a no-brainer really. I was however, somewhat surprised about what listening to the echoes sounded like, and not for the reason one might think.
I have worked with ultrasonic ranging and ultrasonic transducers for decades in many different applications. I have seen the signals in data and on oscilloscopes and over this time I have gradually developed a mental idea of how this inaudible world sound. To hear that the experience is _exactly_ as I have imagined is the real surprise!
Future work
I think it could be interesting to make a “stereo” version of the pitched down audio system. With a central transmitter and two “ears” spaced some appropriate distance apart one might give a whole different spatial experience. Humans are better at using sound for spatial orientation that we give ourselves credit for and I am sure that a even small room would sound like a giant cathedral when all distances gets multiplied by some large factor. Perhaps, the way it is done in this project could help a visually impaired person to *see with sound” differently than other projects in this realm.
In the above experiments I used the built in transmitter modules to generate the ultrasonic pulses. This limits the transmitting pulse to one frequency and whatever duration the modules are designed for. I would like to try generating the transmit pulse from sample data. Complex waveforms like music or even voices could be pitched up and send into the ultrasonic echo-scape.
Here is how you can try it too
I have prepared the code and schematic to recreate this project. The code and schematic are provided _AS_IS_ and I _DO_NOT_ offer any support or guarantee of success.
You need
- ESP32 microcontroller (I have not tried any of the ESP32-Cxx variants)
- PCM1808 I2S ADC module
- HC-SR04 or equivalent ultrasonic module.
Optional
MAX98357 I2S DAC module or equivalent I2S audio output device.
Caveats
The project should open and compile in Arduino without any external libraries installed. You need to install the 2.0.xx version of the ESP32 framework for Arduino. I used 2.0.17. Version 3.xx.xx has changed some of the the I2S stuff so that is currently not compatible. (Be aware of Arduino auto upgrade)
You need to hack the HC-SR04 module to get to the audio signal. I recommend finding the exact same module as I have shown above and solder a wire in the same place.
Failing to hack the module you can just solder a ultrasonic transducer directly to the input of the PCM1808 module.
Schematic
How it works
Once compiled and uploaded the ESP32 should offer a WiFi network called “SONAR” with the default password “Secret1234”. (This can be changed at the very top of the Arduino code)
Once connected to this network your computer or phone should automatically open a web page (captive portal) with a graph showing the echo signal and some controls. If the page does not open automatically navigate to 192.168.4.1 in a browser.
You can use the controls to adjust timing and turn audio output on/off on the device itself via. I2S or via. the browser.
Code
Here is the Arduino project for the device
If you want to make your own interface or use this for something else
The page opening in the browser communicates with the device via. a websocket connection.
Commands are sent to the device as text strings in the format “key:value”
Commands and trace data are received from the device in JSON format.
Audio data is sent as raw binary int16 sample data.
Look at the source for the page for more details.


