Skip to main content
HC-SR04 Ultrasonic Distance Sensor

Pin names

Power
Voltage supply (5V)
Digital Input
Pulse to start the measurement
Digital Output
Measure the high pulse length to get the distance
Ground
Ground

Attributes

string
default:"400"
Initial distance value, in centimeters

Operation

To start a new distance measurement set the TRIG pin to high for 10uS or more. Then wait until the ECHO pin goes high, and count the time it stays high (pulse length). The length of the ECHO high pulse is proportional to the distance. Use the following table to convert the ECHO pulse length in microseconds into centimeters / inches:

Setting the distance

To change the distance while the simulation is running, click on the HC-SR04 drawing in the diagram and use the slider to set the distance value. You can choose any value between 2cm and 400cm.

Arduino code example

Try this example on Wokwi

Simulator examples