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.

  • 505parts on the bench
  • 45boards running now
  • 1.00xreal time, on every board
Arduino Uno: parking sensorlive0.000 s 0.00x
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.

How it works

Three steps from a blank tab to a running circuit.

  1. Open the editor

    It runs in your browser tab. Nothing to install, and you need no account to start.

    Your first project
  2. Wire it and write the code

    Drag parts onto a breadboard, run each wire from pin to pin, and write the sketch beside it.

    Wiring
  3. Press Run

    Your sketch compiles in the tab and runs on a model of the chip, with the Serial Monitor beside it.

    Run and serial
Reviews

What makers say about Mokxi.

“The AI checker helps check and correct my circuits, and it has way more components than Tinkercad. As a student I really appreciate the tool.”

ChloeEngineering student

“I’d been looking for a real online electronic simulator that was intuitive and complete when I found Mokxi. I was able to quickly import a Fritzing file and my Arduino sketch and start testing. Mokxi has all of the makings for the best online Arduino simulator I have been able to find!”

JayModel builderFeatured in his DioramaOS post

“I’m using Mokxi to develop and simulate an STM32-based four-wheel-drive robot car before building the actual hardware. It allows me to experiment with DC motor control, PWM, RPM measurement using photointerrupters, and different acceleration, braking, and reversing strategies, helping me understand and refine the system before implementing it in real life.”

NusretRobot car builder, Hamburg

Share your story

FAQ

Questions, answered.

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.

Measure a distance

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