I want to use the altenate function of GPIO 4 namely the GPCLK0 function. How can one set a GPIO to its alternate function? Many alternate functions are summarized here: elinux-wiki

The best would be if it is possible with the wiringPi Library. But i do not mind to use arbitrary C-code if someone tells me how to do it. After entering an alternative function, i think it is needed to configure it?

7 Answers 7


You can use the following C Macro to change a given GPIO to an alternate function as specified by this table: http://elinux.org/RPi_BCM2835_GPIOs

volatile unsigned *gpio;

#define SET_GPIO_ALT(g,a) *(gpio+(((g)/10))) |= (((a)<=3?(a)+4:(a)==4?3:2)<<(((g)%10)*3))

Example (from http://elinux.org/RPi_Low-level_peripherals#GPIO_hardware_hacking)

The following C Macros would change GPIO g to its ALT0 function:


Always use INP_GPIO(x) before using SET_GPIO_ALT(x,y)

#define INP_GPIO(g) *(gpio+((g)/10)) &= ~(7<<(((g)%10)*3))
  • 3
    Wow. That first macro is...unbelievable. Would you mind explaining how it works?
    – fouric
    Commented Mar 5, 2013 at 0:38

Using the pigpio library to setup GPIO 4 as ALT0 function i.e. GPCLK0:

$ pigs m 4 0

Ref.: changing mode of GPIO


here are some other threads (on www.raspberrypi.org) that are dealing with the GPCLK0 function.

the second link has an exact C example on how to set the GPCLK0 alternate function.

/* Setup GPIO 4 as ALT0 function i.e. GPCLK0 */
mov r0, #4
mov r1, #4 /* ALT0 */
  • Thanks for that answer, i tried a littlebit with that assembler-code, but i think assembler is not my thing. This takes longer than exspected..
    – user61664
    Commented Nov 4, 2012 at 8:13

This tutorial shows different ways you can configure the GPIO pins, such as bash, python, and C using wiringPi. It's as easy as an Arduino.


You'll need to look into the WiringPi library a little more to know exactly what to set for the specific "alternate function" you want, eg SPI, PWM, I²C etc, since you didn't specify.

  • Link requires account to read, if it's even still alive.
    – Phil Hord
    Commented Oct 31, 2022 at 20:14
  • Looks like it might have been taken over, or being migrated. I found it in the wayback cache at web.archive.org/web/20121214004835/http://log.liminastudio.com/… Short version in case that disappears too (this may be way out of date, since it was 2013): Python: sudo pip install rpi.gpio import RPi.GPIO as GPIO GPIO.setup(7, GPIO.OUT) GPIO.output(7, True) GPIO.output(7,False) C: get WiringPi library from wiringpi.com - it is not on github.
    – jbyrnes
    Commented Nov 1, 2022 at 2:36

Use this code to set the alt function

static void bcm2708_set_gpio_alt(int pin, int alt)
     * This is the common way to handle the GPIO pins for
     * the Raspberry Pi.
     * TODO This is a hack. Use pinmux / pinctrl.
#define INP_GPIO(g) *(gpio+((g)/10)) &= ~(7<<(((g)%10)*3))
#define SET_GPIO_ALT(g,a) *(gpio+(((g)/10))) |= (((a)<=3?(a)+4:(a)==4?3:2)        <<(((g)%10)*3))
    unsigned int *gpio;
    gpio = ioremap(GPIO_BASE, SZ_16K);
    SET_GPIO_ALT(pin, alt);
#undef INP_GPIO

I found the pigpio library can do this for several languages, including python (which is where I needed it).

#!/usr/bin/env python3
import pigpio

pi = pigpio.pi()
if pi.connected:
    # Configure RTS0 for alt function 3
    pi.set_mode(RTS, pigpio.ALT3)

You can use raspi-gpio in terminal.
raspi-gpio set 4 a4 - Change GPIO4 to alternative function 4.

Another helpful commands:
raspi-gpio help
raspi-gpio get - Prints state of all GPIOs one per line.
raspi-gpio get 20 - Prints state of 20.
raspi-gpio set 20 pu - Enable GPIO20 ~50k in-pad pull up.
raspi-gpio set 20 pd - Enable GPIO20 ~50k in-pad pull down.

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.