[?] Subscribe To This Site
A PIC sonar
range finding project using a seven segment display and a PIC micro.
distance up to 3m with an ultrasonic range finder...
|Range||~5cm - 300cm|
|Internal oscillator frequency||4MHz|
The project uses 5 standard transistors to receive and transmit the ultrasound and a comparator to set the threshold echo detection level - so there are no special components other than the microcontroller and the ultrasonic TX/RX modules which are standard 40kHz types.
Note that the internal oscillator of the PIC micro is used and this saves two pins - that can be used for normal I/O,
|Compiler||Mikroelectronika C compiler V184.108.40.206-V220.127.116.11 Free!|
|Target||16F88 (retargetable to other PICs that have a CCP module).|
|Software notes||Drives multiplexed display, controls CCP, interrupt use.|
|Hardware notes||High gain transistor amp.|
|Project version||1.01 - changed to new compiler.|
|Project version||1.02 - swapped RB2 and RB5.|
your details to get the Download Link
and get the microcontroller newsletter:
(Note: Your email is safe it will never be sold or rented).
You will get All the C source code and hex file.
Note: Check your email for the project code download link.
Learn to program in C
in easy steps.
Essential & EASY:
PIC C Programming Course:
You can recompile the pic sonar project files if you want examine code operation (using the built in simulator) or change the source code. Note the hex file is contained in the download.
Since the speed of sound is constant through air measuring the echo reflection time lets you calculate the distance to the object using the DST equation :
Distance = (s * t)/2 (in metres)
You need to divide by 2 as the distance is the round trip distance i.e. from transmitter to object and back again.
|s [m/s]||the speed of sound in air|
|t [s]||the round trip echo time.|
Some delay times:
|Round trip echo time||Distance|
|t = 588us||10cm|
|t = 5.8ms||1m|
Note: The speed of sound in air is more or less constant at 330m/s (@ 0ºC) - it varies mainly with temperature (~340m/s @ 20ºC). In this project I am using a value of 340m/s i.e. it is assumed that the project is used indoors. You can change it to whatever you like by modifying the code.
You can get ultrasonic transducers optimized for 25kHz, 32kHz, 40kHz or wide bandwidth transducers. This project uses a 40kHz transducer but it will still work with the others if you make simple changes to the software (where it generates the 40kz signal). The receiver and generator circuits will work as they are.
Note: If you use a different transducer you must change the software to generate the correct frequency for the transducer as they only work at their specific operating frequency.
The 40kz signal is easily generated by the microcontroller but detection requires a sensitive amplifier. I have used a three transistor amplifier for the receiver.
This is followed by a peak detector and comparator which sets the sensitivity threshold so that false reflections (weaker signals) are ignored.
This project makes use of the CCP module (in its capture mode) to accurately measure the signal reception time at the CCP port pin. When a signal triggers the CCP module the value of timer 1 is stored in a CCP register (or captured).
If you store the value of timer 1 and then enable the CCP after transmitting an ultrasound pulse the CCP will trigger when the comparator activates i.e. as soon as an ultrasonic echo is received.
Subtracting the stored value from the CCP register value gives the time delay in machine cycles. Since the project uses a 4MHz main clock then the time delay will be measured in micro-seconds.
The minimum distance of this scheme is about 5cm. Looking at the output of the first receiver amplifier shows a that it should be more accurate at lower distances - it is inaccurate by about 2cm which is still quite good. Probably the addition of amplifiers for the longer range stops accurate short range operation.
The maximum distance is limited by the sensitivity, gain and noise performance of the receive amplifier and also the transmit power and duration of transmission.
For this circuit the maximum distance is about 3m.
You can use any PIC microcontroller that has an internal CCP module and enough memory to hold the program.
You can program the PIC in circuit through the ICSP connector.
The circuit uses a three transistor amplifier and a two transistor output driver (it does not use any special components so it can be constructed out of standard components). The comparator is an LM311 type which is also a standard component (you could use an opamp - with suitable circuit changes).
The AC gain of these transistors is RC over RE (but at AC the capacitor has impedance of 40Ohms at 40kHz) so the effective Re is the intrinsic transistor emitter resistance (re~25ohms) plus the impedance of the capacitor (re is temperature dependent). So the gain is 2k7/65 ~ 40. If used at different temperatures you will get some gain variation.
The last transistor uses fixed biasing to set the bias point. For a more stable amplifier (less affected by Beta variation) use the same amplifier as the other two. I have just used it to see how it works as it can be seen quite often in other circuits and it seems to work well. It will however be dependent on the exact transistor used (its Beta value) and it will also be dependent on temperature - which will both affect its bias point and gain.
The comparator is setup as a standard circuit will a small amount of hysteresis (to stop oscillation if the input changes slightly) - the 1M ohm feeds back to set the hysteresis level.
Using an oscilloscope monitor the signals RB3 and RB0. Use RB3 as the trigger as this is the signal that regularly generates the ultrasound. RB0 is the detected echo.
Set the output of the comparator (RB0) low by turning preset VR2 fully in one direction. Point the transducers at an object at about 1 metre away and turn the preset until a signal appears (at about 6ms after RB3).
Set the output of the comparator (RB0) low by turning preset VR2 fully in one direction. Point the transducers at an object at about 1 metre away and turn the preset until a the display generates '100' (approx).
Move the board back and forwards to check that it displays a larger and then smaller number. Check the longer distance e.g. point at the ceiling and then a closer object e.g. a wall 20-50cm away. Adjust the preset as necessary.
You could remove the comparator and use the internal analogue comparator but this would require more software to set the comparator level. It would require 'Up' and 'Down' buttons to control the level settings.
With a temperature sensor you could change the value used for the speed of sound (currently fixed at 340m/s for 20ºC operation i.e. indoors!). This would make the pic sonar range finder more accurate in different environments.
Compiler project files
C Source files.
For a tutorial on compiling these pic sonar project files click here.
This contains all the code except the bit manipulation routines found in bit.h.
It enters a continuous a continuous loop calling ulta_gen - the routine that generates the ultrasound at 40kHz.
The ultra_gen routine is set up using the simulator to set the timing of the output signal for a period of 25us (40kHz). This is then repeated every 40ms. The required refresh rate of the seven segment display is 20ms so the display update routine (seg_display_int) is called twice over the 40ms period. (I should really say that the display update routine takes 20ms and calling this twice creates the total 40ms delay).
The display relies on persistence of vision to make it appear that the display is not flickering - a refresh rate of 50Hz or more does the job ( 1/50Hz = 20ms).
In theory the maximum distance that you could measure is 40ms*340m = (13.6) 6.8m (half the round trip time delay ) but in practice this is limited by the signal conditioning circuits. If they were changed you could get more range.
If a capture occurs indicated by gCapInt then the DST calculation is performed and the value of variable val is updated. val is the value displayed by the seven segment display routine 'seg_display_int' so val is continuously refreshed to the seven segment display.
The interrupt routine is only enabled when required and when the capture occurs (if it does) only the first capture is stored - so that later reflections are ignored (by resetting gCapOn).
The first reflection should be the strongest and therefore the closest object. When captured the variables t_capL,t_capH and t_capO are set to the value of the capture register which will be the value of timer 1 when the capture module triggered.
At the moment I have not used t_capO (and should do so) as it accounts for the roll over when timer 1 overflows. All that happens is that occasionally (when an overflow occurs) the wrong value will be generated - for hand held use it is not noticeable at all.
1. generate ultrasound at 40kHz (a few pulses of a square wave) to the TX
2. Turn on receiver.
3. Count time from end of 40kHz pulses to start of reception of reflected ultrasound
4. Ultrasound takes a set time to travel through air (varies mainly with temperature)
5. Apply D=SxT (Distance, Speed, Time)
You know the time taken for the round trip – the capture module is started at the end of the TX pulse – this just counts pulses of timer1 until it receives an input – when it does -the capture interrupt makes the program jump the interrupt routine where the current captured time is stored in:
t_capL = CCPR1L;
variable gCapInt = 1; // signal that a capture occurred.
calc = ((s1)<<8)+s2;
Multiply by 340 m/s
so calc * (340/100)/2
i.e. calc * 340/200
Final calculation is
calc * (((340/100)/2)/10) the same as 340/2000
But you don't want to do this calculation all at once if using only
calc = calc * 34;
calc = calc / 2000;
its the same as calc * 340/2000
but does not
overflow the calc integer store.
variable val is assigned the
and then automatically displayed on the 7segments.