Programming the Pi with Eclipse and Java

May 1, 2014 | 6 min Read

Raspberry Pi is a trademark of the Raspberry Pi Foundation

In this tutorial we will show how you can program a Raspberry Pi using Eclipse and Java. We will wire a simple circuit and control it using the General Purpose I/O pins on the Pi. Finally, we will connect the output of the circuit to Twitter, making this a basic IoT device. This is the first tutorial in a series on how to develop software for the Raspberry Pi using Eclipse technologies.


The Raspberry Pi is a credit card sized, single board computer. The Raspberry Pi has a Broadcom BCM2835 system on a chip (SoC), which includes an ARM1176JZF-S 700 MHz processor and a GPU. The Pi has several ports, including an HDMI video output, audio output, ethernet and two USB jacks. In addition to these ports, the Pi has a number of pins that can be configured for General Purpose I/O (GPIO) as well as serial communication, power and more.

Using the GPIO pins, the Pi can be used as a hardware controller; and with Java, you can use all the existing enterprise libraries and tools you’re familiar with.


For this tutorial you will need a Raspberry Pi with an up-to-date Operating System (may I suggest Raspbian), and Oracle Java 8 Installed. Make sure you use a JVM that uses the Hardware FPU (Floating Point Unit). We will also wire a simple circuit that detects an input and acts upon that. We will use a Photoconductive Cell as a switch and an LED as an output. When the cell absorbs light, the LED will be switched off. When no light is being absorbed, the LED will be turned on. You will need a photoconductive cell (or a simple switch) and a 10K resistor. You will also need an LED and another (270Ω resistor). You will also find that a breadboard and some small jumper wires will come in handy.

Finally, you will need Eclipse, EGit and m2e (plus Maven) to develop and build the software. To help get you started, I’ve created a Yoxos profile that you can get from our website.


In this tutorial we will use a very simple circuit. In fact, this entire circuit could be done without the controller, but it provides an easy to follow example (plus the controller will allow us to integrate with Twitter). The circuit will use a photoconductive cell as a switch. When the cell absorbs light, the circuit will be completed sending a ‘High’ value to a pin on the Raspberry Pi. When no light is being absorbed, a ‘Low’ value will be sent. We will write a program in Java that detects the value of this input, and controls an LED. When there is no light, the LED will be turned on, and when there is light, the LED will be turned off. The photoconductive cell is also attached to ground through a 10K pull-down resistor. This helps reduce noise in the system when there is no current. For a good explanation of why this is needed, see here.

GPIO 1 will be used as an input to the Raspberry Pi and GPIO 6 will be used as an output (to control the LED). The other wires are connected to the 3.3v output and ground. Here is picture of the wired circuit.


The controller for our circuit will be written in Java, developed in Eclipse and built using Maven. The Pi4J library will be used to communicate with the GPIO and Twitter4J will be used for integrating with Twitter. Using the Pi4J API, changes to GPIO state can be detected with interrupts and handled using Event handlers. This is a better approach than busy-waiting and polling!

1. Initializing the GPIO

To start developing with Pi4J, you first need to configure the GPIO.  Each Pin can be set as either an input or an output. We will configure Pin 6 as an output (and connect our LED to it), and Pin 1 as an input (with the photoconductive cell attached).

final GpioController gpio = GpioFactory.getInstance();
final GpioPinDigitalOutput led = gpio.provisionDigitalOutputPin(RaspiPin.GPIO_06);
final GpioPinDigitalInput sensor = gpio.provisionDigitalInputPin(RaspiPin.GPIO_01, PinPullResistance.PULL_DOWN);

2. Event Handling

Changes to the GPIO can be handled using a  GpioPinListenerDigital listener. Whenever the state of the Pin changes, an event will be fired. For inputs, the current state can be checked with event.getState(), and for outputs it can be set with led.low() and led.high().

sensor.addListener(new GpioPinListenerDigital() {
 public void handleGpioPinDigitalStateChangeEvent(final GpioPinDigitalStateChangeEvent event) {
  handleSensorInput(led, event);

 private void handleSensorInput(final GpioPinDigitalOutput led, final GpioPinDigitalStateChangeEvent event) {
  if (event.getState().isLow()) {
   notifyLightsOff(twitter, led);
  } else {
   notifyLightsOn(twitter, led);

 private void notifyLightsOn(final Twitter twitter, final GpioPinDigitalOutput led) {
  updateTwitterStatus(twitter, LightStatus.ON);

 private void notifyLightsOff(final Twitter twitter, final GpioPinDigitalOutput led) {
  updateTwitterStatus(twitter, LightStatus.OFF);

3. Waiting and Shutdown

Finally, we simply wait until the user exits the application. When the application is terminating, the GPIO should be shutdown.


4. Twitter Integration (Optional)

Twitter4J is a Java library for the Twitter API. It’s not required for this tutorial, but it does demonstrate how you can integrate other Java libraries into your Raspberry Pi applications. For this tutorial, we’ve created a Twitter App that posts updates to Twitter Stream ( In order to use this, you first need to register your Twitter application, and configure your account to allow your app to post to a stream. This is beyond the scope of this tutorial, but there is some good documentation on this on the Twitter4J page. Once an App has been configured, the Twitter API can be initialized as follows:

 TwitterFactory factory = new TwitterFactory();
 twitter = factory.getInstance();
 AccessToken accessToken = loadAccessToken();
 authenticateTwitter(accessToken, twitter);

A status message can be set which includes the state of the Light and the current time.

 twitter.updateStatus(getLightStateMessage() + " : " + new Date());

5. Deployment

The Application will be built and packaged using Maven. There are only two dependencies needed for this application (Twitter4J and Pi4J). To make things easy, we will use the maven-dependency-plugin to copy the dependencies into a lib/ folder along side the main application. See here for the complete pom.xml.

By placing the Raspberry Pi in my dark basement, I can now follow a Twitter stream to see if my basement lights have been left on.\_iot/status/461932519034605568


Making use of enterprise Java libraries and existing development tools such as Eclipse to program IoT devices with the Raspberry Pi is relatively easy. The Pi4J library allows you to integrate with the General Purpose I/O pins on the Pi and Java 8 is a stable VM with an excellent track record. The source for this tutorial is available on GitHub.

For more Tips and Tricks with Eclipse technology and the Raspberry Pi, follow me on Twitter.

Ian Bull

Ian Bull

Ian is an Eclipse committer and EclipseSource Distinguished Engineer with a passion for developer productivity.

He leads the J2V8 project and has served on several …