Tuesday, December 19, 2017

Raspberry PI - Program a Blinking LED


  1. Connect your LED's cathode (short leg) to a 0V or Ground pin of the Raspberry PI. 
  2. Again connect your LED's Anode (Long Leg) to a GPIO pin via a resistor around 150 ohms (Resistor will save your LED from burnings due to high voltages). Here we use GPIO 8 to be connected to the Anode.
  3. Write the following code.

  4. #include <wiringPi.h> //import the wiringPi lib
    #include <stdio.h>

    #define LedPin 8  //GPIO index which will be connected to the LED's Anode

    int main(void) {
    //Initializing the wiringPi
           if(wiringPiSetup() == -1) {
                   printf("setup wiringPi failed !\n");
                   return -1;
           }
                                    
                                    //Set the PIN mode as output
           pinMode(LedPin, OUTPUT);

           while(1) {
                   digitalWrite(LedPin, LOW);   //led on
                   printf("led on\n");
                   delay(1000);                 // wait 1 sec
                   digitalWrite(LedPin, HIGH);  //led off
                   printf("led off\n");
                   delay(1000);                 // wait 1 sec
           }
           return 0;

    }


  5. Save above code as blinking_led.c then compile and run as below.
  • gcc blinking_led.c -o blinking_led -lwiringPi
  • ./blinking_led



No comments:

Post a Comment