## What is it?

This is a design for a sonar module.  It’s something I’ve been wanting to play with for some time, and I finally had an excuse so I whipped something up.

The electronics are pretty general purpose and could be used in a number of sonar ranging situations.  The software is specific to a simple toy as a demo of sonar, but easily adaptable for real applications.  The toy is just a navigation aid.  You plug in headphones and by listening you can walk around blindfolded or in the dark. When an obstacle is ahead, the toy makes a note with the pitch indicating the distance to the obstacle (high pitch means close).

More generally, the electronics are intended to be used in robotic ranging and similar applications.  The resolution is on the order of millimetres and the range is up to a few metres for large obstacles (but see below on how to increase the range).

## Design

This design uses a TL074 quad low-noise JFET op-amp, but can be fairly easily substituted with other similar quads (many are pin and spec compatible).

Here’s the schematic (click to zoom in).  You can also download this as a PDF file.

### Power module

The power module consists of two 9V batteries, providing a total PD of 18V with rails for ground, +9V and -9V.  Having a negative supply rail is essential for signal analysis applications.  Two batteries is a bit of a pain but I used it here entirely for simplicity.  In a robotics application, you would normally already have a suitable negative supply rail (-12V for example).

A standard 6 pin double pole slide switch (S1) is used to simultaneously switch both batteries on/off.

The power module also regulates the power for the microcontroller using a 7805 (U5).

### Control and transmitter module

The control module is a minimal PIC16F627 configuration using the internal 4MHz RC oscillator to reduce part counts.

The microcontroller is connected directly to the transmitter transducer (U3).  The pins RB1 and RB2 drive the transducer at 40kHz.  It is just driven as a square wave, but due to the tight frequency response of the transducers, the harmonics are eliminated and give a pretty good sine wave output. The two pins are always in the opposite logic state so that with respect to one of the transducer leads, the other is always +5V or -5V for an effective 10V potential difference from just a 5V supply.  It is not an ideal voltage though.  It is a very minimalist configuration and the low transmitter power is one of the limiting factors of the range of this module.  A simple enhancement would be to use an H-Bridge to provide the full supply power range to the transducer.  For this design with an 18V range, the total voltage that could be put across the transducer would effectively be 36V with an H-Bridge design and would dramatically improve the sensing range.

Headphones or a small speaker can be connected to CONN1 to listen to the tone generated by the microcontroller. The microcontroller uses the PWM output to generate a tone that depends on the distance to the first detected object. In this simple implementation, the strength of the echo and any subsequent echoes are ignored.

The receiver is based around a low noise op-amp (U2).  It is in a state variable filter configuration consisting of U2/a, U2/b, and U2/c.  The fourth op-amp in the TL074 (U2/d) is used as an additional amplification stage.

The primary reason for using the state variable filter design is that it is stable even with poor tolerance components and provides an easy way to adjust response frequency, gain and Q.

For the purposes of calculating the filter response, for centre frequency f0, gain G and a Q of Q the relationships are:

$$R_F = 5.03 \times 10^7 / f_0$$

$$R_Q = 10^5 / (3.48Q + G – 1)$$

$$R_G = 3.16 \times 10^4Q/G$$

In this circuit RG corresponds with R3, RF corresponds with R7 and R5 and RQ corresponds with R6.

Because of the interrelationship between G and Q in the equation above, there isn’t complete freedom in the selection of Q and G.

Solving for RF:

$$R_F = 5.03 \times 10^7 / f_0$$

$$= 5.03 \times 10^7 / 40,000$$

$$= 1257.5 \Omega$$

That’s a pretty awkward value though, so using a conventional resistor value of 1200Ω gives

$$f_0 = 5.03 \times 10^7 / 1200$$

$$= 42,627 \mathrm{Hz}$$

That’s close enough provided the Q (response) of the filter isn’t too crazy sharp. Let’s aim for a Q of around 5 or 10 (let’s say 7) and a gain of around 20, then:

$$R_Q = 10^5 / (3.48Q + G – 1) = 11,025\Omega$$

$$R_G = 3.16 \times 10^4Q/G = 2306\Omega$$

More straightforward values would be Rq=10kΩ and Rg=2200Ω. Re-jigging those equations a bit gives

$$G= 262500\,{\frac {R_{{q}}+ 100000}{R_{{q}} \left(29\,R_{{g}}+ 262500 \right) }}$$

$$Q= {\frac {25 R_{{g}} \left( R_{{q}}+ 100000 \right) }{3 R_{{q}} \left( 29\,R_{{g}}+ 262500 \right)}}$$

And therefore for Rq=10kΩ and Rg=2200Ω, G will be 22.1 and Q will be 7.

The main sources of signal interference are mains hum (50Hz or 60Hz) and RF signals from radio and TV transmitters. Because the resulting filter is a bandpass filter that only allows the 40kHz ultrasonic pulses through, sources of interference are reduced or eliminated. The signal is amplified too, as an added bonus.

A final stage of amplification is added with the remaining op-amp in U2. The gain is determined by the ratio of R10 to R9. In this case, the ratio is about 45. In reality it’s not going to get that much gain, and trying for so much probably gives a little distortion. Those are just resistors I happened to have plugged into my breadboard at the time. The 100k resistor R10 should probably be a 47k, for a gain of around 20. The overall gain of the filter/amplifier is then around four hundred times, which converts the tiny voltages from the transducer into something usable and easily measurable.

After filtering and amplification, the signal is passed to an envelope detector consisting of D1, R11 and C4.  This removes the 40kHz modulation and produces a clean DC voltage that corresponds with the received signal strength.  This DC voltage is ideal for easily processing in a microcontroller.

## Construction

There are no extremely high frequencies or other considerations in this design, so for the purposes of the demonstration toy the electronics were just fabricated on a stripboard for that hacky bodged feel.  Throw in some hot glue and black insulation tape, and there isn’t much to it.

## Calibration

The ability for the module to detect extremely close objects and extremely far objects depends on some adjustment of the transducer placement.

Some sound from the transmitter will be directly picked up by the receiver before it bounces off an object.  So there will always be an apparent echo at close to zero distance (actually, it will apparently be at the distance of separation of the transmitter transducer from the receiver transducer).  This limits the closest objects that can be detected somewhat because they will be lost in this zero echo signal.  So naturally this suggests you want the transmitter and receiver transducers as close to each other as possible to decrease the minimum detection distance. To a degree, the zero echo can be detected and compensated for in software when its characteristics are known.  By adding some sound-proofing between receiver and transmitter, the problem can be reduced further.

There are three test points indicated on the schematic:

• TP1 is a sync pulse generated by the microcontroller just prior to starting a transmit/listen cycle.  This should be connected to the sync input of an oscilloscope.
• TP2 is the amplified received ultrasonic signal.
• TP3 is the DC envelope of the received signal.

Using an oscilloscope, monitor both TP2 and TP3 (using TP1 as the sync).  Adjust the positioning of the transducers to reduce the zero echo and other parasitic effects.  You can also use this to fine tune for best long range detection.

Experiment with sound baffles between the transmitter and receiver to see how it affects the zero echo size.  Even very small changes can have a dramatic effect.  Simple materials like a piece of cardboard work quite well.  Changing the baffle angle changes the sound refraction and can reduce the strength.  Experiment and find what works well for the situation.  This step is really important for optimal operation.