Posted on Leave a comment

Assign fixed USB port names to your Raspberry Pi

Raspberry Pi Fixed USB port name

If you have multiple devices connected to the USB ports of your Raspberry PI, the automatically assigned USB names could unexpectedly change after a reboot and could end up confusing when using this USB port name in your scripts or Node-RED flows. Your device assigned to ttyUSB1 could be suddenly assigned the ttyUSB0 port name for example. In this tutorial we’ll learn how to assign a fixed USB port name for each connected device. After completed this tutorial, you will be able to assign a fixed USB port name to each connected device and eliminate any possible mismatch. Afterwards, this will allow you to use this self-assigned name in your scripts.

What you’ll need for this tutorial

For this tutorial, you need to have your Raspberry Pi running on Raspbian.

Figuring out the connected devices

First, we need to find out which device has been assigned to which USB port number. The easiest way to figure this out is to disconnect all devices and reboot your Pi. Once your Pi is up and running, connect the first device and open the Terminal.

At the command line we enter :

dmesg | grep ttyUSB

ttyUSB0

 

Start a list and write down the information of your first device:

  • the name of your connected device (you can’t find it in the terminal window, instead you give your device a name that will allow you to recognize it later): in our case : DEVICE1
  • the characters after “usb” : in our case : 1-1.2
  • the automatically assigned USB port : in our case : ttyUSB0

Connect your second device now and enter the same command at the command line :

ttyUSB1

 

Have a look at the newly added line on your Terminal and add following information to the list you just started:

  • the name of your second connected device : in our case : DEVICE2
  • the characters after “usb” : in our case : 1-1.3
  • the automatically assigned USB port : in our case : ttyUSB1

If you have more than 2 connected devices, continue this process by connecting one by one each additional device to your Pi.

Looking for the attributes of your connected devices

To be able to recognize a connected device later, we’ll use some specific attributes of that device. We’re looking for the attributes which have unique properties. Typically these attributes are : vendor ID, product ID, serial number.

Using the automatically assigned USB port names we wrote down previously, we are able to find the attributes for all our devices. For the device connected to ttyUSB0 for example, we have to enter following command in the Terminal :

udevadm info --name=/dev/ttyUSB0 --attribute-walk

The info we get here, are all the attributes of the complete chain of devices linked to the specified USB port. We now have to take our previously written list and look for the characters after “usb”. In our case we look for : “1-1.2” .

 

In the list of attributes, we now have to choose some unique properties of the device. In our case we’ll take following 2 attributes :

  • idProduct : 7523
  • idVendor : 1a86

Write the attribute name and value down, as we’ll need them later.

Repeat the same command in the Terminal for your other USB port names : ttyUSB1 , … And again write down the gathered attributes and values. Make sure at least one value is different between all your devices. If necessary, to be able to distinguish your devices, you can choose more attributes.

Creating the file with the USB port name rules

It’s time now to create the link from the USB ports to the devices. For this, we’ll create a file which will specify the rule for each USB device based on the unique properties we just wrote down. In the Terminal, enter following command to access the file with the rules :

sudo nano /etc/udev/rules.d/10-usb-serial.rules

An empty file should appear on your screen. For each device we’ll write one line with the rule. At the end of the line, type the device name you have chosen previously. In our case “ttyUSB_DEVICE1” for example. Replace the code below with the attributes and the values with those of your written list. In our example it looks as follows :

SUBSYSTEM=="tty", ATTRS{idProduct}=="7523", ATTRS{idVendor}=="1a86", SYMLINK+="ttyUSB_DEVICE1"
SUBSYSTEM=="tty", ATTRS{idProduct}=="6001", ATTRS{idVendor}=="0403", SYMLINK+="ttyUSB_DEVICE2"

Once you’ve finished, use Ctrl + x to exit. And save the changes by entering “Y”.

Loading the new rules

Enter following command in the Terminal to let the rules take effect :

sudo udevadm trigger

Checking the new USB port names

 

You can check the new names you just created by entering the following command in the Terminal :

ls -l /dev/ttyUSB*

 

As shown in the window above, you should see the new names (in light blue) of the USB ports. You can now use these names in your scripts or Node-RED flows. Even after rebooting or when the device has been disconnected, the name will remain the same and there is no chance for confusing any more.

Remark : If a self assigned USB port name doesn’t appear, there is a big chance you made a typo in the file with the rules. Check your attribute names and attribute values.

Great! You made it much more stable now to access USB connected devices to your Raspberry Pi.

Raspberry Pi GPIO discovery kit And if you are interested in learning to program electronic components on your Raspberry Pi, don’t hesitate to visit our shop. We have a nice kit which contains all the things you need tot start.