Wire.available() only returning zero?

Hello,

I am trying to get a temperature sensor working with my Nano, but the code cannot bypass a simple check that Wire.available() is at least 2 before proceeding. (Right now, the program is only supposed to grab the first byte of the electronic ID, just as a test case, before I get onto more ambitious programming.) Does anyone have any idea why this might be happening?

Thanks!

Here is the pdf for the temperature sensor...

https://www.mouser.com/ds/2/368/Si7050-1-3-4-5-A20-907443.pdf

And here is the code:

#include <Wire.h>

int sens_address = 0x40; //the temperature sensor address

void setup() {
Wire.begin();
Serial.begin(9600);
delay(1000);
}

void loop() {
Wire.beginTransmission(sens_address);
byte vals [2];
//read electronic id 1st byte
vals[0] = 0xFA;
vals[1] = 0x0F;
Wire.write(vals, 2); //when Wire.writing an array, you must specify the number of bytes in the array to write (2 in this case)
/*
Wire.write(0xFA); //read electronic id 1st byte
Wire.write(0x0F); //read electronic id 1st byte
*/
Wire.endTransmission();
Wire.requestFrom(sens_address, 1);
while (Wire.available()<=1); //THIS LINE IS THE PROBLEM - it never passes.
int b = Wire.read()<<8;
b |= Wire.read();

}

Wire.requestFrom(sens_address, 1);

You ask for 1 byte. Why are you expecting at least 2?

Pete

Before you post code again, read How to post code properly and then use the </> icon to create

[code]...[/code]

tags around your code so that it is easier to read.

Pete

el_supremo:

Wire.requestFrom(sens_address, 1);

You ask for 1 byte. Why are you expecting at least 2?

Pete

Right, thanks. Fixed that - it was a remnant of an earlier version of this code, sorry.

el_supremo:
Before you post code again, read How to post code properly and then use the </> icon to create

[code]...[/code]

tags around your code so that it is easier to read.

Pete

Yes, thanks for pointing me to the proper etiquette/formatting. I am sorry for the earlier breach :slight_smile: Here is the new version of the code, after fixing the 2-byte mistake; it still has the same problem, even though I've fixed the Wire.available() evaluation such that it checks for a single byte rather than two. That line is never passed.

#include <Wire.h>
int sens_address = 0x40; //the temperature sensor's address

void setup() {
  Wire.begin();
  Serial.begin(9600);
  delay(1000); //to give serial time to prepare itself
}

void loop() {
  Wire.beginTransmission(sens_address);
  byte vals [2];
  //ask to read electronic id 1st byte
  vals[0] = 0xFA;
  vals[1] = 0x0F;
  Wire.write(vals, 2); //when Wire.writing an array, you must specify the number of bytes in the array to write (2 in this case)
  Wire.endTransmission();
  Wire.requestFrom(sens_address, 1);
  Serial.println("Printing just before the Wire.available line");
  while (!Wire.available());
  Serial.println("Printing just after the Wire.available line");
  byte b = Wire.read();

  //delay for readability, then do it again
  delay(500);
}

So, any ideas? I am stumped... thank you so much for your help!

Do you have 10k pullup resistors on SDA and SCL as shown on page 9 of the datasheet?

The diagram on page 15 indicates that the device sends 4 bytes of the ID in response to the first request. BUT it also implies that each of the four bytes is followed by a CRC byte suggesting that there are 8 bytes in all. Then the request for the second 4 bytes of the ID also results in a total of 8 bytes being sent.

Maybe try sending the first request for the ID and request 8 bytes. Print out whatever you get.

Pete