Range sensors and lidar
Ultrasonic, time-of-flight, 2-D lidar and mmWave presence sensors: the HC-SR04 timing and beam cone in a scene you drag (with multipath ghosts), the speed of sound against temperature and humidity, a ToF range/ambient model and a VL53L5CX 8×8 grid, a live LD06/RPLIDAR point cloud over Web Serial or a simulated room with an occupancy grid and toy ICP SLAM, LD2410 gate energies and LD2450 targets, and a comparison table with a recommender.
Added
Works with
- Ultrasound, Doppler and dose numbers Medical engineeringthe pulse-echo maths in medicine — depth from echo time, impedance, Doppler — and the same HC-SR04 exercise
- Serial monitor and ESP flasher IoT and embeddeda plain Web Serial terminal to check a sensor's baud rate and raw output before decoding it here
- Sensor data logger Sensorslog an HC-SR04 or ToF distance printed by your board over time, with markers and CSV export
- Kalman filter playground Mathematicssmooth noisy range readings, and fuse them with motion — the next step after the toy SLAM here
- Sensor register decoder IoT and embeddeddecode the I2C registers of the other sensors on the same board
How to use it
Four kinds of distance sensor a maker reaches for, each with its own physics and its own way of going wrong. Every tab works without hardware — the scenes, rooms and radars are simulated — and three of them talk to the real part over Web Serial.
How to use it
-
Ultrasonic — pick a module (HC-SR04, JSN-SR04T, MaxBotix LV-EZ) and a scene. The top view traces the beam: green rays come straight back from a surface that faces the sensor, purple ones arrive after a mirror bounce and give a ghost reading. Drag the sensor, the poles, the boxes and the wall ends; tap one and make it soft (fabric) or delete it. The echo plot underneath shows every echo and the threshold: the reading is the first echo over it, not the strongest. Set the air temperature and humidity to see the speed of sound change. Below: the trigger/burst/echo timing for the current reading, the module’s limits, and code for Arduino
pulseIn(), an ESP32 interrupt and MicroPythontime_pulse_us. -
Time of flight — choose a VL53L0X, VL53L1X, VL53L5CX, TFmini-S or TF-Luna and a ranging mode; set the timing budget, the target’s reflectance (white paper to black cloth), the ambient light and the distance. You get the reading or "no target", the maximum range in those conditions, the repeatability and the spot size. On the VL53L1X, shrink the ROI to narrow its 27° cone to ≈ 15°. The VL53L5CX panel ray-casts each of its 8×8 (or 4×4) zones at a wall, a box and a hand you drag on the grid — or press Wave a hand. A TFmini or TF-Luna on a USB–serial adapter can be connected for live readings.
-
2D lidar — on Simulated room a virtual lidar (LD06, STL-27L or an RPLIDAR) spins in a small flat: drag the robot, turn it by its ring, or press Drive a tour. Each simulated revolution is encoded into the sensor’s own packets and fed through the same decoder a real one uses. The right view is the live point cloud coloured by intensity, with the scan rate, points per second and packet counts; the left view builds an occupancy grid from the scans. Edit the room: add walls, boxes and pillars; make a wall glass or black and watch what the lidar makes of it. Export the last scan as CSV or PLY. On Web Serial, connect a real lidar.
-
mmWave radar — the LD2410 simulator plays a story (someone walks in, sits down, leaves), or set the person yourself. The bar chart shows each 0.75 m gate’s moving and still energy against its threshold, and the history the last 30 s. Change a gate’s thresholds and see what still counts as a person, or switch on a ceiling fan. On Web Serial the page turns on engineering mode, reads the parameters, and writes thresholds you choose. The LD2450 plots up to three targets' x/y positions with trails.
-
Compare — pick what you want to do; the three best fits are explained, and the table of range, accuracy, field of view, rate, interface, price, sunlight and glass/black-surface behaviour is sorted to match.
Connecting real sensors
Web Serial needs desktop Chrome, Edge or Opera. Use a 3.3 V USB–serial adapter (CP2102, CH340, FT232): the sensor’s TX to the adapter’s RX, ground to ground, and 5 V to the sensor from USB.
| Sensor | Baud | Notes |
|---|---|---|
LDROBOT LD06 / LD19 |
230400 |
Streams as soon as it has power; nothing is sent to it. The PWM pin can be left floating for the default 10 Hz. |
LDROBOT STL-27L |
921600 |
Same packet format as the LD06, more points per packet stream. |
Slamtec RPLIDAR A1 / A2M8 |
115200 |
The page sends STOP, then SCAN (A5 20) or EXPRESS_SCAN (A5 82 …). On Slamtec’s USB adapter DTR runs the motor; the page drives it low to start. |
Slamtec RPLIDAR C1 |
460800 |
Standard scan; the motor runs on its own. |
Benewake TFmini-S / TF-Luna |
115200 |
The TF-Luna must be in UART mode (pin 5 open or high). |
HLK-LD2410 / B / C, LD2450 |
256000 |
Engineering mode is switched on for the LD2410 when you connect. The B/C models can also change baud rate and use Bluetooth — not covered here. |
How it works
Ultrasound
The HC-SR04 sends eight cycles of 40 kHz after a 10 µs trigger, then holds ECHO high until the first echo arrives, so the distance is d = c·t/2. The speed of sound comes from the ideal gas, c = √(γ_RT_/M), with water vapour mixed in at the mole fraction h = RH·psat(T)/p (Buck’s saturation pressure), which lowers the molar mass and very slightly γ. That gives 343.2 m/s at 20 °C in dry air; 50 % humidity adds ≈ 0.6 m/s, matching the often-quoted c ≈ 331.4 + 0.606 T + 0.0124 RH.
The beam is traced as 121 rays across twice the datasheet half-angle, weighted by a Gaussian beam pattern whose −6 dB point is the half-angle. At 40 kHz (λ ≈ 8.6 mm) most hard surfaces are mirrors, so a ray returns only if it meets a surface within 10° of its normal; otherwise it reflects and is traced again (up to two bounces). Each returning ray’s strength is pattern × reflectance × cos⁴(incidence) / path² × air absorption (≈ 1.3 dB/m), echoes within 3 cm are merged, and the first group over a fixed threshold is the reading. It is a teaching model — real echoes also diffract around edges, and every module’s threshold and gain ramp differ — but it reproduces the things that catch people out: a thin pole only seen near the beam’s centre, a tilted wall that vanishes, soft furniture that reads through, and corridor ghosts.
Time of flight
The ToF model is approximate and says so on the page. Signal photons S = K·ρ·T/d² for a target that fills the cone, ambient counts A = Ka·E·T, and a target is reported while S/√(S + A) ≥ 5. K and Ka for each mode are fitted to two points from the datasheet: the maximum range on a white target in the dark, and under ST’s 200 kcps/SPAD ambient test (for the VL53L1X: short 136/135 cm, medium 290/76 cm, long 360/73 cm; for the TFmini-S: 12 m indoors and 7 m at 100 klux). It then predicts the trends: range ∝ √ρ (the long mode’s 1.6 m on 17 % grey against the datasheet’s 1.7 m), SNR ∝ √budget, and why the short mode is the one to use in a bright room. The VL53L1X field of view scales with the ROI side, 27° at 16 × 16 SPADs down to ≈ 15° at 4 × 4 (ST UM2555 and AN5191). The VL53L5CX grid casts one ray per zone across 45° × 45° and reports the perpendicular distance, as the ULD driver does.
Lidar protocols
-
LD06 / LD19 / STL-27L (LDROBOT development manual): 47-byte packets, header
54 2C(2C= one packet type, 12 points), rotation speed in °/s, a start angle and an end angle in 0.01°, 12 × (distance in mm, intensity), a 15-bit millisecond timestamp, and a CRC-8 over the first 46 bytes — polynomial 0x4D, initial value 0, from the manual’s 256-entry table. Point angles are interpolated from start to end, wrapping through 0°. The lidar turns clockwise; the page converts to counter-clockwise angles. -
RPLIDAR (Slamtec Interface Protocol and Application Notes, LR001): requests are
A5 cmd, orA5 cmd len payload checksumwith the checksum the XOR of all bytes. SCAN (A5 20) answers with the descriptorA5 5A 05 00 00 40 81and then 5-byte nodes: quality (6 bits) with the start flag S and its inverse, the angle in Q6 with a check bit C = 1, and the distance in Q2 mm. EXPRESS_SCAN (A5 82 05 00 00 00 00 00 22) sends 84-byte capsules: sync nibbles A and 5 with a checksum, a start angle, and 16 cabins of two 14-bit distances with 6-bit angle offsets; each capsule’s 32 points are spaced between its start angle and the next capsule’s, as in the Slamtec SDK.
The simulated lidar casts one ray per point against the room’s segments, boxes and circles, adds Gaussian range noise (1 cm, or 1 % of range for the RPLIDAR A1/A2), computes an intensity from reflectance, incidence angle and range, drops returns from black surfaces far away, and sees glass only within ≈ 14° of square-on. The occupancy grid is the log-odds inverse sensor model of Thrun, Burgard and Fox (Probabilistic Robotics, ch. 9): every cell a beam crosses gets −0.4, the end cell +0.85, clamped at ±4. The toy SLAM is scan-matching odometry: point-to-line ICP (Chen and Medioni, 1991; Censi’s PL-ICP, 2008) between consecutive scans — nearest neighbours from a hash grid, a shrinking outlier gate, the distance from each point to the line through its match and that match’s neighbours, linearised and solved as a 3 × 3 least-squares problem, starting from the last motion. (Plain point-to-point ICP, Besl and McKay 1992, lets the sparse points slide along walls and under-reads every turn by about a degree; it is in the engine as an option.) The increments are composed into a pose and the scans are mapped from it. With no loop closure it drifts, which is the point of showing the true path next to it.
mmWave radar
The LD2410 frames follow HLK’s LD2410 serial communication protocol: commands
FD FC FB FA, a length, a command word and value, 04 03 02 01, always between "enable
configuration" (0x00FF) and "end configuration" (0x00FE); reports F4 F3 F2 F1, a length, the type
(1 engineering, 2 basic), AA, the target state (0 none, 1 moving, 2 still, 3 both), moving
distance and energy, still distance and energy, the detection distance, and in engineering mode
the maximum gates and every gate’s moving and still energy — then 55 00 F8 F7 F6 F5. Engineering
mode is command 0x0062, reading the parameters 0x0061, and a gate’s thresholds 0x0064 (gate 0xFFFF
sets all). The LD2450 (its own HLK protocol document) reports AA FF 03 00, three targets of x, y
(mm), speed (cm/s) and resolution, and 55 CC; x, y and speed are a 15-bit magnitude with the top
bit set for a positive value. The simulator spreads a person’s movement and breathing energy
over neighbouring gates with noise, and decides the state the way the module does, with each
gate’s threshold and the "no-one" hold time.
The comparison figures are typical datasheet values; prices are rough bands. Check the datasheet of the exact part you buy — clone modules vary.
Feedback is a gift. Something to improve, a feature to add, or a bug? Tell me.
Runs entirely in your browser — network requests go only to the services named on this page.