SImple RaspberryPi example of demonstrating GPIO with LEDs

This project is based on the Raspberry Pi B+.  It uses three GPIO pins and Ground:

  • GPIO17 - pin 6
  • GPIO22 - pin 8
  • GPIO27 - pin 7
  • GND      - pin 5

Parts needed

3 x 220 ohms resistor

3 x LED

4 or 5 jumber wires (M-F)

1 x breadboard

 

Place the LEDs on the breadboard with the cathode lead in connected to the Ground and the anode lead on the main breadboard..  The resistors connect across the center divider and jumper wires from the resistors to pins 6, 7 and 8.

The last jumper wire connection is from pind 5 (GND) to the ground connector the LEDs share.

 

Code -

Assumes you have the WiringPi library installed (http://wiringpi.com/).  

 

On Raspberry PI the WiringPi library supports both C and C++ development. Below is an example of cycling through three GPIO pins to light up a set of LEDs.  The code is also in the attached pi_LED2.c file.


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 on the Raspberry Pi with the folowing:

#gcc pi_LED2.c -o piLED -lwiringPi

 

The run with # ./piLED