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.
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!
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:

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:
- Send SMS from Raspberry Pi using the GSM module
- Connect to Raspberry Pi from your Laptop/Desktop using VNC Viewer
- Not Enough ROM/RAM error with micro-controllers
- Read ThingSpeak Channel using ESP8266 and Arduino
- Reading SMS with Arduino
- Caller ID detection using Arduino
- Make an MPPT Solar charge Controller with Synchronous Buck Converter
- STM32 as USB Device
- Energy-saving innovative power switch for microcontroller circuit
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:
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:
or,
Visit LabProjectsBD.com for more inspiring projects and tutorials.
Thank you!
0 Comments