Raspberry Pi + C = Blinking LED

This time we are going to write C application for Raspberry Pi to do a blinking LED.

We would reuse the same scheme as we use in Python example.

For C example we will use wiringPi library.
It’s more then just a C library it’s also a command line tool, so it’s easy to verify that it’s installed – just type gpio -v in terminal and if you don’t get “app not found” you are good to go.

Otherwise get it:

git clone git://git.drogon.net/wiringPi
cd wiringPi
./build

Now open Vi/Vim and enter a program:

#include <wiringPi>
#include <stdio.h>

LED 0 /* note, this is pin 11 if you use wiringPiSetup*/

int main()
{
    if(-1 == wiringPiSetup())
    {
         printf("something bad had happend\n");
         return -1;
    }

    pinMode(LED, OUTPUT);
    while(1)
    {
         digitalWrite(LED, HIGH);
         delay(1000);
         digitalWrite(LED, LOW);
         delay(1000);
    }

    return 0;
}

save it as blink.c and compile with

gcc -o blink blink.c -l wiringPi

and run

./blink

The LED should start blinking.

Leave a Reply