Flowing LED on RaspberryPi using Java

  • 23 March 2017
  • ADM

 

Flowing LED on RaspberryPi using Java - images/logos/raspberrypijava.jpg

 

In this article will make a flowing LED strip flowing in both directions using C. If you need more info on LEDs and how can be connected please follow Blinking LED on Raspberry Pi using Java tutorial. Also if you want to see the same flowing LED circuit in other programming languages, please check the references section.

Components

  • 1 * Raspberry Pi (I am using Raspberry Pi 3 Model B)
  • 5 * 220Ω Resistor
  • 5 * LED
  • 1 * Breadboard
  • 6 * 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. For this tutorial, I will connect the cathode to the Raspberry Pi GPIO, also it is possible to connect the anode to the Raspberry Pi GPIO, check Blinking LED on Raspberry Pi using Java tutorial for details.

Here is the schematic connecting all 5 LEDs using the cathode of the LED connected to the Raspberry Pi GPIO. Using the Pinout website for reference, we can check the numbering, starting from physical pin 11(BCM pin 17, Wiring Pi pin 0).

Flowing LED on RaspberryPi using Java - /images/FlowingLED01.png

And the physical visualization of the circuit.

Flowing LED on RaspberryPi using Java - /images/FlowingLED02.png

Hardware

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

Flowing LED on RaspberryPi using Java - /images/FlowingLED03.jpg

Software

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 FlowingLed.java

Copy/paste the following code.

import com.pi4j.io.gpio.GpioController;
import com.pi4j.io.gpio.GpioFactory;
import com.pi4j.io.gpio.GpioPinDigitalOutput;
import com.pi4j.io.gpio.RaspiPin;

public class FlowingLed {

    public static void main(String[] args) {
	try {
	    /** create gpio controller */
	    final GpioController gpio = GpioFactory.getInstance();

	    final GpioPinDigitalOutput[] pins = new GpioPinDigitalOutput[5];

	    /** Initialize the pins as output */
	    for (int i = 0; i < pins.length; i++) {
		pins[i] = gpio.provisionDigitalOutputPin(RaspiPin.getPinByAddress(i));
	    }

	    /** Reset the values */
	    for (int i = 0; i < pins.length; i++) {
		pins[i].high();
	    }

	    while (true) {
		/** Blink every second - one direction */
		for (int i = 0; i < pins.length; i++) {
		    pins[i].low();
		    Thread.sleep(500);
		    pins[i].high();
		}

		/** Blink every second - in reverse */
		for (int i = pins.length - 1; i >= 0; i--) {
		    pins[i].low();
		    Thread.sleep(500);
		    pins[i].high();
		}
	    }

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

Save and close vim.

Test

To compile the code run the following command:

$ sudo javac FlowingLed.java  -classpath .:classes:/opt/pi4j/lib/'*'
To run the application execute the following command:
$ sudo java -cp .:/opt/pi4j/lib/'*' FlowingLed

You should see the LEDs light flowing from one side to the other and back.

Flowing LED on RaspberryPi using Java - /images/FlowingLED04.jpg

Flowing LED on RaspberryPi using Java - /images/FlowingLED05.jpg

 

References