Python as root and accessing the GPIO ports


8 posts
by texy » Sun Jun 03, 2012 11:56 am
Hi,
in order to access the GPIO lines using the pypi.GPIO module, I need to 'run the script' as root.
So I startx to go into the gui (perhaps thats the first mistake?), open a terminal and type
suso su -
to enter root. Then I have typed the following :

root@raspberrypi:/etc/python2.6# python
Python 2.6.6 (r266:84292, Dec 27 2010, 21:57:32)
[GCC 4.4.5 20100902 (prerelease)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import RPi.GPIO as GPIO
>>> GPIO.setup(17, GPIO.OUT)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/usr/local/lib/python2.6/dist-packages/RPi.GPIO-0.2.0-py2.6.egg/RPi/GPIO/__init__.py", line 92, in setup
id = _GetValidId(channel)
File "/usr/local/lib/python2.6/dist-packages/RPi.GPIO-0.2.0-py2.6.egg/RPi/GPIO/__init__.py", line 82, in _GetValidId
raise InvalidChannelException
RPi.GPIO.InvalidChannelException
>>>

so as you can see, the import function is accepted, but it doesn' like the setup command.
What am I doing wrong?
What I am trying to do is switch on a LED on GPIO-0 (bcm 17). I can do it from the shell using
echo "17" > /sys/class/gpio/output
etc
but not from python.
Do I have to create the python program using an editor first and then run it?

Any help welcome,
Texy
"2.8inch TFT LCD + Touch screen" add-on boards for sale here :
http://www.raspberrypi.org/phpBB3/viewtopic.php?f=93&t=65566
50p goes to the Foundation ;-)
Forum Moderator
Forum Moderator
Posts: 2411
Joined: Sat Mar 03, 2012 10:59 am
Location: Berkshire, England
by croston » Sun Jun 03, 2012 12:47 pm
I think you need to use:

Code: Select all
GPIO.setmode(GPIO.BCM)


in order to use the BCM numbers. It defaults to the RPi board pin number.
User avatar
Posts: 457
Joined: Sat Nov 26, 2011 12:33 pm
Location: Blackpool
by texy » Sun Jun 03, 2012 1:25 pm
Ok I will give it a go later. All I did was copy the example in the wiki and change 11 (or 12) to 17 though.
Is 11 or 12 valid, and 17 not, that would prove your theory ;-)

Texy
"2.8inch TFT LCD + Touch screen" add-on boards for sale here :
http://www.raspberrypi.org/phpBB3/viewtopic.php?f=93&t=65566
50p goes to the Foundation ;-)
Forum Moderator
Forum Moderator
Posts: 2411
Joined: Sat Mar 03, 2012 10:59 am
Location: Berkshire, England
by texy » Sun Jun 03, 2012 5:15 pm
Hi,
yes that has solved it - I was being a dumb-a$$, and didn't read the example code properly.
Many thanks.
Incidently in order to run the python code i had to open LXterminal, switch to root, then goto the directory where the the python programs I wrote to type in 'python gpio_play.py'.
How can I run my python code from within the editor, either Geany or SPE? They do not run as it is not in 'root' mode at that point.

Texy
"2.8inch TFT LCD + Touch screen" add-on boards for sale here :
http://www.raspberrypi.org/phpBB3/viewtopic.php?f=93&t=65566
50p goes to the Foundation ;-)
Forum Moderator
Forum Moderator
Posts: 2411
Joined: Sat Mar 03, 2012 10:59 am
Location: Berkshire, England
by andyl » Mon Jun 04, 2012 9:13 am
What I would do is make all the gpio files accessible to a group called gpio. Then add your user(s) to that group. This is what gpio-admin does I think (I haven't played with gpio yet) but you can do it manually.

As root
Code: Select all
groupadd gpio
useradd -G gpio pi
cd /sys/devices/virtual/gpio
chgrp -R gpio *


The last two commands might need to be executed every time you start your Pi. You could instead of that add some initialise scripts to automatically chgrp the gpio files at boot time.

As this seems to be quite a common question, and to do it properly with a group is pretty easy I would recommend that the standard distribution is updated to have a gpio group and that the group ownership of the virtual devices are set accordingly on boot.
Posts: 265
Joined: Tue Jan 10, 2012 11:05 am
by kghunt » Sun Jun 17, 2012 7:58 pm
Or create a udev rule.
Posts: 360
Joined: Sun Mar 04, 2012 9:28 am
by MattHawkinsUK » Tue Jun 19, 2012 11:37 am
The GPIO library uses Pin numbers by default. Pin 11 is GPIO 17 and Pin 12 is GPIO 18.

As mentioned by Croston you can change this to use the Broadcom labels.

Grumpy Mike has updated the WIki diagram as it was confusing.

Apart from the 2 power pins, ground and the Do Not Connect pins you can use all the others as plain inputs and outputs. That's 17 pins of LED/Switch/Motor/Relay/Laser goodness.

NEVER ever use the artificial "GPIO 0 - GPIO 7" labelling scheme as that is no use to anyone and conflicts with the Broadcom names. If you use GPIOxx it must refer to a genuine Broadcom label.
My Raspberry Pi blog and home of the BerryClip Add-on board : http://www.raspberrypi-spy.co.uk/
Follow me on Google+, Facebook, Pinterest and Twitter (@RPiSpy)
User avatar
Posts: 491
Joined: Tue Jan 10, 2012 8:48 pm
Location: UK
by karl101 » Sun Jun 24, 2012 12:15 am
I have a fix for this

viewtopic.php?f=44&t=8999

Karl.
Posts: 63
Joined: Wed Jan 11, 2012 10:09 am