Projects Research & Design Hub

Build a Sonar Echolocation Ranger

Using the PIC32 MCU

These two Cornell students built a biologically inspired multimode echolocation system that can detect object distances and the rate of change of the surface-normal vector of an object. The Sonar Echolocation Ranger (SER) is implemented using a Microchip PIC32 MCU, which sends out ultrasonic pulses to an analog amplifier and then to a central speaker module.

  • How to build a multimode echolocation system using a PIC32 MCU

  • How to develop the  SER circuit

  • How to design the user interface

  • How to produce distance and perturbation predictions

  • How to test the results

  • Microchip PIC32 MCU

  • Microchip MPLABX/XC32 environment

  • Adam Dunkels’ Protothreads

  • Adafruit’s 1480 TFT display

  • On Semi’s 1N5362BRLG 28V Zener Diode

  • E-Switch PS1024ABLK Pushbutton Switch from Mouser

  • Diodes Inc.’s 1N4001 Diode

  • 3D printer

  • TFT graphics library developed by Adafruit

The Sonar Echolocation Ranger (SER) is a biologically inspired, multimode echolocation system that can detect object distances and the rate of change of the surface-normal vector of an object. The rates of change to the surface-normal vector are called “perturbations.” The SER operates in two modes: distance-measurement mode and perturbation mode. In distance mode, the system can measure the distance of flat and smooth objects approximately 0.2m to 2.1m away to within 3% relative error. In addition, the system can be calibrated to detect perturbations at different tolerance levels. The exterior of the system is shown in Figure 1.

FIGURE 1 – Exterior of the Sonar Echolocation Ranger, a high-performance echolocation system

The inspiration for our project came from animals that use high-frequency tones to acquire information about their surroundings. Some bat species, for example, are able to contract their larynxes to produce ultrasonic clicks, which then get reflected off surrounding objects. Bats use the time delay between producing the sound and receiving the echo as a measure of the distance to an object of interest—or example, a nearby wall or an insect.

We wanted to develop a highly accessible and portable augmented reality (AR) system that mimics the natural processes of bats, to localize object distances and to detect disturbances in 3D space. The SER is activated by switching on a power button, which is located on the side wall. By default, the system is in distance measurement mode, and will immediately begin displaying distances on the LCD screen. The mode can be changed by pressing the push button on the side of the SER [1].

The SER is implemented using a Microchip Technology PIC32 MCU, which sends out ultrasonic pulses to an analog amplifier and then to a central speaker module. The PIC32 monitors the ultrasound microphones immediately after emitting pulses, to detect echoes, compute object distances and identify object perturbations. The MCU converts the echo data into distance/perturbation data, which are displayed on the LCD screen.

SYSTEM DESIGN

We used the Microchip MPLABX/XC32 environment to develop the software for our project on the PIC32 MCU. We took advantage of the XC32 Peripheral Libraries and Standard Libraries to interact with the PIC32’s embedded hardware [2]. The added versatility of these libraries was critical for configuring the analog digital channels and the PWM (pulse width modulation) signal.

We used Protothreads [3], a cooperative multithreading system, to get threading functionality. Because there is no thread preemption in Protothreads, we used a round-robin scheduler to call each of our threads sequentially. Protothreads provides a lightweight, easy-to-use and modular approach to designing our software.

— ADVERTISMENT—

Advertise Here

Figure 2 shows the complete SER circuit diagram. The SER requires a 28V DC rail line and a ground line to deliver power to the MCU and amplifiers. We achieve this with five 9V batteries in series. The positive terminal of the first battery is fed into the input of the 5V voltage regulator, which is used to power the PIC32 board, and then creates our 3.3V power line. To regulate the 28V power source, we use a Zener diode circuit [4]. The positive terminal of the last battery in series is connected to a 1kΩ resistor, and then a 28V Zener diode is placed in reverse-bias configuration between a 1kΩ resistor and ground. To ensure stability in the power line, we inserted a large electrolytic 200μF capacitor between the 28V DC line and ground. The Zener diode is sufficient, because the average current draw of the circuit is low.

FIGURE 2 – Circuit schematic for the Sonar Echolocation Ranger

To create the on-off switch, we placed a switch between the negative terminal of the first battery in series and the ground line. When the switch is closed, the ground line is shorted to the negative terminal of the first battery in series, allowing current to flow. When the switch is open, virtually no current flows through the circuit. Although we never encountered any errors with this circuit configuration, we acknowledge that leakage current may flow through the 5V voltage regulator when the switch is open, and that this could drain the batteries. This power management system may be optimized by using a multi-pole switch to fully cut-off current flow.

USER INTERFACE

When engineering the SER, we focused on creating a user interface that would allow for the best possible AR experience, while maximizing the effective range of the system. This was achieved by creating an intuitive visual display for the user, and by creating a physical housing for the circuitry that optimizes ease of use and effectiveness.

To program the AR display, we used a graphics library [5], text and rectangles on the TFT display [6]. For distance-measurement mode, the LCD displays a circle with a radius inversely proportional to the distance of the object. This was implemented in our code by setting the radius size of the circle to be drawn to be a negative linear function of the object distance. To show the object distance on the display, we simply print out the variable corresponding to the object distance to the desired location on the LCD screen. For perturbation mode, the LCD displays a blue circle if it detects a perturbation, and a red circle otherwise, with text indicating whether there is a perturbation.

The physical design of the SER includes secure housing for the electrical components, acoustic isolation of the speakers and microphones, and maximal concentration of the central sound lobe. Acrylic material was laser cut and used for walls (Figure 3). The MCU and circuitry was housed inside the acrylic enclosure. We used a 3D-printed mount to securely hold the TFT against the back wall. Velcro was used to affix the circuit and MCU parts to the walls, and to allow seamless opening and closing of the side walls. The speaker and microphone module were attached to the wall opposite the TFT. We used polyurethane adhesive and 3D-printed support struts to connect the walls and secure the Velcro strips.

FIGURE 3 – Exploded view of the Sonar Echolocation Ranger. The green component in the center is the circuit board.

To acoustically isolate the speakers, we used 3D-printed cylindrical holders. The cylindrical holders prevent sound from traveling laterally. To further improve sound isolation, we inserted foam around each of the holders. To maximize the central lobe of the ultrasonic wave, we arranged holders in a phased-array manner. This entailed separating the individual speaker transducers by a wavelength of sound (λ = 1.36cm) to increase the intensity of the central lobe without causing destructive interference of the waves.

ULTRASONIC EMISSION

To produce distance and perturbation predictions, the SER must be able to emit ultrasonic waves. This was achieved by generating an ultrasonic PWM wave on the MCU, and amplifying the signal with an op amp [7]. We generate 25kHz PWM pulses on the MCU by using the output compare unit [8] along with Timer3 [9].

To begin generating PWM pulses, we set the PWM on time to be half the period length. We made the duty cycle 0.5 to reduce the amount of harmonic noise in our signal, while maximizing the intensity of the first harmonic. To stop outputting PWM pulses, we set the PWM on time to 0. The SER will repeatedly turn on/off the PWM pulses approximately every 15ms, in a separate thread to observe the induced echoes.

The speaker circuit was designed so that the PWM signal produced by the MCU can be amplified to maximize the SER’s effective range. Amplification was achieved by using a non-inverting op amp (comparator) circuit. The positive terminal of the op amp is set to DC 1.65V (achieved using a voltage divider). The negative terminal is set to the PWM wave produced by the MCU. The op amp is powered by the 28VDC power line.

— ADVERTISMENT—

Advertise Here

DETECTING ECHOES/PERTURBATIONS

One of the most important features of the SER is its ability to detect incident ultrasonic waves. This is made possible through a microphone amplification/filtration circuit, and a software module designed to average and threshold the microphone signal via the MCU’s ADC channel [10]. This averaged signal is then used for computing distance and perturbation predictions. The microphone circuit amplifies the microphone input enough to pick up on faint echo signals, while ensuring that noise doesn’t degrade the signal. Because the analog voltage inputted into the MCU cannot exceed 3.3V, we clipped the input signal with a diode and a resistor.

The raw microphone input is fed into the positive terminal of an op amp. Furthermore, the input voltage is biased at 1.65V (via a voltage divider), since the MCU can only handle positive voltages. Note that the circuit rolls off to unity gain at DC, to avoid saturating the amplified signal. The op amp is in closed-loop mode by having a 100kΩ resistor between the input and output.

The second stage of the amplifier has the output of the first op amp fed into the positive terminal of the second op amp. The second op amp is also in closed-loop mode with a 1MΩ resistor straddling the negative terminal and output. Unity gain at DC is also achieved for the second op amp to avoid signal saturation.

To cut down on current draw through the snubber diode [11], there’s a 1kΩ resistor after the dual stage amplifier. Afterward, the signal is connected to a diode with its negative terminal connected to the 3.3V rail line, to ensure that no voltage above 3.3V enters the MCU. To further reduce current draw into the ADC at high voltage, the signal passes through 1,300Ω of impedance before entering the MCU ADC input.

To read echo data coming from the microphones, we used the ADC hardware on the PIC32, which allows us to multiplex different ADC input signals from a readable buffer. To calculate distance measurements, the SER calculates the time at which the microphone voltage exceeds a certain threshold after the speaker begins emitting sound. Once the speakers are turned on (after they have been turned off for 15ms), the SER repeatedly probes all three ADC channels corresponding to the three microphone inputs, until the timer we used has reached capacity (which occurs after about 13.7ms, corresponding to a distance of about 3m), or the SER has detected that all three microphone inputs have exceeded the necessary voltage threshold for an echo.

ADC SAMPLING

To improve accuracy and determine if a threshold has been reached, the system performs five-sample windowed averages of the ADC samples for each microphone. Furthermore, the averaged ADC sample for each microphone must exceed the threshold value several times before the system registers that an echo was received. In addition, the system averages the value of the echo cycle time for each microphone over several pulse cycles (Figure 4). All these steps are taken to reduce noise in the calculations that would adversely affect the AR system. The averaged echo times are stored in a global variable to be used by another thread responsible for processing the echo cycle times.

FIGURE 4 – Flowchart depicting process of calculating echo times

To calculate object distances, the system converts the averaged echo cycle times for the three microphones into meters, by directly converting the echo cycle times into seconds. This is done by multiplying the echo cycle time by 8 (timer has a prescaler of 8), and then dividing this value by 40 million (since the clock frequency is 40MHz). We then multiply the traversal time in seconds by 343m/s—the speed of sound in air—to get a distance measurement. To improve the precision of our distances, we average the echo distances of all three microphones and subtract twice the length of the transducer holder. This corrects the traversal distance, so that it doesn’t account for the length of the transducer holders. The object separation distance between an object and the center speaker is defined using the equation:

where the variables d and h are defined according to Figure 5.

FIGURE 5 – Positional layout of speaker, microphone and object used to calculate distance

Therefore, to find d, the physical variable of interest, we compute:

That formula becomes necessary for computing distances at short range, where d ≈ h. We found that using this equation produces a constant positive error in our distance measurement. We correct for this error by subtracting a constant distance offset from the computed distance value, which we found to be equal to 5cm, as discussed later in the Testing and Results section.

When the system is in perturbation mode, it generates ultrasonic pulses in a manner similar to when it is localizing object distances. However, instead of monitoring the input signals for threshold crossings, the system creates bins for each microphone, corresponding to the average ADC value received at the microphone, for the duration of echo time for each bin (Figure 6). In addition, the system keeps track of these same values for the previous sonar pulse cycle.

FIGURE 6 – Depiction of average binning for perturbation mode

To output a classification (“perturbation” or “no perturbation”), the system finds the greatest difference in bin value from the previous pulse cycle for all three microphones. This operation essentially quantifies the degree to which the input signal for each microphone changed from one pulse cycle to the next. After this norming, three values correspond to the three microphones. These three values are then squared and summed, giving an overall quantification of how much all three microphone inputs changed from one pulse cycle to the next.

To determine if a perturbation is present, the system checks if this number exceeds a threshold. If so, then a perturbation is registered, and the AR display outputs this information. Otherwise, the AR display indicates that there is no perturbation. The threshold is defined in software as a macro that can be changed by the user, depending on the desired sensitivity to surface-normal perturbations.

TESTING AND RESULTS

Several environmental factors affected the performance of the SER. First, the position of the SER with respect to walls, floors and ceilings, affects the acoustic signal received by the microphones. Second, the surface material has a significant effect on the ability of sound to reflect, and thus for echoes to be detected. Third, sound attenuation can change depending on the ambient temperature and pressure of the air. Because of the range of conditions in which the SER can be placed, we needed to create a controlled experiment to get a rigorous measure of the effectiveness of the system.

Distance-measurement mode: To test the distance-measurement mode, the true distance between the front surface of the SER and a smooth wall was determined with a tape measure. Starting at a separation distance of about 0.2m, we queried the SER for a distance, and then recorded the distance indicated by the measuring tape. We would then move the SER back a few centimeters and repeat the experiment. We stopped taking measurements when the SER could no longer detect the presence of the wall (at about 2.1m).

— ADVERTISMENT—

Advertise Here

All the SER’s distance measurements were within a few centimeters of the true values. In fact, the relative error of the measurements never exceeded 3%. Therefore, the SER has a high degree of accuracy in measuring distances for a range from 0.2m to 2.1m). Figure 7 depicts the SER’s output in distance mode when no offset term is applied. Note that the linear regression equation includes a positive term that is equal to roughly 5cm. This indicates that the system is constantly overestimating the true object distance by 5cm.

FIGURE 7 – Plot showing additive constant present in raw SER distance mode measurements

From the linear regression, we found that we needed to subtract 5cm from the raw distance mode measurements. These results are shown in Figure 8. A direct comparison of the relative errors when the offset is and is not applied is shown in Figure 9. Our results demonstrate that applying the corrective offset term is successful in reducing the relative error of the distance measurements.

FIGURE 8 – SER distance mode measurements when offset is applied
FIGURE 9 – Relative errors from raw measurements superimposed with relative errors from offset-adjusted measurements

Perturbation mode: The perturbation mode on the SER is designed to work for a wide range of sensitivities, which is why we included a programmable threshold parameter. To test this mode, we assessed the SER’s ability to distinguish between constant-intensity perturbations and no perturbations at different threshold values. This testing procedure allowed us to evaluate the underlying algorithm for detecting perturbations, and to see whether the thresholding algorithm outperforms a random guess classifier.

To conduct this test, we set the SER’s perturbation threshold to a particular value. Then we put the SER in perturbation mode, while a stationary plastic board located 0.60m away was held. We videotaped the AR display output for 30 seconds, and then we rotated the plastic board at a constant rate for another 30 seconds (while the display was being videotaped). We calculated the false-positive rate as the fraction of time when the board was still that the display indicated a perturbation.

Conversely, we calculated the true-positive rate as the fraction of time when the board was being perturbed that the system detected a perturbation. We ran this testing procedure for a wide range of thresholds, to see how the classifier performance changes depending on the value of the internal threshold. Subsequently, we generated a receiver operating characteristic (ROC) curve from the data, to see how our perturbation algorithm compared to a random classifier (Figure 10).

FIGURE 10 – Receiver operating characteristic (ROC) plot demonstrating accuracy of perturbation detection mode

The ROC curve shows that for virtually all the thresholds tested, the ROC points lie above the diagonal line. This indicates that our system is always making classifications that are significantly better than a random classifier.

The ROC curve also reveals that for all the thresholds tested, we are not able to achieve a false-positive rate above 0.4. However, for many thresholds we were able to achieve a very high false-negative rate. This indicates that our algorithm is biased toward correctly classifying no perturbations over correctly classifying perturbations. 

Authors’ Notes: We would like to acknowledge the work of Adam Dunkels for developing the Protothreads library. We also used the TFT graphics library, which was developed by Adafruit and modified by Syed Tahmid Mahbub. This project would not have been possible without the work of Rajiv Kommareddy, who helped implement the project but did not take part in the writing of this article. Finally, we would like to extend our gratitude to Prof. Bruce Land [3] of Cornell University for helping us with the project.

RESOURCES

References:
[1]  E-Switch, Datasheet, “PS1024ABLK Pushbutton Switch”.
https://www.mouser.com/datasheet/2/140/E_Switch_PS1024-1165682.pdf
[2]  Microchip Technology Inc., “PIC32 Peripheral Libraries for MPLAB C32 Compiler”.
http://ww1.microchip.com/downloads/en/DeviceDoc/60001120F.pdf
[3]  Cornell University ECE4760 ProtoThreads PIC32MX250F128B
https://people.ece.cornell.edu/land/courses/ece4760/PIC32/index_Protothreads.html
[4] On Semiconductor, Datasheet, “1N5362BRLG 28V Zener Diode”.
https://www.onsemi.com/pub/Collateral/1N5333B-D.PDF
[5]  TFT graphics library, developed by Adafruit and modified by Syed Tahmid Mahbub
 https://tahmidmc.blogspot.com/2014/10/interfacing-color-tft-display-with.html
[6]  Adafruit Industries, Datasheet, “Model 1480 TFT display”.
https://cdn-learn.adafruit.com/downloads/pdf/2-2-tft-display.pdf?timestamp=1553019626
[7]  Texas Instruments, Datasheet, “LF353 Op Amps”.
http://www.ti.com/lit/ds/symlink/lf353-n.pdf
[8]  Microchip Technology Inc., Reference Manual, “Section 16. Output Compare”.
http://ww1.microchip.com/downloads/en/DeviceDoc/61111E.pdf
[9]  Microchip Technology Inc., Reference Manual, “Section 14. Timers”.
http://ww1.microchip.com/downloads/en/DeviceDoc/61105F.pdf
[10]  Microchip Technology Inc., Reference Manual, “Section 17. 10-bit Analog-to-Digital Converter (ADC)”.
http://ww1.microchip.com/downloads/en/DeviceDoc/61104E.pdf
[11] Diodes Inc., Datasheet, “1N4001 Diode”.
https://www.diodes.com/assets/Datasheets/ds28002.pdf

Adafruit Industries | www.adafruit.com
Diodes Inc. | www.diodes.com
E-Switch | www.e-switch.com
Microchip Technology | www.microchip.com
Mouser Electronics | www.mouser.com
On Semiconductor | www.onsemi.com
Texas Instruments | www.ti.com

PUBLISHED IN CIRCUIT CELLAR MAGAZINE • JUNE 2021 #371 – Get a PDF of the issue

Keep up-to-date with our FREE Weekly Newsletter!

Don't miss out on upcoming issues of Circuit Cellar.


Note: We’ve made the May 2020 issue of Circuit Cellar available as a free sample issue. In it, you’ll find a rich variety of the kinds of articles and information that exemplify a typical issue of the current magazine.

Would you like to write for Circuit Cellar? We are always accepting articles/posts from the technical community. Get in touch with us and let's discuss your ideas.

Sponsor this Article
+ posts

ABOUT THE AUTHORS

Hailing from the Silicon Valley, David Yang is a graduate of Cornell University, majoring in Electrical and Computer Engineering and minoring in Computer Science. He is currently a software engineer for Venmo. David's primary interests include sensors and communications systems. He presided over the Cornell Karate Club. David enjoys hikes and attempts to write short stories.

Originally from the Maryland suburbs of Washington D.C., Evan Kravitz is is a graduate of Cornell University, majoring in Electrical and Computer Engineering and minoring in Computer Science. His primary interests include embedded systems, signal processing, and data science. He is currently a software engineer for Amazon Web Services. In his free time, Evan likes to go hiking, go fishing, listen to music and cook food sous vide.

Supporting Companies

Upcoming Events


Copyright © KCK Media Corp.
All Rights Reserved

Copyright © 2023 KCK Media Corp.

Build a Sonar Echolocation Ranger

by Circuit Cellar Staff time to read: 14 min