Can't make TSL2561 example code run

Using the imp001 and an april board. Running the code from and using a new TSL2561 brand new sensor from adafruit, which runs fine on an arduino.

It runs to the power check, returns 3 or 51 and then hangs indefinitely. When I look at the library from adafruit, there is no reference anywhere to the TSL2561_REGISTER_ADC0_LSB. Looking over the datasheet, but I’m a bit new to this. If there’s anything I should know can you please help out?

Wiring is solid. I’ve tried both i2c12 and i2c89

First up, TSL2561_REGISTER_ADC0_LSB is broadly equivalent of TSL2561_REGISTER_CHAN0_LOW in the Adafruit library.

The TSL2561 is a tricky one. To read from the Channel 0, you need to send an I2C sub-register address (the value of TSL2561_REGISTER_ADC0_LSB), which combines some command bits plus the address of the register you want (which is specified in Adafruit’s library by TSL2561_REGISTER_CHAN0_LOW). So

TSL2561_REGISTER_ADC0_LSB = command bits + TSL2561_REGISTER_ADC0_LSB

The table at the top of datasheet page 14 shows you this and what the command bits are. It’s clearer if you look at the pseudocode on page 19 of the datasheet.

You can see how the sub-register is used in the function used in the functions readSensorAdc0() and readSensorAdc1() in the article’s code.

Adafruit updated its TSL2561 breakout in June 2014 (I2C Explained was written before that) and the boards are different. My datasheet (downloaded from Adafruit when I researched the article) is dated 2005; Adafruit now links to a 2009 version, but as far as I can see nothing has substantially changed, but there may be subtle differences. Adafruit has updated its library to v2, I note.

FWIW, I’ve updated the article’s code to make the naming of constants more clear, and added comments during the set-up phases (right at the bottom of the device code) to make that process more clear too.

They say:
“v2.0 - Rewrote driver for Adafruit_Sensor and Auto-Gain support, and
added lux clipping check (returns 0 lux on sensor saturation)”

But I also noticed that the package is different, which means a different set of values for lux calculation.

I tried just calling read_sensor_adc_0(i2c_addr) and printing the lumo value and I am geting “the index ‘read’ does not exist”. I don’t understand what would cause this to happen. Maybe it means the timing is not correct? That does seem to be the main difference between the adafruit library and the squirrel code. I’m going to work away on that, but this is my first time doing any of this so I’m sure you could figure it out much more quickly. If you have any insights please do let me know.

The error message you’re get suggests the value of i2c in local word =, TSL2561_REGISTER_ADC0_LSB, 2); isn’t right – most likely you have the wrong I²C address. Note that


should be


ie. the bus object, not the I²C address.

One other thing to try. Insert.

server.log("I2C read error: " + i2c.readerror());

right after the above…) line, and see what value you get. Possible read errors are listed here.

I’ve just made some simplification tweaks to the example code in the I²C article. Please use this instead of what you have, and let me know how it goes.

I just tried uploading the new example code from the article

i2c.readerror() returns 0, but the code hangs after that.

What error are you getting from the log at the hang? I’ll see if I can dig out my TSL2561 today and see if I can replicate the error.

My TSL2561 is borked now. I’ve ordered another one and will see how that works out.

@FMurray, there is a small bug in the code, introduced when I updated it last week. This is now fixed, so if you re-acquire the example code from the I²C article, your sensor should now generate results in the log.

Apart from my error, the new version of the Adafruit TSL2561 board works with the code. I have just confirmed this with my new board.


Thank you so much for your assistance! It’s working for me as well now. What was the bug?