Research and design of ultrasonic distance measurement system based on single chip microcomputer

Publisher:黄金大花猫Latest update time:2011-01-28 Reading articles on mobile phones Scan QR code
Read articles on your mobile phone anytime, anywhere

School of Communication Engineering, Nanjing Institute of Electronic Science and Technology, Sha Aijun

In daily production and life, many occasions such as car reversing, robot obstacle avoidance, industrial well logging, reservoir level measurement, etc. require automatic non-contact distance measurement. Ultrasonic waves refer to mechanical shock waves with a frequency greater than 20 kHz generated in elastic media. They have the characteristics of strong directivity, slow energy consumption, and relatively long propagation distance, so they are often used for non-contact distance measurement. Since ultrasonic waves are insensitive to light, color, and electromagnetic fields, ultrasonic distance measurement has good adaptability to the environment. In addition, ultrasonic measurement can also achieve a good compromise in real time, accuracy, and price.

Therefore, this paper attempts to design an ultrasonic rangefinder with small size, low price, high accuracy, temperature compensation, real-time LCD display and alarm using a pair of 40 kHz piezoelectric ultrasonic sensors based on the single-chip microcomputer AT89S52.

1 Principle of Ultrasonic Distance Measurement

Ultrasonic sensors are divided into two categories: mechanical and electrical. They are actually a kind of transducer. At the transmitting end, they convert electrical energy or mechanical energy into sound energy, and vice versa at the receiving end. The ultrasonic sensor designed this time uses a piezoelectric ultrasonic transducer in the electrical mode, which works by the resonance of a piezoelectric crystal. It has two piezoelectric chips and a resonance plate. When a pulse signal is applied to its two poles, and its frequency is equal to the natural oscillation frequency of the piezoelectric chip, the piezoelectric chip will resonate and drive the resonance plate to vibrate, generating ultrasonic waves. On the contrary, if no voltage is applied between the two electrodes, when the resonance plate receives ultrasonic waves, it will press the piezoelectric chip to vibrate, converting mechanical energy into electrical signals, and it becomes an ultrasonic receiver. In the ultrasonic circuit, the transmitting end outputs a series of pulse square waves. The larger the pulse width, the more the number of outputs, the greater the energy, and the farther the distance that can be measured. The ultrasonic transmitting transducer and the receiving transducer are slightly different in structure. When using them, you should distinguish the signs on the device.

There are many methods of ultrasonic distance measurement: such as round-trip time detection method, phase detection method, and sound wave amplitude detection method. This design uses the round-trip time detection method to measure distance. The principle is that the ultrasonic sensor emits ultrasonic waves of a certain frequency, which are transmitted through the air medium and reflected back after reaching the measurement target or obstacle. After reflection, the ultrasonic receiver receives the pulse. The time it takes is the round-trip time, which is related to the distance of the ultrasonic wave propagation. The distance can be obtained by testing the transmission time.

Assuming s is the distance between the measured object and the rangefinder, the measured time is t/s, and the ultrasonic propagation speed is v/m·s-1, then we have the relationship (1)

s=vt/2 (1)

When the accuracy requirement is high, it is necessary to consider the effect of temperature on the ultrasonic propagation velocity and correct the ultrasonic propagation velocity according to formula (2) to reduce the error.

v=331.4+0.607T (2)

Where T is the actual temperature in °C, and v is the propagation speed of ultrasound in the medium in m/s.

The speed of sound at some temperatures

2 Overall system design

The system consists of ultrasonic emission, echo signal reception, temperature measurement, display and alarm, power supply and other hardware circuit parts and corresponding software parts. The system principle block diagram is shown in Figure 1.

System Block Diagram

The whole system is controlled by the single-chip computer AT89S52. The ultrasonic sensor adopts a split-type transmitter and receiver, which consists of an ultrasonic transmitter transducer TCT40-16T and an ultrasonic receiver transducer TCT40-16R. The ultrasonic signal is transmitted into the air by the ultrasonic transmitter transducer, and the echo is received by the ultrasonic receiver transducer after being reflected by the object to be measured. After relevant processing, the INT0 pin of the input single-chip computer generates an interrupt, and the intermediate time is calculated. At the same time, the corresponding sound speed is calculated according to the specific temperature. According to formula (2), the corresponding distance can be obtained for display. Of course, in some occasions, the distance alarm value can also be set according to needs.

3 Hardware Design

3.1 Ultrasonic emission part

The ultrasonic transmitting part is to enable the ultrasonic transmitting transducer TCT40-16T to send a square wave pulse signal of about 40 kHz to the outside world. There are usually two ways to generate a square wave pulse signal of about 40 kHz: using hardware such as 555 oscillation or software such as single-chip software programming output. This system uses the latter. The programming outputs a square wave pulse signal of about 40 kHz from the P1.0 port of the single-chip microcomputer. Since the output power of the single-chip microcomputer port is not enough, the 40 kHz square wave pulse signal is divided into two paths and sent to a push-pull circuit composed of 74HC04 for power amplification so that the transmission distance is far enough to meet the measurement distance requirements. Finally, it is sent to the ultrasonic transmitting transducer TCT40-16T to be emitted into the air in the form of sound waves. The circuit of the transmitting part is shown in Figure 2. The pull-up resistors R31 and R32 at the output end in the figure can improve the driving ability of the inverter 74HC04 to output a high level on the one hand, and on the other hand, it can increase the damping effect of the ultrasonic transducer and shorten its free oscillation time.

Transmitter circuit

3.2 Ultrasonic receiving part

The TCT40-16T transmits the ultrasonic wave in the air and returns when it encounters an obstacle. The ultrasonic receiving part is to smoothly receive the reflected wave (echo) to the ultrasonic receiving transducer TCT40-16R to convert it into an electrical signal, and then amplify, filter, and shape the electrical signal. Here, the integrated chip CX20106 produced by Sony is used to obtain a negative pulse and send it to the P3.2 (INT0) pin of the microcontroller to generate an interrupt. The circuit of the receiving part is shown in Figure 3.

Ultrasonic receiving part

It can be seen that the integrated chip CX20106 plays a great role in the receiving circuit. CX20106 is a widely used special chip for infrared detection and reception. It has the advantages of strong functions, superior performance, simple peripheral interface and low cost. Since the commonly used carrier frequency 38 kHz of infrared remote control is close to the ultrasonic frequency 40 kHz of ranging, and the filter center frequency f05 set inside CX20106 can be adjusted by its 5-pin external resistor, the larger the resistance, the lower the center frequency, ranging from 30 to 60 kHz. Therefore, this design uses it as the receiving circuit. The CX20106 is composed of a preamplifier, a limiting amplifier, a bandpass filter, a detector, an integrator and a shaping circuit. The working process is as follows: the received echo signal first passes through the preamplifier and the limiting amplifier to adjust the signal to a rectangular pulse of appropriate amplitude, and the filter selects the frequency to filter out the interference signal, and then it is shaped and sent to the output terminal 7 pin. When receiving an echo signal that matches the center frequency of the CX20106 filter, its output pin 7 outputs a low level, and the output pin 7 is directly connected to the INT0 pin of the AT89S52 to trigger an interrupt. If there is some error in the frequency, the external resistor R42 of the chip pin 5 can be adjusted to set the center frequency of the filter at 40 kHz to achieve the ideal effect.

3.3 Other main circuits

(1) Temperature measurement part.

Since the speed of sound varies at different temperatures, a temperature compensation function is used to improve the accuracy of the system. The main component used here is the single-bus digital temperature sensor DS18B20 produced by Dallas Semiconductor Corporation in the United States, which has the characteristics of high accuracy, intelligence, small size, and simple circuits. Temperature measurement can be achieved by connecting the DS18B20 data line to the P1.1 port of the microcontroller, as shown in Figure 4.

Temperature measurement part

(2) LCD display part.

The display part of this design uses a character type TC1602 liquid crystal to display the measured distance value. The capacity of TC1602 display is 2 lines and 16 characters. The liquid crystal display has many advantages such as low power consumption, small size, rich display content, ultra-thin and light, easy to use, etc. Compared with the digital tube, it looks more professional and beautiful. When using, P0 can be connected to the data line of the LCD, and the P2 port can be connected to the control line of the LCD, as shown in Figure 5.

LCD display part Among them, the 4th pin RS of TC1602 is the register selection, the 5th pin RW is the read/write signal line, and the 6th pin E is the enable terminal. The 7th to 14th pins: D0 to D7 are 8-bit bidirectional data lines. It should be noted here that for the convenience of wiring, D0 to D7 on the microcontroller side are connected to D1 to D0 on the LCD/602, which is just the opposite. Therefore, it is necessary to do some processing when writing the software to make the reading correct.

(3) Alarm part.

A buzzer is used, and a signal of a certain frequency is output by P1.2. Before being connected to the buzzer, it is amplified by a transistor 9012. The connection of the alarm part is shown in Figure 6.

Alarm part

(4) Power supply circuit: 220 V is stepped down by a 9 V transformer, then rectified by D1~D4 bridges and stabilized by 7805 to supply power to various parts of the circuit.

(5) Crystal oscillator circuit: Use a 12 MHz crystal oscillator.

4 Software

The system software design adopts modular design, which mainly includes main program design, T1 interrupt service subroutine, INT0 external interrupt service subroutine, temperature measurement subroutine, distance calculation subroutine, display subroutine, delay subroutine and alarm subroutine design.

When compiling the system software, the connection of related hardware should be considered, and the allocation and use of storage space, registers, timers and external interrupt pins should also be carried out. In this design, the P1.0 pin is connected to the 7 HC04 push-pull amplifier circuit and then to the ultrasonic transmitter sensor. The output of the P1.0 pin will be a 40 kHz square wave generated by software, and P3.2 (INT0) is used to receive the echo. Timers T1 and T0 both work in working mode 1, which is a 16-bit count. The T1 timer is used to start a ranging process and start a transmission measurement cycle with its overflow as a mark. The T0 timer is used to calculate the pulse round-trip time. Their initial values ​​are set to 0.

After the system is initialized, timer T1 starts counting from 0. At this time, the main program enters the waiting state. When it reaches 65 ms, T1 overflows and enters the T1 interrupt service subroutine. In the T1 interrupt service subroutine, a new ultrasonic emission will be started. At this time, a 40 kHz square wave will be generated on the P1.0 pin, and timer T0 will be started. In order to avoid diffraction of the direct wave, a delay of 1 is required. ms later, INT0 interrupt is enabled again; after INT0 interrupt is enabled, if P3.2 (INT0) pin is low at this time, it means that the echo signal is received, and an interrupt request will be made to enter the INT0 interrupt service subroutine. In the INT0 interrupt service subroutine, timer T0 will be stopped, the time value of timer T0 will be read to the corresponding storage area, and the reception success flag will be set at the same time; once the main program detects the reception success flag, it will call the temperature measurement subroutine to collect the ambient temperature during ultrasonic ranging, and convert the accurate sound speed and store it in the RAM storage unit; the microcontroller will call the distance calculation subroutine to calculate and calculate the distance between the sensor and the target object; after that, the main program calls the display subroutine to display; if the minimum alarm distance is exceeded, the speaker alarm will be activated; when the process of transmitting, receiving and displaying is completed, the system will delay 100 ms to reset T1 to the initial value again, and start T1 again to overflow and enter the next ranging. If the obstacle is too far away and exceeds the range, so that the echo has not been received when T0 overflows, "ERROR" will be displayed and return to the main process to enter a new round of testing. The block diagrams of the main program and the timer T1 and external interrupt INT0 interrupt service subroutines are shown in Figures 7 to 9 respectively.

Block diagram of the subroutine

Block diagram of the subroutine

In addition, there are a few points that need to be explained:

(1) Timer T1 overflows every 65 ms because it is a 16-bit timer/counter (65,535). When using a 12MHz crystal oscillator, since the period T = 1/f = 1/[(12×106)/12] = 1μs, one machine cycle is 1μs, and the counter overflows every 65 ms.

(2) In this design, the generation of the 40 kHz square wave is implemented by software: the P1.0 port is controlled to output a high level for 12 μs and then a low level for 13 μs, thus obtaining a cycle of 40 kHz pulses, which are then sent 8 times in a cycle.

(3) After the CPU stops sending the pulse group, due to electrical damping, the transducer cannot stop sending ultrasonic waves immediately, and will continue to send them for a period of time. Therefore, during this period, INT0 cannot be immediately turned on to receive the echo. It is necessary to wait for a period of time to avoid that part of the direct wave from the transmitter is directly diffracted to the receiver without passing through the object being measured. This period is called "false reflection wave". From the start of transmission to the end of the "false reflection wave", the INT0 interrupt application is not opened, which can effectively avoid interference, but it will also cause a "blind area" in the test. This time it is set to 1 ms. Assuming the temperature is 20℃, the measurement blind area is s=1×10-3×344/2≈17.2 cm.

(4) The maximum test distance will depend on: the minimum time interval between two pulse groups and the energy of the pulse. Generally speaking, the more pulses the transmitter has, the greater the energy, and the farther the distance that can be measured. However, it is not unlimited. This time, the count value of timer T0 is read. The maximum test distance is that T0 has not overflowed. Therefore, at a temperature of 20°C, the maximum test distance is s=vt/2=65535×344/(2×106)=11.272 m. In some periodic ultrasonic transmission equipment, if the maximum distance to be tested is 10 m, the minimum time between two pulse groups is t=2×s/v=2×10/344≈60:ms.

5 Conclusion

In order to verify the measurement accuracy of the system, field measurements were carried out in the laboratory. The system was used to conduct multiple tests in the range of 20 to 1 000 cm. After compensation, the maximum error reached 2 cm, and the linearity, stability and repeatability were relatively good. The system has the advantages of simple structure, small size, real-time LCD display and alarm, temperature compensation, and good anti-interference performance. The error of the system mainly comes from the fact that the ultrasonic wave emitted by the transmitting probe is diffused in a horn shape, the surface of the object being measured is not smooth and is not necessarily perpendicular to the axis of the two probes, so the reflected waves may be obtained from different points. In addition, the delay and interference of the electronic components themselves also have a certain impact. According to the specific occasion, the appropriate power probe can be selected, and the frequency, width and number of pulses in the program can be adjusted to improve the accuracy or measurement distance and expand the application range of the system.

Reference address:Research and design of ultrasonic distance measurement system based on single chip microcomputer

Previous article:Using single chip microcomputer to simulate 2272 software decoding
Next article:Design of intelligent lighting control system based on single chip microcomputer

Latest Microcontroller Articles
  • Download from the Internet--ARM Getting Started Notes
    A brief introduction: From today on, the ARM notebook of the rookie is open, and it can be regarded as a place to store these notes. Why publish it? Maybe you are interested in it. In fact, the reason for these notes is ...
  • Learn ARM development(22)
    Turning off and on interrupts Interrupts are an efficient dialogue mechanism, but sometimes you don't want to interrupt the program while it is running. For example, when you are printing something, the program suddenly interrupts and another ...
  • Learn ARM development(21)
    First, declare the task pointer, because it will be used later. Task pointer volatile TASK_TCB* volatile g_pCurrentTask = NULL;volatile TASK_TCB* vol ...
  • Learn ARM development(20)
    With the previous Tick interrupt, the basic task switching conditions are ready. However, this "easterly" is also difficult to understand. Only through continuous practice can we understand it. ...
  • Learn ARM development(19)
    After many days of hard work, I finally got the interrupt working. But in order to allow RTOS to use timer interrupts, what kind of interrupts can be implemented in S3C44B0? There are two methods in S3C44B0. ...
  • Learn ARM development(14)
  • Learn ARM development(15)
  • Learn ARM development(16)
  • Learn ARM development(17)
Change More Related Popular Components

EEWorld
subscription
account

EEWorld
service
account

Automotive
development
circle

About Us Customer Service Contact Information Datasheet Sitemap LatestNews


Room 1530, 15th Floor, Building B, No.18 Zhongguancun Street, Haidian District, Beijing, Postal Code: 100190 China Telephone: 008610 8235 0740

Copyright © 2005-2024 EEWORLD.com.cn, Inc. All rights reserved 京ICP证060456号 京ICP备10001474号-1 电信业务审批[2006]字第258号函 京公网安备 11010802033920号