Projects   /   Sensors & Temperature   /   PIC16F88

Sonar range finder using PIC16F88 Microcontroller

The PIC sonar range finder works by transmitting a short pulse of sound at a frequency inaudible to the ear (ultrasonic sound or ultrasound).

Published

At a glance

CategorySensors & Temperature
PIC modelPIC16F88
Reading time~4 min
Jump to a section
  1. How the PIC Sonar rangefinder works
  2. CCP - Capture mode
  3. Quick Solutions to Questions related to PIC Sonar Range Finder:
  4. Related projects

Summary of Sonar range finder using PIC16F88 Microcontroller

This article describes a PIC-based ultrasonic range finder using a 40kHz transducer and a microcontroller. It explains the echo-ranging principle, calculating distance via sound speed and round-trip time. The system employs five transistors for amplification, a comparator for threshold detection, and the PIC's CCP module to measure timing accurately. The design uses an internal 4MHz oscillator to save I/O pins and allows code modification for different frequencies or environments.

Parts used in the PIC Sonar Range Finder:

  • PIC Microcontroller
  • Five standard transistors
  • Comparator
  • 40kHz Ultrasonic Transducers
  • Three transistor amplifier
  • Peak detector
  • CCP module (Capture mode)
  • Timer 1

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

Range ~5cm - 300cm
Accuracy +/-3cm
Transducer frequency 40kHz
nternal 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.

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.

PIC Sonar rangefinder

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.

Where:

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.

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.

For more detail: Sonar range finder using PIC16F88 Microcontroller

  • How does the project calculate distance?
    It calculates distance by measuring the time from pulse transmission to echo reception and applying the formula Distance = (s * t)/2.
  • Can this project work with transducers other than 40kHz?
    Yes, it will work with other frequencies like 25kHz or 32kHz if you modify the software to generate the correct signal frequency.
  • What is the accuracy of the range finder?
    The accuracy is specified as plus or minus 3cm.
  • Does the project use external oscillators?
    No, the project uses the internal oscillator of the PIC microcontroller which saves two pins for normal I/O.
  • How is the echo signal detected?
    A sensitive three-transistor amplifier followed by a peak detector and comparator sets the sensitivity threshold to ignore false reflections.
  • What role does the CCP module play?
    The CCP module in capture mode accurately measures the signal reception time by storing the Timer 1 value when the comparator activates.
  • What speed of sound is assumed in the calculations?
    The project assumes a speed of sound of 340m/s, which corresponds to indoor conditions at approximately 20 degrees Celsius.
  • Is the source code available for modification?
    Yes, users can recompile the project files to examine code operation or change the source code.
Motors & Robotics · PIC16F88

Exploring the PIC16F88 Microcontroller for Remote-Controlled Robotics

I. Design Summary The objective of this mechatronics system design project is to develop a straightforward control and interface platform for robotics...

Meters & Test Instruments · PIC16F688

Determine capacitance by measuring the charging time using PIC16F688

When TEST botton is pressed, RA3 pin is set to "1". R2 and R3 are divide the voltage to 1/2 Vra3 which...

Beginner Tutorials · PIC16F1827

Breadboard module for 18-pin PIC16F microcontrollers (PCB version)

Because of their compact size, ease of use and many built-in peripherals, the 18-pin PIC16F series processors (PIC16F628A, PIC16F88, and now PIC16F1827/47)...

Sensors & Temperature

Spectrum Analyzer using TMS320F28023DA with Proteus Simulation

Introduction This project demonstrates a real-time audio spectrum analyzer using the TMS320F28023DA Piccolo microcontroller, simulated in Proteus VSM. The system captures an...

Sensors & Temperature

DHT22 Low Cost Humidity Sensor using PIC18F25K20 with Proteus Simulation

IntroductionThis microcontroller project demonstrates how to interface a DHT22 (RHT03) digital temperature and humidity sensor with a PIC18F25K20 using Proteus simulation.The project...

Sensors & Temperature

Crafting a Microcontroller-Based Temperature Monitoring System

Abstract As technological advancements progress, processes become increasingly intricate, demanding a corresponding expansion in the parameters necessary for efficient analysis. Data acquisition,...

Ibrar Ayyub

Ibrar Ayyub

I am an experienced technical writer holding a Master's degree in computer science from BZU Multan, Pakistan University. With a background spanning various industries, particularly in home automation and engineering, I have honed my skills in crafting clear and concise content. Proficient in leveraging infographics and diagrams, I strive to simplify complex concepts for readers. My strength lies in thorough...