Page 1 of 1

GPIO (new) problems

Posted: Sat Nov 09, 2013 5:49 pm
by GlennP
Hi All,
Thanks to a little confusion on my part last week I was having problems installing the GPIO library for python. I think I got around that issue now, now the problem is:

Traceback (most recent call last):

File "/home/pi/Pyton_Source/gpiooutput.py", line 5, in <module>

GPIO.setup (11, GPIO.OUT)
RuntimeError: No access to /dev/mem. Try running as root!
>>>

This was an example from the Rapsberry Pi Users Guide book by Ebdon Upton & Gareth Halfacree
I have done the
sudo python
routine to ensure python can access the GPIO,
giving back:

[email protected] ~/Pyton_Source $ sudo python
Python 2.7.3 (default, Jan 13 2013, 11:20:46)
[GCC 4.6.3] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>>

The script I am using is:

#First Play with GPIO 9/11/2013 16:29
import RPi.GPIO as GPIO
import time
GPIO.setup (11, GPIO.OUT)
while True:
GPIO.output(11,True)
time.sleep(2)
GPIO.output(11, False)
time.sleep(2)
from page 195 of the user guide. Am I missing something ?
Glenn

Re: GPIO (new) problems

Posted: Sat Nov 09, 2013 5:52 pm
by rpdom
Are you using sudo to run your script?

Re: GPIO (new) problems

Posted: Sat Nov 09, 2013 6:02 pm
by GlennP
I did use "sudo su" before I started the desktop, I have also entered it into the terminal window...

Re: GPIO (new) problems

Posted: Sat Nov 09, 2013 6:15 pm
by DougieLawson
Test whether you're running as UID(0) with the whoami command.

Code: Select all

[email protected] ~ $ whoami
pi
[email protected] ~ $ sudo whoami
root
[email protected] ~ $ sudo -i
[email protected] ~ # whoami
root
[email protected] ~ # exit
logout
[email protected] ~ $ sudo su
[email protected] /home/pi # whoami
root
[email protected] /home/pi # exit
exit
[email protected] ~ $
I created a gp.py file with your script then ran it with
sudo python gp.py
and it works OK.

Re: GPIO (new) problems

Posted: Sat Nov 09, 2013 8:02 pm
by gordon77
As davef21370 said in another thread ยป Fri Nov 08, 2013 5:08 pm

Start a fresh as if you've just booted up and typed 'startx' to enter the GUI.
On the desktop you should have a black icon for LXTerminal, double click that and wait for the window to open, this will look like the OS prior to typing 'startx' ie. basic text input/output.
Now, in that window, type 'sudo idle' which will (hopefully) run Idle for Python 2.7 with admin privileges which you need to access the GPIO.
Go to the file menu and select 'new'.
In the new window enter your code
Save the file as whatever and press F5 to run it.

=----------------

I dont think you need to install gpio it's already in the pi.

Re: GPIO (new) problems

Posted: Sat Nov 09, 2013 11:11 pm
by GlennP
Hi,
Sorry about the delay I have just tried it and got the same error,
I think I have the GPIO module installed correctly as if I type
import RPi.GPIO as GPIO
it gives back >>> and it does not appear to error there in script, it is the line
GPIO.setup (11, GPIO.OUT)
making me think is pin 11 on rev A board available as an output or do
I have to declare
GPIO.setup (17, GPIO.OUT), pin 11 being GPIO pin 17, or is the 11 the pin on the header
which I have taken it as.....

Re: GPIO (new) problems

Posted: Sat Nov 09, 2013 11:24 pm
by gordon77
Have you added a line to tell it BCM or BOARD ?

Eg
GPIO.setmode(GPIO.BCM)

BCM will mean GPIO 11
BOARD will mean pin 11

I imagine without this it won't know which pin to use

Re: GPIO (new) problems

Posted: Sun Nov 10, 2013 8:13 am
by DeeJay
GlennP wrote:Hi,
Sorry about the delay I have just tried it and got the same error,
I think I have the GPIO module installed correctly as if I type
import RPi.GPIO as GPIO
it gives back >>> and it does not appear to error there in script, it is the line
GPIO.setup (11, GPIO.OUT)
Do you mean it looks like this -

Code: Select all

[email protected] ~ $ sudo python
Python 2.7.3 (default, Jan 13 2013, 11:20:46)
[GCC 4.6.3] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import RPi.GPIO as GPIO
>>> GPIO.setup(11, GPIO.OUT)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
RuntimeError: Please set pin numbering mode using GPIO.setmode(GPIO.BOARD) or GPIO.setmode(GPIO.BCM)
>>> 
If so, it's what @gordon77 said: you need to put one of those two lines of code in your script before you can mention a pin number. (The reason the book is 'wrong' is that this command was added to the RPi.GPIO module, and was made mandatory, after the book was published.)

Re: GPIO (new) problems

Posted: Tue Nov 12, 2013 1:02 am
by gelfling6
Pretty sure it's as others have said, and the error message noted.. Python can only be run as root.. the work-around I've always used, on any linux based machine I use, as long as I'm the owner, is under terminal, issued the command:


sudu -s


On most platforms, it asks for your log-on password, and You're logged in as root. I think the last time I fired-up my Pi, it didn't ask for any password, and immediately dropped into root mode. it remains in this mode until at least one EXIT command is issued, and you're back to your normal account. The drawback, is everything you write or save in this mode, will be locked to username:group root:root , and will require you to chown them to your username:group.

Re: GPIO (new) problems

Posted: Tue Nov 12, 2013 6:27 am
by rpdom
gelfling6 wrote:Python can only be run as root.
Not quite. Python can be run by any user and should not be run as root in most cases. However, accessing the GPIOs from python (and most other methods) does need to be run as root or equivalent.

Re: GPIO (new) problems

Posted: Tue Nov 12, 2013 6:47 pm
by DeeJay
gelfling6 wrote: Python can only be run as root..
That's simply and proveably untrue as a general statement.

It IS the case that code which uses the RPi.GPIO module needs to be run with root privilege. Failure to do so results in an error message that explains the situation and how to resolve it.