Raspberry PI RGB example in C

In this example we will flash an RGB LED using C

Schematics

Here is a schematic and layout

Pi and RGB Led WiringPi_schem

Pi and RGB Led WiringPi_bb

Code

This example requires wiringPi to be installed – http://wiringpi.com/download-and-install/

#include <wiringPi.h>
#include <stdio.h>
 
#define   REDLED   0
#define   GREENLED  1
#define   BLUELED 2
 
void init(void)
{
    pinMode(REDLED, OUTPUT);
    pinMode(GREENLED, OUTPUT);
    pinMode(BLUELED, OUTPUT);
    digitalWrite(REDLED, 1);
    digitalWrite(GREENLED, 1);
    digitalWrite(BLUELED, 1);
}
 
void AllOff(void)
{
    digitalWrite(REDLED, 1);
    digitalWrite(GREENLED, 1);
    digitalWrite(BLUELED, 1);
}
 
int main(void)
{
    int i;
 
    if(wiringPiSetup() == -1)
	{ 
        printf("setup of wiringPi failed !");
        return 1;
    }
 
    init();
 
    for(i =0;i < 10; i++)
	{
		digitalWrite(REDLED, 0);
        delay(1000);
		AllOff();
		delay(1000);
		digitalWrite(GREENLED, 0);
        delay(1000);
		AllOff();
		delay(1000);
		digitalWrite(BLUELED, 0);
        delay(1000);
		AllOff();	
		delay(1000);		
    }
 
    return 0;
}

 

SHARE
Previous articleRaspberry Pi and 74hc595 wiringPi example
Next articleRGB Example