Trying out the TSL2561

Since the TSL2561 sensor is an advanced digital sensor, we will need to communicate with over I2C, since we can't read it as an analog sensor. Unlike other simpler light sensors, the TSL2561 chip measures both infrared and visible light to better approximate the response of our own eyes. As many of the components used in this book, the TSL2561 breakout board we will be using is from Adafruit, which also supplies a library for this sensor. This makes communicating with the sensor a bit easier. You can find the library on their GitHub: https://github.com/adafruit/Adafruit_TSL2561.

Download and install the library into you Arduino library folder. If you don't remember how to do this or if it is your first time installing a library, please take a look at Chapter 2, Working with Sensors where we explained the processes.

Once you have your library installed, its time to connect the TSL2561 sensor to you FLORA board. For testing the TSL2561 sensor, we will use alligator clip wires to connect the FLORA board. Remember there is not a whole lot of space in between the pins, so makes sure that the alligator clips do not connect to any other pins. Figure 3.2 shows the necessary connections that need to be made to the FLORA Board:

Trying out the TSL2561

Figure 3.2: Showing the connections from the TSL2561 to the FLORA board

The 3.3 V pin on the TSL2561 board connects to the 3.3 V pin on the FLORA board. The SDA pin on the TSL2561 board connects to the SDA on the FLORA, and the SCL pin connects to the SCL pin. Don't forget to connect GND on both boards. As always, in order to be complete a circuit, ground needs to be shared in-between components.

Once everything is connected, we can upload the following code to the FLORA board to check if we can receive any data:

//Include the libraries needed
#include <Wire.h>
/*If you a missing the Adafruit sensor library have a look at chapter 2 for a link to the library and install instructions*/
#include <Adafruit_Sensor.h>
#include <Adafruit_TSL2561_U.h>

//Give the sensor a ID which in this case is 12345
Adafruit_TSL2561_Unified tsl = Adafruit_TSL2561_Unified(TSL2561_ADDR_FLOAT, 12345);

//Function to configure the sensor
void configureSensor()
{
  /* This set the gain to auto, you can also manual set the gain to no gain or 16x the gain.*/
tsl.enableAutoRange(true);
  // To manually set the gain use the following commans
  // tsl.setGain(TSL2561_GAIN_1X);
  // tsl.setGain(TSL2561_GAIN_16X);

  /* modifying the integration time gives you better a resolution on you readings (402ms = 16-bit data) */

/*Faster reading but lower resolution
tsl.setIntegrationTime(TSL2561_INTEGRATIONTIME_13MS);
Medium speed and medium resolution tsl.setIntegrationTime(TSL2561_INTEGRATIONTIME_101MS);
High resolution but slow speed which we will be using for this example*/
  tsl.setIntegrationTime(TSL2561_INTEGRATIONTIME_402MS);

}

void setup()
{
//Start serial communication
  Serial.begin(9600);
//Print test message
  Serial.println("Light Sensor Test");
  
  /* Initialize the sensor */
  if(!tsl.begin())
  {
    /* There was a problem detecting the sensor check your wires */
    Serial.print("Cant detect the TSL2561 detected Check your wiring");
    while(1);
  }
    
  /* Setup the sensor gain and integration time */
  configureSensor();
  
  
}

void loop()
{  
  /* Get a new sensor event */
  sensors_event_t event;
  tsl.getEvent(&event);
 
  /* If there is light display the results */
  if (event.light)
  {
    Serial.print(event.light); Serial.println(" lux");
  }
  else
  {
    /* If there is no light  */
    Serial.println("Cant see anything..Show me some light");
  }
  delay(250);
}

Note that you can set the gain of this sensor. Gain is the measurement of the ability to increase the power or amplitude of a signal. What this means is that you can set the sensitivity of a sensor by increasing or decreasing the gain. The TSL2561 can also set the gain automatically using the command:

tsl.enableAutoRange(true);

In order to get a sense of how the sensor reacts under different light conditions, I recommend you do some testing with the sensor outdoors.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.191.236.174