Skip to main content
Explore ERDILRF

Press ↑↓ to move, Enter to open, Esc to close.

Laser Distance Sensor + Arduino UART Guide (2026)

Read a laser distance sensor with an Arduino over UART: connect the module TX to the board RX, RX to TX, GND to GND, start the serial port at the model’s baud rate and call the ErdiLRF library.

ERDI LRF22VB laser rangefinder module
LRF22VB, one of the modules discussed here. Specifications and price.
Module pin Arduino (Mega / Leonardo / ESP32) Note
VCC 3.3 V or 5 V per the model page (LRF1200A1: 3.3 V)
GND GND common ground
TX RX1 3.3 V TTL output
RX TX1 through a divider or level shifter from a 5 V board
#include <ErdiLRF.h>



ErdiLRF lrf(Serial1, ERDI_LRF1200A1);



void setup() {

  Serial.begin(115200);

  Serial1.begin(erdiDefaultBaud(ERDI_LRF1200A1));  // 115200 bps

  lrf.setTimeout(1000);

}



void loop() {

  ErdiMeasurement m;

  if (lrf.measure(&m) == ERDI_OK) {

    Serial.println(m.metres, 3);

  }

  delay(100);

}

Library and the full BasicMeasure example: ErdiTech/erdi-lrf-drivers (arduino/ErdiLRF). For ESP32 see the ESP32 UART guide.

Short answer: LR1000E2 and LR1200E2 document 8-byte 55 AA messages and a 115200 bps baud rate. Their electrical setup must be confirmed for the delivered module before connecting an Arduino. In particular, do not infer signal voltage, enable polarity or serial framing from a generic sketch. The examples below explain byte handling; they are not a complete hardware setup.

Two protocols, not one

Identify the exact model before writing code. The four models compared below illustrate command/response and streamed-output layouts, but a family name does not settle electrical settings, start/stop behavior or every frame length. Confirm the delivered configuration and operating mode before expecting output.

Property LR1000E2 LR1200E2 LRF22VB LRF50H
Protocol family A A B B
Frame header 55 AA 55 AA 5C 5C
Frame length 8 bytes 8 bytes 4 bytes 4 bytes
Baud rate 115200 115200 460800 (adjustable) 460800 (adjustable)
Mode command / response command / response Confirm configured output mode 1 kHz default; confirm output mode
Distance encoding actual value × 10 actual value × 10 little-endian, millimetres little-endian, centimetres
Supply voltage 3.3–5 V 3.3–5 V 3.0–3.6 V DC 9–36 V DC

Look at the distance-encoding row carefully. LRF22VB reports millimetres and LRF50H reports centimetres out of an identically shaped 4-byte frame, and nothing in the bytes tells you which. Hard-code the wrong one and every reading is off by a factor of ten.

Confirm wiring and electrical requirements first

The LR1200E2 source lists pin 6 as SW-SHOT, with both “Function Enable (active high)” and a note about compatibility with an active-low control requirement. The LR1000E2 source likewise contains both statements. Preserve both; do not choose the delivered unit’s GPIO polarity from this article. Confirm the control requirement, connector viewing direction and electrical limits before connecting or driving the pin. Do not infer a wire color from a pin number.

Two non-optional cautions:

  • Supply and UART signal voltage are separate requirements. The LR1200E2 manual does not separately specify a numerical TXD/RXD logic level. The LR1000E2 manual likewise does not add written signal-level evidence to its supply row. Confirm the delivered electrical requirement in writing before connecting a host; adding an unspecified level shifter is not a substitute for knowing both sides’ limits.
  • Use the exact supply requirement. LRF22VB lists 3.0–3.6 V DC; LRF50H lists 9–36 V DC. Check the actual board and supply design against the module’s current document, including its connector definition. Do not apply one model’s supply or signal limits to another.

LR1000E2 and LR1200E2: byte-handling helpers

The retained helper functions illustrate the documented LR1000E2/LR1200E2 command and reply byte handling. They are excerpts, not a standalone sketch: LRF denotes an already configured serial stream. They do not initialize a UART or drive an enable pin. The helper’s 500 ms default is an illustrative host-side read timeout, not a sensor response specification. LR1000E2’s source states 8 data bits but does not specify parity or stop bits; do not infer 8N1 from an Arduino default. Resolve missing serial settings, electrical levels and the enable configuration before using the current model-specific driver resources.

// Byte-handling excerpts for LR1000E2 / LR1200E2 only.



// LRF is an already configured stream; this is not hardware setup code.



// Confirm wiring, UART framing and the delivered enable configuration first.



const uint8_t FN_SINGLE = 0x88;







// Transmit checksum: function code + the four data bytes ONLY.



uint8_t txChecksum(uint8_t fn, const uint8_t *d) {



  uint16_t s = fn;



  for (uint8_t i = 0; i < 4; i++) s += d[i];



  return (uint8_t)(s & 0xFF);



}







// Reply checksum: BOTH header bytes + function code + the four data bytes.



uint8_t rxChecksum(const uint8_t *f) {



  uint16_t s = 0;



  for (uint8_t i = 0; i < 7; i++) s += f[i];



  return (uint8_t)(s & 0xFF);



}







void sendCommand(uint8_t fn) {



  uint8_t d[4] = {0xFF, 0xFF, 0xFF, 0xFF};



  uint8_t frame[8] = {0x55, 0xAA, fn, d[0], d[1], d[2], d[3], txChecksum(fn, d)};



  LRF.write(frame, 8);



}







bool readMeasurement(float &metres, uint32_t timeoutMs = 500) {



  uint8_t frame[8], n = 0;



  uint32_t start = millis();



  while (millis() - start < timeoutMs) {



    if (!LRF.available()) continue;



    uint8_t b = LRF.read();



    if (n == 0) { if (b == 0x55) frame[n++] = b; continue; }



    if (n == 1) { if (b == 0xAA) frame[n++] = b; else n = (b == 0x55) ? 1 : 0; continue; }



    frame[n++] = b;



    if (n < 8) continue;



    n = 0;                                        // resync for the next frame



    if (rxChecksum(frame) != frame[7]) continue;  // corrupt: wait for the next



    if (frame[2] != FN_SINGLE) continue;         // accept only single-shot replies



    if (frame[3] != 0x01) continue;               // STA != 1: measurement failed



    metres = (float)(((uint16_t)frame[5] << 8) | frame[6]) / 10.0f;



    return true;



  }



  return false;



}







readMeasurement accepts only single-shot replies whose function byte is FN_SINGLE (0x88), with the documented successful status and checksum. Continuous-ranging replies and other response types require separate handling against the exact model protocol; do not pass them through this single-shot helper. For these two source-documented examples, 0x89 and 0x8E identify the listed continuous and stop commands. Confirm their applicability and configuration before sending them. This article does not choose a startup delay or command rate for an unverified setup.

Keep command and reply checksum scopes separate

In the two documented models used by these helpers, the transmit and reply byte scopes differ. Keep txChecksum and rxChecksum separate, and compare the exact received message with the model source before accepting a distance. Other models may have source conflicts or driver restrictions; consult the current driver resources rather than extrapolating these helpers across the catalog.

LRF22VB and LRF50H: a four-byte parsing example

For the documented LRF22VB/LRF50H four-byte layout, the checksum is the inverted sum of the distance bytes. Confirm output mode before expecting a stream. This helper only returns raw values; its caller must handle the exact model’s invalid-result marker and unit before using a distance. It does not cover the five-byte LRF1500H frame.

// Four-byte 5C parsing example; confirm actual output configuration first.



// LRF22VB raw units are MILLIMETRES; LRF50H raw units are CENTIMETRES.



bool readFamilyB(uint16_t &raw) {



  static uint8_t f[4], n = 0;



  while (LRF.available()) {



    uint8_t b = LRF.read();



    if (n == 0) { if (b == 0x5C) f[n++] = b; continue; }



    f[n++] = b;



    if (n < 4) continue;



    n = 0;



    uint8_t sum = f[1] + f[2];              // 2nd byte through 2nd-last byte



    if ((uint8_t)(~sum) != f[3]) continue;  // checksum is the inverted sum



    raw = (uint16_t)f[1] | ((uint16_t)f[2] << 8);   // little-endian



    return true;



  }



  return false;



}



Check it against the manual’s own example, 5C 02 11 EC: the sum of 02 and 11 is 0x13, inverted is 0xEC, and the little-endian distance 0x1102 is 4354, 4354 mm on LRF22VB, 4354 cm on LRF50H. Treat an out-of-range reading as a distinct state rather than a distance: LRF22VB outputs 20000 (its range is 0–20000), LRF50H outputs 65535 (range 0–65535).

Measure the actual report cadence and host receive behavior before choosing buffer sizes or a processing rate. A listed measurement frequency does not prove that a particular Arduino, driver or application receives every valid report.

ESP32 and PlatformIO: an explicit UART2 example

This LRF3000A1 example gives the module its own HardwareSerial(2) while Serial remains the UART0 console. A local compile and link passed on 15 October 2026 using PlatformIO Core 6.2.0, platformio/espressif32@7.1.3, Arduino core 2.0.17 (the exact package below), and the existing ErdiLRF 0.1.1. No physical board or module was connected or tested.

With PlatformIO Core 6.2.0 installed, create an empty project containing these two files. The selected esp32dev profile represents a classic ESP32/WROOM-style target without PSRAM. GPIO16 (host RX) and GPIO17 (host TX) must be exposed and unused by other peripherals. These are host choices, not an ERDI connector pinout. Check the actual board, module connector orientation, supply, logic level and enable requirements before wiring; do not apply 5 V logic to ESP32 GPIOs.

platformio.ini

[env:esp32dev_uart2]



platform = platformio/espressif32@7.1.3



board = esp32dev



framework = arduino



platform_packages =



    platformio/framework-arduinoespressif32@4.20017.260907+sha.dcc1105b



lib_deps =



    erdi-tech/ErdiLRF@0.1.1



src/main.cpp

// SPDX-License-Identifier: MIT



// Local compile candidate; confirm the delivered module manual before wiring.



#include <Arduino.h>



#include <ErdiLRF.h>







// Host GPIO choices for this esp32dev/WROOM-style profile only.



// GPIO16/17 must be available and must not be occupied by PSRAM or peripherals.



constexpr int kLrfRxGpio = 16;



constexpr int kLrfTxGpio = 17;



constexpr ErdiModel kModel = ERDI_LRF3000A1;



constexpr uint32_t kConsoleBaud = 115200;







HardwareSerial lrfUart(2);



ErdiLRF lrf(lrfUart, kModel);







void setup() {



  Serial.begin(kConsoleBaud);



  // UART2 is distinct from the UART0 console. These are host pins, not an ERDI pinout.



  lrfUart.begin(erdiDefaultBaud(kModel), SERIAL_8N1, kLrfRxGpio, kLrfTxGpio);



  lrf.setTimeout(1000);



  Serial.println("LRF3000A1 single-measurement example; confirm unit revision and wiring.");



}







void loop() {



  ErdiMeasurement reading{};



  const ErdiStatus status = lrf.measure(&reading);



  if (status == ERDI_OK && reading.valid && !reading.outOfRange && !isnan(reading.metres)) {



    Serial.print("distance_m=");



    Serial.print(reading.metres, 3);



    Serial.print(" raw=");



    Serial.println(reading.raw);



  } else {



    Serial.print("status=");



    Serial.println(static_cast<int>(status));



  }



  delay(500);



}



Build only, from the project directory:

python -m platformio run --environment esp32dev_uart2

erdiDefaultBaud(kModel) returns 115200 here, matching the default in the retained LRF3000A1 manual (v1.2, October 2025; retrieved 14 October 2026). The manual also lists 9600. A supplied unit may have a different configuration: replace that first lrfUart.begin(...) argument with its confirmed baud when necessary. The console baud is a separate setting. SERIAL_8N1 is this example’s host choice; the inspected manual establishes eight data bits but does not establish parity or stop bits. Confirm those settings and the supplied revision before hardware use.

The loop requests one measurement at a time with lrf.measure(&reading), then waits 500 ms. It does not select continuous mode, and blocking/timeout time means this is not a fixed 2 Hz rate. Compilation does not verify UART replies, electrical compatibility, ranging performance or other models/boards. Main dependencies are pinned here; fresh transitive dependency resolution may still differ from the recorded build.

The example source and ErdiLRF source use MIT; retain the notice below when redistributing. Other SDK/toolchain components have their own licenses. The retained model manual is a separate manufacturer reference, not MIT-licensed by this example; no complete manual is bundled here.

MIT source notice
MIT License







Copyright (c) 2026 ERDI TECH LTD







Permission is hereby granted, free of charge, to any person obtaining a copy



of this software and associated documentation files (the "Software"), to deal



in the Software without restriction, including without limitation the rights



to use, copy, modify, merge, publish, distribute, sublicense, and/or sell



copies of the Software, and to permit persons to whom the Software is



furnished to do so, subject to the following conditions:







The above copyright notice and this permission notice shall be included in all



copies or substantial portions of the Software.







THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR



IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,



FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE



AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER



LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,



OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE



SOFTWARE.



Prepared by the ERDILRF marketing and technical support team with AI assistance.

Frequently asked questions

Why do I get valid frames but every checksum fails?

A wrong checksum scope is one possible cause, but not the only one. For the two LR examples here, the reply checksum includes the header bytes while the command checksum does not. Also verify the exact frame layout, byte alignment and source-defined rule before changing the decoder.

My distances are exactly 10× too large or too small. Why?

For the exact examples here, LR1000E2/LR1200E2 use the documented value-times-ten representation; LRF22VB uses millimetres and LRF50H uses centimetres. Confirm the model, status and unit instead of assuming one rule for an entire family.

Can I use I²C instead of UART?

On LRF22VB, yes, it exposes a shared UART/I²C interface at default address 0x52 (7-bit), with a maximum clock of 400 kHz, on the same pins 3 and 4. The H-series LRF100H and above list UART/I²C, and the LRF200H model record publishes a default address of 0x33 with a 1 kHz–400 kHz clock. The LRF50H is UART only. For the other H-series models, the address and register map are not published in the same record; request them for the exact model.

Does this code work for every ERDILRF model?

No. The helpers are limited to the named layouts and do not set up electrical connections, enable pins or serial framing. LRF1500H has a five-byte frame with a three-byte distance field and a source checksum-example conflict. Do not use the four-byte helper for it or resolve the conflicting example by assumption.

Frames not decoding on your board? Send the exact model, manual revision, host configuration and a short byte trace with private data removed. Get in touch or email tommy@erdimail.com for a model-specific review. Find the current documents and driver resources before selecting a wiring or setup example.

Need a current loop instead of a serial frame? See 4–20 mA analog output for laser distance modules.

ERDI TECH LTD · 905 nm laser rangefinder modules · tommy@erdimail.com · WhatsApp +86 133 0819 5790 · erdilrf.com

Explore knowledge

Related technical guides

Models mentioned in this article