Skip to main content

wokwi-hc-sr04 Reference

HC-SR04 Ultrasonic Distance Sensor

Pin names#

NameDescription
VCCVoltage supply (5V)
TRIGPulse to start the measurement
ECHOMeasure the high pulse length to get the distance
GNDGround

Attributes#

NameDescriptionDefault value
distanceInitial distance value, in centimeters"400"

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:

UnitDistance
CentimetersPulseMicros / 58
InchesPulseMicros / 148

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#

#define PIN_TRIG 3
#define PIN_ECHO 2
void setup() {
Serial.begin(115200);
pinMode(PIN_TRIG, OUTPUT);
pinMode(PIN_ECHO, INPUT);
}
void loop() {
// Start a new measurement:
digitalWrite(PIN_TRIG, HIGH);
delayMicroseconds(10);
digitalWrite(PIN_TRIG, LOW);
// Read the result:
int duration = pulseIn(PIN_ECHO, HIGH);
Serial.print("Distance in CM: ");
Serial.println(duration / 58);
Serial.print("Distance in inches: ");
Serial.println(duration / 148);
delay(1000);
}

Try this example on Wokwi

Simulator examples#