Communicate with I2C sensor

This is where you talk about the EV3 software itself, installation issues, and programming talk.

Moderators: roger, gloomyandy, skoehler

Communicate with I2C sensor

Postby s991116 » Wed Jun 04, 2014 7:05 am

Hi

I have installed LejOS on EV3 and have an I2C sensor (MPU6050 3 Axis Gyroscope + Accelerometer).
I can communicate with the sensor using and Arduino with I2C.

But I dont know how to create and I2C sensor in LejOS. It is not and "Lego I2C sensor".
I guess I should use the I2CSensor class, but how do I construct it ?

I cant find any guides on this.
s991116
New User
 
Posts: 7
Joined: Wed Jun 04, 2014 6:42 am

Re: Communicate with I2C sensor

Postby gloomyandy » Wed Jun 04, 2014 8:31 am

Hi,
have you looked at the source of any of the existing sensor classes? They should give you a good idea of what it is you need to do. Take a look at something like the following:
https://sourceforge.net/p/lejos/ev3/ci/ ... teIMU.java

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

Re: Communicate with I2C sensor

Postby s991116 » Wed Jun 04, 2014 9:07 pm

Thanks for the link. I have tested the followin code, but cannot detect any communication on the I2C-line when I run the program (and press Enter).

I2Sensor class.

Code: Select all
import lejos.hardware.port.Port;
import lejos.hardware.sensor.I2CSensor;

public class GyroSensor extends I2CSensor
{
   public static final int DEFAULT_I2C_ADDRESS = 0x68;
   public static final int MPU6050_RA_WHO_AM_I = 0x75;

   public GyroSensor(Port port, int address) {
      super(port, address);
   }

   public GyroSensor(Port port) {
      this(port, DEFAULT_I2C_ADDRESS);
   }
   
   public boolean TestConnection() {
        byte[] buf = new byte[1];
        this.getData(MPU6050_RA_WHO_AM_I, buf, 1);
        return (buf[0] == 0x68);
   }   
}


And the Main class.

Code: Select all
import java.rmi.RemoteException;
import lejos.hardware.Button;
import lejos.hardware.lcd.LCD;
import lejos.hardware.port.SensorPort;

public class Main {

   public static void main(String[] args) throws RemoteException {   
     LCD.drawString("Gyro. Test", 0, 1);
     GyroSensor gs = new GyroSensor(SensorPort.S1);
     
     LCD.drawString("ESC to Quit", 0, 3);
      while (Button.ESCAPE.isUp())
      {
         if(Button.ENTER.isDown())
         {
             if(gs.TestConnection())
             {
                LCD.drawString("Test passed.", 0, 2);
             }
             else
             {
                LCD.drawString("Test failed.", 0, 2);
             }           
         }
      }

      gs.close();
   }
}
s991116
New User
 
Posts: 7
Joined: Wed Jun 04, 2014 6:42 am

Re: Communicate with I2C sensor

Postby gloomyandy » Thu Jun 05, 2014 1:06 am

Are you sure you have the correct address? leJOS uses 8 bit i2c addresses some datasheets etc. specify a 7 bit address. So you may need to double the address value (i.e. if the datasheet says the address is 2 try 4). So in your case instead of using 0x68 try using 0xd0. Do you have the correct pull up resistors in place? Note that for use with the EV3/NXT the pull up resistors must have a value of around 68K, which is larger than normal.
User avatar
gloomyandy
leJOS Team Member
 
Posts: 4119
Joined: Fri Sep 28, 2007 2:06 pm
Location: UK

Re: Communicate with I2C sensor

Postby s991116 » Thu Jun 05, 2014 7:52 am

I did not use any pullup-resistor.
I only used a oscilloscope to check SCL and SDA. Nothing else was connected to the sensor-port.
I will try with pullup resistors.
s991116
New User
 
Posts: 7
Joined: Wed Jun 04, 2014 6:42 am

Re: Communicate with I2C sensor

Postby gloomyandy » Thu Jun 05, 2014 11:21 am

Have you tried changing the address first?
User avatar
gloomyandy
leJOS Team Member
 
Posts: 4119
Joined: Fri Sep 28, 2007 2:06 pm
Location: UK

Re: Communicate with I2C sensor

Postby s991116 » Thu Jun 05, 2014 1:27 pm

Why should I change the Address ?
And what should I change it to ?
My setup is as shown below.
Code: Select all
                       /------GND (RED Pin 3) -------> Oscilo. Gnd
                       |------+5 (GRENN Pin 4) ------> 47kOhm to SCL and 47kOhm to SDA
EV3 Sensor Port 1 -----|------SCL (YELLOW PIN 5) ----> Oscilo. Channel 1
                       \------SDA (BLUE PIN 6 ) -----> Oscilo. Channel 2


When I have this setup I can see some "messages" on the Oscilo. when the EV3 is turned on.
s991116
New User
 
Posts: 7
Joined: Wed Jun 04, 2014 6:42 am

Re: Communicate with I2C sensor

Postby s991116 » Thu Jun 05, 2014 2:15 pm

Found the error.
The trigger on my Oscil. was set wrong. :oops:

Thanks for the advices anyway.
s991116
New User
 
Posts: 7
Joined: Wed Jun 04, 2014 6:42 am

Re: Communicate with I2C sensor

Postby gloomyandy » Thu Jun 05, 2014 2:30 pm

As to the address did you actually read my previous post?

Are you sure you have the correct address? leJOS uses 8 bit i2c addresses some datasheets etc. specify a 7 bit address. So you may need to double the address value (i.e. if the datasheet says the address is 2 try 4). So in your case instead of using 0x68 try using 0xd0.
User avatar
gloomyandy
leJOS Team Member
 
Posts: 4119
Joined: Fri Sep 28, 2007 2:06 pm
Location: UK

Re: Communicate with I2C sensor

Postby s991116 » Fri Jun 06, 2014 6:21 am

Hi gloomyandy

My first problem was that I could not see any communication on the I2C wire (SCL and SDA).
Now I can see that EV3 is trying to send some data on the I2C wire. But I can't see any response.

I dont have any logic-analyser but it looks like the EV3 is trying 4 times to send the first message, but I dont think it gets any answer.

I used the I2CSensor.getdata to read 1 byte from the Gyresensor.
I have tried with both address 0x68 and 0x0D.

I am able to communicate with the GyroSensor from my arduino.
s991116
New User
 
Posts: 7
Joined: Wed Jun 04, 2014 6:42 am

Re: Communicate with I2C sensor

Postby gloomyandy » Fri Jun 06, 2014 2:06 pm

The address is 0xd0 not 0x0d... You may need to use larger pull up resistors as I said the recommended values are 68K. Getting i2c devices working with the EV3 and NXT is tricky, there are often a mixture of voltages (5V and 3.3V) and the NXT and EV3 have various resistors and other components hooked up the I/O pins to provide other functions and to provide protection. Having said that I'm pretty sure that people have managed to get the device you have working and there are lots of i2c sensors working with the EV3, you just need to keep working at it.

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

Re: Communicate with I2C sensor

Postby misa.zhu » Thu Jun 12, 2014 6:08 pm

I used a oscilloscope to check SCL and SDA as well, with nothing else connected. but it just can not open the port, exception "unable to open port".

My question is : can I open a I2C port with nothing connected? I set the i2c address with 0x8. thank you

Code: Select all

public static void main(String[] args) {
      System.out.println("I2C Test");
      I2CSensor arduino = null;
      
      try {
                  arduino = new I2CSensor(SensorPort.S4, 8);
      }
      catch(Exception e) {
         System.out.println("Error open port");
         arduino = null;
      }
        
      while (Button.ESCAPE.isUp()) { }
        
        if(arduino != null)
           arduino.close();
   }
misa.zhu
New User
 
Posts: 7
Joined: Wed Jun 11, 2014 5:16 pm

Re: Communicate with I2C sensor

Postby gloomyandy » Thu Jun 12, 2014 6:20 pm

No, with the current release you need to have a sensor that the EV3 recognizes as an i2c sensor before you can open it. To be honest I don't see the point in doing what you are doing with the scope. Why no simply attach your sensor and use the scope to see what actually happens. Also with that code you will not see any i2c activity as you are not aming any i2c requests. If you are playing around with i2c sensors please make sure that you have the correct pull up resistors attached to the bus (approx 68K), if you don't things are unlikely to work.
User avatar
gloomyandy
leJOS Team Member
 
Posts: 4119
Joined: Fri Sep 28, 2007 2:06 pm
Location: UK

Re: Communicate with I2C sensor

Postby misa.zhu » Thu Jun 12, 2014 7:34 pm

Got yo! Thanks!
misa.zhu
New User
 
Posts: 7
Joined: Wed Jun 11, 2014 5:16 pm

Re: Communicate with I2C sensor

Postby s991116 » Thu Jun 12, 2014 7:56 pm

Hi Gloomyandy

I have changed the pull-up resistor to 41K but I'm still not able to read the response on EV3 from the sensor.
When the Scope is connected, the I2C-Sensor is also connected, now.
I'm not sure how the EV3 (LejOS) recognize and I2C sensor. It seems like the two ground signals needs to be connected when LejOS open an I2CSensor port..

I have seen a site where the sensor (Gyro MPU6050) is connected to and NXT and communicates with the sensor (MPU6050) using NXC.

Maybe I should try using NXC with the EV3 instead.
s991116
New User
 
Posts: 7
Joined: Wed Jun 04, 2014 6:42 am

Next

Return to EV3 Software

Who is online

Users browsing this forum: Google [Bot] and 2 guests

cron
more stuff