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
Python as root and accessing the GPIO ports
8 posts
"!.8inch TFT LCD + Switch Shield" add-on boards for sale here :
http://www.raspberrypi.org/phpBB3/viewtopic.php?f=59&t=40674
50p goes to the Foundation
http://www.raspberrypi.org/phpBB3/viewtopic.php?f=59&t=40674
50p goes to the Foundation
- Moderator
- Posts: 1274
- Joined: Sat Mar 03, 2012 10:59 am
- Location: Berkshire, England
I think you need to use:
in order to use the BCM numbers. It defaults to the RPi board pin number.
- Code: Select all
GPIO.setmode(GPIO.BCM)
in order to use the BCM numbers. It defaults to the RPi board pin number.
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
Is 11 or 12 valid, and 17 not, that would prove your theory
Texy
"!.8inch TFT LCD + Switch Shield" add-on boards for sale here :
http://www.raspberrypi.org/phpBB3/viewtopic.php?f=59&t=40674
50p goes to the Foundation
http://www.raspberrypi.org/phpBB3/viewtopic.php?f=59&t=40674
50p goes to the Foundation
- Moderator
- Posts: 1274
- Joined: Sat Mar 03, 2012 10:59 am
- Location: Berkshire, England
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
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
"!.8inch TFT LCD + Switch Shield" add-on boards for sale here :
http://www.raspberrypi.org/phpBB3/viewtopic.php?f=59&t=40674
50p goes to the Foundation
http://www.raspberrypi.org/phpBB3/viewtopic.php?f=59&t=40674
50p goes to the Foundation
- Moderator
- Posts: 1274
- Joined: Sat Mar 03, 2012 10:59 am
- Location: Berkshire, England
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
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.
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
Or create a udev rule.
- Posts: 234
- Joined: Sun Mar 04, 2012 9:28 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.
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/
- Posts: 60
- Joined: Wed Jan 11, 2012 10:09 am