Arduino TSL2561 Ambient Light Sensor Interface

Last Updated on March 16, 2024

It is very important to choose correct and perfect Sensor when we design Luminosity based projects. Different light sensors are available in the market but each sensor has some light spectrum and intensity limitations so we need to pick correct sensor depends on our project requirements. In overall here is the Ambient Light Sensor TSL2561 from ams which detects most of the visible light spectrum and Infrared spectrum.




This sensor converts light intensity to digtal signal and provides output through I2C interface. Inside this sensor there are two light detecting photo diode channel is present one is for Visible and IR another one is for IR only,these are interconnected with two ADCs and they provide 16 bit output resolution. This sensor comes in tiny package (1.25 x 1.75 mm) and suitable for handheld electronics, wearables, IoT and where the light sensor need.

Arduino TSL2561 Ambient Light Sensor Interface is simple when we use breakout board, we have used TSL2561 breakout board from sparkfun and they provide arduino library also. Due to integrated peripherals in TSL2561 sensor no need for vast exteranl components and this breakout board provides I2C pins hence we can connect with Arduino board or microcontrollers having I2C communication.

Arduino TSL2561 Hookup

Connect 3V3 pin of sensor breakout to Arduino 3.3V power pin (Do not connect with 5V, that voltage might damage the sensor) and connect GND to GND of Arduino, then connect SCL to A5 and SDA to A4. Here we have used Arduino Uno board and refer I2C pin of your own arduino board before make the connections. You don’t need to connect INT (Interrupt) pin for basic reading operation from the sensor. Get the arduino TSL2561 Library and use it on sketch, You need include library from sparkfun and also wire library for sensor operation.

Arduino Sketch for TSL2561

#include <SparkFunTSL2561.h>
#include <Wire.h>

SFE_TSL2561 light;

// Global variables:

boolean gain;     // Gain setting, 0 = X1, 1 = X16;
unsigned int ms;  // Integration ("shutter") time in milliseconds

void setup()
{
  
  
  Serial.begin(9600);
  Serial.println("TSL2561 example sketch");

  
  light.begin();

 
  unsigned char ID;
  
  if (light.getID(ID))
  {
    Serial.print("Got factory ID: 0X");
    Serial.print(ID,HEX);
    Serial.println(", should be 0X5X");
  }
 
  else
  {
    byte error = light.getError();
    printError(error);
  }

  
    gain = 0;

 
  unsigned char time = 2;
    
  Serial.println("Set timing...");
  light.setTiming(gain,time,ms);

   
  Serial.println("Powerup...");
  light.setPowerUp();
    
}

void loop()
{
 
  delay(ms);
  
  unsigned int data0, data1;
  
  if (light.getData(data0,data1))
  {
       
    Serial.print("data0: ");
    Serial.print(data0);
    Serial.print(" data1: ");
    Serial.print(data1);
        
    double lux;    // Resulting lux value
    boolean good;  // True if neither sensor is saturated
    
    // Perform lux calculation:

    good = light.getLux(gain,ms,data0,data1,lux);
        	
    Serial.print(" lux: ");
    Serial.print(lux);
    if (good) Serial.println(" (good)"); else Serial.println(" (BAD)");
  }
  else
  {
   
    byte error = light.getError();
    printError(error);
  }
}

void printError(byte error)
  
{
  Serial.print("I2C error: ");
  Serial.print(error,DEC);
  Serial.print(", ");
  
  switch(error)
  {
    case 0:
      Serial.println("success");
      break;
    case 1:
      Serial.println("data too long for transmit buffer");
      break;
    case 2:
      Serial.println("received NACK on address (disconnected?)");
      break;
    case 3:
      Serial.println("received NACK on data");
      break;
    case 4:
      Serial.println("other error");
      break;
    default:
      Serial.println("unknown error");
  }
}


Going further

  1. Sparkfun breakout board 
  2. Arduino Library
  3. TSL2561 Sensor : http://ams.com/eng/Products/Light-Sensors/Ambient-Light-Sensors/TSL2561




Leave a Reply

Your email address will not be published. Required fields are marked *