Skip to content

Ambient Light Sensor TinyShield Tutorial

The Ambient Light Sensor TinyShield allows you to measure light intensity in your next TinyDuino project. But not only is this sensor able to measure light, it can also be programmed for proximity detection with the help of an LED. Check out page 5 of the TSL2572 datasheet if you're interested in this application!

If you have a smartphone, you probably carry an ambient light sensor in your pocket every day. Current iPhones use this type of sensor to control screen brightness, and for proximity detection to turn off the screen during phone calls when the device is near your face.

To learn more about the TinyDuino Platform, click here


Description

Measure light with your TinyDuino - this TinyShield features a TAOS TSL2572 Ambient Light Sensor, that approximates human eye response to light intensity under a variety of lighting conditions and through a variety of attenuation materials. Accurate ALS measurements are the result of TAOS’ patented dual-diode technology and the UV rejection filter incorporated in the package. In addition, the operating range is extended to 60,000 lux in sunlight when the low-gain mode is used.

The Ambient Light Sensor TinyShield incorporates level shifters and a local power supply to ensure proper and safe operation over the entire TinyDuino operating voltage range between 3 – 5V.

To see what other TinyShields are compatible with this TinySheild, see the TinyShield Compatibility Matrix

Technical Details TAOS TSL2572 Ambient Light Sensor Specs
  • Approximates Human Eye Response
  • 45,000,000:1 Dynamic Range
  • Operation to 60,000 lux in Sunlight
  • Package UV Rejection Filter
  • Wide Magnetic Field Range (+/-8 Oe)
  • Low Power – Active: 200uA, Wait: 90uA, Sleep: 2.2u
TinyDuino Power Requirements
  • Voltage: 3.0V - 5.5V
  • Current: 200uA (Active). Due to the low current, this board can be run using the TinyDuino coin cell option.
Pins Used
  • A5/SCL - I2C Serial Clock line
  • A4/SDA - I2C Serial Data line
Dimensions
  • 20mm x 20mm (.787 inches x .787 inches)
  • Max Height (from lower bottom TinyShield Connector to upper top TinyShield Connector): 5.11mm (0.201 inches)
  • Weight: .96 gram (.03 ounces)

Notes

  • You can also use this shield without the TinyDuino – there are 0.1" spaced connections for power, ground, and the two I2C signals along the side of the TinyShield to allow you to connect a different system. 

Materials

TinyZero and Ambient Light Sensor TinyShield

Hardware

Software


Hardware Assembly

On top of your processor board of choice (i.e. TinyDuino), place the Ambient Light Sensor TinyShield. Plug a MicroUSB cable into the USB port and then plug the cable into a free USB port on your computer. Make sure the processor is switched on.

An assembled stack of a TinyDuino, USB TinyShield, and Ambient Light Sensor TinyShield.


Software Setup

First, open the Arduino IDE. If you don't have it installed, check out our TinyDuino Setup Tutorial.

No special libraries are required for this tutorial.


The Code

Press here to download a .zip file of the program. View the .ino Sketch file here:

Code
/*
  TinyCircuits Ambient Light TinyShield Example Sketch
  Using AMS TAOS TSL2572

  This example code is in the public domain.

  Written 25 October 2015
  By Ken Burns
  Modified 07 January 2019
  By Hunter Hykes

  https://TinyCircuits.com
*/

#include <Wire.h>

#define TSL2572_I2CADDR     0x39

#define GAIN_1X 0
#define GAIN_8X 1
#define GAIN_16X 2
#define GAIN_120X 3

//only use this with 1x and 8x gain settings
#define GAIN_DIVIDE_6 true 

int gain_val = 0;

void setup()
{
  Wire.begin();
  Serial.begin(9600);
  TSL2572nit(GAIN_1X);
}


void loop()
{
  float AmbientLightLux = Tsl2572ReadAmbientLight();
  Serial.print("Lux: ");
  Serial.println(AmbientLightLux);

  delay(1000);
}


void TSL2572nit(uint8_t gain)
{
  Tsl2572RegisterWrite( 0x0F, gain );//set gain
  Tsl2572RegisterWrite( 0x01, 0xED );//51.87 ms
  Tsl2572RegisterWrite( 0x00, 0x03 );//turn on
  if(GAIN_DIVIDE_6)
    Tsl2572RegisterWrite( 0x0D, 0x04 );//scale gain by 0.16
  if(gain==GAIN_1X)gain_val=1;
  else if(gain==GAIN_8X)gain_val=8;
  else if(gain==GAIN_16X)gain_val=16;
  else if(gain==GAIN_120X)gain_val=120;
}


void Tsl2572RegisterWrite( byte regAddr, byte regData )
{
  Wire.beginTransmission(TSL2572_I2CADDR);
  Wire.write(0x80 | regAddr); 
  Wire.write(regData);
  Wire.endTransmission(); 
}


float Tsl2572ReadAmbientLight()
{     
  uint8_t data[4]; 
  int c0,c1;
  float lux1,lux2,cpl;

  Wire.beginTransmission(TSL2572_I2CADDR);
  Wire.write(0xA0 | 0x14);
  Wire.endTransmission();
  Wire.requestFrom(TSL2572_I2CADDR,4);
  for(uint8_t i=0;i<4;i++)
    data[i] = Wire.read();

  c0 = data[1]<<8 | data[0];
  c1 = data[3]<<8 | data[2];

  //see TSL2572 datasheet
  cpl = 51.87 * (float)gain_val / 60.0;
  if(GAIN_DIVIDE_6) cpl/=6.0;
  lux1 = ((float)c0 - (1.87 * (float)c1)) / cpl;
  lux2 = ((0.63 * (float)c0) - (float)c1) / cpl;
  cpl = max(lux1, lux2);
  return max(cpl, 0.0);
}

Upload the program using the Tools selections:

Tools selections for TinyDuino (Your COM# will probably be different)

Once the upload is complete, open up the Serial Monitor at 9600 baud and you'll be able to observe the data gathered by the sensor!

Data from the Ambient Light Sensor TinyShield here at TinyCircuits

For your project that involves sensing light, you will probably have to take some readings first to find out what those readings would be. Make sure you account for day and night light if the sun may be involved!


Contact Us

If you have any questions or feedback, feel free to email us at info@tinycircuits.com or make a post on the forum.

Show us what you make by tagging @TinyCircuits on Instagram, Twitter, or Facebook so we can feature it!

Thanks for making with us!


Downloads