Sie sind auf Seite 1von 15

Kerry D.

Wong
Search...

Home About

Digital Metronome Multiplexing the Drive Circuit

A Sensitive DIY Ultrasonic Range Sensor


January 22, 2011, 9:43 pm I needed some ultrasonic range finders for my project. But most of the commercial sensors like Parallaxs PING sensor and other similar products are quite expensive, especially if multiple units are needed. So I thought why not building it myself? The theory behind ultrasonic ranging is quite simple. Typically a short ultrasonic burst is transmitted from the transmitter. When there is an object in the path of the ultrasonic pulse, some portion of the transmitted ultrasonic wave is reflected and the ultrasonic receiver can detect such echo. By measuring the elapsed time between the sending and the receiving of the signal along with the knowledge of the speed of sound in the medium, the distance between the receiver and the object can be calculated. The picture below (source: Wikipedia) illustrates this basic principal:

Ultrasonic Ranging (Courtesy of Wikipedia) In my design, I used separate transducers for transmitter and receiver. It is possible to multiplex the transmission and receiving with a single transducer (e.g. Maxbotix range finders), but the design would be significantly more complex.

Ultrasonic Transducer

There are quite a few ultrasonic transducers to choose from, and the main criteria are the resonant frequency, radiation pattern and sensitivity. Generally speaking, these parameters affect the measurement in the following ways: a higher resonant frequency can provide finer details of the surroundings due to the shorter wavelength. A more directional radiation pattern can also enhance the resolution of the measurement. Sensitivity affects the efficiency of the transducer and also attributes to the SNR (signal to noise ratio). I bought these 24 kHz transducers on sale (see picture below). These transducers are very inexpensive (around a dollar each, and even cheaper when on sale) but effective. With properly designed circuits these sensors can easily achieve a range of more than 20 feet. Of course, using the higher priced 40 kHz sensors should achieve even better performance.

24 kHz Ultrasonic Transducers But for these DIY ultrasonic range finders, the choice of the transducers are really not that critical and this transducer really hits the performance to price sweet spot.

The Transmitter
The ultrasonic transmitter is powered from ATmega328s counter 1 PWM output (chip pin 16 and Arduino digital pin 10). In order to achieve the maximum output power of the transducer for a given supply voltage, I used the bridged output design as shown in the following schematics:

Ultrasonic Transmitter This bridged circuit produces an output voltage roughly twice the Vcc. I used +5V for Vcc and the result is already quite good (more than 20 feet of range). For even longer range measurement, you can safely increase this driving voltage to around 12 Volts as most ultrasonic transducers can be driven with voltage as high as 20 to 30 volts. If you increase the voltage significantly above 5V however, you will have to change the transistors to allow more power dissipation. With 2N3904 and 2N3906 the transistors get warm during normal operation and would heat up drastically with voltage above 6V. Here is the output of the ultrasonic burst measured at the output transducers terminals:

Output Waveform The small ladders at the half-way voltage point in the output waveform is due to the slight added delay of the inverted signal stage due to the use of an extra NPN transistor. To obtain purer rectangular wave form and reduce switching loss, a PNP transistor with similar timing parameters can be used on the side that is directly connected to the driving signal. For this

application though, the waveform is more than adequate and the added switching loss is negligible. The transmitter and receiver transducers can be mounted on a circuit board with approximately one inch of spacing (see below).

Ultrasonic Range Sensor In order to reduce possible interference from the reflected ultrasonic waves, the components are mounted on the reverse side of the board (below is the H-bridge circuit that drives the ultrasonic transducer, a few decoupling capacitors are used to reduce noise and they are not shown in the schematics above):

H Bridge The code to drive the transducer is similar to that I used previously, except that I changed the pre-scalar to 1 so that the output frequency can be controlled more precisely in the kHz range. ? 1 void startTransducer(float freq, float dutyCycle)

2 { if (dutyCycle > 0.5) dutyCycle = 0.5; 3 else if (dutyCycle < 0) dutyCycle = 0; 4 5 cli(); 6 TCCR1B = _BV(WGM13) | _BV(CS10) | _BV(ICNC1); //f0 = fclk / (2 * N * Top) 7 long topv = (long) ((float) F_CPU /(freq * 2.0 * 1.0)); 8 ICR1 = topv; 9 10 OCR1A = (int) ((float) topv * dutyCycle); 11 OCR1B = (int) ((float) topv * (1 - dutyCycle)); 12 DDRB |= _BV(PORTB1) | _BV(PORTB2); 13 TCCR1A = _BV(COM1A1) | _BV(COM1B1); 14 sei(); } 15 16void stopTransducer() 17{ 18 cli(); 19 TCCR1B = 0; 20 sei(); digitalWrite(9,LOW); 21 digitalWrite(10,LOW); 22} 23 24 25 26

The Receiver
The performance of the range sensor is largely determined by the sensitivity of the receiver for a given transmitter power level. Because the received signal is usually very weak (less than 1 mV), a high gain low noise amplifier is needed to ensure optimal performance. I used a two stage inverted band-pass amplifier design (see below). Each stage has a gain of around 67 (36.5 dB) and the circuit has a combined voltage gain of 73 dB. The operational amplifier I used is Nationals LPC662. In general, any operational amplifier with a sufficient gain bandwidth product should work just as well. Each stage has a band-pass filter that is centered around the operation frequency (24 kHz). Because the amplifier has a very high gain, we must pay special attention to the circuit layout in order to prevent parasitic oscillation. The connection between the receiver transducer and the circuit input (6.8n capacitor) needs to be shielded to reduce noise and unwanted coupling.

Ultrasonic Receiver Because we are using a single power supply the output voltage of the opamp is centered at around Vcc/2 (2.5V). In order to make it easier to process the echo, a diode (IN4148), capacitor (0.1uF) and resistor (10k) are used to demodulate the signal and a coupling capacitor (1uF) is used to rid the demodulated signal of the DC component. You can see the demodulated envelope waveform from the following oscilloscope screenshots (you can ignore the frequency measurement as these signals are none-periodical the frequency readings are meaningless). The higher amplitude waveforms in both images are the results of the ultrasonic burst, the lower amplitude waveforms are from the echo. In the first screenshot on the left, two echoes can be seen.

Echo and the demodulated waveform 1

Echo and the demodulated waveform 2

The following screenshot shows the relationship between the ultrasonic pulses (measured from ATmega328 pin 16) from the transmitter and the demodulated echo output. One key observation is that the received signal takes much longer time to fade then the original pulse duration and thus we must add in some delay after the transmission of the ultrasonic pulses. A delay of 1 to 2 millisecond is typical. With a 1 millisecond delay, the shortest measurable distance is around 30 centimeters or one foot.

Demodulated Echo And here is a picture of the finished project.

Ultrasonic Range Finder

Range Calculation
Since the measured distance is a function of the time interval between the time at which the pulse is transmitted and the time at which the echo is received, we need to reliability detect the echo. Empirically, we can measure the peak of the received echo and use the time displacement to calculate the distance. We assume that the strongest echo comes from the closest object (this may not always be true as the reflectivity of different objects are different, but generally achieves very good results in real-world situations) and thus the peak measurement corresponds to the closest objects position. The code snippet below assumes that we are interested in measuring objects with a range of up to about 20 feet. After the pulses are transmitted from the transmitter, we wait for a millisecond for the initial receiver signal to fade. Then we search the peak value in the next 20 milliseconds or so

(the loop limit 256 is set empirically, in the code below this setting corresponds to a 20 milliseconds interval between transmitted pulses which is suitable for distance measurement up to approximately 20 feet. To measure longer distance, the upper limit for the loop needs to be increased correspondingly) and assume that the peak comes from the first echo. ? 1 2 3 byte a = 0; 4 unsigned long t_start = 0; 5 unsigned long t_peak = 0; 6 unsigned long t = 0; 7 byte v_peak = 0; const float SPEED_OF_SOUND_20C = 0.0003432; //meters per micro-second 8 float d = 0; 9 10void loop() 11{ 12 startTransducer(24000.0, 0.5); delayMicroseconds(300); 13 stopTransducer(); 14 15 v_peak = 0; 16 t_start =micros(); 17 t_peak = t_start; 18 delayMilliseconds(1); 19 for (int i = 0; i < 256; i++) { 20 a = analogRead(0); 21 t = micros(); 22 if (a > v_peak) { 23 t_peak = t; 24 v_peak = a; 25 } 26 } 27 28 t = t_peak - t_start; 29 d = (float) t * SPEED_OF_SOUND_20C / 2.0; 30 Serial.println(d , 2); } 31 32 33 Heres the full code listing for this project: UltraSonicRangeFinder.tar.gz The bill of material for this ultrasonic range finder is less than 5 dollars (excluding the MCU since it can be incorporated into your projects).

Update
I would like to thank Thomas for pointing out the mistakes in my H-Bridge schematic. The PNP transistors collector and emitter are swapped by mistake (I should have double checked the schematics. Anyway, the photo shows the correct orientation) and I have updated the image. Also on the receiver side, the first OPs output was missing connection to the 0.1uF capacitor, I have updated it as well. Be Sociable, Share!

inShare

Tags: Arduino, ATMega328, H-Bridge, Ultrasonic, Ultrasonic Range Finder Category: AVR/Arduino | Comment (RSS) | Trackback

232 Comments
Older Comments

1.

victor says: May 27, 2013 at 12:30 pm kerry so far so good. my progress has been minimal i effected the change in the source code for developing a 40KHZ frequency to run the transmitter. but unfortunately sir. my measurement on a frequency metter is 1.86KHZ. . pls what can be done sir

sir i was likewise getting 0.19 on my serial board without any variation in the value. weather the object goes further or closer. the value on d serial board remains constant. i also reduced the transducer decay value. which was immediately after the statement Tpeak T-start. but any change made there would increase or reduce d serrial value to another constant value without any variation. sir my changes to ur circuit was dat am using an LM324 op amp. i also edited the resonant circuit and filter capacitor used at the negative input of each op amp stage. finally i dnt use the capacitor that ran through vcc and ground in th H-bridge circuit. what effect can dis have sir. am really sorry for bothering. i am running out of time to the completion of the project sir. i would appreciate a personal contact to u sir. u can email me personnaly sir on engrtor151@gmail.com. Reply to this comment

2.

Randy says: May 30, 2013 at 11:28 pm The bridge transmitter doesnt look right. The waveform should be +(5v 1.2v) to -(5v 1.2v) peak-to-peak. The 1.2v drops would be due to 0.6v base-emitter drops on the PNP and NPN transistor on either side of the transducer. With nothing connected to Pin 10 driver, there is a sneak circuit through the PNP BaseEmitter junction, through first 1.5K, through second 1.5K, through both NPN transistors. The Arduino Pin 10 driver source would have to source a lot of current to overcome the sneak circuit and it only seems to be sinking current. Anyway, you are not getting +3.8v to -3.8v peak-to-peak across the transducer and would be better off with a single P-Channel MOSFET highside switch or a single N-channel lowside switch. If you want a higher signal output, use a 1:10 transformer to raise the transducer voltage and match the higher transducer impedance. The transformer would put about 50v peak-to-peak across the transducer when using +5v Vcc. Very interesting and thanks for posting. Reply to this comment

3.

Randy says:

May 31, 2013 at 2:26 am Yes, the Transmitter bridge circuit is upside down. NPN should be on top (+) and PNP on bottom (-), emitters connected to transducer, with perhaps a 2K resistor across the transducer for base current to flow in the transistors. http://www.electroschematics.com/1026/dog-repellent-circuit/ Reply to this comment

Jon says: January 20, 2014 at 8:10 pm Brilliant, Randy! I spent a day struggling with this trying to figure out where my stray current was going. With the original setup, during the 1-2 second Arduino startup when pin 10 is still an INPUT (default), there was about 100mA of current flowing to the circuit. Then when pin 10 was set to an output, there was still stray current: 13mA when pin 10 was HIGH and 35mA when pin 10 was LOW. If I didnt configure pin 10 to be an OUTPUT at all, the 100mA would remain and start to cook my transistors, literally. Anyway, I did as you said and it worked perfectly. When pin 10 is set to LOW or as an input, it consumes < 2mA including during startup. Presumably this is solely due to the resistor. And regarding the resistor it reduces ringing quite a bit it seems. Kudos! Reply to this comment

4. google says: July 5, 2013 at 3:06 pm google G http://images.google.com.hk/ Reply to this comment

5.

Alex says:

July 16, 2013 at 5:10 pm love your write up on this, very well done indeed. i remember doing similar work on this when i was younger for a micromouse competition, where i was told it was impossible to use ultrasonics in such small places, and that was by the so called head of robotics for the UK. anyhow it worked, lol. just wondering if you mind if we used a couple of your photos and a bit of text for a review blog on our site of this, with your name and site mentioned and linked to? Regards Al Reply to this comment

kwong says: July 16, 2013 at 9:00 pm Hi Alex, I dont mind at all. Thanks in advance! Reply to this comment

6. Build your own Sensitive Ultrasonic Range Sensor | LiveBots UK says: July 17, 2013 at 5:49 am [...] result measurements, circuits and code is all avaliable on Kerrys website at: http://www.kerrywong.com/2011/01/22/a-sensitive-diy-ultrasonic-rangesensor/comment-page-3/#comment- [...] Reply to this comment

7.

mikel says: July 22, 2013 at 7:36 am helo mr. Kwong, im using PIC18F.. as what ive read, you are using arduino, how would i connect the circuit to the PIC18F? Reply to this comment

8.

MMK says:

August 4, 2013 at 8:34 am Hey how about if we want to implement it in such a way that we get a visualization just like in this link below any thoughts about it?? PART #01:http://luckylarry.co.uk/arduino-projects/arduino-processing-make-a-radar-screen-tovisualise-sensor-data-from-srf-05-part-1-setting-up-the-circuit-and-outputting-values/ PART #02:http://luckylarry.co.uk/arduino-projects/arduino-processing-make-a-radar-screen-tovisualise-sensor-data-from-srf-05-part-2-visualising-the-data/ Reply to this comment

9.

Cheryl says: September 10, 2013 at 1:02 pm Hi Randy, Am i able to use other chip instead of lpc662? in addition, do u mind if i have a clearer picture of the circuit you have done? Thanks! Reply to this comment

10.

kshitij says: January 1, 2014 at 5:46 am Respected sir. Which op amp can i use instead of lpc662. Expecting ur reply at the earliest. THANKS 1 in advance. Reply to this comment

kwong says: January 1, 2014 at 6:56 pm pretty much any opamp should work as lpc662 is just a general purpose opamp. But, the higher the gain-bandwidth-product the better.

Reply to this comment

11.

GarfFun says: January 3, 2014 at 6:41 am Hi Kerry, First of all, great tutorial, it has been a great help in my project. I have a small question regarding the receiving of the signal. I understand you are using the single-ended ADC conversion of the microcontroler. What is the threshold voltage you are accepting as an echo? Should it be anything higher than 2.5V- the ground of the receiver output or something more precise? Thanks for any help! Reply to this comment

kwong says: January 3, 2014 at 8:23 pm Thanks! For this simple example, I used the peak in the returned signal. But to make it more robust, you could use the peak along with a threshold (so when the object is too far away and there is no echo, the reading does not jump around). The threshold should be referenced to ground instead of 2.5V (Vcc/2) though since the cap at the ADC pin removed the DC offset already. Reply to this comment

GarfFun says: January 4, 2014 at 6:56 pm Well, I forgot about the capacitor at the end. Anyway, I have another question. The amplification of each stage is 67. That makes 67*67=4489. Assuming we have echo peak at around 20mV, that would give us almost 90V output. How come the output signal doesnt exceed 2.5V? Sorry for the stupid question, but Im a begginer if it comes to electronics. Reply to this comment

kwong says: January 4, 2014 at 8:24 pm In practice, the actual gain will be much smaller (as some pointed out, the first stage was probably not quite ideal as it was configured as none-inverting, which means the input impedance is quite low, never the less, the circuit does work quite well over all), the best way to determine whether the gain is adequate is to use a scope to measure the output. Reply to this comment

12.

mashr says: January 24, 2014 at 12:14 pm hi. i have two Ultrasonic Transducer RC40K5 and OC40K5. Can someone please tell me which one is Transmitter & Receiver. how to get to know about it. and at what frequency it operatesthanks Reply to this comment

Das könnte Ihnen auch gefallen