Introduction

This article describes how to implement the popular “Blinking LED” with Raspberry Pi using Java. This will also pave the ground for more complex programs as it will add the basic libraries necessary to access the Raspberry Pis GPIO hardware.

The Hardware

Necessary Components

We need the following components:

• Raspberry Pi
• 1 x red LED
• 1 x Resistor (220Ω to 1kΩ)
• Jumper wires

Circuit Layout

The circuite is simple:

1. Connect pin GPIO 17 with the anode pin of the LED
2. Connect the kathode pin of the LED with a ballast resitor (100Ω to 1kΩ is fine)
3. Connect the ballast resistor with the ground pin.

Pin GPIO 17 is an output pin that we can switch on and off via software. It will provide a voltage of 3,3V when switched on.

A simple red LED will have a voltage drop of about 2V and it can hande a current maximum of about 20mA. However, an LEDs will not provide noteworthy resitance in the circuite.

So in order to limit the current running through the LED and in order to not burn out the Raspberry Pis 3,3V rail, we have to apply a ballast resitor in row with the LED.

Since the LED will drop the votage by roughly 2V, the remaining 1,3V will be applied to the resistor. We can use the Ohm’s Law to calculate a suitable resitor to stay below 20mA:

$$R=\frac{U}{I}$$

So for a maximum current of 20mA, we have to use resistor of at least $I=\frac{1,3V}{20mA} = 65\Omega$. Hence a 100Ω resitor is sufficient, but anything up to 1kΩ should be fine without dimming the LED too much.

The Software

For starters, we have to create a Java Maven project in Eclipse. We can use the project created for Hello World with Eclipse and Java as a template.

pom.xml

Since we now want to access the Raspberry Pis GPIO hardware, we need two libraries from the Pi4J project:

• pi4j-core
• pi4j-gpio-extension

We are using Maven to manage our project; so we have to add these two libraries as dependencies in the pom.xml.

In addition, we have to configure Maven to assemble a so called “fat JAR”. A fat JAR will not only contain our own code, but also all libraries our project depends on. Since it is completely self-sufficient, it can be executed with a simple java -jar <file name>  command.

I used the Maven Assembly Plug-In to do this and added the related configuration to the pom.xml as well:

remotedebug.xml

The Maven Assembly Plug-In will produce a fat JAR file with suffix “jar-with-dependencies”.

In order for the remotedebug.xml ANT script to pick up the correct JAR file, we have to modify it slightly to filter for this suffix (see line 10):

Java Code

Finally, we need some Java code. The code below will let the LED blink 5 times.

Please note that pin GPIO 17 is referred to as RaspiPin.GPIO_00 . Pi4J uses the same numbering as WiringPi, the C library for accessing the Raspberry Pis I/O pins. Unfortunately, this does not match the GPIO numbering nor the physical number of the pin. For an interactive overview of the available pins and their numbers, take a look at the Raspberry Pinout website.

Congratulations!

We can now build the code via Maven-install and remote-execute it with the remotedebug.xml ANT script.

If everything works correctly, the LED connected to the Raspberry Pi will blink five times.

This site uses Akismet to reduce spam. Learn how your comment data is processed.