Blink

Blink is the “Hello World” of the microcontroller world. It usually does nothing more than flash an LED or 2.

So, assuming that you have your Pi connected to your Gertboard as described in the last page then we can write our first program for it using wiringPi.

One thing to note: wiringPi has it’s own numbering convention which is somewhat different to the native chip numbers that are on the Pi’s SoC. Traditionally we’d refer to the numbers which are the actual bit positions of the signals inside the internal GPIO registers – this is fine, but on the Pi they are not intuitive and worse, some of them changed from Rev 1 to Rev 2, so from now on I’ll be using the wiringPi numbers, but will indicate their “real” numbers by referring to the BCM_GPIO number. The wiringPi pin numbers remain constant regardless of the board revision.

Our blink program (One LED)

The blink program should be included as part of the wiringPi distribution in the examples directory – it’s reproduced here for convenience though:

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

// LED Pin - wiringPi pin 0 is BCM_GPIO 17.

#define LED     0

int main (void)
{
  printf ("Raspberry Pi - Gertboard Blink\n") ;

  wiringPiSetup () ;

  pinMode (LED, OUTPUT) ;

  for (;;)
  {
    digitalWrite (LED, 1) ;     // On
    delay (500) ;               // mS
    digitalWrite (LED, 0) ;     // Off
    delay (500) ;
  }
  return 0 ;
}

The important part is the #include <wiringPi.h> the rest should be fairly straightforward if you have programmed in C or C++ before. To compile and run:

gcc -o blink blink.c -lwiringPi
sudo ./blink

You need to link with the wiringPi library, hence the -lwiringPi and you also need to be root to run the program, as only root can directly access the GPIO.

The wiringPi functions we are using are:

  • wiringPiSetup()

This must be called before anything else – it opens the GPIO devices and allows our program to access it.

  • pinMode()

This set the mode of the pin – usually in or out, but there are some other functions too.

  • digitalWrite()

Outputs a value (0 or 1) to the given pin.

  • delay()

This delays for a number of milliseconds.

So there should be nothing out of the ordinary here – and some will be very familiar if you have used an Arduino in the past.

When your program is running, you can press Control-C to stop it and return to command mode.

More LEDs

Now lets wire up 7 more LEDs so we have 8 in total. We’ll wire them from Gertboard pins GP17, GP18, GP21, GP22, GP23, GP24, GP25 and GP4 to B1 through B8 respectively as shown in this table:

J2 <- Jumper Wire -> J3
GP17 <–> B1
GP18 <–> B2
GP21 <–> B3
GP22 <–> B4
GP23 <–> B5
GP24 <–> B6
GP25 <–> B7
GP4 <–> B8

The result should be something like below:

gertb2I’ve used colour coded cable here, so if you know your resistor colour codes then you may be able to work out which wire is which, however this takes the first 8 GPIO pins on the Pi and connects them to the first 8 buffer inputs.

LED Sequence (8 LEDs)

Look for the blink8.c program in the examples directory.

This program will send a simple sequence to the LEDs. Setting them on one at a time, then turning them off again. As before, compile and run with:

gcc -o blink8 blink8.c -lwiringPi
sudo ./blink8

Examine the program and note that we’re using wiringPi pin numbers here and that they conveniently map 0 -> 7 to the first 8 GPIO pins.

This is almost as far as we can go with simple LED blink programs – what you should do now is modify these programs an write your own to do some more tasks.

All the wiringPi functions are documented here.

Next, we’ll go for broke and wire up all 12 LEDs.

Comments

Blink — 9 Comments

  1. This website nice. but can any body say, where i will get information about wiring pi package built in functions and Serial communication

  2. Hi.
    When I run the program, all 12 LED’s from GertBoard turn to red. Is this a normal behaviour, considering that this is an example for just 1 LED? Also, what can I do to fix it?
    Thanks!

    • It’s due to the LEDs being connected via high impedance input buffers. Try grounding them. (or connect more to the GPIO pins)

      -Gordon

      • Hi, Gordon!
        I tried doing the test with 4 connected LEDS(same problem), and after that I added a jumper connecting the BUFF pin to the GND right above the LED ( the LED went off, but wouldn’t go on).
        Also, kindly check your e-mail when you have time. Thanks!

  3. Hi,
    I have some problems by compile the blink.c

    In functio ´_start’:
    (.text+0x34): undefined reference to ´main’

    I don´t know why ???
    need help please

    -Fabian

    • I’ve no idea what you’re doing here. Probably not using the makefile (try: make blink) or using the -c flag to gcc. To hand-compile blink:

      gcc -o blink blink.c -lwiringPi

      -Gordon

  4. if i will start the blink.c programm i will get this error:
    fatal error: wiringPi.h: No such file or directory
    compilation terminated.

    the gcc don’t find the library of wiringPi.h..
    any idea how i can fix this problem?