10 years, 6 months ago.

TSL2561 Light Sensor

Dear mbed developers,

I am working on TSL2561 light sensor. Unfortunately there is no mbed code for this sensor yet. So i am trying to convert Arduino Example Code into Mbed code. However, it does not work as i expected. The thing is i can not check whether the mbed board communicates with the sensor successfully or not. Sensor data still comes out, but there is no change.

Please feel free to discuss and also convert the code and share together. My question is about how to convert 1-1 Arduino lib into Mbed lib? Particularly, here how to convert the I2C protocol as in the Arduino lib for TSL2561 into Mbed lib? Please take a look at the following links for Arduino library.

Thank you very much,

/media/uploads/anhnt2407/tsl2561-arduino-library-master.zip

https://github.com/adafruit/TSL2561-Arduino-Library/blob/master/TSL2561.cpp https://github.com/adafruit/TSL2561-Arduino-Library/blob/master/examples/tsl2561/tsl2561.pde https://github.com/adafruit/TSL2561-Arduino-Library/blob/master/TSL2561.h

2 Answers

10 years, 6 months ago.

As example their read16 function, with that you should at least be able to try to make the rest:

uint16_t TSL2561::read16(uint8_t reg)
{
  uint16_t x; uint16_t t;

  Wire.beginTransmission(_addr);
#if ARDUINO >= 100
  Wire.write(reg);
#else
  Wire.send(reg);
#endif
  Wire.endTransmission();

  Wire.requestFrom(_addr, 2);
#if ARDUINO >= 100
  t = Wire.read();
  x = Wire.read();
#else
  t = Wire.receive();
  x = Wire.receive();
#endif
  x <<= 8;
  x |= t;
  return x;
}

So like usual it needs to send the address and register it wants to read, then read two. Dunno if it needs a restart, guess not from that code. So in mbed I2C:

uint16_t TSL2561::read16(uint8_t reg)
{
  char receivedata[2];    //The two bytes we are going to receive

  i2c.write(_addr << 1, reg, 1);  //We send a single byte with value reg. Shift _addr 1 since arduino uses 7-bit address afaik, mbed 8-bit
  i2c.read(_addr<<1, receivedata, 2); //Receive the two bytes

  //Return them as one 16-bit:
  return receivedata[0]+receivedata[1] << 8;
}

See also: http://mbed.org/handbook/I2C

Accepted Answer

Thank you Erik for your answer. However, it seems not working here. I have changed it, but the data coming out does not change when i change the ambient light environment. I have published the lib and program, please take a look at the following link and help me in one more step to sort out this issue. Thank you so much,

Lib: http://mbed.org/users/anhnt2407/code/TSL2561/

Program: http://mbed.org/users/anhnt2407/code/TSL2561_Townsend/

posted by Tuan Anh Nguyen 10 Sep 2013

I guess your code tells you it found a sensor? It seems to me the code will pretty much always tell you it found the i2c device even if it didnt.

But your i2c statements are wrong. You need to send start, address byte including R/W bit set/cleared, register, (send stop), send start, address byte including R/W bit set/cleared, and then you can read data and afterwards send stop. But it is easier to just use the commands I showed above.

posted by Erik - 10 Sep 2013

Dear Erik

Thank you so much for your help. It works like a charm now. I am very happy

Thanks, AnhNT

posted by Tuan Anh Nguyen 24 Sep 2013

Nice, good to hear :)

posted by Erik - 24 Sep 2013
10 years, 6 months ago.