I2C serial read

Hello,

The task i'm try to accomplish is to get data from Arduino and pass it through serial connection to Matlab in order to proccess it.
However, i'm a begginer and, a simple code just to read the data from a sensor which only uses I2C, looks like something really hard to me.
I found a good library for AHRS (which is all about) for Arduino, but unfortunately is too complicated for me to understand it.
The code i want to write must be simple.
Just some commands to trigger the clock and to read the data so at the end i can get e.g. Serial.println(sensordata);
Can you please help me out here?

Thank you in advance.

With respect,

Minas

Can you please help me out here?

No. You have not described how you have the sensor connected to the Arduino. You haven't even defined what sensor it is. I'm pretty sure you did not go to mouser.com and order two "sensors, quantity 1 each, shipped in plain white box".

The code i want to write must be simple.

Simple for me is not necessarily going to be simple for you.

At first thank you for you replay,

Excuse me, the editing i did somehow didn't worked and you saw that incomplete post.

The sensor is MiniMu-9 a 3-axis accelerometer/magetometer/compass

It has SCL and SDA pins which i connect respectively to 5 and 4 analog pins of my arduino Duemilanove.

In order to define what means simple code for me, i can tell you that the best code i'v composed until now is just a void loop that reads a pontesiometer from an analog input or just light a led with a delay.

My task is to get the data that sensor gives me and serially pass it to Matlab. In the latter i will do the data fusion and filtering via Kalman filter in order to implement an AHRS.

Best regards,

Minas

It has SCL and SDA pins which i connect respectively to 5 and 4 analog pins of my arduino Duemilanove.

That's good. It also has GND and VIN which need to be connected.

In order to define what means simple code for me, i can tell you that the best code i'v composed until now is just a void loop that reads a pontesiometer from an analog input

Reading from that device really isn't any harder.

Sample Code

We have written a basic L3GD20 Arduino library and LSM303 Arduino library that make it easy to interface the MinIMU-9 with an Arduino. The libraries make it simple to configure the sensors and read their raw gyro, accelerometer, and magnetometer data.

Well, there, that made it even easier.

My task is to get the data that sensor gives me and serially pass it

That's exactly what the example in the provided library does.

It could hardly get any simpler.

Ok, firstly thank you for pointing out that im stupid :slight_smile:

You didn't help at all tho..

I'v tried that code that MiniMu-9 gives, is awesome and does a very good work. But i can't figure out how exactly works.
Thats why i asked from someone who knows to help me with the code.
I can understand that dealing with someone newbee isn't that exiting or not challenging at all but this could really solve me a problem that's delaying my thesis.

All i want is get the data from the sensor. Lets say i will name it sensordata and then do a Serial.Println(sensordada);
Can you please write me a code that implements that task?

Thank you alot and sorry for bothering you.

Minas

Can you please write me a code that implements that task?

There was a library on that page, with example code. Why is that not sufficient? What do you want that the library does not provide, or the example does not do?

Ok, Here is the simple code you refering too. Well a part of if because it exceeds the 9500 characters

#include <Wire.h>
#define GRAVITY 256
#define ToRad(x) ((x)*0.01745329252)
#define ToDeg(x) ((x)*57.2957795131)

// L3G4200D gyro: 2000 dps full scale
// 70 mdps/digit; 1 dps = 0.07
#define Gyro_Gain_X 0.07
#define Gyro_Gain_Y 0.07
#define Gyro_Gain_Z 0.07
#define Gyro_Scaled_X(x) ((x)*ToRad(Gyro_Gain_X))
#define Gyro_Scaled_Y(x) ((x)*ToRad(Gyro_Gain_Y))
#define Gyro_Scaled_Z(x) ((x)*ToRad(Gyro_Gain_Z))
#define M_X_MIN -421
#define M_Y_MIN -639
#define M_Z_MIN -238
#define M_X_MAX 424
#define M_Y_MAX 295
#define M_Z_MAX 472
#define Kp_ROLLPITCH 0.02
#define Ki_ROLLPITCH 0.00002
#define Kp_YAW 1.2
#define Ki_YAW 0.00002
#define OUTPUTMODE 1
#define PRINT_ANALOGS 0
#define PRINT_EULER 1
#define STATUS_LED 13
float G_Dt=0.02; // Integration time (DCM algorithm) We will run the integration loop at 50Hz if possible
long timer=0; //general purpuse timer
long timer_old;
long timer24=0; //Second timer used to print values
int AN[6]; //array that stores the gyro and accelerometer data
int AN_OFFSET[6]={0,0,0,0,0,0}; //Array that stores the Offset of the sensors

.................

AN[0] = gyro.g.x;
AN[1] = gyro.g.y;
AN[2] = gyro.g.z;
gyro_x = SENSOR_SIGN[0] * (AN[0] - AN_OFFSET[0]);
gyro_y = SENSOR_SIGN[1] * (AN[1] - AN_OFFSET[1]);
gyro_z = SENSOR_SIGN[2] * (AN[2] - AN_OFFSET[2]);
}

void Accel_Init()
{
compass.init();
if (compass.getDeviceType() == LSM303DLHC_DEVICE)
{
compass.writeAccReg(LSM303_CTRL_REG1_A, 0x47); // normal power mode, all axes enabled, 50 Hz
compass.writeAccReg(LSM303_CTRL_REG4_A, 0x08); // high resolution output mode
compass.writeAccReg(LSM303_CTRL_REG4_A, 0x20); // 8 g full scale: FS = 10 on DLHC
}
else
{
compass.writeAccReg(LSM303_CTRL_REG1_A, 0x27); // normal power mode, all axes enabled, 50 Hz
compass.writeAccReg(LSM303_CTRL_REG4_A, 0x30); // 8 g full scale: FS = 11 on DLH, DLM
}
}

// Reads x,y and z accelerometer registers
void Read_Accel()
{
compass.readAcc();

AN[3] = compass.a.x;
AN[4] = compass.a.y;
AN[5] = compass.a.z;
accel_x = SENSOR_SIGN[3] * (AN[3] - AN_OFFSET[3]);
accel_y = SENSOR_SIGN[4] * (AN[4] - AN_OFFSET[4]);
accel_z = SENSOR_SIGN[5] * (AN[5] - AN_OFFSET[5]);
}

void Compass_Init()
{
compass.writeMagReg(LSM303_MR_REG_M, 0x00); // continuous conversion mode
// 15 Hz default
}

void Read_Compass()
{
compass.readMag();

magnetom_x = SENSOR_SIGN[6] * compass.m.x;
magnetom_y = SENSOR_SIGN[7] * compass.m.y;
magnetom_z = SENSOR_SIGN[8] * compass.m.z;
}
void printdata(void)
{
Serial.print("!");

#if PRINT_EULER == 1
Serial.print("ANG:");
Serial.print(ToDeg(roll));
Serial.print(",");
Serial.print(ToDeg(pitch));
Serial.print(",");
Serial.print(ToDeg(yaw));
#endif
#if PRINT_ANALOGS==1
Serial.print(",AN:");
Serial.print(AN[0]); //(int)read_adc(0)
Serial.print(",");
Serial.print(AN[1]);
Serial.print(",");
Serial.print(AN[2]);
Serial.print(",");
Serial.print(AN[3]);
Serial.print (",");
Serial.print(AN[4]);
Serial.print (",");
Serial.print(AN[5]);
Serial.print(",");
Serial.print(c_magnetom_x);
Serial.print (",");
Serial.print(c_magnetom_y);
Serial.print (",");
Serial.print(c_magnetom_z);
#endif
/#if PRINT_DCM == 1
Serial.print (",DCM:");
Serial.print(convert_to_dec(DCM_Matrix[0][0]));
Serial.print (",");
Serial.print(convert_to_dec(DCM_Matrix[0][1]));
Serial.print (",");
Serial.print(convert_to_dec(DCM_Matrix[0][2]));
Serial.print (",");
Serial.print(convert_to_dec(DCM_Matrix[1][0]));
Serial.print (",");
Serial.print(convert_to_dec(DCM_Matrix[1][1]));
Serial.print (",");
Serial.print(convert_to_dec(DCM_Matrix[1][2]));
Serial.print (",");
Serial.print(convert_to_dec(DCM_Matrix[2][0]));
Serial.print (",");
Serial.print(convert_to_dec(DCM_Matrix[2][1]));
Serial.print (",");
Serial.print(convert_to_dec(DCM_Matrix[2][2]));
#endif
/
Serial.println();

}

long convert_to_dec(float x)
{
return x*10000000;
}

It may be simple to you but for me is greek...

So can you please help me with a simpler code? or that code is the simplest to implement a reading from I2C?

Has to be that hard? noone can help me?

Trying and searching and reading stuff about I2C i concluded to that all i need to do is just to send a tringging pulse in the clock and then just read the data.

so my code goes like this;

int pin4= A4;

void setup()
{
pinMode(pin4, OUTPUT);
Serial.begin(9600);
}

void loop()
{
int sensorValue=analogRead(A5);
digitalWrite(pin4, HIGH);
delay(1);
digitalWrite(pin4, LOW);
Serial.println(sensorValue);
delay(1);
}

Is that correct? will it work?

Ok, Here is the simple code you refering too.

That does not look anything like the code I looked at. The Arduino library link on the polulo site takes you to GitHub - pololu/l3g-arduino: Arduino library for Pololu L3G4200D and L3GD20 boards. The ZIP link on the page will download a zip file, containing a library. The library comes with an example that looks like:

#include <Wire.h>
#include <L3G.h>

L3G gyro;

void setup() {
  Serial.begin(9600);
  Wire.begin();

  if (!gyro.init())
  {
    Serial.println("Failed to autodetect gyro type!");
    while (1);
  }

  gyro.enableDefault();
}

void loop() {
  gyro.read();

  Serial.print("G ");
  Serial.print("X: ");
  Serial.print((int)gyro.g.x);
  Serial.print(" Y: ");
  Serial.print((int)gyro.g.y);
  Serial.print(" Z: ");
  Serial.println((int)gyro.g.z);

  delay(100);
}

Now, that looks pretty simple to me.