[?] Subscribe To This Site
A PIC sonar
range finding project using a seven segment display and a PIC micro.Measure
distance up to 3m with an ultrasonic range finder...
The PIC sonar range finder works by transmitting a short pulse of sound at a frequency inaudible to the ear (ultrasonic sound or ultrasound).
Afterwards the microcontroller listens for an echo.
The time from transmission to echo reception lets you calculate the distance from the object.
PIC Sonar Specification
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.
The ultrasonic transducers 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,
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.
How the PIC Sonar rangefinder works
The time from transmission of the pulse to reception of the echo is the time taken for the sound energy to travel through the air to the object and back again.
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.
Some delay times:
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.
CCP - Capture mode
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.
PIC Sonar Practical limits
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.
(Click diagram to open a pdf).
IMPORTANT NOTE : Above schematic and code has an error which is that all RA port ipins are used as output and RA5 can not be an output. The error is not obvious because the various pullups and diodes do allow the relevant LED segment to light up but it is slightly fainter than the others!
To solve this swap PORT RA and PORT RB when you wire this up and re-code the sofware by re-writing the C code to swap PORTA and PORTB. Port RA will be the transistor control and Port B will be the segment drives.
PIC Sonar Range Finder Hardware
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 first two transistor amplifiers use standard biasing to set the output at the collector in the middle of the supply. If you look at the dc conditions the two (100k) input bias resistors across 5V to 0V set the input bias point at 2.5V. When the Vbe voltage is dropped across the transistor's emitter junction the voltage at the emitter is Vbias - 0.6 (approx 2V). So the emitter current =2/2k2 ~ 1mA. Ic=Ie. So the dc bias point is 5V-IcRc 2.7k*1mA ~ 2.5V.
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.
Setting up the PIC Sonar range finder.
PIC Sonar Oscilloscope setup
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).
PIC Sonar Manual setup
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.
Improvements to the PIC Sonar Range Finder
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.
PIC Sonar Range Finder Software
Project files for the PIC sonar range finder
Compiler project files
C Source files.
For a tutorial on compiling these pic sonar project files click here.
PIC Sonar Description
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.
PIC Sonar Interrupts
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.
A few more notes on the code operation
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;
t_capH = CCPR1H;
t_capO = T1_O;
variable gCapInt = 1; // signal that a capture occurred.
indicates to the main program that an echo was captured
This is where the distance is calculated
calc = ((s1)<<8)+s2;
change to 16 bit number
Multiply by 340 m/s
to get cm divide by 100.
divide by 2 to get half the complete round trip delay
so calc * (340/100)/2
i.e. calc * 340/200
But I have 4 digits and only want to display on the right 3
so use another divide by 10 to shift the digits along 1 to the
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
integers as it will overflow so its split into 2 pieces
calc = calc * 34;
calc = calc / 2000;
its the same as calc * 340/2000
but does not
variable val is assigned the
Jump from pic sonar page
to Best Microcontroller Projects Home Page.