Getting to know the Raspberry Pi I2C bus

Programming in Java

To control the GPIO with Java, you need to use the corresponding library of the Pi4J project [6], but you should undertake some preparatory work first. Once again, you need the WiringPi library, this time for Java. Subsequently, you should install the current Java development kit, JDK, from Oracle [7].

The easiest approach to this installation is to transfer the JDK via SFTP onto the Raspberry Pi (Listing 5, line 1). (The best way to do this in Windows is with WinSCP [8].) Next, log on to the Rasp Pi via SSH (line 2) and decompress the tarball into /opt/java (lines 3-5).

Listing 5

Setting Up Java

$ scp jdk*.tar.gz pi@<RaspPi-IP>:~
$ ssh pi@<RaspPi-IP>
$ sudo -s
$ mkdir -p -v /opt/java
$ tar -xzf jdk*.tar.gz -C /opt/java/
$ update-alternatives --install "/usr/bin/java" "java" "/opt/java/jdk<Version>/bin/java" 1
$ update-alternatives --set java /opt/java/jdk<Version>/bin/java
$ java -version

After that, you should let the system know via update-alternatives which version of Java it should use. Finally, you should make Java display information about the version (line 8).

The Oracle JDK is now ready to go. To be on the safe side, you immediately should set the environment variable $JAVA_HOME, because some programs need this. If you want to specify the variable for the entire system, you have to enter it with root rights in the /etc/environment file.

During initial trials, it is fine simply to establish the variable in the user-specific ~/.bashrc (Listing 6) so that it becomes available immediately after the next login. If you want to continue working without logging out, an exception allows you to set the variable for the current session at hand.

Listing 6

Java Environment Variable

$ echo 'export JAVA_HOME="/opt/java/jdk<Version>/bin"' >> ~/.bashrc
$ export JAVA_HOME="/opt/java/jdk<Version>/bin"

Now all preconditions for the system have been fulfilled, and the pathway to the Pi4J library should be open. The quickest way to install is to use a setup script, which you can load from the Internet and execute in one step:

$ curl -s | sudo bash

Installation will take a bit of time, but the wait is worthwhile.

As in the first example, the Java program from Listing 7 makes the four LEDs light up one after the other. Likewise in Java, you should first create a connection to the I2C device and then write or read byte by byte. The example program available online [3] can be the basis for developing your own ideas.

Listing 7

Java – Light LEDs

01 import;
02 import;
03 import;
04 public class TestI2C {
05   private static final int i2cBus = 1;
06   private static final int address = 0x20;
07   public static void main(String[] args) {
08     try {
09       I2CBus bus=I2CFactory.getInstance(I2CBus.BUS_1);
10       I2CDevice dev = bus.getDevice(address);
11       dev.write((byte)0x10);
12       Thread.sleep(5000);
13       dev.write((byte)0x20);
14       Thread.sleep(5000);
15       dev.write((byte)0x40);
16       Thread.sleep(5000);
17       dev.write((byte)0x80);
18       Thread.sleep(5000);
19       dev.write((byte)0x00);
20     }
21     catch(Exception e) {
22       System.out.println(e);
23     }
24   }
25 }

To make your Java life a little easier, the developers have included a small script with their pi4j library that can be used to set the correct class path automatically when you compile.

This addition makes it somewhat easier to try out small Java programs. The pi4j --help command illustrates additional program functions.

To compile the example, enter:

$ pi4j -c

The -c switch builds the program. If Pi4J doesn't report an error, you should find a file named TestI2C.class in the updated directory. This is the Java class you will execute with Pi4J, this time using the -r switch:

$ pi4j -r TestI2C

The LEDs on the test circuit should turn on if this process has been successful.

Outlook for the Future

In this article, I laid out the basics for examining the extensive possibilities afforded by the I2C bus when it is controlled using terminal commands or the C or Java API.

In future articles, I will look more closely at various components of the I2C family. Table 4 provides an overview of the components to be examined, so stay tuned to learn more basics that you can use to create many new projects.

Table 4

I2C Bus Microcontroller








PWM component


Temperature sensor










8-channel ADC

Buy this article as PDF

Express-Checkout as PDF

Pages: 6

Price $2.95
(incl. VAT)

Buy Raspberry Pi Geek

Get it on Google Play

US / Canada

Get it on Google Play

UK / Australia

Related content