I2C Arduino and Raspberry pi using pi4j

Hello again,

first I have to say, I wanted to connect my Arduino UNO to my Raspberry Pi by using SPI and not I2C. But it didn’t work. SPI has the advantage of full-duplex communication which the I2C doesn’t offer. I tried these solutions:

but at the end  I didn’t manage to get everything to work. I got the Arduino receive some data from the raspberry pi, but to be honest “some” must be replaced by “anything not related to what the pi sent”… :/ So I rethought my requirements regarding the communication between the two devices. I recognized that I don’t need full-duplex communication for my project. In every state of the processing in my project only one device should send data at the same time. So I switched to I2C.

I2C

I2C works as a master-slave bus. This means one device is master and data is only send from and to master. If a slave has data to be sent to the master, it has to wait until the master requests data from the device. This is really straightforward, I think more robust and is specified in more detail than SPI.

Raspberry Pi and Arduino

So how to configure the two devices to talk to each other using I2C? Just follow these instructions, they are working very well: http://blog.retep.org/2014/02/15/connecting-an-arduino-to-a-raspberry-pi-using-i2c/

If you use a Raspberry Pi B+, take a look at the pin layout described here, for finding the right pins: http://pi4j.com/pins/model-b-plus.html

If everything works correct, you should be able to send a byte to the Arduino and the Arduino sends data back to the raspberry pi.

pi4j

So how to do this in Java? The Arduino of course remains unchanged. Regarding the pi, take a look at how to set up the pi for java and how to create an executable jar-file including pi4j.

As usual, the result is simple (but it took some time to figure this out). These are the lines of code you need for sending a byte to Arduino and printing out the answer using pi4j:

try {
  System.out.println("Creating I2C bus");
  I2CBus bus = I2CFactory.getInstance(I2CBus.BUS_1);
  System.out.println("Creating I2C device");
  I2CDevice device = bus.getDevice(0x04);

  byte[] writeData = new byte[1];
  long waitTimeSent = 5000;
  long waitTimeRead = 5000;

  while (true) {
   //negative values don't work
   new Random().nextBytes(writeData);
   System.out.println("Writing " + writeData[0] + " via I2C");
   device.write(writeData[0]);
   System.out.println("Waiting 5 seconds");
   Thread.sleep(waitTimeSent);
   System.out.println("Reading data via I2C");
   int dataRead = device.read();
   System.out.println("Read " + dataRead + " via I2C");
   System.out.println("Waiting 5 seconds");
   Thread.sleep(waitTimeRead);
  }
 } catch (IOException ex) {
  ex.printStackTrace();
 } catch (InterruptedException ex) {
  ex.printStackTrace();
 }

As you can see this is straightforward. First, you have to create an instance of the I2C bus (the B+ has two of them) and then create the I2C device. The bus is 0 or 1 in case of the B+. The I2C device creation needs a address. This is the address from the Arduino C code, which is 0x04 in our case.

After this basic setup, just use the read and write methods of the I2C device to communicate with the Arduino. Mind that you have to give the Arduino time to receive the data and send it back. Of course 5s are too much, but It wasn’t important for me to speed things up in this case. If you want to transfer data more quickly reduce the waiting times. I think the values should around 20ms or something like that.

You can get the code here:

https://github.com/Kaputnik120/AllProjects/blob/I2CPi4j/Pathfinder/src/main/java/de/buschbaum/java/pathfinder/Main.java

So this was just a short blog entry about how to use pi4j to do I2C communication. If you’ve questions, remarks or whatever, just leave a comment.

Regards

Uli

 

Advertisements

One response to “I2C Arduino and Raspberry pi using pi4j

  1. Pingback: Using the MPU6050 with pi4j | Dev Stuff·

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s