micro:bit and TSL2561 Luminosity Sensor example

The TSL2560 and TSL2561 are second-generation ambient light sensor devices. Each contains two integrating analog-to-digital converters (ADC) that integrate currents from two photodiodes. Integration of both channels occurs simultaneously. Upon completion of the conversion cycle, the conversion result is transferred to the Channel 0 and Channel 1 data registers, respectively. The transfers are double-buffered to ensure that the integrity of the data is maintained. After the transfer, the device automatically begins the next integration cycle. Communication to the device is accomplished through a standard, two-wire SMBus or I2C serial bus.

Consequently, the TSL256x device can be easily connected to a microcontroller or embedded controller. No external circuitry is required for signal conditioning, thereby saving PCB real estate as well. Since the output of the TSL256x device is digital, the output is effectively immune to noise when compared to an analog signal.

The TSL256x devices also support an interrupt feature that simplifies and improves system efficiency by eliminating the need to poll a sensor for a light intensity value. The primary purpose of the interrupt function is to detect a meaningful change in light intensity. The concept of a meaningful change can be defined by the user both in terms of light intensity and time, or persistence, of that change in intensity. The TSL256x devices have the ability to define a threshold above and below the current light level. An interrupt is generated when the value of a conversion exceeds either of these limits.

Again a module is recommended, here is the one that I bought

Connections

 

Connect SCL to micro:bit P19
Connect SDA to micro:bit P20
Connect VDD to micro:bit 3.3V
Connect GROUND to micro:bit ground

Something like this

microbit and TSL2561
microbit and TSL2561

 

Code

You will need the Adafruit TSL2561 library, you can either download this from the following link or in a newer version of the IDE you can import it automatically – https://github.com/adafruit/TSL2561-Arduino-Library

[codesyntax lang=”cpp”]

#include <Wire.h>
#include <Adafruit_Sensor.h>
#include <Adafruit_TSL2561_U.h>

   
Adafruit_TSL2561_Unified tsl = Adafruit_TSL2561_Unified(TSL2561_ADDR_FLOAT, 12345);


void setup(void) 
{
  Serial.begin(9600);
  Serial.println("Light Sensor Test"); 
  Serial.println("");
  
  /* Initialise the sensor */
  if(!tsl.begin())
  {
    Serial.print("No TSL2561 detected");
    while(1);
  }
  
  tsl.enableAutoRange(true);
  tsl.setIntegrationTime(TSL2561_INTEGRATIONTIME_13MS);
  
  Serial.println("");
}


void loop(void) 
{  
  /* Get a new sensor event */ 
  sensors_event_t event;
  tsl.getEvent(&event);
 
  /* Display the results (light is measured in lux) */
  if (event.light)
  {
    Serial.print(event.light); 
    Serial.println(" lux");
  }
  else
  {
    Serial.println("Sensor overload");
  }
  delay(250);
}

[/codesyntax]

 

Output

Again the output can be seen via the serial monitor

3.00 lux
6.00 lux
8.00 lux
3.00 lux
3.00 lux
19.00 lux
13.00 lux
19.00 lux
11.00 lux
16.00 lux
12.00 lux
17.00 lux

 

Links

TSL2561 Luminosity Sensor Breakout infrared Light Sensor integrating sensor

Leave a Comment

This div height required for enabling the sticky sidebar
Ad Clicks : Ad Views : Ad Clicks : Ad Views :