Skip to main content

Python library for serial laser distance measurement modules

Project description

serial-laser-ranger

PyPI version License: MIT DeepWiki

Python library for serial laser distance measurement modules.

Laser Distance Sensor Module Sensor Board

Works with generic laser ranging modules commonly found on AliExpress, as well as compatible units like the DFRobot SEN0366. These modules communicate via serial (UART) at 9600 baud.

Specifications

Parameter Value
Operating Voltage DC 3.3V - 5V
Measuring Range Indoor: 0.05 - 50m, Outdoor: 0.05 - 80m
Accuracy ±1.0mm (standard deviation)
Laser Type 620 - 690nm, Class II (<1mW)
Laser Spot 6mm @ 10m, 30mm @ 50m
Measurement Time 0.05 - 1s
Operating Temperature -10°C to +60°C
Dimensions 48 × 42 × 18mm
Weight ~60g
Protection IP40

Note: Performance may degrade in strong ambient light, extreme temperatures, or with poorly reflective surfaces.

Real-World Limitations

Despite marketing claims of 1-20Hz measurement rates, the actual maximum is ~4 Hz (~235ms per measurement). This is a hardware limitation:

  • The set_frequency() method has no observable effect on measurement speed
  • The set_data_return_interval() only adds delay between measurements; it cannot speed them up
  • Maximum continuous rate is achieved with interval set to 0

Motion constraints: If the target distance changes faster than ~25 cm/s (0.25 m/s), the sensor will reset and return errors. This limits use for tracking fast-moving objects.

Measuring Range Diagram

Installation

pip install serial-laser-ranger

Quick Start

from serial_laser_ranger import LaserRanger

with LaserRanger("/dev/ttyUSB0") as ranger:
    distance = ranger.measure_single()
    print(f"{distance:.3f} m")

Or even simpler:

from serial_laser_ranger import measure

print(f"{measure('/dev/ttyUSB0'):.3f} m")

Features

  • Single and continuous measurement modes
  • Laser on/off control
  • Configurable resolution (1mm or 0.1mm)
  • Configurable range (5m to 80m)
  • Adjustable measurement frequency
  • Distance offset calibration
  • Error handling with descriptive messages

Usage

Basic Measurement

from serial_laser_ranger import LaserRanger

with LaserRanger("/dev/ttyUSB0") as ranger:
    distance = ranger.measure_single()
    print(f"Distance: {distance:.3f} m")

Continuous Measurement

with LaserRanger("/dev/ttyUSB0") as ranger:
    # Take 10 measurements
    for distance in ranger.measure_continuous(count=10):
        print(f"{distance:.3f} m")

    # Or run indefinitely (Ctrl+C to stop)
    for distance in ranger.measure_continuous():
        print(f"{distance:.3f} m")

With Callback

def on_measurement(distance):
    print(f"Distance: {distance:.3f} m")

with LaserRanger("/dev/ttyUSB0") as ranger:
    for _ in ranger.measure_continuous(callback=on_measurement, count=100):
        pass

Configuration

with LaserRanger("/dev/ttyUSB0") as ranger:
    # Set 0.1mm resolution
    ranger.set_resolution(LaserRanger.RESOLUTION_01MM)

    # Set 30m range
    ranger.set_range(LaserRanger.RANGE_30M)

    # Set 20 Hz measurement frequency
    ranger.set_frequency(20)

    # Apply distance offset (e.g., for mounting offset)
    ranger.set_distance_offset(50)  # +50mm offset

Laser Control

with LaserRanger("/dev/ttyUSB0") as ranger:
    ranger.laser_on()   # Turn laser on
    ranger.laser_off()  # Turn laser off

Error Handling

from serial_laser_ranger import LaserRanger, LaserRangerError

with LaserRanger("/dev/ttyUSB0") as ranger:
    try:
        distance = ranger.measure_single()
    except LaserRangerError as e:
        print(f"Error {e.code}: {e.message}")

Error codes:

  • ERR-1e: Low power
  • ERR-14: Calculation error
  • ERR-15: Laser low power
  • ERR-18: Weak signal or measurement time too long
  • ERR-20: Strong ambient light
  • ERR-74: Out of range

API Reference

LaserRanger

Constructor

LaserRanger(
    port="/dev/ttyUSB0",  # Serial port
    baudrate=9600,         # Baud rate (default 9600)
    timeout=2.0,           # Read timeout in seconds
    address=0x80           # Device address (default 128)
)

Methods

Method Description
measure_single() Take a single measurement, returns distance in meters
measure_continuous(callback=None, count=0) Generator yielding continuous measurements
laser_on() Turn the laser on
laser_off() Turn the laser off
set_resolution(resolution) Set measurement resolution
set_range(range_m) Set measurement range
set_frequency(freq) Set measurement frequency (Hz)
set_distance_offset(offset_mm, negative=False) Set distance offset
set_start_point(from_top) Set measurement reference point
set_power_on_start(enabled) Enable/disable auto-start on power

Constants

Constant Value Description
RESOLUTION_1MM 0x01 1mm resolution
RESOLUTION_01MM 0x02 0.1mm resolution
RANGE_5M 0x05 5 meter range
RANGE_10M 0x0A 10 meter range
RANGE_30M 0x12 30 meter range
RANGE_50M 0x32 50 meter range
RANGE_80M 0x50 80 meter range

Hardware

Pinout Diagram

Wiring

Module Pin Connection
VCC 5V
GND Ground
TX RX on USB-Serial adapter
RX TX on USB-Serial adapter

Serial Settings

  • Baud rate: 9600
  • Data bits: 8
  • Stop bits: 1
  • Parity: None

Examples

See examples.py for practical examples including:

  • Basic measurement
  • Statistics collection
  • Real-time monitoring
  • CSV logging
  • Motion detection
  • Calibration checking

Run examples with:

python examples.py basic
python examples.py statistics
python examples.py monitor

Applications

  • UAV automatic landing
  • Robotics distance sensing
  • Industrial level monitoring
  • Electronic measurement tools
  • Security systems

References

License

MIT License

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

serial_laser_ranger-0.1.3.tar.gz (10.7 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

serial_laser_ranger-0.1.3-py3-none-any.whl (20.6 kB view details)

Uploaded Python 3

File details

Details for the file serial_laser_ranger-0.1.3.tar.gz.

File metadata

  • Download URL: serial_laser_ranger-0.1.3.tar.gz
  • Upload date:
  • Size: 10.7 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.12.0

File hashes

Hashes for serial_laser_ranger-0.1.3.tar.gz
Algorithm Hash digest
SHA256 7550cbff406654d713e838d0892d0dc1ac6f291eef2ad879f3c476c42134d49e
MD5 12b8b00bea66f6ee07352b76b79002c4
BLAKE2b-256 e3ad4c9ae682624487126fb8d7059a206d2d1786d7c5d2df77afda37d4cc6932

See more details on using hashes here.

File details

Details for the file serial_laser_ranger-0.1.3-py3-none-any.whl.

File metadata

File hashes

Hashes for serial_laser_ranger-0.1.3-py3-none-any.whl
Algorithm Hash digest
SHA256 d82076d393e59f28af77e76382e91dd968a1ee8cbc515b5aebb5e63c711d6392
MD5 30d8e641360b66db5bfded6a3ee29ebd
BLAKE2b-256 060ff080b2673c59ffd6bcd3a375b98dcf8a96f12472647331d458cf04ecb015

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page