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.

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.1.tar.gz (10.4 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.1-py3-none-any.whl (19.7 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: serial_laser_ranger-0.1.1.tar.gz
  • Upload date:
  • Size: 10.4 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.1.tar.gz
Algorithm Hash digest
SHA256 cbc526071a0bdacba60b552baf62dba2ec9046249295e556db276bb2fd3ebdcf
MD5 4654f2322d57586bac9e6ca4d8709495
BLAKE2b-256 0eb8eebb6a44e8b82884fe328c643607b542fa9645c42e69a662f06d3a849d79

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for serial_laser_ranger-0.1.1-py3-none-any.whl
Algorithm Hash digest
SHA256 d5a00b84f6ed30cead05d5dbb88dacc4598a6a55e5e985e3be0cd5d258063ff2
MD5 929d2243569d60f0e32d7119d941aade
BLAKE2b-256 f045d95cd515a9a18fd1369cbd71c78b78ab8bb57ea3a1c7b3f0cdbd0369933a

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