Go Down

Topic: I2C serial read (Read 767 times) previous topic - next topic


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_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()
 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
   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()
 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()
 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)

     #if PRINT_EULER == 1
     #if PRINT_ANALOGS==1
     Serial.print(AN[0]);  //(int)read_adc(0)
     Serial.print (",");
     Serial.print (",");
     Serial.print (",");
     Serial.print (",");
     /*#if PRINT_DCM == 1
     Serial.print (",DCM:");
     Serial.print (",");
     Serial.print (",");
     Serial.print (",");
     Serial.print (",");
     Serial.print (",");
     Serial.print (",");
     Serial.print (",");
     Serial.print (",");

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);

void loop()
int sensorValue=analogRead(A5);
digitalWrite(pin4, HIGH);
digitalWrite(pin4, LOW);

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 https://github.com/pololu/L3G. The ZIP link on the page will download a zip file, containing a library. The library comes with an example that looks like:
Code: [Select]
#include <Wire.h>
#include <L3G.h>

L3G gyro;

void setup() {

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


void loop() {

  Serial.print("G ");
  Serial.print("X: ");
  Serial.print(" Y: ");
  Serial.print(" Z: ");


Now, that looks pretty simple to me.

Go Up