Sunday, September 6, 2015

Sample C Code to turn a LED On/Off on Raspberry Pi on GPIO Pin


This "C" code below will turn the LED on Pin 17, also known as  GPIO17, also known as GPIO GEN0/GPIO17, (6th pin on inside column from top - if you are holding RPi with the USB ports facing down )


This was tested on a Raspberry Pi Model B.
 
This is defined by the API call "wiringPiSetupGpio()" in the beginning of the code.

However, there is yet another API called "wiringPiSetup()", Here Pin 17 would mean +3V !! - so have to watch out !

Be Careful how you want to define your pin numbering, and use the appropriate API call before sending signals to the Pins on the RPi.

#include <wiringPi.h>
#include <stdio.h>
int main (void)
{
  if ( wiringPiSetupGpio () == -1 ){
        printf ("wiring failed - returning ! ");
        return 0;
  }
  pinMode (17, OUTPUT) ;
  for (;;)
  {
    digitalWrite (17, HIGH) ;
        delay (500) ;
    digitalWrite (17,  LOW) ;
        delay (500) ;
  }
  return 0 ;
}

No comments:

Post a Comment