Setting I2C permissions for non-root users

Alexander Rüedlinger - - I2C , Arch Linux , ARM , RPi2 , Raspberry Pi

This weekend I take time again to play arround with ArchLinux|ARM on a Raspberry Pi 2. My weekend project is about writing a basic sensor-based application on the ArchLinux|ARM platform. However, before I can write the first line of code to access an I2C sensor, I need to fix an I2C permission problem. Unlike Raspbian, where some things are already nicely setup like I2C permissions for non-root users, this is not the case on the ArchLinux|Arm platform :-).

I2C permission problem

For example, the listing below demonstrates the permission error that you get when you run the command i2cdetect -y 1 as a non-root user. In short, i2cdetect is a handy userspace program to scan an I2C bus for devices.

xander@alix:~
▶ i2cdetect -y 1
Error: Could not open file `/dev/i2c-1': Permission denied

Usually, the command outputs a table of detected devices on a specific I2C bus, like /dev/i2c-1. With the help of the command ls -l /dev/i2c-1, we can check why the command does not work. As the output below reveals, only the root user can read and write to the i2c-1 bus.

xander@alix:~
▶ ls /dev/i2c-1 -l
crw------- 1 root root 89, 1 Sep 25 12:48 /dev/i2c-1

Solution

Fortunately, setting up I2C permissions for non-root users is not that difficult. Basically, we need to do six things:

  1. Create new user group called i2c.
  2. Change the group ownership of /dev/i2c-1 to i2c.
  3. Change the file permissions of the device /dev/i2c-1 so users of the i2c group can read and write to the device.
  4. Add your user to the group i2c.
  5. Logout and login again so the changes take effect.
  6. Make the changes permanent with the help of udev.

Instructions

Here are the necessary commands to setup I2C permissions for non-root users.

1) Create new user group called i2c:

xander@alix:~
▶ sudo groupadd i2c

2) Change the group ownership of /dev/i2c-1 to i2c:

xander@alix:~
▶ sudo chown :i2c /dev/i2c-1

3) Change the file permissions of the device /dev/i2c-1 so users of the i2c group can read and write to the device:

xander@alix:~
▶ sudo chmod g+rw /dev/i2c-1

4) Add your user to the group i2c:

xander@alix:~
▶ sudo usermod -aG i2c xander

5) After you logout and login again you should be able to run i2cdetect -y 1.

The listing blow shows an example output of the command. As one can see, I connected three I2C devices to my Raspberry Pi 2.

xander@alix:~ 
▶ i2cdetect -y 1
     0  1  2  3  4  5  6  7  8  9  a  b  c  d  e  f
00:          -- 04 -- -- -- -- -- -- -- -- -- -- -- 
10: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- 
20: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- 
30: -- -- -- -- -- -- -- -- -- 39 -- -- -- -- -- -- 
40: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- 
50: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- 
60: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- 
70: -- -- -- -- -- -- -- 77

6) Lastly, we need to make the changes permament, so we don't need to repeat them on every reboot.

Login as the root user:

su root 

Next, create a udev rule file as shown below:

# echo 'KERNEL=="i2c-[0-9]*", GROUP="i2c"' >> /etc/udev/rules.d/10-local_i2c_group.rules

That's it, we're done :D.

Resources