Home Code Raspberry PI RGB example in C

Raspberry PI RGB example in C

by shedboy71

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/

[codesyntax lang=”c”]

#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;
}

[/codesyntax]

 

You may also like

This website uses cookies to improve your experience. We'll assume you're ok with this, but you can opt-out if you wish. Accept Read More