Introduction
In this tutorial, we’ll use a compact laser distance sensor—the TDSN5200E or TDSN5200—to build a simple presence detector with a Raspberry Pi.
The sensor connects over USB, measures distances of up to 6 meters, and is small enough to fit into compact projects. It’s a great choice for detecting when someone enters or leaves an area, or for checking whether an obstacle is in front of a robot.
What You’ll Need
| Item | Details |
|---|---|
| Raspberry Pi with an OS installed | A Raspberry Pi 4 or 5 running Raspberry Pi OS is recommended |
| TDSN5200 distance sensor | Connects using a USB Mini-B cable |
| Internet connection | Required to install dependencies and download the source code |
Install the Required Tools
First, update your Raspberry Pi and install the development tools and libraries required to build the sensor utility:
sudo apt update && sudo apt upgrade -y
sudo apt install -y git build-essential libusb-dev
Configure USB Permissions
Before accessing the sensor as a regular user, you’ll need to configure the appropriate USB permissions.
Create a udev rule for the sensor:
sudo tee /etc/udev/rules.d/99-usb-tokyodevices.rules <<'EOF'
SUBSYSTEM=="usb", ATTR{idVendor}=="32ee", ATTR{idProduct}=="177c", MODE="0666"
EOF
Then reload the rules and apply the changes:
sudo udevadm control --reload-rules
sudo udevadm trigger
If the sensor is already connected, unplug it and reconnect it after running these commands.
Build the TD-USB Command-Line Tool
We’ll use TD-USB, the official command-line utility for communicating with the sensor.
Clone the repository and build the executable:
git clone https://github.com/tokyodevices/td-usb.git
cd td-usb
make
Run the executable to verify that the build completed successfully:
./td-usb
If the command displays version or usage information, the tool is ready to use.
Read the Sensor from Python
Next, we’ll write a Python script that continuously reads distance measurements and reports when an object enters or leaves the detection range.
Create a file named presence.py in the td-usb directory and add the following code:
#!/usr/bin/env python3
import subprocess
import time
# Distance threshold in millimeters
THRESHOLD = 100
CMD = ["./td-usb", "tdsn5200", "listen", "--loop"]
proc = subprocess.Popen(
CMD,
stdout=subprocess.PIPE,
text=True,
)
state = None
for line in proc.stdout:
try:
distance = int(line.strip())
except ValueError:
continue
is_present = distance < THRESHOLD
if state is None:
state = is_present
if is_present and not state:
print(
f'[{time.strftime("%H:%M:%S")}] '
f'Object detected ({distance} mm)'
)
elif not is_present and state:
print(
f'[{time.strftime("%H:%M:%S")}] '
f'Object moved away ({distance} mm)'
)
state = is_present
The THRESHOLD value determines how close an object must be to count as present. In this example, the threshold is set to 100 mm. Adjust it to suit your sensor placement and use case.
Run the Presence Detector
Start the script from the td-usb directory:
$ python3 presence.py
[12:03:15] Object detected (78 mm)
[12:03:19] Object moved away (65535 mm)
[12:03:25] Object detected (63 mm)
The script prints a message only when the detection state changes, so it won’t flood the terminal with every measurement.
Where to Go from Here
With a simple presence detector in place, you can use the sensor to build projects such as:
- Turning on a light when someone enters a room
- Activating an LED when someone approaches a kiosk or vending machine
- Detecting obstacles in front of a robot
The TDSN5200 is compact, easy to connect, and approachable for first-time sensor projects. Try adjusting the detection threshold or connecting the script to GPIO devices, notifications, or other services to turn it into a complete automation system.


