Special Pin Functions

WiringPi defines 17 pins, (21 on a Rev. 2 board) but some of them and the functions we can use may potentially cause problems with other parts of the Raspberry Pi Linux system.

  • Pins 0 through 7 (BCM_GPIO 17, 18,  21, 22, 23, 24, 25, 4 respectively, substitute 27 for 21 on a Rev. 2 board): These are safe to use at any time and can be set to input or output with or without the internal pull-up or pull-down resistors enabled.
  • PWM: You can change the function of pin 1 (BCM_GPIPO 18) to be PWM output, however if you are currently playing music or using the audio system via the 3.5mm jack socket, then you’ll find one channel of audio PWM coming through the pin! If you are not using the audio at all, (or the audio is going via the HDMI cable), then this pin is free to be used in PWM mode.
  • Pins 8 and 9 (BCM GPIO 0 and 1 on a Rev. 1 board, 2 and 3 on a Rev. 2 board): These are the I2C pins. You may use them for digital IO if you are not using any I2C drivers which use these pins, however note that they have on-board 1.8KΩ resistors pulling the signals to the 3v3 supply. This feature does make them handy for switch inputs where the switch simply shorts the pin to ground without having to enable the internal pull-up resistors
  • Pins 10, 11, 12, 13 and 14 (GPIO 8, 7, 10, 9 and 11 respectively): These are used for the SPI interface. Like the I2C interface, if you are not using it, then you can freely use them for your own purposes. Unlike I2C, these pins do not have any external pull up (or pull down) resistors.
  • Pins 15 and 16 (GPIO 14 and 15): These are used by the UART for Tx and Rx respectively. If you want to use these pins as general purpose I/O pins then you need to make sure that you reboot your Pi with the serial console disabled. See the file /boot/cmdline.txt and edit it appropriately.
  • Pins 17, 18, 19 and 20: (BCM_GPIO 28, 29, 30 and 31) These are additional GPIO pins on the Rev. 2 board.

Remember: The Raspberry Pi is a 3.3 volt device! Attempting to connect to any 5V logic system will very likely result in tears…

Comments

Special Pin Functions — 24 Comments

  1. Best and most useful info on how to use the Raspberry PI and how to access the I/O Header I found so far. maybe you elaborate also on how to use SPI, I2C and UART in that style. Thanks Gordan!

    • At present, there seems to be more than one driver for the I2C and SPI interfaces so rather then write my own (which I may still do), I’m waiting to see how these pan out. However the UART is fairly standard, so I can easilly include a page for that. Watch this space, as they say!

      -Gordon

      • For an RPi UART – PC RS232 connection can I use the MAX3232 IC with 4/5 capacitors?

        How can I connect a 5 V device to the UART (e. g. a PIC microcontroller)? The above mentioned IC can make this type of conversion, too?

        How can I send/receive data over UART with RPi (to/from PC or microcontroller)?

        • Yes – just remember the Pi’s serial is 3.3v.

          For a simple 5V device, then a level convertor will work – e.g. this one:
          http://www.skpang.co.uk/catalog/logic-level-converter-p-511.html

          The Pi is a Linux system, so any of the standard Linux serial IO code will work. If you need to look at human readable type output then a terminal program like minicom will be fine on the Pi. If you need a program to exchange data, then look at the wiringPi librarys I’ve written – there is a serial module in it which might help.

          -Gordon

          • Thanks!

            I plan to connect a PIC MC, which has a built-in Analog-to-Digital Converter. The PIC would send the measured data through UART to the Pi, which would store the values. Will a simple level convertor do for this?

            Sure, an I2C ADC IC could be used, but the Debian image doesn’t contain i2c support. 🙁

          • Yes, the level convertor I posted earlier should be fine.
            There are I2C and SPI drivers in some of the later kernels, but they’re not in the mainstream releases yet though.
            I’ve interfaced Arduinos to the Pi – but only using the USB serial to the Arduno. That worked well. e.g. see: http://unicorn.drogon.net/piduino.jpg

            Another option might be to run the PIC at 3.3v if possible – I’m doing this with some ATmega chips, then they just directly connect to the Pi’s serial port.
            -Gordon

          • I’ve got a PIC16LF876A. I think, that the ‘L’ in the name could mean that low-voltage capable model.
            Could the Pi supply enough current through its 3.3 V line to the PIC or I’ll need an independent power supply?

            By the way, using an USB-serial converter cable, I can connect the PIC to one of Pi’s USB ports, too (of course, a TTL/RS232 “converter” is required). 🙂

          • You can take about 50mA out of the 3.3v connector on the GPIO, so it might be enough…. You can take more out of the 5V connector – it’s shared with the rest of the board and limited by the 700mA polyfuse…

            -Gordon

  2. Thanks!
    Which drivers do we need for UART communication in Linux? E. g. to connect the Pi to other USB host-enabled embedded Linux devices (like some Android smartphones) via UART. Because my USB host-enabled Android smartphone doesn’t seem to recognize out of the box my USB-serial adapter cable.
    Or support cannot be supplied through kernel objects, so custom built kernel is required?

    • The UART on the GPIO is a standard driver in the Linux kernel. It appears as /dev/ttyAMA0. Lookup any standard Linux serial handling code if you want to use it, but you might need to disable the kernel frpm printing messages to it (see /boot/cmdline.txt) and Linux from running a login process on it (see /etc/inittab).

      If you want some code to talk over it, then have a look at wiringPi – there are some serial access functions in that to talk to a remote device via a Linux serial port (the on-board UART, or a serial USB adapter)
      -Gordon

  3. Hello, great information and help, thanks, I think there’s only a small error in “SPI pins 10,11,12,13 and 14 Pins (GPIO 8, 7, 10, 9 and 14 respectively)” 14 should be 11 SCLK

  4. Can you document the I2S pinouts and functionality for RPi V2 ?

    I am interested in finding information and pointers on how to use I2S interfaces on RPi.
    According to information there is a possibility to configure for 3 I2S interfaces.
    My hope is to find or inspire some one to describe and implement linux JACK drivers for at least 4 channels 24bit 48kHz (maybe 96KHz also) ADC interface.
    This would be a great step forward to create a digital recorder.

    Sadly most I2S users concentrate on DAC circuits.
    – bosse

    • I’m afraid that I2S is something I know very little about so it’s not even on my radar to interface to. There have been a few posts on the Raspberry Pi forums though..

      -Gordon

  5. Gordon,
    Thank you for your excellent notes and examples. From the Special Pin Functions above I understand that I could replace a simple circuit like the one shown at http://en.wikipedia.org/wiki/File:Pullup_Resistor.png with the even simpler circuit of a wire running from pin 8 (SDA0, physical pin 3) to a switch and a wire running from the other side of the switch to ground (physical pin 6). Is this correct?

    If it is correct, do I simply use gpio to configure pin 8 (SDA0) for input? Do I need to do anything to cause it to use the 1k8 pull-up resistor?

    My goal is to connect a switch to pin 8 and ground and then use interrupts to detect the falling edge when the switch is closed. Is this possible?

    • Yes, it’s possible. Should be relatively easy too. The on-board pull-up will keep the pin biased high, you just need to make sure you don’t load the I2C kernel module (it’s not loaded by default though).

      -Gordon

  6. Hi, nice share 🙂
    But, i want to ask u, where should I put my sensor Input/Output that use RX/TX? I use ultrasonic sensor SRF04.
    Based on your explanation pin RX/TX is in GPIO 14 and 15. Is that GPIO 14 and 15 in pin 15 and 16? but in other source they write that RX/TX is in pin 8 and 10.
    And what should I write on my python to access the RX/TX pin?
    for example:
    GPIO.setup(14, GPIO.OUT)
    GPIO.setup(15, GPIO.IN)
    or
    GPIO.setup(15, GPIO.OUT)
    GPIO.setup(16, GPIO.IN)

    sorry, i really need the explanation, because I still new with this raspberry.
    thankyou so much Gordon. 🙂

    • GPIO.setup() is not one of my functions, and all my code is in C, not Python. You’d be better off asking in whatever forums/etc. of that particular software.

      However if you want to use the serial port, then you do not need to change any pin modes. If you do, then you take it out of serial mode and into GPIO mode.

      -Gordon

      • ah..yaa.. i know, pin configuration is depend on what mode that i use to access the GPIO, right?
        Thank u so much Gordon 🙂

  7. Hi Gordon,

    Great work!, I was wondering if you set the uart pins (Gpio 14/15) to an output or input, how do you change them back to Tx/Rx to use as uart again?

    • Reboot the Pi.

      Probably not the answer you wanted, but it’s the easiest way.

      I have considered putting some extra stuff into the GPIO program to do things like this though – wondering how often it might be used though… ?

      -Gordon

  8. Hi, I am trying to connect atmega 8 which used 5v to raspberry pi and am having some problem with voltage.. I made a daugther board for raspberry pi, which job is to supply 5v to raspberry pi and connect SDA and SCL signals.. I connected a I2c bus to atmega circuit.. So when i connect atmega8 to daugther board, the current will be directly be supplied to atmega from daughter without involving raspberry pi, but the SDA and SCL pins are connected to raspberry pi.. Do i need any converter in this case ?

    • Use serial, not I2C. Get I2C wrong on the ATmega and you’ll crash the Pi.

      Using serial you’ll need a resistor divider to take the 5v serial from the ATmega to the Pi, but the other way will be OK.

      If you want to continue using I2C (why?) then you will probably get away with not using any converters as it’s a pull-down bus and the Pi keeps it at 3.3v (do not enable the pull-ups on the Atmega). To do it properly, you need a pair of MOSFETs running as level converters. See e.g. http://www.flickr.com/photos/linux-works/6433044007/

      But really – dump I2C. it’s not fast, complex to program and prone to crashing the Pi if you get it wrong. I’ve just finished a project involving an ATmega talking to a Pi via I2C and never plan to do another.

      -Gordon