RGB LED on Raspberry Pi using Java

  • 31 March 2017
  • ADM

 

RGB LED on Raspberry Pi using Java - images/logos/raspberrypijava.jpg

 

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:

RGB LED on Raspberry Pi using Java - /images/rgbLed002.png

the symbol for common cathode RGB LED:

RGB LED on Raspberry Pi using Java - /images/rgbLed001.png

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

Components

You will need the following components:

  • 1 * Raspberry Pi (I am using Raspberry Pi 3 Model B)
  • 3 * 220Ω Resistor
  • 1 * RGB LED
  • 1 * Breadboard
  • 4 * Jumper wires
  • software: Pi4J

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.

RGB LED on Raspberry Pi using Java - /images/rgbLed003.png

And the physical visualization of the circuit.

RGB LED on Raspberry Pi using Java - /images/rgbLed004.png

Hardware

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

RGB LED on Raspberry Pi using Java - /images/rgbLed005.jpg

Software

Key functions

  • Gpio.wiringPiSetup()

    Initialize the wiringPi library, this is needed for PWM.

  • SoftPwm.softPwmCreate(int pin, int value, int range)

    This creates a software-controlled PWM pin. You can use any GPIO pin and the pin numbering will be that of the wiringPiSetup function you used. Use 100 for the pwmRange, then the value can be anything from 0 (off) to 100 (fully on) for the given pin.

  • SoftPwm.softPwmWrite(int pin, int value)

    This updates the PWM value on the given pin. The value is checked to be in-range and pins that haven't previously been initialized via softPwmCreate will be silently ignored.

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:

  • adds the Pi4J APT repository to the local APT repositories
  • downloads and installs the Pi4J GPG public key for signature validation
  • invokes the "apt-get update" command on the Pi4J APT repository to update the local package database
  • invokes the "apt-get install pi4j" command to perform the download and installation

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.

 

References