- Connect your LED's cathode (short leg) to a 0V or Ground pin of the Raspberry PI.
- 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.
- Write the following code.
- Save above code as blinking_led.c then compile and run as below.
#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;
}
- gcc blinking_led.c -o blinking_led -lwiringPi
- ./blinking_led