smbaker
Posts: 1
Joined: Sun Mar 15, 2015 5:13 am

Python GPIO does not work on Raspberry Pi 2

Sun Mar 15, 2015 5:17 am

I'm using the latest downloadable Raspbian image. Rpi.GPIO 0.5.11 is installed. I've also tried building Rpi.GPIO from mercurial checkout.

gpio from the command line works fine:

Code: Select all

gpio -g mode 27 out
gpio -g write 27 1
gpio from python does not:

Code: Select all

import RPi.GPIO as IO
import time

IO.setmode(IO.BCM)
IO.setup(27, IO.OUT)
IO.output(27, IO.HIGH)

try:
    while True: 
       time.sleep(1)
finally:
    IO.cleanup()
The pin never goes high with the python code. Am I doing something wrong? Similar code works just fine on my B+ boards.

Thanks,
Scott

Cole1717
Posts: 29
Joined: Mon Mar 16, 2015 10:44 pm

Re: Python GPIO does not work on Raspberry Pi 2

Mon Mar 16, 2015 10:46 pm

I am having the exact same problem and I can't find anything online addressing it. I might try reinstalling Raspbian if no one finds a solution.

User avatar
slmaws
Posts: 32
Joined: Wed Nov 13, 2013 6:41 pm
Location: Poland
Contact: Website

Re: Python GPIO does not work on Raspberry Pi 2

Tue Mar 17, 2015 12:52 am

Does it change to output mode? You can check it with

Code: Select all

gpio readall
See my Raspberry Pi tips on http://rpitips.com

Cole1717
Posts: 29
Joined: Mon Mar 16, 2015 10:44 pm

Re: Python GPIO does not work on Raspberry Pi 2

Mon Mar 23, 2015 10:36 pm

slmaws wrote:Does it change to output mode? You can check it with

Code: Select all

gpio readall
I tried this, but readall isn't working. Any idea why? I can't figure it out. It is version 0.5.11.

Code: Select all

import RPi.GPIO as gpio
gpio readall
throws a syntax error.

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

Re: Python GPIO does not work on Raspberry Pi 2

Mon Mar 23, 2015 10:52 pm

Cole1717 wrote:

Code: Select all

import RPi.GPIO as gpio
gpio readall
throws a syntax error.
You've mixed things up nicely. RPi.GPIO is a python library. gpio is a command line program (written in C).

Open an LXTerminal window & type
gpio readall
then press [ENTER]
Microprocessor, Raspberry Pi & Arduino Hacker
Mainframe database troubleshooter
MQTT Evangelist
Twitter: @DougieLawson

2012-18: 1B*5, 2B*2, B+, A+, Z, ZW, 3Bs*3, 3B+

Any DMs sent on Twitter will be answered next month.

User avatar
joan
Posts: 13407
Joined: Thu Jul 05, 2012 5:09 pm
Location: UK

Re: Python GPIO does not work on Raspberry Pi 2

Mon Mar 23, 2015 11:00 pm

Are you sure? Which parts? http://abyz.co.uk/rpi/pigpio/python.html

Cole1717
Posts: 29
Joined: Mon Mar 16, 2015 10:44 pm

Re: Python GPIO does not work on Raspberry Pi 2

Tue Mar 24, 2015 11:54 am

That explains it. I don't know why I didn't realize that's what slmaws meant. I'll let you know what it says when I get home from work. I still haven't been able to get python to set a pin high yet.

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

Re: Python GPIO does not work on Raspberry Pi 2

Tue Mar 24, 2015 12:48 pm

Cole1717 wrote:I still haven't been able to get python to set a pin high yet.

Code: Select all

#!/usr/bin/sudo /usr/bin/python
import RPi.GPIO as g
from time import sleep
g.setmode(g.BCM) # broadcom numbering
g.setup(17, g.OUT) # GPIO17 == physical pin#11
while True:
  g.output(17, g.HIGH)
  sleep(0.5)
  g.output(17, g.LOW)
  sleep(0.5)
save that as /home/pi/b.py
Open an LXterminal window
chmod 755 b.py # press [ENTER]
./b.py # press [ENTER] to run it
[CTRL]+[C] to make it stop.
Microprocessor, Raspberry Pi & Arduino Hacker
Mainframe database troubleshooter
MQTT Evangelist
Twitter: @DougieLawson

2012-18: 1B*5, 2B*2, B+, A+, Z, ZW, 3Bs*3, 3B+

Any DMs sent on Twitter will be answered next month.

craigo2015
Posts: 1
Joined: Sun Mar 15, 2015 5:41 pm

Re: Python GPIO does not work on Raspberry Pi 2

Wed Mar 25, 2015 8:02 pm

Couldnt get Python GPIO to work on my Pi 2 either.

I can output to GPIO pins by echoing values to /sys/class/gpio and using the wiringpi gpio utility but not with rpi.GPIO
I will check whether gpio readall shows rpi.GPIO setting the output correctly at the weekend.

User avatar
mahjongg
Forum Moderator
Forum Moderator
Posts: 10645
Joined: Sun Mar 11, 2012 12:19 am
Location: South Holland, The Netherlands

Re: Python GPIO does not work on Raspberry Pi 2

Wed Mar 25, 2015 11:57 pm

If rpi.GPIO hasn't had an update since the B2 came out then that explains it, all libraries that drive GPIO's must check whether they run on a pre-2B or a 2B or later (on a quadcore ARMv7) or check the memory size, because of the upgrade to 1GB RAM some register addresses had to relocate, and so any writes there now most be dependent on where the register bank is actually located.

DirkS
Posts: 8059
Joined: Tue Jun 19, 2012 9:46 pm
Location: Essex, UK

Re: Python GPIO does not work on Raspberry Pi 2

Thu Mar 26, 2015 11:12 am

mahjongg wrote:If rpi.GPIO hasn't had an update since the B2 came out then that explains it, all libraries that drive GPIO's must check whether they run on a pre-2B or a 2B or later (on a quadcore ARMv7) or check the memory size, because of the upgrade to 1GB RAM some register addresses had to relocate, and so any writes there now most be dependent on where the register bank is actually located.
It is updated for the Pi2 since 0.5.10 with some fixes in 0.5.11, see https://pypi.python.org/pypi/RPi.GPIO
Current version in the repo also is 0.5.11:
python-rpi.gpio
Installed: 0.5.11-1
Candidate: 0.5.11-1
Version table:
*** 0.5.11-1 0
500 http://archive.raspberrypi.org/debian/ wheezy/main armhf Packages
100 /var/lib/dpkg/status

Cole1717
Posts: 29
Joined: Mon Mar 16, 2015 10:44 pm

Re: Python GPIO does not work on Raspberry Pi 2

Mon Mar 30, 2015 10:10 pm

I tried doing what you said @DougieLawson, but it didn't work. it says:

Code: Select all

./b.py:5: RuntimeWarning: This channel is already in use, continuing anyway. Use GPIO.setwarnings(False) to disable warnings.
g.setup(17, g.OUT) 
Also, "gpio readall" from the terminal is saying "bash: gpio: command not found" .....

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

Re: Python GPIO does not work on Raspberry Pi 2

Mon Mar 30, 2015 11:37 pm

You need to install the Wiring Pi code.
cd /tmp
git clone git://git.drogon.net/wiringPi
cd wiringPi
./build
Microprocessor, Raspberry Pi & Arduino Hacker
Mainframe database troubleshooter
MQTT Evangelist
Twitter: @DougieLawson

2012-18: 1B*5, 2B*2, B+, A+, Z, ZW, 3Bs*3, 3B+

Any DMs sent on Twitter will be answered next month.

Cole1717
Posts: 29
Joined: Mon Mar 16, 2015 10:44 pm

Re: Python GPIO does not work on Raspberry Pi 2

Mon Apr 06, 2015 5:18 pm

I installed wiringPi. Ran my simple script and did "gpio readall" while the script was running. The pin isn't changing to output mode, it is staying in input mode with V = 0. Is there an explanation for this?

B.Goode
Posts: 5989
Joined: Mon Sep 01, 2014 4:03 pm
Location: UK

Re: Python GPIO does not work on Raspberry Pi 2

Mon Apr 06, 2015 5:46 pm

What is in "my simple script". I don't think you have posted a copy of it during this thread?

Cole1717
Posts: 29
Joined: Mon Mar 16, 2015 10:44 pm

Re: Python GPIO does not work on Raspberry Pi 2

Tue Apr 07, 2015 10:07 pm

Sorry, I am usually at work when I post. Here is my code that i execute via:

Code: Select all

sudo python led.py

Code: Select all

import os
import time
import RPi.GPIO as GPIO

GPIO.setmode(GPIO.BOARD)
GPIO.setup(38, GPIO.OUT)

try:
     while True:
              GPIO.output(38, GPIO.HIGH)
except:
     GPIO.cleanup()


B.Goode
Posts: 5989
Joined: Mon Sep 01, 2014 4:03 pm
Location: UK

Re: Python GPIO does not work on Raspberry Pi 2

Wed Apr 08, 2015 6:51 am

That works OK for me.

Here is the output from the end of a gpio readall command, one run before your script, and one run while your script is executing.

Code: Select all

 |  19 |  24 | GPIO.24 |   IN | 0 | 35 || 36 | 0 | IN   | GPIO.27 | 27  | 16  |
 |  26 |  25 | GPIO.25 |   IN | 0 | 37 || 38 | 0 | IN   | GPIO.28 | 28  | 20  |
 |     |     |      0v |      |   | 39 || 40 | 0 | IN   | GPIO.29 | 29  | 21  |
 +-----+-----+---------+------+---+----++----+---+------+---------+-----+-----+
 | BCM | wPi |   Name  | Mode | V | Physical | V | Mode | Name    | wPi | BCM |
 +-----+-----+---------+------+---+---Pi 2---+---+------+---------+-----+-----+

 |  19 |  24 | GPIO.24 |   IN | 0 | 35 || 36 | 0 | IN   | GPIO.27 | 27  | 16  |
 |  26 |  25 | GPIO.25 |   IN | 0 | 37 || 38 | 1 | OUT  | GPIO.28 | 28  | 20  |
 |     |     |      0v |      |   | 39 || 40 | 0 | IN   | GPIO.29 | 29  | 21  |
 +-----+-----+---------+------+---+----++----+---+------+---------+-----+-----+
 | BCM | wPi |   Name  | Mode | V | Physical | V | Mode | Name    | wPi | BCM |
 +-----+-----+---------+------+---+---Pi 2---+---+------+---------+-----+-----+
Pin 38 in Board numbering has changed mode to OUT and changed level from 0 to 1.

Cole1717
Posts: 29
Joined: Mon Mar 16, 2015 10:44 pm

Re: Python GPIO does not work on Raspberry Pi 2

Wed Apr 08, 2015 11:40 am

That's very strange. I'll try that again when I get the chance. If it doesn't work, do you think re-installing the OS and everything could be worth a try?

User avatar
joan
Posts: 13407
Joined: Thu Jul 05, 2012 5:09 pm
Location: UK

Re: Python GPIO does not work on Raspberry Pi 2

Wed Apr 08, 2015 11:42 am

RPi.GPIO only works if you are using device tree. If you are not using device tree that would explain the symptoms you are seeing.

Cole1717
Posts: 29
Joined: Mon Mar 16, 2015 10:44 pm

Re: Python GPIO does not work on Raspberry Pi 2

Wed Apr 08, 2015 12:02 pm

I'm fairly certain I left that disabled because I didn't know what it was. What is it exactly? I'll definitely try enabling it.

User avatar
joan
Posts: 13407
Joined: Thu Jul 05, 2012 5:09 pm
Location: UK

Re: Python GPIO does not work on Raspberry Pi 2

Wed Apr 08, 2015 12:09 pm

Cole1717 wrote:I'm fairly certain I left that disabled because I didn't know what it was. What is it exactly? I'll definitely try enabling it.
It's something you have to be using if you want to use RPi.GPIO.

I'm not competent to explain its purpose.

If your /boot/config.txt contains a line such as the following then remove it.

device_tree=

Cole1717
Posts: 29
Joined: Mon Mar 16, 2015 10:44 pm

Re: Python GPIO does not work on Raspberry Pi 2

Tue Apr 21, 2015 10:44 pm

That worked! thank you so much. Enabled the device tree, rebooted and ran the script and it worked.

Good work team.

proj964
Posts: 9
Joined: Tue Jan 01, 2013 8:34 pm

Re: Python GPIO does not work on Raspberry Pi 2

Thu Jun 04, 2015 10:25 am

This thread really helped out. Thanks.

ktb
Posts: 1380
Joined: Fri Dec 26, 2014 7:53 pm

Re: Python GPIO does not work on Raspberry Pi 2

Thu Jun 04, 2015 11:08 am

proj964 wrote:This thread really helped out. Thanks.
It's nice to see that you got that issue figured out. Good job.

User avatar
MarkHaysHarris777
Posts: 1820
Joined: Mon Mar 23, 2015 7:39 am
Location: Rochester, MN
Contact: Website

Re: Python GPIO does not work on Raspberry Pi 2

Thu Jun 04, 2015 12:35 pm

joan wrote:
Cole1717 wrote:I'm fairly certain I left that disabled because I didn't know what it was. What is it exactly? I'll definitely try enabling it.
It's something you have to be using if you want to use RPi.GPIO.
I thought device tree was enabled by default... did you disable it deliberately or did you find it disabled and left it that way?
marcus
:ugeek:

Return to “Troubleshooting”

Who is online

Users browsing this forum: ejolson, rpwoods and 53 guests