A PIC Ultrasonic distance meter project using a Seven Segment display and a PIC micro.
The PIC Ultrasonic distance meter 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 Ultrasonic Distance Meter Specification
|Range||~5cm – 300cm (approx.)|
|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: As you approach 300cm it is more difficult to receive a reflected signal so the practical range is probably nearer 200cm. The SR04 unit will perform better since it uses higher gain opamps.
This design is experimental and educational since you can buy ready made modules such as the SR04 which are undoubtedly convenient – you supply a pulse trigger and they provide a distance (pulse width coded). These are quick and very easy to use but do not demonstrate what is going on.
This design uses external components, comparators and transistors to achieve the same result.
The 16F88 also has a built in comparator and reference level generator which would save components but this design can be used with any microcontroller that has a capture module. It is probably possible to use it with a 16F84 using some careful coding for time measurement.
Note that the internal oscillator of the PIC micro is used and this saves two pins – that can be used as normal I/O pins.
|Compiler||Mikroelectronika MikroC compiler 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 files||Enter your details to get the Download Link
and get the microcontroller newsletter:
(Your email is safe it will never be sold or rented).
Note: Check your email for the project code download link.
You can recompile the 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 so you do not have the recompile the source code.
How the PIC Ultrasonic Distance Meter 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.
|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. 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 Ultrasonic Distance Meter 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.
We recommend EasyEDA for electronic circuit design.
From Schematic Drawing to PCB Production, Just Need One Tool
Cheap PCB Prototype: 10 pcs 2 layers only $10, quick delivery, 100% E-test
Current Project / Post can also be found using:
- Pic 18f4550 projects with ultrasonic sensor
- pic 18f4550 ultrasonic distance meter project
- Ultrasonic Controller PIC