Update: 14th May, 2013
wiringPi version 2 has been released and now has its own website (http://wiringpi.com/) to look after it. Most of the documentation on the projects site has been copied over to it the new site, but there may still be 1 or 2 pages that are still missing. I’d encourage you to use the new site if possible where there will be a forum and wiki (when I get time to implement them!)
Here are two small example programs, demonstrating the use of the WiringPi library. These programs are presen in the examples directory of the wiringPi distribution. Please see refer to the versions there for the current “best practices” for using wiringPi.
The first test program needs 8 LEDs wired to the 8 normal GPIO outputs – WiringPi pins 0 through 7. It also has a switch connected to WiringPi pin 8 shorting to ground when pushed. (That will normally read 1 when not pushed due to the on-board 1k8 pull-up resistor – ignore the 2k2 comment in the code, it’s 1k8!)
/* * test1.c: * Simple test program to test the wiringPi functions */ #include <wiringPi.h> #include <stdio.h> #include <stdlib.h> #include <stdint.h> // Simple sequencer data // Triplets of LED, On/Off and delay uint8_t data [] = { 0, 1, 1, 1, 1, 1, 0, 0, 0, 2, 1, 1, 1, 0, 0, 3, 1, 1, 2, 0, 0, 4, 1, 1, 3, 0, 0, 5, 1, 1, 4, 0, 0, 6, 1, 1, 5, 0, 0, 7, 1, 1, 6, 0, 1, 7, 0, 1, 0, 0, 1, // Extra delay // Back again 7, 1, 1, 6, 1, 1, 7, 0, 0, 5, 1, 1, 6, 0, 0, 4, 1, 1, 5, 0, 0, 3, 1, 1, 4, 0, 0, 2, 1, 1, 3, 0, 0, 1, 1, 1, 2, 0, 0, 0, 1, 1, 1, 0, 1, 0, 0, 1, 0, 0, 1, // Extra delay 9, 9, 9, // End marker } ; int main (void) { int pin ; int dataPtr ; int l, s, d ; printf ("Raspberry Pi wiringPi test program\n") ; if (wiringPiSetup () == -1) exit (1) ; for (pin = 0 ; pin < 8 ; ++pin) pinMode (pin, OUTPUT) ; pinMode (8, INPUT) ; // Pin 8 SDA0 - Has on-board 2k2 pull-up resistor dataPtr = 0 ; for (;;) { l = data [dataPtr++] ; // LED s = data [dataPtr++] ; // State d = data [dataPtr++] ; // Duration (10ths) if ((l + s + d) == 27) { dataPtr = 0 ; continue ; } digitalWrite (l, s) ; if (digitalRead (8) == 0) // Pressed as our switch shorts to ground delay (d * 10) ; // Faster! else delay (d * 100) ; } return 0 ; }
This 2nd test demonstrates the PWM function: (WiringPi Pin 1 connected to a standard LED via a 220Ω resistor)
/* * test2.c: * Simple test program to test the wiringPi functions * PWM test */ #include <wiringPi.h> #include <stdio.h> #include <stdlib.h> #include <stdint.h> int main (void) { int pin ; int l ; printf ("Raspberry Pi wiringPi PWM test program\n") ; if (wiringPiSetup () == -1) exit (1) ; for (pin = 0 ; pin < 8 ; ++pin) { pinMode (pin, OUTPUT) ; digitalWrite (pin, LOW) ; } pinMode (1, PWM_OUTPUT) ; for (;;) { for (l = 0 ; l < 1024 ; ++l) { pwmWrite (1, l) ; delay (1) ; } for (l = 1023 ; l >= 0 ; --l) { pwmWrite (1, l) ; delay (1) ; } } return 0 ; }