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 ⚠️

Working with electricity involves serious risk. Ensure you have the necessary skills and take proper safety precautions before attempting any electrical projects. Proceed at your own risk — the author assumes no responsibility for any damage, injury, or issues resulting from the use or misuse of the information provided.

Advertisements

All content on this website is original and protected by copyright. Please do not copy or reproduce content without permission. While most of the resources shared here are open-source and freely accessible for your learning and benefit, your respect for our intellectual effort is appreciated.

If you find our tutorials helpful, consider supporting us by purchasing related materials or sharing our work — it helps keep the content flowing.

Need help or have questions? Leave a comment below — the author is always happy to assist!

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
import serial.tools.list_ports
GSM_port_name = 'CP2102'
port_range = 20

def find_port(cnt_p,name):
    port=''
    print('Finding port for GSM')
    while(cnt_p):
        cnt_p-=1
        p=serial.tools.list_ports.comports()
        n=len(p)
        for i in range(0,n):
            des=p[i][1]
            if name in des:
                port = p[i][0]
                cnt_p=0
                print('Found Port for GSM:'+str(port))
                break
            else:
                print('.\n')
        time.sleep(0.5)
    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

End:

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

Liked this article? Subscribe to our newsletter:

Loading

or,

Visit LabProjectsBD.com for more inspiring projects and tutorials.

Thank you!


MKDas

Mithun K. Das. B.Sc. in Electrical and Electronic Engineering (EEE) from KUET. Senior Embedded Systems Designer at a leading international company. Welcome to my personal blog! I share articles on various electronics topics, breaking them down into simple and easy-to-understand explanations, especially for beginners. My goal is to make learning electronics accessible and enjoyable for everyone. If you have any questions or need further assistance, feel free to reach out through the Contact Us page. Thank you for visiting, and happy learning!

0 Comments

Leave a Reply

Avatar placeholder

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