How do you allow non-root users to access I2C on the Raspberry Pi 2?
I've compiled this code for accessing an MPU6050 sensor via I2C, and it works perfectly, but only when I call it via sudo.
I tried adding the pi user to the spi and i2c groups, and adding udev rules as explained here and here, but even after rebooting, those had little effect. I can run /usr/sbin/i2cdetect
as a non-root user, but the C code I2Cdev::initialize()
still crashes if run without sudo.
This is documented in the underlying bcm2835 library:
If the library runs with any other effective UID (ie not root), then bcm2835_init() will attempt to open /dev/gpiomem, and, if successful, will only permit GPIO operations. In particular, bcm2835_spi_begin() and bcm2835_i2c_begin() will return false and all other non-gpio operations may fail silently or crash.
Is there any workaround for this? Even if I add the pi user to the root group, it still silently crashes.