Home Alternative Distance calculation. How to use an ultrasound device to...

Distance calculation. How to use an ultrasonic device to detect distance

Ultrasonic distance sensing can be achieved with an ultrasonic device added only to an op amp and microcontroller that has four I/O pins, including an internal built-in comparator.

To see how it works, let's take an ultrasonic transmitter that emits a 40kHz pulse or several oscillations at that frequency. The microcontroller can send a square wave of one or more wavelengths to the ultrasonic device to generate the pulse. This pulse is reflected by any object with a higher density than air, and part of the emitted pulse returns to the receiver. The total round trip time is measured and converted to distance by knowing the speed of sound.

Sound needs a medium through which to travel, such as air, water, or steel. In general, the denser the medium, the faster the sound propagates. The speed of sound in air varies depending on temperature, humidity, and altitude. At room temperature it can be assumed to be constant and approximately 343 m/s. It is an ideal speed for using microcontrollers to measure the round trip duration of a pulse emitted a few meters away.

The type of reflective surface is not critical; at 40 kHz almost all surfaces reflect the incoming sound wave. Perpendicular contact with the surface is preferable as the reflected pulses return back to the receiver. As the angle of incidence with the surface increases, the proportion of the pulse reflected at the receiver decreases.

An ultrasound transducer works similar to a piezoelectric buzzer, but at a higher, inaudible frequency. When an electric current flows through the piezoelectric device, it deforms or bends and returns to its original shape when the current is removed. When a 40 kHz square wave is applied to the pins of the device, a 40 kHz pulse of sound is radiated. An ultrasound receiver works in the opposite way and generates a voltage with a much lower amplitude than the incoming ultrasound.

An ultrasonic device can be a transmitter, receiver, or both, and are supplied in an open or closed hermetic type format. This application employs separate receiving and transmitting open-type devices. This type is the easiest to handle as sealed transmitters require a higher control voltage.

Fig. 1 shows the equivalent circuit of an ultrasonic device. It acts as a capacitive load, but due to inductive and capacitive factors it adjusts to a resonant frequency of 40 kHz. The transmitter is set for maximum output value while the receiver is set for maximum output voltage of an incoming 40 kHz signal. This is an attenuating filtering effect at all other frequencies and is useful for removing noise when the received signal is amplified. Typical values ​​of the devices used in the demonstration are listed at the bottom of Fig. 1.

Control of an ultrasonic device

Controlling a capacitive ultrasonic transmitter with a differential signal provides the highest transmission current and maintains a 0,0V offset in the device. Differential pin control also eliminates the need for a negative power supply to drive the device.

One problem with ultrasonic transducers is that they will continue to oscillate or transmit after the control signal is removed. This is due to the resonant mechanical behavior of the transducer. The transducer is set to oscillate like a bell at the specified ultrasonic frequency when so driven, and it needs a certain period of time to stop oscillating when the drive is removed. When the transmitter is oscillating, the signal will couple through the board or travel through the air between transmitter and receiver, and it looks like a received signal. Therefore, a delay is needed before turning on the receiver to ensure that it has stopped oscillating, and that any signal received is that of a reflected pulse. The time required for it to stop oscillating determines the minimum detectable distance from the receiver (see Fig. 2).

Ultrasonic devices should be controlled at a frequency as close as possible to the specified frequency to maximize power output. The 8 MHz internal oscillator of Microchip's PIC16F690, for example, can be easily split to generate a 40 kHz control signal. Two I/O pins of a PIC microcontroller can be used to generate the 40 kHz differential signal that drives the ultrasound transmitter. This application uses the Timer0 overflow interrupt to create the time base for the output.

As an alternative, a more automatic method for the control of an ultrasonic device would consist in the use of the ECCP module that many PIC microcontrollers incorporate. The module can be configured to generate a PWM of the selected frequency on both pins, P1A and P1B, in half-bridge mode and with an inverted output. The ECCP module uses Timer2 to establish a time base for PWM. Enabling Timer2's scaling allows the user to set the number of pulses generated before activating the interrupt warning. This method makes it possible to send an ultrasound pulse with a single interruption.

Once the ultrasonic signal is generated and sent from the ultrasonic transmitter, the next task is to detect and time a returned reflected pulse. The returned sound waveform is significantly attenuated and needs to be amplified before the signal is detected by a comparator. This amplification can be done with a single op amp in a differential amp configuration.

differential amplifier

Fig.3 shows an example circuit for a differential amplifier. This circuit based on an operational amplifier amplifies the voltage that reaches the ultrasound receiver connected between the two input pins. Common mode noise at the output is minimized by adjusting the input bias between resistors R2 and R4 and resistors R1 and R3.

The ultrasonic receiver acts as a high-quality tuned filter. The differential op amp amplifies the filtering effect of this receiver. The first op amp amplifies and filters the incoming signal against common mode noise. Later stages of the op amp will amplify any noise and need to add other filters. Selection of the proper op amp for the first gain stage of the ultrasound receiver can eliminate the need for more than one op amp and filters.

This demo uses Microchip's MCP6022 op amp as it has a Unity Gain Bandwidth (UGBW) of 10 MHz. Higher UGBW means that the op amp gain is higher at a given frequency, as per example 40kHz.

The demo circuit used has a gain between 250 and 300 at 40 kHz because the gain is limited by the UGBW and is not determined by R1/R2.

A data isolator is a common circuit in many communication applications. Compares a threshold voltage (V_th) with the amplified input to detect the signal. V_th in Fig. 4 is set slightly below the average value of the amplified ultrasound signal (V_US).

Each time an ultrasound pulse is amplified, the value at pin C2IN- will be less than the value of C2IN+, thus causing the comparator to switch. The value of R2 should be much larger than R1, but not so large that it causes the comparator to switch due to noise in the amplified signal. Ideally, the value of R1 is set such that the value of the threshold voltage is just above the noise of the received ultrasonic signal. The closer the threshold voltage is to the received signal, the greater the distance detectable by the receiver.

The C 2 OUT pin of the PIC microcontroller can be used to debug this stage of the ultrasound receiver. A common problem is incorrectly setting the threshold voltage. If it is too low, the detection range of the receiver will be limited. If it is too high, the comparator will switch the noise spikes on the line, making it impossible to tell when a signal is present.

The Timer1 gate function provides a trigger signal for Timer16's 1-bit counter clock signal. The comparator output (C2OUT) can be selected as the internal source of the Timer1 gate. The count is activated while the C2OUT signal is low. Once an ultrasonic signal is detected and the value of C2OUT changes, it stops counting. The value stored in the Timer1 registers (TMR1H:TMR1L) is the counted round trip time of the ultrasonic signal. Depending on the speed of the device's oscillator, the number of counts will have a certain time value.

After detecting the return ultrasound pulse, Timer1 stores a count value corresponding to the travel time of the ultrasound pulse. This number of beads can be converted to distance by dividing by two and multiplying by the speed of sound. It is divided by two because it is the measurement of a round trip and can be done by varying the counted value by one bit.

The wavelength of the carrier frequency determines the resolution of the system. A frequency of 40 kHz has a waveform of about 0,85 cm. Going to a higher carrier frequency increases the resolution but decreases the directivity and shortens the range.

Resolution can also be affected by the precision of the oscillator used to time the return pulse, and it delays the propagation of the signal through a more complex filter. Taking all this into account, the resolution of an ultrasound system operating at 40 kHz can be approximately 1 cm and does not depend on range, detecting only the return pulse.

There are two ways to extend the maximum detectable distance in this application: increase the transmit power and increase the receiver sensitivity. This demo uses I/O pins to control the transmitter up to a maximum of 20 mA and 5 V; MOSFET driver circuits can be used to boost the control current and voltage. The receiver gain at 40 kHz determines what the comparator can detect.

The demo uses a single op amp as the differential amplifier. Since there is only one stage, filtration is not needed. A multistage receiver would need some filtering between the first gain stage and the comparator to reduce noise. Careful control of the threshold voltage towards the comparator will also ensure detection of the smallest retrace pulse.

If a separate transmitter and receiver are used, they should both be aligned in the same direction. The transmitted signal and any subsequent oscillation will be bypassed by the board to the receiver circuitry. Leaving more space or clearance between devices on the board will help minimize this leakage. Rubber or silicon is often used in the mounting of ultrasonic transducers to limit the amount of ultrasound signal drifted to/from the surrounding material.

Conclusion

Ultrasonic distance sensing is performed using a PIC16F690, MCP6022 operational amplifier, and Murata's MA40S4R/S ultrasonic device. Two pins on the PIC microcontroller port provide enough control power to transmit an ultrasound pulse. Timer0 and Timer1 are used to generate a 40 kHz signal and to calculate the retrace pulse time. The MCP6022 amplifies the signal in the receiver and the internal analog comparator PIC16F690 is used to detect the presence of the flyback pulse in the signal.



Tags
operational comparator