STM32 Nucleo and TSL2561 Luminosity Sensor example

This TSL2561 is an I2C light-to-digital converter TSL2561 that transforms light intensity to a digital signal. The TSL2561 features a selectable light spectrum range due to its dual light sensitive diodes: infrared and full spectrum. You can switch among three detection modes to take your readings. They are infrared mode, full spectrum and human visible mode.

When running under the human visible mode, this sensor will give you readings just close to your eye feelings.

Features

Selectable detection modes
High resolution 16-Bit digital output at 400 kHz I2C Fast-Mode
Wide dynamic range: 0.1 – 40,000 LUX
Wide operating temperature range: -40°C to 85°C
Programmable interrupt function with User-Defined Upper and lower threshold settings

Here is a typical module that makes it easier to work with the sensor

tsl2561

Layout and Connection

 

Code

We use this as a basis – https://os.mbed.com/users/anhnt2407/code/TSL2561_Light_sensor/

Update the tsl2561.h for STM32 nucleo

#define TSL2561_I2C_PINNAME_SDA PB_9
#define TSL2561_I2C_PINNAME_SCL PB_8

 

#include "mbed.h"
#include "TSL2561.h"
Serial PC(USBTX, USBRX);
#define PC_PRINTX(z,x) if(z==1) PC.printf(x);
#define PC_PRINTLNX(z,x) if(z==1) {PC.printf(x); PC.printf("\r\n");}
#define PC_PRINTXY(z,x, y) if(z==1) PC.printf(x, y);
#define PC_PRINTLNXY(z,x, y) if(z==1) {PC.printf(x, y); PC.printf("\r\n");}
DigitalOut myled(LED1);
TSL2561 tsl2561(TSL2561_ADDR_FLOAT);
Timer setuptimer;
Timer executetimer;
void setup(void){
if (tsl2561.begin()) {
PC_PRINTLNX(1,"TSL2561 Sensor Found");
} else {
PC_PRINTLNX(1,"TSL2561 Sensor not Found");
}
// You can change the gain on the fly, to adapt to brighter/dimmer tsl2561 situations
tsl2561.setGain(TSL2561_GAIN_0X); // set no gain (for bright situtations)
tsl2561.setTiming(TSL2561_INTEGRATIONTIME_402MS); // longest integration time (dim tsl2561)
// Now we're ready to get readings!
}
int main() {
PC_PRINTLNX(1,"----------START-------------");
setuptimer.start();
setup();
setuptimer.stop();
PC_PRINTLNXY(1,"Setup time: %f",setuptimer.read());
setuptimer.reset();
uint16_t x,y,z;
while(1) {
executetimer.start();
x = tsl2561.getLuminosity(TSL2561_VISIBLE);
y = tsl2561.getLuminosity(TSL2561_FULLSPECTRUM);
z = tsl2561.getLuminosity(TSL2561_INFRARED);
executetimer.stop();
PC_PRINTLNXY(1,"Visible: %d",x);
PC_PRINTLNXY(1,"Full Spectrum: %d",y);
PC_PRINTLNXY(1,"Infrared: %d",z);
PC_PRINTLNXY(1,"Execution Time: %f",executetimer.read());
executetimer.reset();
// wait(1);
PC_PRINTLNX(1,"----------COMPLETE-------------");
}
}

Testing

Using a terminal program like teraterm you should see something like this

tsl2561 output

tsl2561 output

 

Links

Free Shipping 1pcs GY-2561 TSL2561 Luminosity Sensor Breakout infrared Light Sensor module integrating sensor AL

Share

Comments are closed.