AlexanderMan
Posts: 2
Joined: Tue Apr 24, 2012 1:38 pm

Re: GPIO Permission Denied

Tue Apr 24, 2012 2:56 pm

Hi Raspberry Pi Forum!
I've managed to get one in the first batch of raspberry pi's and have been enjoying making python scripts. I've also been a bit adventurous trying to interact with the GPIO that the pi offers but I'm failing to talk to it.

I attempted to do a simple bash call 'echo "1" > /sys/class/gpio/export' (following the example at http://elinux.org/Rpi_Low-leve....._script.29) to set up GPIO1, however I receive an error message "/sys/class/gpio/export: Permission denied".

Is there a was to enable permission for GPIO? Thanks in advance

User avatar
RaTTuS
Posts: 10498
Joined: Tue Nov 29, 2011 11:12 am
Location: North West UK
Contact: Twitter YouTube

Re: GPIO Permission Denied

Tue Apr 24, 2012 3:07 pm

run elevated

i.e.

sudo echo "1" > /sys/class/gpio/export'

or

sudo ./bashscript
How To ask Questions :- http://www.catb.org/esr/faqs/smart-questions.html
WARNING - some parts of this post may be erroneous YMMV

1QC43qbL5FySu2Pi51vGqKqxy3UiJgukSX
Covfefe

User avatar
Gert van Loo
Posts: 2486
Joined: Tue Aug 02, 2011 7:27 am
Contact: Website

Re: GPIO Permission Denied

Tue Apr 24, 2012 3:33 pm

I found that you can not "sudo echo ...."

I had to start a shell using sudo and then use "echo ...."

So your first example won't work.

User avatar
RaTTuS
Posts: 10498
Joined: Tue Nov 29, 2011 11:12 am
Location: North West UK
Contact: Twitter YouTube

Re: GPIO Permission Denied

Tue Apr 24, 2012 3:50 pm

ah

sudo the script then

in the script have

#!/bin/bash

as the 1st line

and change to permissions to be 777

chmod 777 script

then you can

sudo ./script

otherise it would have to be

sudo sh ./script
How To ask Questions :- http://www.catb.org/esr/faqs/smart-questions.html
WARNING - some parts of this post may be erroneous YMMV

1QC43qbL5FySu2Pi51vGqKqxy3UiJgukSX
Covfefe

User avatar
jojopi
Posts: 3085
Joined: Tue Oct 11, 2011 8:38 pm

Re: GPIO Permission Denied

Tue Apr 24, 2012 4:13 pm

You can sudo echo, but you can not sudo ">".  That is because redirections are set up by your shell before sudo is even started.  Common workarounds are:echo "1" |sudo tee /sys/class/gpio/export
sudo sh -c 'echo "1" > /sys/class/gpio/export'
The first has the slight disadvantage of also printing to the terminal.  The second quickly gets very hairy if the command needs multiple types of quotes.

You can also put the echo in a script and sudo the whole script.  But arguably the best thing is to try to change the permissions so that you do not need to be superuser to access gpio:sudo chgrp -R pi /sys/class/gpio
sudo chmod -R g+rw /sys/class/gpio
(I am not able to test this.)

(Please never "chmod 777" anything.  Those are literally never the right permissions for any file or directory on a correctly-configured system.)

User avatar
Gert van Loo
Posts: 2486
Joined: Tue Aug 02, 2011 7:27 am
Contact: Website

Re: GPIO Permission Denied

Tue Apr 24, 2012 4:49 pm

Thank you. Now I understand why you can't do "sudo ...>" which make it easier to remember and apply to other situations.

(With hindsight: yes it is logical that the > is not part of the sudo especially if you know how tasks are started in Unix.)

AlexanderMan
Posts: 2
Joined: Tue Apr 24, 2012 1:38 pm

Re: GPIO Permission Denied

Tue Apr 24, 2012 5:09 pm

Thanks for the discussion, I've managed to find a topic posted about a week ago discussing how to read/write to GPIOs (http://www.raspberrypi.org/for.....ke-im-five) and was able to read a button press (hello world for digital input) successfully.

naicheben
Posts: 344
Joined: Sat Jan 28, 2012 12:28 pm
Contact: Website

Re: GPIO Permission Denied

Tue Apr 24, 2012 5:14 pm

I don't realy understand the userpermissions jet. But wouldn't you add user pi to the group witch has permission to the GPIO? Of course not to root, but maybe add a group "gpio", then "chown -R gpio:gpio /sys/class/gpio"and add pi and later other users to the group so they can access the GPIOs?

User avatar
croston
Posts: 703
Joined: Sat Nov 26, 2011 12:33 pm
Location: Blackpool
Contact: Website

Re: GPIO Permission Denied

Tue Apr 24, 2012 5:23 pm

I"ve started looking into the possibility of creating a GPIO group that a user could be added to. I have never played with udev before but this looks like it controls this type of thing for hardware permissions. Are there any udev experts out there that could give me some pointers?

alias_neo
Posts: 108
Joined: Fri Jul 29, 2011 8:55 am
Location: London, England
Contact: Website

Re: GPIO Permission Denied

Tue Aug 28, 2012 8:49 pm

croston wrote:I"ve started looking into the possibility of creating a GPIO group that a user could be added to. I have never played with udev before but this looks like it controls this type of thing for hardware permissions. Are there any udev experts out there that could give me some pointers?
Doesn't look like anyone has replied here in a while, but I'v been playing with this for a little project I just started and basically, you can great a group called gpio, add the user to it, export the gpio etc as follows:

Code: Select all

sudo groupadd gpio
sudo usermod -aG gpio <myusername>
su <myusername>
sudo chgrp gpio /sys/class/gpio/export
sudo chgrp gpio /sys/class/gpio/unexport
sudo chmod 775 /sys/class/gpio/export
sudo chmod 775 /sys/class/gpio/unexport
This gives your user the ability to export and un-export.

Once you export the pins you can chgrp and chmod them as well (don't forget their contents) and use them as usual.

Code: Select all

echo "18" > /sys/class/gpio/export
chgrp -HR /sys/class/gpio/gpio18
chmod -R 775 /sys/class/gpio/gpio18
Now your user has access to the first GPIO pin 18.

Code: Select all

echo "out" > /sys/class/gpio/gpio18/direction
echo "1" > /sys/class/gpio/gpio18/value
This turns it on.

Code: Select all

echo "0" > /sys/class/gpio18/value
This turns it off

Code: Select all

echo "18" > /sys/class/gpio/unexport
and finally this un-exports it.
http://2byt.es - Just my two bytes - RPi - Android - Arduino - Blog

Return to “Beginners”