I'm very new to Raspberry Pi/Linux/Python world. I'm trying to identify one of the ports on my RPi, so if someone, for example, connects a USB Flash drive (or anything) to that specific port an action will occur like turning on a LED and if it's disconnected the LED will turn off. I want the program to run infinitely until interrupted.

I've already downloaded PyUSB, and I tried coding but it doesn't work because the only way I figured out how to detect if something is connected to the port is by identifying the idVendor and idProduct of the thing I'm going to connect and then if the program sees that the product is connected then it turns on the LED.

Since I won't always connect the same device to the USB port, I don't think that:

dev = usb.core.find(idVendor=0x****, idProduct=0x****)

will work.

3 Answers 3


lsusb will show the USB devices. This has a number of options e.g. lsusb -t will print a tree.

/:  Bus 01.Port 1: Dev 1, Class=root_hub, Driver=dwc_otg/1p, 480M
    |__ Port 1: Dev 2, If 0, Class=hub, Driver=hub/5p, 480M
        |__ Port 1: Dev 3, If 0, Class=vend., Driver=smsc95xx, 480M
        |__ Port 3: Dev 4, If 0, Class=HID, Driver=usbhid, 1.5M
        |__ Port 4: Dev 5, If 0, Class=vend., Driver=rt2800usb, 480M
        |__ Port 5: Dev 6, If 0, Class=HID, Driver=usbhid, 1.5M
        |__ Port 5: Dev 6, If 1, Class=HID, Driver=usbhid, 1.5M
  • yeah, but if I already know the Port that I want to designate for the turn on/off of the LED when a device is plugged into it how can I command it to notify me when is being used? Oct 30, 2014 at 13:36
  • Because for finding an specific device being used (one that I know its id) I can use usb.core.find(idVendor=0x****, idProduct=0x****), but I don't know what to use for a port... Oct 30, 2014 at 13:40

One thing you could do, sorta building off of @Milliways answer, is use a python library like popen or another similar tool to run lsusb and parse the output, looking for whether your port or your device shows up. This would allow you to react to different devices being plugged in as well as port because lsusb gives a lot of info. I think there are other ways to do this as well, but that's one way that's definitely do able.

Also you asked "how can I command it to notify me right when it is being used?" and I believe the only way to achieve this functionality (without like editing drivers or doing something low level like that) is to have a program that is always running and continuously checks, so that it can react immediately. If you need to run other stuff on your pi and don't want it to be constantly checking you can also check on a timer, ie have it check every minute or so. Good Luck!

  • 1
    ICYI: The question was posted over 6 years ago. You probably already knew this, but when you closed with Good Luck!... I just wanted to make sure you knew that.
    – Seamus
    Nov 15, 2020 at 20:33
  • So I didn't know when I answered, but I noticed right as I hit post lol. Sorry for reviving an ancient question, but maybe my answer will help the next person who wants to do this
    – Dyskord
    Nov 15, 2020 at 21:53
  • No need to apologize, you didn't revive it... the man behind the curtain did :) I only mentioned it because IMO, new answers to old questions are rarely noticed. Several reasons for this; e.g. the Q has become irrelevant. I've decided they're largely a waste of time (hmmm...), but I still answer an "oldie" on occasion.
    – Seamus
    Nov 15, 2020 at 22:19

It may be off-topic, but I found a solution using Python. What you have to do is name your USB stick (if it is a USB stick) with a known name (say "stick") then have python try to open it from time to time (trying in the address /media/pi/stick).

It appliest to any USB device that has a known name to it. Here is an example using the OS package:

if (os.path.isdir('/media/pi/stick/')):

If the directory exists (meaning the device is connected) then you have a TRUE in the IF condition.

Disclaimer: before any of you come to comment a million weaknessess to this code, it worked for me and solved my problem, so it may solve his.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.