GlennP
Posts: 11
Joined: Fri Nov 01, 2013 11:31 am

GPIO (new) problems

Sat Nov 09, 2013 5:49 pm

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

User avatar
rpdom
Posts: 15212
Joined: Sun May 06, 2012 5:17 am
Location: Chelmsford, Essex, UK

Re: GPIO (new) problems

Sat Nov 09, 2013 5:52 pm

Are you using sudo to run your script?

GlennP
Posts: 11
Joined: Fri Nov 01, 2013 11:31 am

Re: GPIO (new) problems

Sat Nov 09, 2013 6:02 pm

I did use "sudo su" before I started the desktop, I have also entered it into the terminal window...

User avatar
DougieLawson
Posts: 36156
Joined: Sun Jun 16, 2013 11:19 pm
Location: Basingstoke, UK
Contact: Website Twitter

Re: GPIO (new) problems

Sat Nov 09, 2013 6:15 pm

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.
Note: Having anything humorous in your signature is completely banned on this forum. Wear a tin-foil hat and you'll get a ban.

Any DMs sent on Twitter will be answered next month.

This is a doctor free zone.

gordon77
Posts: 4174
Joined: Sun Aug 05, 2012 3:12 pm

Re: GPIO (new) problems

Sat Nov 09, 2013 8:02 pm

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.

GlennP
Posts: 11
Joined: Fri Nov 01, 2013 11:31 am

Re: GPIO (new) problems

Sat Nov 09, 2013 11:11 pm

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.....

gordon77
Posts: 4174
Joined: Sun Aug 05, 2012 3:12 pm

Re: GPIO (new) problems

Sat Nov 09, 2013 11:24 pm

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

User avatar
DeeJay
Posts: 2027
Joined: Tue Jan 01, 2013 9:33 pm
Location: East Midlands, UK

Re: GPIO (new) problems

Sun Nov 10, 2013 8:13 am

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.)
How To Ask Questions The Smart Way: http://www.catb.org/~esr/faqs/smart-questions.html
How to Report Bugs Effectively: http://www.chiark.greenend.org.uk/~sgtatham/bugs.html

gelfling6
Posts: 23
Joined: Sat Dec 01, 2012 8:39 pm
Location: Canton, Connecticut, USA, Planet Earth, SOL-3, Milkyway Galaxy, 3rd star on the left...
Contact: Website

Re: GPIO (new) problems

Tue Nov 12, 2013 1:02 am

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.

User avatar
rpdom
Posts: 15212
Joined: Sun May 06, 2012 5:17 am
Location: Chelmsford, Essex, UK

Re: GPIO (new) problems

Tue Nov 12, 2013 6:27 am

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.

User avatar
DeeJay
Posts: 2027
Joined: Tue Jan 01, 2013 9:33 pm
Location: East Midlands, UK

Re: GPIO (new) problems

Tue Nov 12, 2013 6:47 pm

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.
How To Ask Questions The Smart Way: http://www.catb.org/~esr/faqs/smart-questions.html
How to Report Bugs Effectively: http://www.chiark.greenend.org.uk/~sgtatham/bugs.html

Return to “General discussion”