I2C Connection with the Arduino


3 posts
by Falcony » Fri Aug 02, 2013 4:26 pm
Hi guys,

I m trying to set up a connection from the arduino to the raspberry pi. i want to send measurement data to the pi.
i thought i d use i2c to do so. (or is there a better/easyer way?)

i found this tutorial: http://neophob.com/2013/04/i2c-communic ... a-arduino/

This is my java code:
Code: Select all
   
package com.neophob.com;

import com.pi4j.io.i2c.I2CBus;
import com.pi4j.io.i2c.I2CDevice;
import com.pi4j.io.i2c.I2CFactory;
 
public class I2CExample {
 
     public static void main(String[] args) throws Exception {

        // get I2C bus instance
        final I2CBus bus = I2CFactory.getInstance(I2CBus.BUS_1);
       
        I2CDevice arduino = bus.getDevice(0x04);
       
        byte[] buffer = new byte[2];
        arduino.write(buffer, 0, buffer.length);
}
}


Arduino code:
Code: Select all
#include <Wire.h>
 
void setup()
{
  Wire.begin(4);                // join i2c bus with address #4
  Wire.onReceive(receiveEvent); // register event
  Serial.begin(115200);         // start serial for output
  Serial.println("hello!");
}
 
void loop() {
  delay(100);
}
 
void receiveEvent(int howMany) {
  int a=0;
  while(Wire.available()>0)
  {
    Wire.read();
    a++;
  }
  Serial.print("read bytes: ");
  Serial.println(a);
}


so, the pi sends 2 bytes and the arduino receives them. this works.
But what i need is the arduino sending and the pi receiving.
I can't Work that out, i checked the javaDoc from (http://pi4j.com/apidocs/index.html) from pi4j and there is no receiveEvent or something under the i2c package. Just some read methodes, i tried to use them but that was not successful...

help please :)
Posts: 5
Joined: Fri Jul 12, 2013 11:43 am
by nomdediot » Wed Aug 28, 2013 11:25 am
Hello,

There is not this kind of mechanism because the Rapsberry Pi is the I2C master, so the Raspberry Pi performs the read operation (it drives the clock particularly). In other words, the Raspberry Pi initiates both reading and writing. The Arduino will not initiate any transfer itself.

Use read function from Pi4J, you should be able to get it working very quickly. Pi4J provides an example. Of course you must first modify the Arduino code to handle the request made by the Pi.

Try and post your Arduino and Raspberry Pi codes if you don't suceed to do it :-)

Joël

Edit: I have made quite the same thing, but with a Freescale microcontroller. I notice many issues during the reading operation, solved by decreasing the I2C speed at Raspberry Pi level (50kB/s instead of 100kB/s). It was due to a strange behavior of the clock line - not understood. If after implementing your application you notice that the data are sometimes corrupted, think to this.
Last edited by nomdediot on Wed Aug 28, 2013 11:31 am, edited 1 time in total.
Posts: 10
Joined: Sat Feb 02, 2013 3:04 pm
by mahjongg » Wed Aug 28, 2013 11:30 am
even though its possible to use I2C, most users prefer to use the serial (UART) port, with a resistor divider between the Arduino's 5V TxD output signal and the PI's RxD input signal. The divider should be 3.3/5 Most often a 2K2 and a 3K3 resistor is used for 3/5 division. The PI's TxD signal can drive the Arduino's RxD signal directly.

if you still prefer I2C then the trick is that the master needs to poll the slave to ask if it needs to "speak to him".
This means that a protocol needs to be set up so that several message types can be distinguished.
User avatar
Forum Moderator
Forum Moderator
Posts: 4931
Joined: Sun Mar 11, 2012 12:19 am