Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save cheriimoya/0ab9696766d7baeb8d5ce0eed80f130a to your computer and use it in GitHub Desktop.
Save cheriimoya/0ab9696766d7baeb8d5ce0eed80f130a to your computer and use it in GitHub Desktop.
NixOS: Arduino Connection

Arduino Connection on NixOS

First we need lsusb command from the usbutils package.

> nix-env -i usbutils
> lsusb
Bus 001 Device 001: ID 1d6b:0002 Linux Foundation 2.0 root hub
Bus 001 Device 011: ID 0403:6001 FTDI FT232 USB-Serial (UART) IC
Bus 001 Device 010: ID 0403:6001 FTDI FT232 USB-Serial (UART) IC
Bus 001 Device 005: ID 0402:5632 ALi Corp. USB 2.0 Host-to-Host Link
Bus 002 Device 005: ID 0403:6001 FTDI FT232 USB-Serial (UART) IC

Run the lsusb before and after you plug in the Arduino. If a new device appears, then this will give us some parameters we can use to identify our connection. Unfortunately the vendor:product numbers in hexadecimal are not unique, so we cannot use these to uniquely identify the USB device. However we can use the bus and device numebrs as a way of accessing them.

For example to access bus 2 device 5, you can use /dev/bus/usb/002/005. The problem with this, is that this can be different the next time you plug the device in a different port. (Also this might not be the actual serial device.)

However for Arduino, a named device will also appear in /dev/serial/usb-www.freetronics.com_.... So if you run ls /dev/serial you should be able to a persistent device file to use. This appears to be the most reliable. On my system, this redirects to /dev/ttyACM0. Which apparently means that the Arduino device is masqerading as a serial modem.

The next step to grant privileges to your Arduino program arduino or ino to allow them to access the serial device. Usually the serial devices are owned by root:dialout. So you can either launch the program with root privileges or add yourself to the dialout group with sudo usermod -a -G dialout user. Another alternative is to change the permissions on the device itself, we can do this by running chmod or to be more secure using POSIX ACL. Also I haven't tested this, but there is a POSIX capability called CAP_SYS_RAWIO which appears to grant USB access. But I'm not sure if this applies to USB serial devices.

If you just use sudo, beaware that your environment variables will change (such as your home directory), and any created files from the program will be owned by root. It is recommended to use gksudo, kdesudo or pkexec for graphical programs, as sudo wasn't for non-graphical programs. The is the main disadvantage of using sudo on the graphical programs just to access a USB serial device.

Another alternative is to create udev rules so that when a device is detected to have been connected, commands can be triggered to change the permissions of the device, or to make the device be owned by certain users. This is probably the best way to do it if you make use of device often while changing only the permissions on the device file. (Remember programs in Linux doesn't have permissions, they only inherit the ambient authority of users. Users the primary holders of privileges).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment