ADMFactory
Tutorials from a developer perspective

RGB LED on Raspberry Pi using Java

by ADM

 

 


In this article will make an RGB LED to change colors using Raspberry Pi and Java. The LED will change 8 different colors.

The colors used are:

Color name Hex Value Color
Red 0xFF0000
Green 0x00FF00
Blue 0x0000FF
Yellow 0xFFFF00
Cyan 0x00FFFF
Pink 0xFF00FF
White 0xFFFFFF
Violet 0x9400D3

RGB LED

The LED is the abbreviation of light emitting diode. The LED has two electrodes, a positive electrode and a negative electrode. It lights up only when a forward current passes. The RGB LEDs consist of three LEDs in different colors: red, green and blue. These three colored LEDs are capable of producing any color. Tri-color LEDs with red, green, and blue emitters, in general use a four-wire connection with one common lead (anode or cathode).

The symbol for common anode RGB LED:

the symbol for common cathode RGB LED:

For this tutorial will going to use a common anode RGB LED.

Components

You will need the following components:

Note: you can easily find all components on Amazon or Ebay (in my case, I bought a "Raspberry Pi starter kit" containing a lot of components).

Schematics

To do the schematics I used Fritzing software. The longest pin is the common anode of the three LEDs. The pin is connected to the +3.3V pin of the Raspberry Pi, and the rest pins are connected to pin 11, pin 12, and pin 13 of Raspberry Pi with a current limiting resistor between (220Ω). Using these pins, we can control the color of the RGB LED by 3-channel PWM signal. If you need more details on PWM, please check Breathing Light LED on Raspberry Pi using Java tutorial.

And the physical visualization of the circuit.

Hardware

Now it's time to connect everything according to the diagram presented in the section above.

Software

Key functions

If you didn't installed already, you need to install Pi4j, for this run the following command:

$ curl -s get.pi4j.com | sudo bash

The command will do the following:

Note: all the code will be written directly on Raspberry PI console using vim editor. I've chosen this method because the example is simple and doesn't require a long time to edit. A better approach is to edit the source code locally, on your PC, using your preferable editor and then upload the code to Raspberry PI.

Login to Raspberry PI using ssh command from Linux and MacOS or using putty from windows, then open the vim editor using the following command:

$ sudo vim RGBLed.java

Copy/paste the following code.

import com.pi4j.wiringpi.Gpio;
import com.pi4j.wiringpi.SoftPwm;

public class RGBLed {

    private static int PIN_NUMBER_RED = 0;
    private static int PIN_NUMBER_GREEN = 1;
    private static int PIN_NUMBER_BLUE = 2;

    private static int colors[] = { 0xFF0000, 0x00FF00, 0x0000FF, 0xFFFF00, 0x00FFFF, 0xFF00FF, 0xFFFFFF, 0x9400D3 };

    private int map(int x, int in_min, int in_max, int out_min, int out_max) {
	return (x - in_min) * (out_max - out_min) / (in_max - in_min) + out_min;
    }

    /**
     * set color, for example: 0xde3f47
     * 
     * @param color
     *            0xde3f47
     */
    private void ledColorSet(int color) {
	int r_val, g_val, b_val;

	r_val = (color & 0xFF0000) >> 16; // get red value
	g_val = (color & 0x00FF00) >> 8; // get green value
	b_val = (color & 0x0000FF) >> 0; // get blue value

	r_val = map(r_val, 0, 255, 0, 100); // change a num(0~255) to 0~100
	g_val = map(g_val, 0, 255, 0, 100);
	b_val = map(b_val, 0, 255, 0, 100);

	SoftPwm.softPwmWrite(PIN_NUMBER_RED, 100 - r_val); // change duty cycle
	SoftPwm.softPwmWrite(PIN_NUMBER_GREEN, 100 - g_val);
	SoftPwm.softPwmWrite(PIN_NUMBER_BLUE, 100 - b_val);
    }

    private void init() {
	/** initialize the wiringPi library, this is needed for PWM */
	Gpio.wiringPiSetup();

	SoftPwm.softPwmCreate(PIN_NUMBER_RED, 0, 100);
	SoftPwm.softPwmCreate(PIN_NUMBER_GREEN, 0, 100);
	SoftPwm.softPwmCreate(PIN_NUMBER_BLUE, 0, 100);
    }

    /**
     * Main method called to change the LED colors
     * 
     * @throws Exception
     *             when Thread.sleep is interupted
     */
    private void play() throws Exception {
	/** keep program running until user aborts (CTRL-C) */
	while (true) {
	    for (int i = 0; i < colors.length; i++) {
		ledColorSet(colors[i]);
		Thread.sleep(1000);
	    }
	}
    }

    public static void main(String[] args) {
	try {

	    RGBLed rgb = new RGBLed();

	    /** initialize the boad and pins */
	    rgb.init();

	    /** start the color change */
	    rgb.play();

	} catch (Exception e) {
	    e.printStackTrace();
	}
    }
}

Test

To compile the application run the following command:

$ sudo javac RGBLed.java -classpath .:classes:/opt/pi4j/lib/'*'

To run the application execute the following command:

$ sudo java -cp .:/opt/pi4j/lib/'*' RGBLed

Now you should see the RGB LED flashing colors, based on the list provided.