Design and Implementation of a High-Precision Ultrasonic Ranging System
2026-04-06 03:34:17··#1
Introduction In engineering practice, ultrasound is often used for distance measurement because of its strong directivity, slow energy consumption and long propagation distance in the medium. It is mainly used in reversing radar, rangefinder, level measuring instrument, development of mobile robots, construction sites and some industrial sites, such as distance, liquid level, well depth, pipeline length, flow velocity and other occasions. Ultrasonic detection is often fast and convenient, and the calculation is simple and easy to achieve real-time control. It can also meet the requirements of industrial application in terms of measurement accuracy, so it has been widely used. Basic principle of ultrasonic ranging The ultrasonic generator emits an ultrasonic signal at a certain moment. After encountering the object being measured, it is reflected back and received by the ultrasonic receiver. As long as the time from the emission of the ultrasonic signal to the reception of the echo signal is calculated and the propagation speed in the medium is known, the distance from the object being measured can be calculated: d=s/2=(vt)/2 (1) Where d is the distance between the object being measured and the rangefinder, s is the distance traveled by the ultrasonic wave, v is the propagation speed of the ultrasonic wave in the medium, and t is the time taken for the ultrasonic wave to travel from emission to reception. To improve accuracy, it is necessary to consider the relationship between the speed of ultrasonic waves propagating in air and temperature at different temperatures: v = 331.4 + 0.61T (2) where T is the actual temperature (°C) and v is in m/s. Principle of Piezoelectric Ultrasonic Sensors Currently, ultrasonic sensors can be roughly divided into two categories: one is ultrasonic waves generated by electrical means, and the other is ultrasonic waves generated by mechanical means. Electrical means include piezoelectric, magnetostrictive, and electrodynamic types; mechanical means include Galton whistles, liquid whistles, and airflow whistles. The frequencies, power, and acoustic characteristics of the ultrasonic waves they generate are different, and therefore their applications are also different. In engineering, piezoelectric ultrasonic sensors are currently more commonly used. Piezoelectric ultrasonic sensors actually work by utilizing the resonance of piezoelectric crystals. Inside a piezoelectric ultrasonic generator are two piezoelectric crystals and a resonant plate. When a pulse signal is applied to its two poles and its frequency is equal to the natural oscillation frequency of the piezoelectric crystal, the piezoelectric crystal will resonate and drive the resonant plate to vibrate, thus generating ultrasonic waves. Conversely, if no external voltage is applied between the two electrodes, when the resonant plate receives ultrasonic waves, it will compress the piezoelectric crystal to vibrate, converting mechanical energy into an electrical signal, which then functions as an ultrasonic receiver. Hardware Circuit Design of a Reflective Ultrasonic Rangefinder The hardware circuit of this system consists of a microcontroller minimum system, a temperature compensation circuit, an ultrasonic transmitting circuit, an ultrasonic receiving circuit, and a display circuit, as shown in Figure 1. [img=500,270]http://image.mcuol.com/News/080805134204930.gif[/img] The specific working process of this ultrasonic rangefinder is as follows: After the microcontroller generates a reset signal, the MC9S12DG128B generates a control signal to control the peripheral circuit to generate 40kHz ultrasonic waves. After shaping and amplification, these waves are applied to the ultrasonic transducer to emit ultrasonic waves with a frequency of 40kHz. Simultaneously, the MC9S12DG128B's internal timer measures the time taken for the ultrasonic signal to travel from emission to reception. The received ultrasonic signal, after being amplified, filtered, and shaped by the ultrasonic transducer R, is used as the input capture signal to activate the timer's input capture function, completing one ultrasonic ranging operation. Meanwhile, the current ambient temperature is measured by the DS18B20 temperature sensor, read into the microcontroller, processed, and displayed on the LCD screen along with the current temperature. The MC9S12DG128B microcontroller is a 16-bit microcontroller in Freescale's S12 controller series. It features high integration, abundant on-chip resources, and interface modules including SPI, SCI, I2C, A/D, and PWM. It also boasts strong capabilities in FLASH storage control and encryption. The MC9S12DG128B microcontroller uses an enhanced 16-bit S12 CPU with an on-chip bus clock frequency up to 25MHz. On-chip resources include 8kB RAM, 128kB FLASH, 2kB EEPROM, SCI, SPI, and a PWM serial interface module. The PWM module can be configured as four 8-bit channels or two 16-bit channels with a wide range of selectable clock frequencies. It also provides two 8-channel 10-bit precision A/D converters, a CAN controller area network, and an enhanced capture timer, and supports background debug mode (BDM). Ultrasonic Transmitting Circuit An ultrasonic transmitting circuit generally consists of an ultrasonic reflector T, a 40kHz ultrasonic oscillator, and a drive (or excitation) circuit. This design utilizes gate circuits to generate 40kHz ultrasonic waves. The ultrasonic transmitting circuit is shown in Figure 2. [img=500,270]http://image.mcuol.com/News/080805134204930.gif[/img] In the diagram, the 74LS00 NAND gate and LM386 form an ultrasonic transmitting circuit. The 74LS00 is used to construct a multivibrator. By adjusting the 20k potentiometer, a 40kHz ultrasonic signal can be generated. U3A is the driver, and the circuit oscillation frequency f≈1/2.2RC. The microcontroller's control signal is input through U2A. To increase the ultrasonic transmission frequency, this design utilizes a single operational amplifier LM386, achieving a transmission distance of up to 4m. Ultrasonic Receiving Circuit The ultrasonic receiving circuit is shown in Figure 3. The receiving head uses an ultrasonic receiver R paired with the transmitting head, converting the ultrasonic modulation pulse into an alternating voltage signal. To shape the signal, a CMOS-level 6-NOT gate chip CD4069 is used in the design, reducing circuit complexity and improving the circuit's load-carrying capacity. The shaped signal is coupled to pin 3 of the audio decoding integrated circuit LM567 with a locked loop via C1. When the amplitude of the input signal falls on its center frequency, pin 8 of the logic output of LM567 changes from high level to low level. [img=500,270]http://image.mcuol.com/News/080805134204930.gif[/img] DS18B20 Temperature Compensation Circuit According to formula (2) above, temperature has a significant impact on the speed of sound. If compensation is not performed, it will lead to measurement errors. In order to improve the measurement accuracy of the system, a temperature compensation circuit is designed. The system uses the digital temperature sensor DS18B20 to collect the temperature. The DS18B20 is a 1-wire bus serial digital temperature sensor produced by DALLAS in the United States. It has the advantages of miniaturization, low power consumption, strong anti-interference ability, and easy interface with microprocessors, and is suitable for various temperature measurement and control systems. Its measurement temperature range is -55℃ to +125℃, with an accuracy of 0.0675℃ and a maximum conversion time of 200ms. The biggest difference between digital temperature sensors and analog temperature sensors is that the temperature signal is directly converted into a digital signal and then output through serial communication. Therefore, mastering the communication protocol of DS18B20 is the key to using this device. The protocol defines several signal types: reset pulse, response pulse time slot; write "0", read "1" time slot, read "0", read "1" time slot. After initialization, the sensor outputs two bytes of temperature. After data processing, the actual temperature value is obtained, and the compensated sound velocity can be calculated using formula (2). LCD display circuit Character dot matrix series modules are a type of dot matrix display module specifically used to display letters, numbers, symbols, etc. It has 4-bit and 8-bit data transmission modes. It provides 5×7 dot matrix + cursor and 5×10 dot matrix + cursor display modes. The system provides a display data buffer (DDRAM), a character generator (CGROM), and a character generator (CGRAM). CGRAM can be used to store character pattern data for up to eight user-defined 5×8 dot matrix graphic characters. It offers rich instruction settings: clear display, return cursor to origin, display on/off, cursor on/off, character blinking, cursor shift, display shift, etc. An internal power-on automatic reset circuit is provided; when the external power supply voltage exceeds +4.5V, the module automatically initializes and sets it to the default display operating state. The OCM2X16 displays two lines of characters, with 16 characters per line. This design uses the OCM2X16 to display two lines of characters: one line displays the current ambient temperature, and the other displays the measured distance. System Software Design The system software includes a main program, a temperature acquisition subroutine, a timer subroutine, a calculation subroutine, and an LCD display subroutine. The main program includes initialization and calls to various subroutines, finally displaying the measurement results on the LCD screen (see Figure 4). [img=300,472]http://img.hc360.com/ec/info/images/200808/one_20080804113626978.gif[/img] Conclusion This system uses the Freescale MC9S12DG128B microcontroller as the main controller. It boasts high reliability, strong anti-interference and electromagnetic compatibility, abundant internal resources, significantly reduced software workload, and supports background debugging (BDM) mode, making programming more convenient and flexible. In this design, a 40kHz ultrasonic signal is generated through external hardware circuitry. Therefore, compared to a 40kHz ultrasonic signal generated by the microcontroller, this signal is closer to the resonant frequency of the ultrasonic sensor, maximizing the sensor's output and effectively increasing the measurement distance to within the range of 0.3m to 4m. This design uses a digital temperature sensor to measure and compensate for the temperature of a microcontroller-based ultrasonic ranging system. It also compensates for the sound velocity and corrects for factors that cause measurement errors, thereby improving the system's measurement accuracy and sensitivity. The detection accuracy is kept below 1 cm, achieving excellent results.