How can I connect Arduino and Ev3 with I2C

This is where you talk about the EV3 hardware related topics such as the brick, sensors, LEGO pieces, etc.

Moderators: roger, gloomyandy, skoehler

SaYa
New User
Posts: 5
Joined: Thu May 11, 2017 4:32 pm

How can I connect Arduino and Ev3 with I2C

Postby SaYa » Thu May 11, 2017 5:11 pm

I want to connect Arudino Uno and Ev3 with I2C.
I have Breadboard Adapter for LEGO MINDSTORMS(https://www.amazon.com/gp/product/B00A0P5FY6?ie=UTF8&camp=213733&creative=393177&creativeASIN=B00A0P5FY6&linkCode=shr&tag=dexteindus-20&qid=1399902974&sr=8-1&keywords=lego+breadboard+adapter).

I use this site's(https://lejosnews.wordpress.com/2014/05/17/connecting-to-an-arduino-with-i2c/) code.
Site use different connector, but i think that is not problem.

The lejos code is like this:

Code: Select all

public class main {
    static int I2CSlaveAddress = 8;
    static byte[] buffReadResponse = new byte[8];

    public static void main(String[] args) {
        System.out.println("Arduino Connection Test");
        I2CSensor arduino = new I2CSensor(SensorPort.S2, I2CSlaveAddress);

        while (Button.ESCAPE.isUp()) {
            int id = Button.waitForAnyPress();
            if (id == Button.ID_ENTER) {
                arduino.getData(0x41,);
                arduino.getData('A', buffReadResponse, buffReadResponse.length);
                arduino.setRetryCount(10);
                System.out.println(new String(buffReadResponse));
            }
        }
        arduino.close();
    }
}


and the arduino code is

Code: Select all

#include <Wire.h>
 
#define SLAVE_ADDRESS 0x04
void setup()
{
    Wire.begin(SLAVE_ADDRESS);
    Wire.onReceive(receiveData);
    Wire.onRequest(sendData);
}
 
byte request;
byte reply[] = "Testing";
 
void loop(){
}
 
void receiveData(int byteCount){
  request = Wire.read();
}
 
void sendData(){
  if (request == 'A') Wire.write(reply,sizeof reply);
}



The EV3 recognize the arduino, So the opening port is success.
but the getData in lejos, the error got happend like this:

Code: Select all

lejos.hardware.port.I2CException: I2C I/O error
   at lejos.internal.ev3.EV3I2CPort.i2cTransaction(EV3I2CPort.java:129)
   at lejos.hardware.sensor.I2CSensor.getData(I2CSensor.java:133)
   at lejos.hardware.sensor.I2CSensor.getData(I2CSensor.java:115)
   at main.main(main.java:101)
   at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
   at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
   at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
   at java.lang.reflect.Method.invoke(Method.java:606)
   at lejos.internal.ev3.EV3Wrapper.invokeClass(EV3Wrapper.java:62)
   at lejos.internal.ev3.EV3Wrapper.main(EV3Wrapper.java:46)


I have to use lejos, but the information are about other way(RoboC or Block).
But I don't know what is worng....
Please help me.........

User avatar
gloomyandy
leJOS Team Member
Posts: 5875
Joined: Fri Sep 28, 2007 2:06 pm
Location: UK

Re: How can I connect Arduino and Ev3 with I2C

Postby gloomyandy » Thu May 11, 2017 9:23 pm

I have moved your post to a different section, the Development section is intended for discussion of developing leJOS not for questions about how to use it.

So a few points.
* What makes you think that the EV3 is recognizing the Audino? Opening the port does not mean that the device has been recognized when using i2c.
* When asking questions like this please be sure to provide details of the version of leJOS that you are using.
* Have you installed the two pull up resistors required for i2c operation? If so what pins have you added them to and what value resistors have you used.
* You have not provided details of which pins you have connected together between the two devices.
leJOS news https://lejosnews.wordpress.com/

SaYa
New User
Posts: 5
Joined: Thu May 11, 2017 4:32 pm

Re: How can I connect Arduino and Ev3 with I2C

Postby SaYa » Fri May 12, 2017 10:52 am

Oh, I use lejos 0.8.1-beta.
At first, the port error happend. so I search about that, and I read something that the error happend because ev3 can't recognize the arduino.
So I thought that it was because I recognized Arduino that no port error occurred. but it was wrong. :(
And I don't know about two pull up registor.
The example of using Breadboard Adapter that I read is not using registor when connect arduino and ev3.

The example is this:
https://www.dexterindustries.com/howto/connect-the-arduino-and-the-lego-mindstorms-together/

Is there anything I missed?

User avatar
gloomyandy
leJOS Team Member
Posts: 5875
Joined: Fri Sep 28, 2007 2:06 pm
Location: UK

Re: How can I connect Arduino and Ev3 with I2C

Postby gloomyandy » Fri May 12, 2017 2:38 pm

Firstly why on earth are you using such an old version of leJOS? It is so old I can't even remember how the i2c worked in that version. So if you want any help from me then please use the most recent version of leJOS (which is 0.9.1).

Secondly you must use pull up resistors, it may be that the adapter you have already has these built in, the article says it does but I've never used it and don't have access to one so I'm sorry you are on your own as far as that is concerned, but without the pull ups i2c will not work.

Thirdly you seem to have added some extra code to that provided by Lawrie, this is not a good idea. So for instance why do you the line:

Code: Select all

                arduino.getData(0x41,);


Does that even compile? It shouldn't as it does not seem syntactically correct. You should almost certainly remove that line of code.

Fourthly I2c addresses can be confusing, you should probably modify your EV3 code and try using address 2, 4, 8 to see if any of them work (leave the arduino address set to 4).

Finally are you sure you have the correct connections between the two systems? You must have 0V/Ground, SCL and SDA. Note that on the EV3 pins 2 nd 3 are not both connected to ground, only pin 3 (red) is a ground connection on the EV3. Also are you sure you are using the correct pins on the Arduino, my understanding is that different versions of the Arduino will use different pins for i2c operations.
leJOS news https://lejosnews.wordpress.com/

SaYa
New User
Posts: 5
Joined: Thu May 11, 2017 4:32 pm

Re: How can I connect Arduino and Ev3 with I2C

Postby SaYa » Sat May 13, 2017 8:31 am

Thank you for your reply.
I upgrade my lejos version to 0.9.1 after reading your reply.

And after my more searching, I realized that the adapter has it's own registor switch.
So I turned high that switch, communication became possible.
Thank you very much. :D

User avatar
gloomyandy
leJOS Team Member
Posts: 5875
Joined: Fri Sep 28, 2007 2:06 pm
Location: UK

Re: How can I connect Arduino and Ev3 with I2C

Postby gloomyandy » Sat May 13, 2017 9:46 am

Thanks for the update. I wasn't aware that the pull up resistors are switchable on the adapter, so that is handy to know!

Feel free to post the details of your project in the projects section of the forum when you have things working. Always interesting to see what people are building with leJOS.
leJOS news https://lejosnews.wordpress.com/


Return to “EV3 Hardware”

Who is online

Users browsing this forum: No registered users and 0 guests