2

I have a Raspberry Pi A+ and I'm running Raspbian GNU/Linux 8.0 (jessie). When I use the gpio command in bash I can connect to the GPIO ports, for example turning on a LED:

gpio mode 25 out
gpio write 25 1
gpio write 25 0

But when I try to do the same thing in Python 2.7 with RPi.GPIO it does not work:

import RPi.GPIO as GPIO
import time

GPIO.setmode(GPIO.BCM)
GPIO.setwarnings(False)

led = 25
GPIO.setup(led, GPIO.OUT)
GPIO.output(led, True)
time.sleep(2)
GPIO.output(led, False)
GPIO.cleanup()

I have updated the python RPi.GPIO libraries using these steps: https://sourceforge.net/p/raspberry-gpio-python/wiki/install/

How can I solve this issue and connect to the GPIO pins from Python?

1 Answer 1

4

You are using two different GPIO numbering systems.

The gpio utility is using wiringPi numbers. wiringPi 25 is actually GPIO26 on pin 37.

RPi.GPIO is using GPIO numbers, so 25 refers to GPIO25 (which is on pin 22).

Probably simplest if you change to use GPIO26 in your Python code, i.e. led = 26.


EDITED TO ADD

In practice you will come across three different Pi GPIO numbering systems.

  1. wiringPi - a system of "logical" numbering based on the numbers used on the first Pi schematics and extended to later models. Generally this numbered GPIO from 1 upwards in the order they were likely to be used.
  2. Broadcom - the GPIO numbers as used by the SoC manufacturer and the hardware. These are apparently randomly ordered in the region 0 to 31.
  3. Pin numbers - a system which numbers the GPIO by the physical pin they are attached to on the expansion header.

wiringPi's gpio readall command will output a diagram showing the correspondence between Broadcom (column BCM), wiringPi (column wPi), and pin (column Physical) numbers.

When you talk about GPIO you need to specify the numbering scheme you are using if there is any doubt. You also need to check which numbering schemes are supported by the software you are using.

Although in some senses the most arbitrary my suggestion is to use the Broadcom numbering scheme (not only because it's the only scheme my pigpio library supports).

5
  • Thank you, I was not aware that there were different, inconsistent, GPIO numbering.
    – agold
    Commented Jun 9, 2016 at 5:59
  • @agold added a little explanation to answer.
    – joan
    Commented Jun 9, 2016 at 8:16
  • thank you for clearing this up! So it's best to use pin numbers since they are consistent between the different software libraries, isn't it?
    – agold
    Commented Jun 9, 2016 at 9:13
  • @agold I use Broadcom numbers from preference. I think Broadcom numbering is the most widely supported.
    – joan
    Commented Jun 9, 2016 at 10:34
  • Indeed - we're only supporting Broadcom numbers in GPIO Zero (which sits on top of several GPIO libraries, including RPi.GPIO, RPIO, and joan's pigpio) for several reasons: it's the only system consistently available across all implementations, it allows access to all GPIOs (including ones not exposed on the header - important for some use cases), and it's increasingly supported by cases (e.g. the pibow cases which have the GPIO numbers burned into the layer surrounding the P1 header).
    – Dave Jones
    Commented Jun 9, 2016 at 13:32

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.