meddyliol
Posts: 118
Joined: Thu Mar 20, 2014 8:28 am
Location: South Wales

GPIO pin numbers?

Tue Mar 24, 2015 10:11 am

I expect that this has been asked before but I am still confused. I have the Pi 2 and if I simply want to flash LED's on and off which pins can I use? Do I use the actual pin numbers or the GPIO numbers. For example GPIO17 is actually pin 11 so which number goes in a Python programme? Sorry for being so stupid but I do not want to blow anything up. :oops: There is loads and loads of info out there but it tends to create confusion, to me anyway.

Thanks

Brian
Raspberry Pi B, Raspberry Pi 2, monitor, powered hub, mouse, keyboard and Volvo V50 car

User avatar
DougieLawson
Posts: 41785
Joined: Sun Jun 16, 2013 11:19 pm
Location: A small cave in deepest darkest Basingstoke, UK
Contact: Website Twitter

Re: GPIO pin numbers?

Tue Mar 24, 2015 10:21 am

Welcome to the most confusing part of Raspberry Pis. The pins have three numbers and may have a fourth name. If we could turn the clock back three years this could have been prevented.

Code: Select all

root@eagle ~ # gpio readall
 +-----+-----+---------+------+---+---Pi 2---+---+------+---------+-----+-----+
 | BCM | wPi |   Name  | Mode | V | Physical | V | Mode | Name    | wPi | BCM |
 +-----+-----+---------+------+---+----++----+---+------+---------+-----+-----+
 |     |     |    3.3v |      |   |  1 || 2  |   |      | 5v      |     |     |
 |   2 |   8 |   SDA.1 | ALT0 | 1 |  3 || 4  |   |      | 5V      |     |     |
 |   3 |   9 |   SCL.1 | ALT0 | 1 |  5 || 6  |   |      | 0v      |     |     |
 |   4 |   7 | GPIO. 7 |   IN | 1 |  7 || 8  | 1 | ALT0 | TxD     | 15  | 14  |
 |     |     |      0v |      |   |  9 || 10 | 1 | ALT0 | RxD     | 16  | 15  |
 |  17 |   0 | GPIO. 0 |   IN | 0 | 11 || 12 | 0 | IN   | GPIO. 1 | 1   | 18  |
 |  27 |   2 | GPIO. 2 |   IN | 0 | 13 || 14 |   |      | 0v      |     |     |
 |  22 |   3 | GPIO. 3 |   IN | 0 | 15 || 16 | 0 | IN   | GPIO. 4 | 4   | 23  |
 |     |     |    3.3v |      |   | 17 || 18 | 0 | IN   | GPIO. 5 | 5   | 24  |
 |  10 |  12 |    MOSI | ALT0 | 0 | 19 || 20 |   |      | 0v      |     |     |
 |   9 |  13 |    MISO | ALT0 | 0 | 21 || 22 | 0 | IN   | GPIO. 6 | 6   | 25  |
 |  11 |  14 |    SCLK | ALT0 | 0 | 23 || 24 | 1 | ALT0 | CE0     | 10  | 8   |
 |     |     |      0v |      |   | 25 || 26 | 1 | ALT0 | CE1     | 11  | 7   |
 |   0 |  30 |   SDA.0 |   IN | 1 | 27 || 28 | 1 | IN   | SCL.0   | 31  | 1   |
 |   5 |  21 | GPIO.21 |   IN | 1 | 29 || 30 |   |      | 0v      |     |     |
 |   6 |  22 | GPIO.22 |   IN | 1 | 31 || 32 | 0 | IN   | GPIO.26 | 26  | 12  |
 |  13 |  23 | GPIO.23 |   IN | 0 | 33 || 34 |   |      | 0v      |     |     |
 |  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---+---+------+---------+-----+-----+
http://pi.gadgetoid.com/pinout

What you have to do is pick ONE naming system and stick to it for everything you do. My choice is GPIO.BOARD because that means I can simply count pins on the header (and I got stuck with it before I knew any better). The recommendation is to ALWAYS use the BCM numbering scheme because that's the most widely used.

I've also printed a full page copy of Image since most of my RPis have the B pinout.
Languages using left-hand whitespace for syntax are ridiculous

DMs sent on Twitter/LinkedIn will be answered next month.
Fake doctors - are all on my foes list.

The use of crystal balls and mind reading is prohibited.

meddyliol
Posts: 118
Joined: Thu Mar 20, 2014 8:28 am
Location: South Wales

Re: GPIO pin numbers?

Tue Mar 24, 2015 10:50 am

Thanks very much for that. So, just to be VERY clear, I could use, for instance BCM 17 which is actually pin 11 or BCM 5 which is pin 29. Are there any GPIO pins I shouldn't use?

Thanks

Brian :)
Raspberry Pi B, Raspberry Pi 2, monitor, powered hub, mouse, keyboard and Volvo V50 car

User avatar
DougieLawson
Posts: 41785
Joined: Sun Jun 16, 2013 11:19 pm
Location: A small cave in deepest darkest Basingstoke, UK
Contact: Website Twitter

Re: GPIO pin numbers?

Tue Mar 24, 2015 11:32 am

If you've got an A+/B+/2B then there's no point trying to reassign the I2C or SPI or UART pins. Start at BCM GPIO29 and work backwards from there (physical pin40 is GPIO29).

If you've got an A/B start at BCM GPIO17 and work on the pins in the middle.

Install WiringPi and you can use the gpio readall command on your system to draw the pretty text graphics picture.
Languages using left-hand whitespace for syntax are ridiculous

DMs sent on Twitter/LinkedIn will be answered next month.
Fake doctors - are all on my foes list.

The use of crystal balls and mind reading is prohibited.

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

Re: GPIO pin numbers?

Tue Mar 24, 2015 12:13 pm

I agree with others its confusing.

In python you use GPIO.setmode(GPIO.BOARD) or GPIO.setmode(GPIO.BCM)

If BOARD then that's physical pin numbers eg. 1 to 26 or 40 dependent on the Pi version,

If BCM then it's the BCM numbers.

meddyliol
Posts: 118
Joined: Thu Mar 20, 2014 8:28 am
Location: South Wales

Re: GPIO pin numbers?

Tue Mar 24, 2015 12:54 pm

Start at BCM GPIO29 and work backwards from there (physical pin40 is GPIO29).

According to the pin diagram I am looking at (http://www.raspberrypi-spy.co.uk/2012/0 ... -2700x900/) pin 40 is GPIO 21. I am getting even more confused now.

Brian
Raspberry Pi B, Raspberry Pi 2, monitor, powered hub, mouse, keyboard and Volvo V50 car

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

Re: GPIO pin numbers?

Tue Mar 24, 2015 1:01 pm

I agree I don't understand the Name column in Dougies chart.

Pin 40 is BCM GPIO21, I don't know where 29 comes from

User avatar
DougieLawson
Posts: 41785
Joined: Sun Jun 16, 2013 11:19 pm
Location: A small cave in deepest darkest Basingstoke, UK
Contact: Website Twitter

Re: GPIO pin numbers?

Tue Mar 24, 2015 1:03 pm

Code: Select all

40 | 0 | IN   | GPIO.29 | 29  | 21  |
Roll on with the confusion. It's so stupid. I'm blaming WiringPi's Gordon for not helping with this crud.
Languages using left-hand whitespace for syntax are ridiculous

DMs sent on Twitter/LinkedIn will be answered next month.
Fake doctors - are all on my foes list.

The use of crystal balls and mind reading is prohibited.

meddyliol
Posts: 118
Joined: Thu Mar 20, 2014 8:28 am
Location: South Wales

Re: GPIO pin numbers?

Tue Mar 24, 2015 1:13 pm

and #set up GPIO using BCM numbering

GPIO.setmode(GPIO.BCM)

If I use this Python code can I assume that I can use ANY GPIO pins as inputs or outputs? (The ones marked GPIO 1-27). That's looking at the diagram at http://www.raspberrypi-spy.co.uk/2012/0 ... -2700x900/

Brian
Raspberry Pi B, Raspberry Pi 2, monitor, powered hub, mouse, keyboard and Volvo V50 car

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

Re: GPIO pin numbers?

Tue Mar 24, 2015 2:06 pm

This is worth a read http://elinux.org/RPi_Low-level_peripherals. https://projects.drogon.net/raspberry-p ... functions/ I'm not sure they have been updated to the latest 40pin versions

Some pins are used by other functions, and may have pull ups already, but I believe you can disable those functions to use them for input / output. Sorry I dont know all the details as I've never used them.

meddyliol
Posts: 118
Joined: Thu Mar 20, 2014 8:28 am
Location: South Wales

Re: GPIO pin numbers?

Tue Mar 24, 2015 2:24 pm

Thanks for that link. I have seen it before but completely forgot about it. Will have a good read of it.

Cheers

Brian
Raspberry Pi B, Raspberry Pi 2, monitor, powered hub, mouse, keyboard and Volvo V50 car

meddyliol
Posts: 118
Joined: Thu Mar 20, 2014 8:28 am
Location: South Wales

Re: GPIO pin numbers?

Tue Mar 24, 2015 2:27 pm

It has been updated and now shows the 40 pin version :D
Raspberry Pi B, Raspberry Pi 2, monitor, powered hub, mouse, keyboard and Volvo V50 car

paulv
Posts: 564
Joined: Tue Jan 15, 2013 12:10 pm
Location: Netherlands

Re: GPIO pin numbers?

Tue Mar 24, 2015 6:39 pm

Sadly, the RPi low level... Wiki page still mentions that GPIO ports are not 5 Volt tolerant. It even puts it in bold typeface.
In absence of the specification, we can only speculate or use what we're being told.

I don't want to cause a firestorm or flaming session, but it has been proven by actual measurements that there is something on the die of the chip that prevents the voltage on the GPIO ports to get much above 5.6V.
This observation can be explained by a diode to the 5V supply, because that would result in a max level of 5V + the junction of the silicium diode of 0.6V.

This is why these ports can be called 5V tolerant. To continue to label it otherwise is wrong, and a disservice to the users, unless a specification can be produced, or the chip designer refutes, denies or agrees with this.

The same measurement has also proven that there is no diode to the 3.3V supply, which seems to be assumed by many, possibly led there by the statement that the ports are 3V3 levels.

Of course it is prudent to limit the voltage and, more importantly, the current going in to the port because in absense of the secifications, we don't know how much energy this clamping diode can take.

Sorry about my rant, but there is too much speculation, and not enough specification.

Return to “Beginners”