This is a brief summary of how to use HC-SR04 ultrasonic sensor.
Note that HC-SR04 is a 5V device, it needs to be powered by 5V and the signal level is 5V. A 5V -> 3.3V level shifter or voltage divider for the Echo Pin (which outputs +5V when the returned pulse is received) is required when HC-SR04 is interfacing with 3.3V device such as Raspberry Pi and ESP8266.
Voltage Divider (for the Echo Pin)
Arduino Sketch (without using the New Ping Library)
The sketch below doesn't use the new ping library.
Arduino Sketch (using the New Ping Library)
Somehow I cannot tell there is much difference in the result from not using the newping library and from using the newping library. For cross-platform compatibility sake, perhaps, it's better not to use the newping library (the sketch using the newping library will fail the compilation for ESP8266 and the solution is to use a version of newping library developed for ESP8266 - see the last link in the reference section below for detail.).
NewPing Library for Arduino
Ultrasonic library for esp 8266 using arduino ide