adafruit / Adafruit_TSL2591_Library

This is an Arduino library for the TSL2591 digital luminosity (light) sensors.
58 stars 48 forks source link

tsl2591.begin() detection issue. #19

Closed zhanko73 closed 6 years ago

zhanko73 commented 6 years ago

This part of the Adafruit's example code for tsl2591 test does not work correctly: if (tsl.begin()) { Serial.println("Found a TSL2591 sensor"); } else { Serial.println("No sensor found ... check your wiring?"); while (1); } REASON: tsl.begin() will hung up if no sensor detected. Expected behavior would be to display a message (no sensor found) the stop running. Instead, it looks like the begin() hungs up. It looks like I'm not alone with this problem (I have just seen on adafruit's forum.)

zhanko73 commented 6 years ago

I analysed more the begin() and found an issue in the read8(). I compared it with the bmp180's cpp and it is handled a bit different way: TSL2951: The "while (! Wire.available());" is waiting for the number of bytes to be read, however, in case of a disconnected sensor this will wait forever. uint8_t Adafruit_TSL2591::read8(uint8_t reg) { Wire.beginTransmission(TSL2591_ADDR); Wire.write(0x80 | 0x20 | reg); // command bit, normal mode Wire.endTransmission();

Wire.requestFrom(TSL2591_ADDR, 1); while (! Wire.available()); return Wire.read(); }

BMP085/BMP180: This does not wait for the number of bytes, only read. Also please note after the write they run an "end transmission" then they open it for a second time when reading. (In case of TSL2591 they do not re-open it again, however it looks working.) uint8_t Adafruit_BMP085::read8(uint8_t a) { uint8_t ret;

Wire.beginTransmission(BMP085_I2CADDR); // start transmission to device

if (ARDUINO >= 100)

Wire.write(a); // sends register address to read from

else

Wire.send(a); // sends register address to read from

endif

Wire.endTransmission(); // end transmission

Wire.beginTransmission(BMP085_I2CADDR); // start transmission to device Wire.requestFrom(BMP085_I2CADDR, 1);// send data n-bytes read

if (ARDUINO >= 100)

ret = Wire.read(); // receive DATA

else

ret = Wire.receive(); // receive DATA

endif

Wire.endTransmission(); // end transmission

return ret; }

ladyada commented 6 years ago

OK try now!