In this article, we will learn how to detect USB ports or devices in Pi automatically. So, you do not need to search from the command box. This simple line of code helps a lot while working on projects. Anyway, let’s start.

Disclaimer: Electricity is always dangerous. Proper skill is required to work with electricity. Do work at your own risk. The author will not be responsible for any misuse or harmful act or any mistake you make. The contents of this website are unique and copyright protected. Kindly don’t do any nonsensical act copying and claiming it as yours. Most of the articles published here are kept open-source to help you. Take the knowledge for free and use it, but if you are interested you can buy the ready resources offered here. If you need any help or guidance feel free to comment below, the author will try to help you. Also, there can be affiliation links in the article. Which will not affect you anyway, but allows the author with some commission. So please don’t take it otherwise. Thanks.

Table of Contents


Manual way:

There is a manual way to detect the port from ‘sudo‘ command. Write ‘ls -m /dev/tty*‘ then the cmd pad will give you something like this:

Detecting USB Port or devices in Pi

From this, you can identify your port manually. How? Simply plug the device and write the command, and then unplug the device and do the same as before. From the difference, you can identify the device. But doing this each time while you are writing a code in Py, will be painful. So?

There is a way to find the port automatically and let the code find the exact port you need to detect.

You can check my other articles that can help you:

Automated way:

When wringing a code, just keep this part to detect the port.

import serial
import time
GSM_port_name = 'CP2102'
port_range = 20

def find_port(cnt_p,name):
    print('Finding port for GSM')
        for i in range(0,n):
            if name in des:
                port = p[i][0]
                print('Found Port for GSM:'+str(port))
    return port

Now call this ‘find_port()’ later on your main code.

p = find_port(port_range,GSM_port_name)
ser = serial.Serial(p, 9600, timeout = 1)#open USB serial

I had a GSM hat device to connect with the Pi through the USB port. And with this code, I can detect that port very easily.

Test Result:

Detecting USB Port or devices in Pi


In the end, is this helps you? If yes, then visit more again to find more helpful articles. Thanks.

For Professional Designs or Help:


Also let me know what you want to get as the next article, comment below!


Mithun K. Das; B. Sc. in EEE from KUET; Head of R&D @ M's Lab Engineering Solution. "This is my personal blog. I post articles on different subjects related to electronics in the easiest way so that everything becomes easy for all, especially for beginners. If you have any questions, feel free to ask through the contact us page." Thanks.


Leave a Reply

Avatar placeholder

Your email address will not be published. Required fields are marked *

%d bloggers like this: