Wire.endTransmission() return 3

Hi all!
i’ll try to make it short.

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

Thank you in advence!

Showing your actual code often helps more than verbally describing it - if you can do both, that’d be best :wink:

Hi ScruffR, thank you for the reply.

Here is the i2c scanner code:


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++;
		}

My code:

int error;
void setup()
{
  Wire.begin();
  Wire.beginTransmission(Addr);
  error = Wire.endTransmission();
  Particle.publish("EndTransValue", String(error)); //here error is 3.
  startNormalOperation();
}

void loop()
{
  readFIFO("command");
  readRegisters();
  delay(1);
}

Thanks again.

Note: the codes are not complete, i copied only the relevant parts.

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 :wink:
It wouldn't be the first time that code that was considered irrelevant actually wasn't.

1 Like

Hi again, i tried it with this code, same result.

#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.

What device OS are you targeting in each IDE respectively?
If your scanner is working via Web IDE just try the test code there as well.

BTW, the two includes should not be required.
If you want to have an include statement you may be better served with #include <Particle.h>

Hi ScruffR, thank you for the answer.

What device OS are you targeting in each IDE respectively?

Im targeting both to the same device OS (0.7.0).

If your scanner is working via Web IDE just try the test code there as well.

I have tried it, same results.

If you want to have an include statement you may be better served with #include <Particle.h>

Thanks for the advice, thats what i'll do. :slightly_smiling_face:

is there any other options i can try to solve it?

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)

const byte Addr = 0x64;
int error;

void setup()
{
  Wire.begin();
}

void loop()
{
  delay(1000);
  Wire.beginTransmission(Addr);
  error = Wire.endTransmission();

  Particle.publish("EndTransValue", String(error)); //here error is 3.
}

BTW, are you sure the address is 0x64 and not 64?
What sensor are you talking to?

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!

edit:

BTW, are you sure the address is 0x64 and not 64?

yes the address of the sensor is 0x64.

What sensor are you talking to?

Im using the ADPD188 smoke sensor.

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!

Thanks alot!

2 Likes