Light up your Christmas lights with Java and Raspberry Pi — JVM Advent
By: Frank Delporte
Tags: Pi4j, Electronics, Raspberry Pi, Java, Programming

The application

TL;DR; run these commands to build and start the application directly on your Raspberry Pi:



$ cd JvmAdvent2020

$ mvn package

$ sudo java -jar target/jvm-advent-2020-1.0-SNAPSHOT-jar-with-dependencies.jar $ git clone https://github.com/FDelporte/JvmAdvent2020.git $ cd JvmAdvent2020$ mvn package$ sudo java -jar target/jvm-advent-2020-1.0-SNAPSHOT-jar-with-dependencies.jar

Maven dependency

This is a Maven project, and the Pi4J-library is added as a dependency in pom.xml:

<dependency>

<groupId>com.pi4j</groupId>

<artifactId>pi4j-core</artifactId>

<version>1.2</version>

</dependency>

PWM

The simplest use of a LED is on or off, but we are going to use them with a PWM (Pulse-Width Modulation) signal. This way we can also have them fading in and out from zero to full brightness. PWM switches fast between low and high. Depending on the duration for the low and/or high state, a “semi-analog output” can be achieved by creating an averaged value. The values to be used in this case are:

On-time: duration the output is high

Off-time: duration the output is low

Period: on-time + off-time

Duty cycle: percentage of the time the output is high

The Raspberry Pi has a few GPIOs that provide hardware-controlled PWM which you need to use for accurate signals. But in our case, we will be using software-controlled PWM as we want to use more LEDs.

To know which GPIO numbers need to be used, you can check the pin-layout drawing above and use the WiringPi-numbers.

Initialization of the LEDs

In our code, we use a list of GpioPinPwmOutput and add all the pins we are using. Pin 32 only supports hardware-PWM so needs to be initialized as such:

List<GpioPinPwmOutput> leds = new ArrayList<>();

leds.add(gpio.provisionSoftPwmOutputPin(

RaspiPin.GPIO_04, "LeftGreen")); // Pin 16

leds.add(gpio.provisionSoftPwmOutputPin(

RaspiPin.GPIO_05, "LeftBlue")); // Pin 18

leds.add(gpio.provisionSoftPwmOutputPin(

RaspiPin.GPIO_06, "LeftRed")); // Pin 22

leds.add(gpio.provisionSoftPwmOutputPin(

RaspiPin.GPIO_10, "Top")); // Pin 24 leds.add(gpio.provisionSoftPwmOutputPin(

RaspiPin.GPIO_11, "LedRightGreen")); // Pin 26

leds.add(gpio.provisionSoftPwmOutputPin(

RaspiPin.GPIO_31, "RightYellow")); // Pin 28

leds.add(gpio.provisionPwmOutputPin(

RaspiPin.GPIO_26, "RightRed")); // Pin 32

As we are using software-PWM we also need to do some configuration:

int PWM_MAX = 100;

Gpio.pwmSetMode(Gpio.PWM_MODE_MS);

Gpio.pwmSetRange(PWM_MAX);

Gpio.pwmSetClock(500);

All on or off

Turning all the LEDs on or off has become very easy with the forEach-function of a list:

private static void allOff() {

leds.forEach(l -> l.setPwm(0));

}

private static void allOn() {

leds.forEach(l -> l.setPwm(PWM_MAX));

}

Fading

By increasing or decreasing the PWM-value we can dim the LEDs, for example, fading one-by-one from 0 to the maximum value:

for (GpioPinPwmOutput led : leds) {

for (int fade = 0; fade <= PWM_MAX; fade += fadeSteps) {

led.setPwm(fade);

Thread.sleep(speed);

}

}

Building and running

The full code contains some more LED methods, so take a look at it to find out what is already there. Get the full Maven project from GitHub, package and run it directly on the Raspberry Pi with these commands:



$ cd JvmAdvent2020

$ mvn package

$ sudo java -jar target/jvm-advent-2020-1.0-SNAPSHOT-jar-with-dependencies.jar

...

Warming up...

LEDs initialized

All flash 5 times at speed 250

All off

All on

...

One by one on and then off 5 times at speed 100

All off

All random flash 20 times at speed 500

All off

All fade at speed 50 with steps of 2

Fading all up

Fading all down

All off

Fade one by one at speed 25 with steps of 2

Fading up GPIO 4

Fading up GPIO 5

...

Fading down GPIO 31

Fading down GPIO 26

All off

Done $ git clone https://github.com/FDelporte/JvmAdvent2020.git $ cd JvmAdvent2020$ mvn package$ sudo java -jar target/jvm-advent-2020-1.0-SNAPSHOT-jar-with-dependencies.jar...Warming up...LEDs initializedAll flash 5 times at speed 250All offAll on...One by one on and then off 5 times at speed 100All offAll random flash 20 times at speed 500All offAll fade at speed 50 with steps of 2Fading all upFading all downAll offFade one by one at speed 25 with steps of 2Fading up GPIO 4Fading up GPIO 5...Fading down GPIO 31Fading down GPIO 26All offDone

And there you have it, the log of the application, controlling the LEDs as you can see in the movie at the start of this article.

What’s next?

You have to agree, this is a perfect, fun, small project for the Christmas holiday, isn’t it?!

Experiment!

This is just a starting point demonstrating one single use of the GPIOs. We only used pins as output to control some LEDs, but you can also use a button as an input to select the LED effect. Or attach more LEDs, or use a chip to control more LEDs with fewer GPIOs … Really, the possibilities are endless, only limited by your imagination.

If you create something, please share it. Use the hashtag #JavaOnRaspberryPi on Twitter to show the world the magic of Java + Raspberry Pi.

Want to learn more?

This year I published my book “ Getting Started with Java on the Raspberry Pi” to inspire more Java developers to start experimenting with electronics components. It’s a great and fun way to learn new stuff when you combine your software knowledge with hardware! Available as an ebook on Leanpub and a paper book on Elektor. A perfect Christmas gift… 😉