Content
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.
Git Repository
The example project for this article is de.raspirecipes.blinkingled available in the following Git repository:
1 |
git clone https://bitbucket.org/raspirecipes/raspirecipes.git |
The Hardware
Necessary Components
We need the following components:
- Raspberry Pi
- 1 x Breadboard
- 1 x red LED
- 1 x Resistor (220Ω to 1kΩ)
- Jumper wires
Circuit Layout
- Connect pin GPIO 17 with the anode pin of the LED
- Connect the kathode pin of the LED with a ballast resitor (100Ω to 1kΩ is fine)
- 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.
I called it org.raspirecipes.blinkingled.
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:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 |
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>org.raspirecipes</groupId> <artifactId>org.raspirecipes.blinkingled</artifactId> <version>0.0.1-SNAPSHOT</version> <packaging>jar</packaging> <url>http://maven.apache.org</url> <properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <raspberryip>192.168.178.53</raspberryip> <raspberryfolder>~</raspberryfolder> </properties> <build> <plugins> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-jar-plugin</artifactId> <configuration> <archive> <manifest> <mainClass>org.raspirecipes.blinkingled.BlinkingLed</mainClass> </manifest> </archive> </configuration> </plugin> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-compiler-plugin</artifactId> <version>3.1</version> <configuration> <source>1.8</source> <target>1.8</target> </configuration> </plugin> <plugin> <artifactId>maven-assembly-plugin</artifactId> <version>2.5.3</version> <configuration> <descriptorRefs> <descriptorRef>jar-with-dependencies</descriptorRef> </descriptorRefs> <archive> <manifest> <mainClass>org.raspirecipes.blinkingled.BlinkingLed</mainClass> </manifest> </archive> </configuration> <executions> <execution> <id>make-assembly</id> <!-- this is used for inheritance merges --> <phase>package</phase> <!-- bind to the packaging phase --> <goals> <goal>single</goal> </goals> </execution> </executions> </plugin> </plugins> </build> <dependencies> <dependency> <groupId>com.pi4j</groupId> <artifactId>pi4j-gpio-extension</artifactId> <version>1.0</version> </dependency> <dependency> <groupId>com.pi4j</groupId> <artifactId>pi4j-core</artifactId> <version>1.0</version> </dependency> </dependencies> </project> |
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):
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 |
<?xml version="1.0" encoding="UTF-8"?> <project name="deploy" default="remotedebug" basedir="."> <property name="raspberrypi" value="192.168.178.53" /> <property name="raspberryfolder" value="~" /> <property name="username" value="pi" /> <property name="password" value="pass123" /> <target name="remotedebug"> <first id="jars"> <fileset dir="target" includes="**/*jar-with-dependencies.jar" /> </first> <pathconvert pathsep="," property="jar.path" refid="jars" /> <basename file="${jar.path}" property="jar.filename" /> <echo>"Found application ${jar.path}"</echo> <echo>"Copying application to ${raspberrypi}:${raspberryfolder}/${jar.filename}"</echo> <scp localfile="${jar.path}" todir="${username}:${password}@${raspberrypi}:${raspberryfolder}" trust="true" /> <echo>"Starting ${raspberrypi}:${raspberryfolder}/${jar.filename} in debug mode"</echo> <sshexec host="${raspberrypi}" username="${username}" password="${password}" failonerror="true" usepty="true" command="sudo java -Xdebug -Xrunjdwp:server=y,transport=dt_socket,address=4000,suspend=y -jar ${jar.filename}" /> </target> </project> |
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.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 |
package org.raspirecipes.blinkingled; 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 BlinkingLed { public static void main( String[] args ) throws InterruptedException { GpioController gpio = GpioFactory.getInstance(); GpioPinDigitalOutput pin0 = gpio.provisionDigitalOutputPin(RaspiPin.GPIO_00); boolean ledOn = true; for(int i = 0; i < 10; i++) { pin0.setState(ledOn); ledOn = !ledOn; Thread.sleep(1000); } } } |
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.