I’ve been asked to help to write a simple PIC code to interface with the ultrasonic sensor.
It’s actually very simple by referring to the timing diagram.
Connect the Trigger pin to an output and the Echo pin to an input. Just output a 10us pulse on the trigger pin and wait for the feedback on the echo pin.
The pulse width of the echo pin will determine the distance. The longer the pulse width, the further the object is. I’m using the PIC’s built-in timer to time the duration of the pulse width.
The code is written for the Hi-Tech C Lite compiler on MPLAB X.
Here is a video demo 🙂