1

I grabbed the image for Ubuntu on Raspberry PI over here https://ubuntu.com/download/raspberry-pi loaded a L928N python program via a virtualenv as the ubuntu user and got an error. Did some digging and some testing turned out to be ubuntu group could not access /dev/gpiomem which belongs to root root

So the option I was left with was to run my python program as root but virtualenv was not really isolating my programs then. It appeared sudo was pulling everything from root's global python setup. Seeing as I run a couple project on the raspberry pi having them overlap can cause me nights of frustration trying to understand why something broke (when a dep updates for one project and not another).

So I made /dev/gpiomem group gpio which ubuntu user is apart of. Viola python, pip, virtualenv and my project worked without sudo in a controlled isolated virtualenv!

Now a couple days later login run the program and it fails. Turns out /dev/gpiomem reverted back to root root.

Questions I have are A) is there a better way to give ubuntu access to /dev/gpiomem B) is there a way to ensure that the group stays the same (assuming A is no).

4

On Ubuntu, instead of the gpio group, add your user to the dialout group to give yourself access to the GPIO pins.

(This is documented in the /usr/share/doc/rpi.gpio-common/README.Debian file that is installed by the rpi.gpio-common package.)

As 4xy pointed out, installing the rpi.gpio-common package will set up the udev rules.

I had to reboot my Pi after installing rpi.gpio-common and adding my user to the dialout group, and then I was able to access the GPIO pins as a normal user.


Putting it all together:

sudo apt install rpi.gpio-common
sudo adduser "${USER}" dialout
sudo reboot
2

I've just ran into this. I added the group gpio and added root and my user to that group. I then copied the udev rule 99-com.rules from a raspbian image. Reboot and all works. I've pasted the rules file here for completeness. This is how the foundation do it so must be right. ;)

SUBSYSTEM=="input", GROUP="input", MODE="0660"
SUBSYSTEM=="i2c-dev", GROUP="i2c", MODE="0660"
SUBSYSTEM=="spidev", GROUP="spi", MODE="0660"
SUBSYSTEM=="bcm2835-gpiomem", GROUP="gpio", MODE="0660"

SUBSYSTEM=="gpio", GROUP="gpio", MODE="0660"
SUBSYSTEM=="gpio*", PROGRAM="/bin/sh -c '\
        chown -R root:gpio /sys/class/gpio && chmod -R 770 /sys/class/gpio;\
        chown -R root:gpio /sys/devices/virtual/gpio && chmod -R 770 /sys/devices/virtual/gpio;\
        chown -R root:gpio /sys$devpath && chmod -R 770 /sys$devpath\
'"

KERNEL=="ttyAMA[01]", PROGRAM="/bin/sh -c '\
        ALIASES=/proc/device-tree/aliases; \
        if cmp -s $ALIASES/uart0 $ALIASES/serial0; then \
                echo 0;\
        elif cmp -s $ALIASES/uart0 $ALIASES/serial1; then \
                echo 1; \
        else \
                exit 1; \
        fi\
'", SYMLINK+="serial%c"

KERNEL=="ttyS0", PROGRAM="/bin/sh -c '\
        ALIASES=/proc/device-tree/aliases; \
        if cmp -s $ALIASES/uart1 $ALIASES/serial0; then \
                echo 0; \
        elif cmp -s $ALIASES/uart1 $ALIASES/serial1; then \
                echo 1; \
        else \
                exit 1; \
        fi \
2
  • I stumbled onto these .rules files but then some other work came up. Thanks for the starting template.
    – nerdlyist
    Apr 30 '20 at 13:53
  • 1
    Thanks for your answer Richard. ubuntu user should be also added to i2c, spi, input and gpio groups. Oct 22 '20 at 20:31
0

No need to manually add these rules.

sudo apt install rpi.gpio-common

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.