Im facing a really weird issue with an i2c sensor.
I have a sensor that communicate with i2c.
The problem is, when Wire.endTransmission is called, the value returned from the function is 3. but when i run i2c scanner the value returned from Wire.endTransmission is 0.
(i2c scanner is checking addresses from 0 to 127 and calling Wire.beginTransmission and Wire.endTransmission for each address, then returns the addresses that returned 0 when Wire.endTransmission called).
So, i tried to do the same as i2c scanner in my code, but got 3 again when Wire.endTransmission called.
(what i tried to do is Wire.beginTransmission(sensorAddress) and right after that calling to Wire.endTransmission() and checking the returned value).
From what read, the returned value meaning is, received NACK on transmit of data.
I really have no idea what can be the cause for this problem.
(im using other devices that comuunicating with i2c and there is no issues with them).
void setup()
{
Wire.begin();
Serial.begin(9600);
delay(10000);
Serial.println("\nI2C Scanner");
}
void loop()
{
byte error, address;
int nDevices;
Serial.println("Scanning...");
Particle.publish("status", "Scanning...");
nDevices = 0;
for(address = 1; address < 127; address++ )
{
// The i2c_scanner uses the return value of
// the Write.endTransmisstion to see if
// a device did acknowledge to the address.
Wire.beginTransmission(address); //this is what im trying to do.
error = Wire.endTransmission();
//Particle.publish("status", String(error));
if (error == 0) //while checking the sensor address, endTransmission returns 0 and publish the address
{
Serial.print("I2C device found at address 0x");
Particle.publish("status", "I2C device found at address 0x");
if (address<16){
Serial.print("0");
}
Serial.print(address,HEX);
Particle.publish("status", String(address) + " " + String(HEX));
Serial.println(" !");
nDevices++;
}
The first difference that pops to mind is timing.
You don’t allow the interface to “stabilise” between Wire.begin() and your first transaction. The scanner code does.
BTW, I don’t think this is your actual code since you are using Addr but that’s not decalred anywhere.
The Wire.beginTransmission() was not there the whole time, it was at a different part of the code.
i tried it with delay(10000) as the scanner does and still, there was no change.
Thanks for the reply.
edit: yes you are right, it is not the actual code, the code is too big to copy it so i copied only the relevant part.
In that case it's always advisable to create a minimal test application that exhibits the same issue in order to have a defined frame of reference
It wouldn't be the first time that code that was considered irrelevant actually wasn't.
#include <application.h>
#include <spark_wiring_i2c.h>
#define Addr 0x64
int error;
void setup()
{
Wire.begin();
delay(10000); //added this delay and still no change..
Wire.beginTransmission(Addr);
error = Wire.endTransmission();
Particle.publish("EndTransValue", String(error)); //here error is 3.
//startNormalOperation();
}
void loop()
{
//readFIFO("command");
//readRegisters();
delay(1);
}
Another thing i forgot to mention, im running the scanner at the web IDE, while my code is running at the desktop IDE, maby there is something related to that?
Thanks.
Targeting the same device is not the same thing as targeting the same device OS.
You can have 0.7.0 installed on the device but still build for 0.5.0 in Desktop IDE and 0.6.3 in Web IDE.
Please double check that you actually have selected the correct target version.
If so, I'm stumped because it shouldn't be.
However, what happens when you run this (build in Web IDE)
Ok, i double checked and i think there is something with the OS as ScruffR said.
Please correct me if im wrong, posibble versions of the OS can be 0.8.0-rc.11 and so on?
If so, my desktop IDE is runing this version ( 0.8.0-rc.11) and my web IDE is runing 0.7.0.
I will try to run the code at my desktop IDE with the same version (0.7.0) and will come back with an answer, thank you very much for the help!
Hi all.
So, after few testings we finally found the problem.
The problem was with the PCB board on wich the sensor was attached to.
I am trying to make it work now (at least now i can communnicate with it), i wiil surely make contact here if i will need any extra help!