lms-line-sensor is a MicroPython and MicroBlocks driver for the LMS Line Sensor board. It communicates with the sensor over I2C and exposes a small API for reading raw or calibrated data, calibration management, and LED control.
- Reads 8 light sensor channels in a single transfer.
- Exposes position, derivative, and line shape.
- Supports raw and calibrated sensor modes.
- Starts calibration and stores calibration values in EEPROM.
- Controls the IR emitter and RGB LED display modes.
- Aimed at ESP23 (LMS-ESP32)
- Open ViperIDE on your device
- Go to Tools → Package Manager
- Select Install Package via Link
- Enter the package link:
https://github.com/antonsmindstorms/lms-line-sensor.git - Follow the on-screen prompts to complete installation
Open a microblocks editor and drag LMS Line Sensor into the browser window.
This package targets MicroPython devices. For packaging and distribution it is published on PyPI, but it must run on hardware that provides the machine module.
Install from PyPI:
pip install lms-line-sensorCopy the installed line_sensor.py module to your MicroPython device if your deployment flow does not install packages directly on the board.
from time import sleep
from line_sensor import LineSensor
sensor = LineSensor(scl_pin=4, sda_pin=5, device_addr=51)
sensor.ir_led_on()
sensor.load_calibration_from_rom()
sensor.mode_calibrated()
while True:
position = sensor.position()
derivative = sensor.position_derivative()
print(position, derivative)
sleep(0.1)Here's a simple program that reads the line shape and distance from center. Shape is an ASCII character in the shape of the line:
SHAPE_NONE = ' ',
SHAPE_STRAIGHT = '|',
SHAPE_T = 'T',
SHAPE_L_LEFT = '<',
SHAPE_L_RIGHT = '>',
SHAPE_Y = 'Y'
The LineSensor class provides the public API.
light_values()returns the 8 raw light sensor values.data(*indices)returns the full 13-byte response or selected entries.position()returns the current line position.position_derivative()returns the derivative of the line position.shape()returns the sensor-reported shape metric.mode_raw()switches to raw reading mode.mode_calibrated()switches to calibrated reading mode.start_calibration()starts sensor calibration.ir_led_on()andir_led_off()control the IR emitter.rgb_led_mode(mode)changes the RGB LED display mode.save_calibration_in_rom()stores calibration data in EEPROM.load_calibration_from_rom()loads calibration data from EEPROM.
You can read the documentation on our API docs site or build the Sphinx documentation from source in docs. Build them with:
sphinx-build -b html docs docs/_build/html- Source module:
micropython/line_sensor.py - API docs entry point:
docs/index.rst - The Sphinx configuration mocks the MicroPython
machinemodule so docs can be built on desktop Python. - Submit update to Pypi:
- Update version in line sensor.py
- Update version in package.json
- Activate venv
rm -rf ./dist && python -m build && twine upload dist/*
