Raspberry PI GPIO

On Raspberry PI I use the WiringPi library for both C and Python development. Below is an example of cycling through three GPIO pins to light up a set of LEDs.

  • GPIO 17 – Pin 6 maps to the WiringPi logical pin 0
  • GPIO 22 – Pin 7 maps to the WiringPi logical pin 2
  • GPIO 27 – Pin 7 maps to the WiringPi logical pin 3
#include <wiringPi.h>

int main (void)
{
	int offset;

	offset = 100;
	wiringPiSetup () ;
	pinMode(0, OUTPUT);
	pinMode(2, OUTPUT);
	pinMode(3, OUTPUT);
	for (;;) // Loop continously
	{
		digitalWrite (0, HIGH) ;
		delay(offset); // delay ‘offset’ miliseconds
		digitalWrite(0, LOW); // turn off 0
		digitalWrite(2, HIGH); // turn on 2
		delay(offset);
		digitalWrite(2, LOW); // turn off 2
		digitalWrite(3, HIGH); // turn on 3
		delay (offset);
		digitalWrite(3, LOW); // turn off 3
	} // Loop back and start over
	return 0 ;
}

Compile using gcc:

#gcc gpiox.c -o wp gcc wpiox.c -o wp -lwiringPi

The ‘-lwiringPi’ tells the gcc compiler to use the wiring Pi library. Then run using…
#./wp

And watch the LEDs flash…