Hi,
I am looking for how to read I2C output using Arduino Uno. I wrote one program to read the I2C output form the on e semiconductor ic. But its not working,can you help me in that.
Please check the attached document for the code and serial monitor output.
#include <Wire.h>
int ADDRESS = 0xDE;
#define data_X0 0x1F
int X0,X1,X_out;
void setup() {
// put your setup code here, to run once:
Wire.begin();
Serial.begin(9600);
delay (100);
Wire.beginTransmission(ADDRESS);
Wire.write(8);
Wire.write(4);
Wire.endTransmission();
}
void loop() {
// put your main code here, to run repeatedly:
Wire.beginTransmission(ADDRESS);
Wire.write(data_X0);
Wire.endTransmission();
Wire.requestFrom(ADDRESS,2);
if(Wire.available ()<=2)
{
X0 = Wire.read();
X1 = Wire.read();
}
Serial.print ("X0= ");
Serial.print(X0);
Serial.print(" X1= ");
Serial.print(X1);
}
I2C Read Using Arduino Uno.pdf (130 KB)
Your code is faulty on the Wire.read() functions. I tried your code with no I2C device connected, and got the same result.
Are you certain you are using the correct I2C address? Are you certain it is wired correctly? Have you tried the I2C scanner program to insure the device is showing up?
http://playground.arduino.cc/Main/I2cScanner
Biggest thing is, is the address correct? Because "the one semiconductor" is as precise as "that red car" if you want to know what engine is in it... You can check with the I2C scanner.
Also, you want to read when you have 2 or more bytes received, not less. So:
if(Wire.requestFrom(ADDRESS, 2) >= 2)
{
X0 = Wire.read();
X1 = Wire.read();
}
I checked with I2c scanner and arduino detecting the device and providing the correct address.
I changed the code as per your suggestion still i m not getting right output.
please check the below code for your reference
#include <Wire.h>
int ADDRESS = 0x60;
#define data_X0 0x1F
int X0,X1,X_out;
void setup() {
// put your setup code here, to run once:
Wire.begin();
Serial.begin(9600);
delay (100);
Wire.beginTransmission(ADDRESS);
//Wire.write(Power_Register);
// Bit D3 High for measuring enable (0000 1000)
Wire.write(8);
Wire.write(8);
Wire.endTransmission();
}
void loop() {
// put your main code here, to run repeatedly:
Wire.beginTransmission(ADDRESS);
Wire.write(data_X0);
Wire.endTransmission();
Wire.requestFrom(ADDRESS,2);
// if(Wire.available ()<=2)
if(Wire.requestFrom(ADDRESS, 2) >= 2)
{
X0 = Wire.read();
X1 = Wire.read();
}
Serial.print ("X0= ");
Serial.print(X0);
Serial.print(" X1= ");
Serial.print(X1);
}
Please provide a link to the I2C device you have connected. Without a datasheet, we would just be guessing.
Hi,
Please check the attached datasheet for you reference.
A1454-Datasheet(2).pdf (427 KB)
You write the register address to the device, then read 4 bytes. See figure 3 on the datasheet. You are reading only two bytes with your code.
Thanks for your help. By reading 4 bytes, i m able to read data from the sensor.
But the last byte is fluctuating from 0x01 to 0x03, actually it required to be 0x00.
I don't see that in the datasheet. Tha data should be in the last two bytes read. The 3rd byte should contain the upper 4 bits and the last byte should contain the lower 8 bits of the sensor output.
how to transfer this I2C data from the sensor to excel file
you have any document on this
I want to send the sensor I2C data to the excel. I m trying using Gobetwino shown in below code. But i m not able send data to excel.
#include <Wire.h>
#define ADDRESS 0x60
//#define ADDRESS1 0x61
#define data_X0 0x1F
int X0,X1,X2,X3,X_south,X_north,X_nofield,X_out;
char buffer[5];
void setup() {
// put your setup code here, to run once:
Wire.begin();
Serial.begin(9600);
delay (100);
}
void loop() {
// put your main code here, to run repeatedly:
Wire.beginTransmission(ADDRESS);
Wire.write(data_X0);
Wire.endTransmission();
if(Wire.requestFrom(ADDRESS, 4) >= 4)
{
X0 = Wire.read();
X1 = Wire.read();
X2 = Wire.read();
X3 = Wire.read();
}
X_out = word(X2,X3);
X_out = X_out & 0x0FFF;
if(X_out<2048)
{
X_south = X_out/2;
X_north = 0;
}
if(X_out>2048)
{
X_north = (X_out- 2048);
X_north = X_north/2;
X_north = (1024- X_north);
X_south = 0;
}
else
{
Serial.println(" ");
}
Serial.print ("\n X0= ");
Serial.print(X0);
Serial.print("\n X1= ");
Serial.print(X1);
Serial.print("\n X2= ");
Serial.print(X2);
Serial.print("\n X3= ");
Serial.print(X3);
Serial.print("\n X_out = ");
Serial.print(X_out);
Serial.print("\n X_south (Gauss) = ");
Serial.print(X_south);
Serial.print("\n X_north (Gauss) = ");
Serial.print(X_north);
//Serial.print("\n X_nofield = ");
//Serial.print(X_nofield);
//Serial.print("\n");
//Serial.print("\n");
Serial.print("\n");
// transfering data to excel sheet
Serial.print("#S|LOGTEST|[");
Serial.print(itoa((X_out), buffer, 10));
// Serial.print(X_out);
Serial.println("]#");
delay (500);
}
hi can you help me in that