11

hopefully this question is rather simple but my quick googling did not find the answer.

I know that the RPi has several GPIOs that I can use as well as specialized GPIOs (SPI, I2C, UART, etc). In my project I am actually running out of the regular GPIOs and would like to use the SPI, I2C, and other GPIOs as "regular" GPIOs as well. I know that this can be done, but I guess I'm not sure how I can set them up to not run in SPI/I2C/etc modes and just run as regular GPIOs.

Do I need to do anything to properly set them up for this?

10

The short answer

No, you (almost) don't. It's actually the other way around - you have to set them up not to be GPIO but to serve their special purpose. The only exception are UART pins. Also and I²C pins somehow special.

UART pins

UART pins are used by the kernel for the console. You will have to configure the system not to use them if you want them to be available for your usage:

  • edit /boot/cmdline.txt and remove console=ttyAMA0,115200 and kgdboc=ttyAMA0,115200 parameters
  • edit /etc/inittab and comment out (put # character in front of it) the line:

    T0:23:respawn:/sbin/getty -L ttyAMA0 115200 vt100
    
  • reboot your RaspberryPi

Now the pins should not be used by the system and you can use them

I²C pins

I²C pins have on-board 1.8k pull-up resistors installed. They can't be disabled. This limits those pins usage in some situations (but can be handy in others).

Additional solutions

If running low on GPIO pins, you can sometimes easily extend it's number by using some additional (but simple) hardware. For output for example, you can use shift registers or I²C I/O expanders (also called `I²C bus expanders), both easily to buy and use. You can find example of it's usage here.

PFC8574 (or similar from this family) are so popular that there is a ready to use kernel module that handles all the communication for you (it's not enabled in the official RaspberryPi kernel, you will need to compile your own kernel to use it). The module is called gpio_pcf857x, providing that you loaded it with modprobe (along with I²C drivers) and that you have the chip connected to I²C bus, you can activate it like this:

echo pcf8574 0x27 > /sys/class/i2c-adapter/i2c-0/new_device

where pcf8574 is your actual chip name (this module can handle few different chips like pca967x and max732x) and 0x27 is its address on I²C (you can configure the address of the chip when wiring it).

This will create entries in /sys/class/gpio/ directory that will allow you to use them the same way as other GPIO ports in RaspberryPi, providing you are using sys interface (for example wiringPiSetupSys() in wiringPi). Unfortunately, Python's RPi.GPIO module does not support this mode so it won't be able to use this pins with this library. The pins will have some high numbers like 248-255 or similar.

Note: PCF8574 is sinking current - it can give drive sink (accept in low state) as much as 20mA but it can only source 0.1mA in high state. This means that you are usually going to use it in reverse logic, i.e. +3.3V -> LED -> RESISTOR -> PCF8574_PIN.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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