Project 2 - Measuring distance of an object using ultrasonic sensor (HC-SR04)
AIM :
In this project we have to write a Code in Microchipp studio to implement it for measuring the distance of an object using ultra sonic distance sensor HC-SR04.
To measure distance of an object using an HC-SR04 ultrasonic sensor, we need to use the following steps:
- Connect the trigger of the pin of the sensor to a digital output pin on our microcontroller .
- Connect the echo Pin of the sensor to the digital input pin of the microcontroller.
- send the short pulse to the trigger pin of the sensor.
- measure the duration of the pulse on the echo pin.
- use the speed of sound to calculate the distance of the object. The distance formula is:
Distance = (duration of pulse) x (speed of sound)/ 2 - Display the distance on a display or store it in variable for further use.
- use speed of sound in the air around 343 m/s.
Goals:
- Measuring the distance of an object using Ultrasonic Distance Sensor (HC-SR04)
- Usage of Input Capture Mode
Key Highlights of the Project:
- Measuring the distance travelled by the wave to the object and gets bounced back from the object to sensor.
- Measuring Pulse width using Input Capture Mode








I have attached below all the files of the project.