from xkc_kl200_python import XKC_KL200
sensor = XKC_KL200(port="/dev/serial0", baudrate=9600)The default connection settings are:
- Baud rate:
9600 - Timeout:
1.0second - Address:
0xFFFF
On Raspberry Pi, /dev/serial0 is the recommended UART device in most setups. If you use a USB serial adapter instead, the device will often be something like /dev/ttyUSB0.
Before using the library on a Pi:
- enable the hardware UART
- disable the Linux serial console on that UART
- ensure the sensor is in
UARTcommunication mode - verify the sensor UART electrical level is compatible with the Pi
3.3VUART - wire
TX -> RX,RX -> TX, andGND -> GND
from xkc_kl200_python import XKC_KL200
with XKC_KL200(port="/dev/serial0", baudrate=9600) as sensor:
print(sensor.read_distance())import time
from xkc_kl200_python import XKC_KL200, XKC_KL200_ReadError
with XKC_KL200(port="/dev/serial0", baudrate=9600) as sensor:
while True:
try:
print(sensor.read_distance())
except XKC_KL200_ReadError:
print("Read failed")
time.sleep(0.1)read_distance() always sends a measurement request and waits for the matching
response frame. If that fresh read fails, it raises instead of returning cached
data. If you want repeated readings, keep the loop in your own code.
This library intentionally does not expose an upload mode. The goal is to keep the UART contract predictable and easy to debug: one command, one response, one parsed measurement. Repeated reads stay in application code rather than inside the library.
The library exposes helpers for:
change_addresschange_baud_rateset_led_modeset_relay_modeset_communication_modehard_resetsoft_reset