Detecting obstacles using an ultrasonic sensor HC-SR04

HC-SR04 is a commonly used ultrasonic sensor which is capable to detect obstacles in a range of 2-300cm. The sensor looks like a small PCB having two metal cylinders on the front-side and a small circuit on the back-side (see Fig.1).
In this article we will provide a simple demo to use HC-SR04. This requires a preliminary read of the sensor user manual: HC-SR04 User Manual
The sensor has 4 PIN:
VCC, which must be connected to 5V; Trig, which is an input PIN to trigger the measurement; Echo, which is an output PIN which sent out a square wave; GND, which must be continue reading...