Python library for serial laser distance measurement modules
Project description
serial-laser-ranger
Python library for serial laser distance measurement modules.
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.
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 powerERR-14: Calculation errorERR-15: Laser low powerERR-18: Weak signal or measurement time too longERR-20: Strong ambient lightERR-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
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
- DFRobot SEN0366 Wiki - Detailed documentation and specifications
- Electromaker Product Page - Additional product information
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
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file serial_laser_ranger-0.1.2.tar.gz.
File metadata
- Download URL: serial_laser_ranger-0.1.2.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
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
24c65a534f56b7a7891a393392b5d68aa674d0cb24b21f44312d8ff9c24e4d56
|
|
| MD5 |
582c93103de02b09d29526c689897e4c
|
|
| BLAKE2b-256 |
78d311965e27f04aed89d46a8b76b0ccfe551292d856c0ba04eb69e8d6bbea4c
|
File details
Details for the file serial_laser_ranger-0.1.2-py3-none-any.whl.
File metadata
- Download URL: serial_laser_ranger-0.1.2-py3-none-any.whl
- Upload date:
- Size: 19.7 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.12.0
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
708ebc3472ca840f6a2fc39ce81d05f57897a013eb2c4e3a5db53de39b58484b
|
|
| MD5 |
91c4319b1f1e440bb9bc68ed94815cb9
|
|
| BLAKE2b-256 |
84c62acd12a01c110eea1065a7ee9ef8753e1518b09da160c2a63b5c0d93da2c
|