Source: https://mokxi.com/ultrasonic-sensor-simulator

Simulator

# An HC-SR04 ultrasonic sensor simulator

Mokxi is a free online ultrasonic sensor simulator that runs in your browser, with nothing to install and no account needed. An Arduino Uno pings an HC-SR04 and beeps a buzzer faster as the wall gets closer, like a car’s parking sensor. Drag the slider on the sensor to move the wall.

Start building free or see the HC-SR04 part

- 455 parts on the bench
- 43 boards running now
- 1.00x real time, on every board

Click to open it in the editor

Drag the slider on the HC-SR04 to move the wall, or click anything else to open it in the editor.

## What you can do here

In the circuit above, or in the editor it opens.

- Set the distance with a slider, from nothing in range to 4 m
- Read the distance in centimeters in the serial monitor
- Hear the beep rate change and see the stop light come on
- Change the air temperature and see the speed of sound change the reading
- Try the VL53L0X and VL53L1X time-of-flight sensors or the Sharp IR ranger
- Build an obstacle-avoiding robot on the same sensor

## What the running circuit shows

An echo, timed and turned into a beep.

TRIG is on pin 7 and ECHO on pin 6. Ten microseconds on TRIG, and the module holds ECHO high for as long as the sound takes to come back; sound covers 0.343 mm a microsecond and makes the trip twice, so the distance is the echo time times 343 over 2000. The buzzer on pin 9 beeps with a gap that grows with the distance, from 90 ms at the stop line to 900 ms at 2 m, and inside 15 cm it holds one long note and lights the red LED on pin 8.

In the engine the wall starts at 1 m and the monitor prints “99 cm beep every 461 ms”: `micros()` counts in 4 microsecond steps, so a wall at exactly 1 m lands a hair either side of 100 cm. At 30 cm it says “beep every 154 ms” and “beep every 155 ms” in turn, for the same reason, at 10 cm “STOP”, and at 2.5 m, past the 2 m the sketch cares about, “nothing in range”.

## How the HC-SR04 is simulated

The timing, not just a number.

The part answers a real TRIG pulse with a real ECHO pulse whose width is the round trip at the speed of sound for its temperature property (20 °C by default), so pulseIn(), interrupt-timed and library code all measure it the same way they would on a bench. With the slider at 0 there is no wall and no echo, which the sketch reports as nothing in range; give pulseIn() a timeout of about 38 ms so an empty room does not hold up the loop.

What it leaves out: there are no stray echoes, no soft surfaces that swallow the ping and no 15 degree beam to aim. The real module is noisier than this one; average a few readings on a bench.

HC-SR04 with Arduino An obstacle-avoiding robot The VL53L0X laser ranger The servo simulator

## Questions

Is it free?

Yes. The circuit on this page, every part in it and the editor it opens in are on the free plan, with no card.

Do I need an account?

No. The circuit runs the moment the page does, and the editor opens without one. A free account is only for saving a project to the cloud and sharing it by link.

Does the NewPing library work?

The circuit on this page uses Mokxi’s small HC-SR04 helper. The part only sees the pulses on TRIG and ECHO, so pulseIn() code and libraries that time the echo read the same distance.

Can I use the HC-SR04 with an ESP32?

Yes, the same code runs. On real hardware an ESP32 is not 5 V tolerant, so put a divider on ECHO.

How do I change the distance?

Drag the slider on the sensor while the circuit runs, from 0 (nothing in range) up to 4 m.

## More simulators

Each one opens on a circuit that is already running.

Online circuit simulator Breadboard simulator ESP32 simulator Op-amp simulator Logic gate simulator LED circuit simulator Servo simulator LCD simulator OLED simulator NeoPixel simulator 7 segment display simulator LED matrix simulator Stepper motor simulator Motor driver simulator Rectifier simulator Digital circuit simulator Potentiometer simulator PWM simulator Relay simulator Shift register simulator DHT11 simulator RFID simulator Accelerometer simulator RGB LED simulator Buzzer simulator MOSFET simulator Circuit simulator for kids Flip-flop simulator Multiplexer simulator Full adder simulator I2C simulator UART simulator Thermistor simulator PIR sensor simulator Oscilloscope simulator Multimeter simulator DC motor simulator Traffic light simulator Diode simulator Capacitor simulator Series and parallel circuit simulator Wheatstone bridge simulator Voltage regulator simulator Soil moisture sensor simulator Zener diode simulator Solar panel simulator AC circuit simulator Thevenin simulator Decoder simulator Encoder simulator PID simulator Robot arm simulator Load cell simulator IR sensor simulator LM35 simulator HC-05 Bluetooth simulator nRF24L01 simulator

## Measure a distance

Open the circuit, move the wall and press Run. Nothing to install, and no account needed.

Start building free Open the editor
