When working with devices like the ESP32 family of development boards, which are widely used in IoT projects, configuring udev becomes essential to ensure smooth operations. In this article, we’ll look into what udev is, why it’s crucial for ESP32 development, and how to configure it effectively, including creating a group for device access.
Understanding udev:
udev is a device manager for the Linux kernel. Its primary function is to manage device nodes in the /dev directory dynamically. When a new device is connected to a Linux system or when existing devices are modified or removed, udev dynamically creates, removes, or modifies the device nodes, ensuring that userspace applications can access and interact with them.
Importance of udev for ESP32 Development:
ESP32 development boards requires user interaction with the development environment for tasks like flashing firmware and monitoring output. Without proper udev configuration, developers will almost certainly encounter permission issues or difficulty in recognizing the connected ESP32 device, hampering the development workflow.
Configuring udev for ESP32 Development:
To configure udev for ESP32 development boards, follow these steps:
1. Identify the device:
First, connect your ESP32 development board to your Linux system via USB. Check if the device is detected using the lsusb
command.
lsusb
Looking at my results you can see that I have 3 different ESP32 devices connected. The red device is an ESP32-S2 Saola board. This board utilizes and external USB to UART bridge from Silicon Labs. The yellow device is a ESP32-C6 DevKitM board. This board has the USB to UART bridge on the SoC. You’ll also notice that it has a JTAG transceiver also. Finally I have an ESP32-S2 Mini by WEMOS. This board doesn’t have a USB to UART bridge instead it uses Device Firmware Upgrade (DFU) to flash and monitor the board. Read my article “Using Device Firmware Upgrade” to learn more about DFU.
Device | idVendor | idProduct |
ESP32-S2 Saola board | 10c4 | ea60 |
ESP32-C6 DevKitM board | 303a | 1001 |
ESP32-S2 Mini | 303a | 0002 |
2. Create a group for device access:
It’s recommended to create a group specifically for devices like Espressif boards to manage permissions efficiently. Use the following command to create a group named “espressif” (you can replace “espressif” with your preferred group name):
sudo groupadd espressif
Now we need to add our user to the group so that we can access it’s resources. This is the command to add a user to a group “sudo usermod -aG <username>” where <username> is the name of the user being added. For this example I will add my Fedora user “fuser”:
sudo usermod -aG espressif fuser
We added our user to the espressif group, but if they are currently logged in they still will not be able to access the espressif groups resources. In order for that change to take effect you need to log out and back in. If you do not want to log out at this time you can use the “newgrp” command to temporarily change your primary group to the espressif group.
newgrp espressif
3. Setting the udev rules
First, you’ll need to create a udev rule to allocate the necessary permissions to the ESP32 device. To do this, generate a new file in the /etc/udev/rules.d directory. The filename should follow the format of PP-descriptive-name.rules, where PP denotes the priority, and the remainder of the name serves as a descriptive label. Priorities can range from 00 to 99, with lower numbers taking precedence over higher ones. For this example, let’s create a file named “99-espressif.rules” and insert the following rule:
SUBSYSTEMS=="usb", ATTRS{idVendor}=="303a", ATTRS{idProduct}=="????", GROUP="espressif", MODE="0666"
SUBSYSTEMS=="usb", ATTRS{idVendor}=="10c4", ATTRS{idProduct}=="ea60", GROUP="espressif", MODE="0666"
Notice that we have three devices, but only two rules are provided. This is because two of the devices share the same idVendor. Therefore, we can use wildcards in the idProduct field to enable one rule to handle multiple devices with the same idVendor.
4. Reload the udev rules:
After creating the udev rule, reload the udev rules using the following command to apply the changes:
sudo udevadm control --reload-rules
sudo udevadm trigger
The first command ‘sudo udevadm control –reload-rules’ instructs udev to reload all the rules from the rules directory. This allows the new rule to take effect without requiring a reboot. The second command ‘sudo udevadm trigger’ triggers udev to reapply it’s rules based on the current device configurations.