HC-SR04 Ultrasonic Sensor code for Distance Measurement
The code written below is for the HC-SR04 Ultrasonic Sensor using the PIC16F877A microcontroller. To start measurement, Trig of SR04 must receive a pulse of high (5V) for at least 10us, this will initiate the sensor will transmit out 8 cycle of ultrasonic burst at 40kHz and wait for the reflected ultrasonic burst. When the sensor detected ultrasonic from receiver, it will set the Echo pin to high (5V) and delay for a period (width) which proportion to distance. To obtain the distance, measure the width (Ton) of Echo pin. Time = Width of Echo pulse, in uS (micro second) ● Distance in centimeters = Time / 58 ● Distance in inches = Time / 148 ● Or you can utilize the speed of sound, which is 340m/s Project Schematic PROJECT CODE For the below code, the measured distance will be displayed in binary on an LED array on PORTC of the microcontroller. Eg. 5cm will = 00000101 You should be able to copy paste it. /****************************...