On an STM32, receive the rangefinder’s UART stream with DMA and the UART idle-line interrupt (HAL_UARTEx_ReceiveToIdle_DMA), push the bytes through a small ring buffer, and verify each frame’s checksum before using the distance. The parser comes from the open-source erdi-lrf-stm32-hal C99 core; the wiring is TX to MCU RX, RX to MCU TX, common ground, 3.3 V logic.
Wiring
| Module | STM32 (Nucleo-F411RE example) |
|---|---|
| TX | UART RX (PA12, USART6) |
| RX | UART TX (PA11, USART6) |
| GND | GND |
| VCC | Own supply per the model manual |
Check the module’s UART logic level against your MCU (3.3 V) before connecting; supply voltage is not UART voltage. Use a level shifter if the module TX is higher than 3.3 V.
Why DMA and idle-line reception
Family B modules (LRF22VB, LRF50VB, H series) stream 4-byte frames continuously at 460800 baud. A per-byte RX interrupt at that rate wastes CPU. With DMA the hardware fills a buffer in the background and you are called only when the line goes idle or the buffer is half or fully filled, so variable-length bursts are handled without polling every byte.
Frame format (from the repository)
| Family | Frame | Check | Baud |
|---|---|---|---|
| B (stream) | 5C lo hi chk (LRF1500H: 5C lo mid hi chk) |
~(sum of distance bytes) | 460800 |
| A (polled) | request 55 AA 88 FF FF FF FF 84, reply 55 AA 88 STA FF DH DL SUM |
reply sum includes the two header bytes | 115200 |
The distance is little-endian. H series count centimetres, VB series count millimetres, family A is metres x 10. Example: 5C 02 11 EC is 43.54 m on an H-series module and 4.354 m on a VB-series module.
Configure the UART and DMA
Set the UART to 8N1 at the model’s baud rate (460800 for LRF22VB, LRF50VB and H series; 115200 for the polled models). The RX DMA must be DMA_CIRCULAR, byte width, memory increment on. In the repository example the 16 MHz HSI clock uses 8x oversampling for baud accuracy at 460800; on your own clock tree, verify the baud error.
Receive to idle with a ring buffer
The parser below is the dependency-free core from include/erdi_lrf_core.h: erdi_parser_feed() takes one byte at a time, validates the checksum, resynchronises on the header and returns true when a valid frame completes. The DMA wiring around it uses the standard STM32 HAL API.
#include "erdi_lrf_core.h"
#include "stm32f4xx_hal.h"
#define RX_BUF 128u
extern UART_HandleTypeDef huart6;
static uint8_t rx[RX_BUF];
static uint16_t old_pos = 0;
static erdi_parser_t parser;
static volatile float last_m = 0.0f;
static volatile bool last_oor = false;
static void feed(const uint8_t *p, uint16_t n) {
erdi_measurement_t m;
for (uint16_t i = 0; i < n; i++) {
if (erdi_parser_feed(&parser, p[i], &m)) {
last_oor = m.out_of_range;
last_m = m.metres; /* metres, unit handled per model */
}
}
}
void lrf_start(void) {
erdi_parser_init(&parser, erdi_find_model("LRF22VB"));
HAL_UARTEx_ReceiveToIdle_DMA(&huart6, rx, RX_BUF);
}
/* Called on idle line, half transfer and transfer complete. */
void HAL_UARTEx_RxEventCallback(UART_HandleTypeDef *huart, uint16_t size) {
if (huart->Instance != USART6) return;
if (size != old_pos) {
if (size > old_pos) {
feed(&rx[old_pos], size - old_pos);
} else { /* DMA wrapped around */
feed(&rx[old_pos], RX_BUF - old_pos);
feed(&rx[0], size);
}
old_pos = size;
}
if (old_pos == RX_BUF) old_pos = 0;
}
In circular mode the DMA keeps writing into the same buffer, so the callback only tracks how far it has read (old_pos). If you lose the transfer after a UART error, call HAL_UARTEx_ReceiveToIdle_DMA again from HAL_UART_ErrorCallback. This receive-to-idle wrapper is our illustration of the public HAL API; the repository’s own driver uses a polled circular-DMA variant instead (see below).
Polled circular DMA (repository driver)
The repository’s HAL wrapper reads the DMA counter in the main loop instead of using idle events. It is the tested path:
#include "erdi_lrf_hal.h"
static erdi_hal_t lrf;
/* UART 8N1 at erdi_hal_baud("LRF22VB"), RX DMA circular, byte-wide */
erdi_hal_start(&lrf, &huart6, "LRF22VB");
for (;;) {
erdi_measurement_t m;
if (erdi_hal_poll(&lrf, &m) && !m.out_of_range) {
use_distance(m.metres);
}
}
For polled family A models call erdi_hal_request(&lrf) at your rate and then erdi_hal_poll(). Poll at least every ERDI_HAL_DMA_BUF / (baud / 10) seconds: 128 bytes at 460800 baud is about 2.8 ms of data. For other families build with -DERDI_HAL_INCLUDE='"stm32g4xx_hal.h"'. The repository notes that its Nucleo example builds in CI but has not yet been run on a bench with a live module, so verify on your own hardware.
Common pitfalls
| Symptom | Likely cause | Fix |
|---|---|---|
| Frequent checksum errors at 460800 | Baud error from the clock source | Use a PLL/HSE clock or 8x oversampling |
| Missing frames | Buffer overrun, slow main loop | Enlarge the buffer or poll from a timer |
| Distance 10x off | Wrong unit assumption | VB mm, H cm, family A metres x 10 |
| No data | TX/RX swapped, no common ground | Module TX to MCU RX |
| Zero or maximum at start-up | Supply sag | Bulk capacitor, separate regulator |
FAQ
Do I need the idle interrupt for a free-running stream?
No. The parser resynchronises on the 5C header, so the half and full transfer events are enough. Idle detection simply delivers data sooner when frames arrive slowly.
Can I use this on STM32G4, L4 or H7?
Yes. The HAL calls are the same; change the HAL header include and the DMA channel or stream for your device.
What if my model is polled (family A)?
Send 55 AA 88 FF FF FF FF 84 with HAL_UART_Transmit and parse the 8-byte reply with the same core; STA equal to 1 means a valid echo.
Where is the frame format for other models?
See the model page at erdilrf.com/models for the frame format and units of your exact model. Questions: tommy@erdimail.com.
Category pages: Laser distance sensor · Laser rangefinder module
Models mentioned in this article
- LRF22VB905 nm · up to 20 m · UART · I2C · 1.5 g1 pc $12 · shipping includedSee price & buy
- LRF50VB905 nm · up to 50 m · UART · I2C · 1.3 g1 pc $70 · shipping includedSee price & buy
- LRF1500H905 nm · up to 1.5 km · UART · I2C · 20 ± 2 g1 pc $400 · shipping includedSee price & buy