├── .gitattributes ├── README.md ├── AP3216 └── AP3216.ino ├── APDS9960_proximity_sensor └── APDS9960_proximity_sensor.ino ├── TCS3200 └── TCS3200.ino ├── APDS9960_color_sensor └── APDS9960_color_sensor.ino ├── ISL29125 └── ISL29125.ino ├── TCS3414 └── TCS3414.ino ├── BH1750autoadjust └── BH1750autoadjust.ino ├── APDS9960_GestureTest └── APDS9960_GestureTest.ino ├── TSL2561 └── TSL2561.ino └── TCS34725 └── TCS34725.ino /.gitattributes: -------------------------------------------------------------------------------- 1 | # Auto detect text files and perform LF normalization 2 | * text=auto 3 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Light Sensors 2 | 3 | Links for video: 4 | 5 | Libraries used:\ 6 | TCS3200: No Library used\ 7 | TSL2561: Adafruit (Arduino IDE)\ 8 | TSL2591: Adafruit (Arduino IDE)\ 9 | ISL29125: Sparkfun Library (Arduino IDE)\ 10 | TCS3414: No library used\ 11 | TCS34725: Adafruit (Arduino IDE)\ 12 | BH1750: https://github.com/RobTillaart/BH1750FVI_RT \ 13 | APDS9960: https://github.com/sparkfun/SparkFun_APDS-9960_Sensor_Arduino_Library \ 14 | -------------------------------------------------------------------------------- /AP3216/AP3216.ino: -------------------------------------------------------------------------------- 1 | #include "Ai_AP3216_AmbientLightAndProximity.h" 2 | 3 | Ai_AP3216_AmbientLightAndProximity aps = Ai_AP3216_AmbientLightAndProximity(); 4 | //Ai_AP3216_AmbientLightAndProximity aps = Ai_AP3216_AmbientLightAndProximity(D5, D6);//custompins 5 | 6 | void setup() { 7 | Serial.begin(115200); 8 | aps.begin(); 9 | aps.startAmbientLightAndProximitySensor (); 10 | } 11 | 12 | void loop() { 13 | long alsValue = aps.getAmbientLight(); 14 | long psValue = aps.getProximity(); 15 | 16 | Serial.print("Ambient Light: "); 17 | Serial.print(alsValue); 18 | Serial.print(", Proximity: "); 19 | Serial.println(psValue); 20 | 21 | delay(200); 22 | } 23 | -------------------------------------------------------------------------------- /APDS9960_proximity_sensor/APDS9960_proximity_sensor.ino: -------------------------------------------------------------------------------- 1 | /*************************************************************************** 2 | This is a library for the APDS9960 digital proximity, ambient light, RGB, and gesture sensor 3 | 4 | This sketch puts the sensor in proximity mode and enables the interrupt 5 | to fire when proximity goes over a set value 6 | 7 | Designed specifically to work with the Adafruit APDS9960 breakout 8 | ----> http://www.adafruit.com/products/3595 9 | 10 | These sensors use I2C to communicate. The device's I2C address is 0x39 11 | 12 | Adafruit invests time and resources providing this open source code, 13 | please support Adafruit andopen-source hardware by purchasing products 14 | from Adafruit! 15 | 16 | Written by Dean Miller for Adafruit Industries. 17 | BSD license, all text above must be included in any redistribution 18 | ***************************************************************************/ 19 | 20 | #include "Adafruit_APDS9960.h" 21 | 22 | //the pin that the interrupt is attached to 23 | #define INT_PIN 5 24 | 25 | //create the APDS9960 object 26 | Adafruit_APDS9960 apds; 27 | 28 | void setup() { 29 | Serial.begin(115200); 30 | pinMode(INT_PIN, INPUT_PULLUP); 31 | 32 | if (!apds.begin()) { 33 | Serial.println("failed to initialize device! Please check your wiring."); 34 | } 35 | else Serial.println("Device initialized!"); 36 | 37 | //enable proximity mode 38 | apds.enableProximity(true); 39 | 40 | //set the interrupt threshold to fire when proximity reading goes above 175 41 | apds.setProximityInterruptThreshold(0, 150); 42 | 43 | //enable the proximity interrupt 44 | apds.enableProximityInterrupt(); 45 | } 46 | 47 | void loop() { 48 | 49 | //print the proximity reading when the interrupt pin goes low 50 | 51 | Serial.println(apds.readProximity()); 52 | delay(500); 53 | } 54 | -------------------------------------------------------------------------------- /TCS3200/TCS3200.ino: -------------------------------------------------------------------------------- 1 | 2 | #define S0 4 3 | #define S1 5 4 | #define S2 2 5 | #define S3 27 6 | #define sensorIn 14 7 | float frequency = 0; 8 | 9 | void setup() { 10 | pinMode(S0, OUTPUT); 11 | pinMode(S1, OUTPUT); 12 | pinMode(S2, OUTPUT); 13 | pinMode(S3, OUTPUT); 14 | pinMode(sensorIn, INPUT); 15 | 16 | // Setting frequency-scaling to 20% 17 | digitalWrite(S0, HIGH); 18 | digitalWrite(S1, LOW); 19 | 20 | Serial.begin(115200); 21 | } 22 | void loop() { 23 | 24 | float red = measure(0, 0); 25 | float green = measure(1, 1); 26 | float blue = measure(0, 1); 27 | float white = measure(1, 0); 28 | 29 | float max = red; 30 | if (green > max) max = green; 31 | if (blue > max) max = blue; 32 | 33 | red = 100.0 * red / max; 34 | green = 100.0 * green / max; 35 | blue = 100.0 * blue / max; 36 | 37 | // Printing the value on the serial monitor 38 | Serial.print("R= ");//printing name 39 | Serial.print(red);//printing RED color frequency 40 | Serial.print(" "); 41 | // Reading the output frequency 42 | // Printing the value on the serial monitor 43 | Serial.print("G= ");//printing name 44 | Serial.print(green);//printing RED color frequency 45 | Serial.print(" "); 46 | // Printing the value on the serial monitor 47 | Serial.print("B= ");//printing name 48 | Serial.print(blue);//printing RED color frequency 49 | // Printing the value on the serial monitor 50 | Serial.print(" "); 51 | Serial.print("C= ");//printing name Serial.print(white);//printing RED color frequency 52 | Serial.print(white);//printing clear color frequency 53 | Serial.println(" "); 54 | 55 | delay(1000); 56 | } 57 | 58 | float measure(int s2_value, int s3_value) { 59 | digitalWrite(S2, s2_value); 60 | digitalWrite(S3, s3_value); 61 | // Reading the output frequency 62 | frequency = 10000.0 / pulseIn(sensorIn, LOW); 63 | return frequency; 64 | } 65 | -------------------------------------------------------------------------------- /APDS9960_color_sensor/APDS9960_color_sensor.ino: -------------------------------------------------------------------------------- 1 | /*************************************************************************** 2 | This is a library for the APDS9960 digital proximity, ambient light, RGB, and gesture sensor 3 | 4 | This sketch puts the sensor in color mode and reads the RGB and clear values. 5 | 6 | Designed specifically to work with the Adafruit APDS9960 breakout 7 | ----> http://www.adafruit.com/products/3595 8 | 9 | These sensors use I2C to communicate. The device's I2C address is 0x39 10 | 11 | Adafruit invests time and resources providing this open source code, 12 | please support Adafruit andopen-source hardware by purchasing products 13 | from Adafruit! 14 | 15 | Written by Dean Miller for Adafruit Industries. 16 | BSD license, all text above must be included in any redistribution 17 | ***************************************************************************/ 18 | 19 | #include "Adafruit_APDS9960.h" 20 | Adafruit_APDS9960 apds; 21 | 22 | void setup() { 23 | Serial.begin(115200); 24 | 25 | if(!apds.begin()){ 26 | Serial.println("failed to initialize device! Please check your wiring."); 27 | } 28 | else Serial.println("Device initialized!"); 29 | 30 | //enable color sensign mode 31 | apds.enableColor(true); 32 | } 33 | 34 | void loop() { 35 | //create some variables to store the color data in 36 | uint16_t r, g, b, c; 37 | 38 | //wait for color data to be ready 39 | while(!apds.colorDataReady()){ 40 | delay(5); 41 | } 42 | 43 | //get the data and print the different channels 44 | apds.getColorData(&r, &g, &b, &c); 45 | 46 | float green = (float)g; 47 | float red = (float)r; 48 | float blue = (float)b; 49 | 50 | float max = green; 51 | if (red > max) max = red; 52 | if (blue > max) max = blue; 53 | 54 | red = 100.0 * red / max; 55 | green = 100.0 * green / max; 56 | blue = 100.0 * blue / max; 57 | 58 | Serial.print(" Red : "); 59 | Serial.print(red) ; 60 | Serial.print(" Green : "); 61 | Serial.print(green); 62 | Serial.print(" Blue : "); 63 | Serial.println(blue) ; 64 | 65 | delay(500); 66 | } 67 | -------------------------------------------------------------------------------- /ISL29125/ISL29125.ino: -------------------------------------------------------------------------------- 1 | /****************************************************************************** 2 | ISL29125_basics.ino 3 | Simple example for using the ISL29125 RGB sensor library. 4 | Jordan McConnell @ SparkFun Electronics 5 | 11 Apr 2014 6 | https://github.com/sparkfun/SparkFun_ISL29125_Breakout_Arduino_Library 7 | 8 | This example declares an SFE_ISL29125 object called RGB_sensor. The 9 | object/sensor is initialized with a basic configuration so that it continuously 10 | samples the light intensity of red, green and blue spectrums. These values are 11 | read from the sensor every 2 seconds and printed to the Serial monitor. 12 | 13 | Developed/Tested with: 14 | Arduino Uno 15 | Arduino IDE 1.0.5 16 | 17 | Requires: 18 | SparkFun_ISL29125_Arduino_Library 19 | 20 | This code is beerware. 21 | Distributed as-is; no warranty is given. 22 | ******************************************************************************/ 23 | 24 | #include 25 | #include "SparkFunISL29125.h" 26 | 27 | // Declare sensor object 28 | SFE_ISL29125 RGB_sensor; 29 | 30 | void setup() 31 | { 32 | // Initialize serial communication 33 | Serial.begin(115200); 34 | 35 | // Initialize the ISL29125 with simple configuration so it starts sampling 36 | if (RGB_sensor.init()) 37 | { 38 | Serial.println("Sensor Initialization Successful\n\r"); 39 | } 40 | } 41 | 42 | // Read sensor values for each color and print them to serial monitor 43 | void loop() 44 | { 45 | // Read sensor values (16 bit integers) 46 | unsigned int redRaw = RGB_sensor.readRed(); 47 | unsigned int greenRaw = RGB_sensor.readGreen(); 48 | unsigned int blueRaw = RGB_sensor.readBlue(); 49 | 50 | float green = (float)greenRaw; 51 | float red = (float)redRaw; 52 | float blue = (float)blueRaw; 53 | 54 | /* float max = green; 55 | if (red > max) max = red; 56 | if (blue > max) max = blue; 57 | 58 | red = 100.0 * red / max; 59 | green = 100.0 * green / max; 60 | blue = 100.0 * blue / max; 61 | */ 62 | 63 | Serial.print("Red : "); 64 | Serial.print(red) ; 65 | Serial.print(" Green : "); 66 | Serial.print(green); 67 | Serial.print(" Blue : "); 68 | Serial.println(blue) ; 69 | 70 | /* 71 | // Print out readings, change HEX to DEC if you prefer decimal output 72 | Serial.print("Red: "); Serial.println(red, HEX); 73 | Serial.print("Green: "); Serial.println(green, HEX); 74 | Serial.print("Blue: "); Serial.println(blue, HEX); 75 | Serial.println(); 76 | */ 77 | delay(1000); 78 | } 79 | -------------------------------------------------------------------------------- /TCS3414/TCS3414.ino: -------------------------------------------------------------------------------- 1 | // Distributed with a free-will license. 2 | // Use it any way you want, profit or free, provided it fits in the licenses of its associated works. 3 | // TCS3414 4 | // This code is designed to work with the TCS3414_I2CS I2C Mini Module available from ControlEverything.com. 5 | // https://www.controleverything.com/content/Color?sku=TCS3414_I2CS#tabs-0-product_tabset-2 6 | 7 | #include 8 | 9 | // TCS3414 I2C address is 0x39(57) 10 | #define Addr 0x39 11 | 12 | void setup() 13 | { 14 | // Initialise I2C communication as MASTER 15 | Wire.begin(); 16 | // Initialise serial communication, set baud rate = 9600 17 | Serial.begin(115200); 18 | 19 | // Start I2C Transmission 20 | Wire.beginTransmission(Addr); 21 | // Select control register with command register, 0x80(128) 22 | Wire.write(0x00 | 0x80); 23 | // ADC enable, Power On 24 | Wire.write(0x03); 25 | // Stop I2C transmission 26 | Wire.endTransmission(); 27 | 28 | // Start I2C Transmission 29 | Wire.beginTransmission(Addr); 30 | // Select gain register with command register, 0x80(128) 31 | Wire.write(0x07 | 0x80); 32 | // Gain = 1x, Prescaler Mode = Divide by 1 33 | Wire.write(0x00); 34 | // Stop I2C transmission 35 | Wire.endTransmission(); 36 | delay(300); 37 | } 38 | 39 | void loop() 40 | { 41 | unsigned int data[8]; 42 | 43 | // Start I2C Transmission 44 | Wire.beginTransmission(Addr); 45 | // Select data register with command register, 0x80(128) 46 | Wire.write(0x10 | 0x80); 47 | // Stop I2C transmission 48 | Wire.endTransmission(); 49 | 50 | // Request 8 bytes of data 51 | Wire.requestFrom(Addr, 8); 52 | 53 | // Read the 8 bytes of data 54 | // green lsb, green msb, red lsb, red msb 55 | // blue lsb, blue msb, cData lsb, cData msb 56 | if (Wire.available() == 8) 57 | { 58 | data[0] = Wire.read(); 59 | data[1] = Wire.read(); 60 | data[2] = Wire.read(); 61 | data[3] = Wire.read(); 62 | data[4] = Wire.read(); 63 | data[5] = Wire.read(); 64 | data[6] = Wire.read(); 65 | data[7] = Wire.read(); 66 | } 67 | 68 | // Convert the data 69 | float green = data[1] * 256.0 + data[0]; 70 | float red = data[3] * 256.0 + data[2]; 71 | float blue = data[5] * 256.0 + data[4]; 72 | float cData = data[6] * 256.0 + data[5]; 73 | 74 | float max = green; 75 | if (red > max) max = red; 76 | if (blue > max) max = blue; 77 | 78 | red=100.0*red/max; 79 | green=100.0*green/max; 80 | blue=100.0*blue/max; 81 | 82 | 83 | // Calculate luminance 84 | float luminance = (-0.32466 * red) + (1.57837 * green) + (-0.73191 * blue); 85 | 86 | // Output data to serial monitor 87 | Serial.print("Red : "); 88 | Serial.print(red) ; 89 | Serial.print(" Green : "); 90 | Serial.print(green); 91 | Serial.print(" Blue : "); 92 | Serial.println(blue) ; 93 | // Serial.print("Clear Data Luminance : "); 94 | // Serial.println(cData) ; 95 | // Serial.print("Ambient Light Luminance : "); 96 | // Serial.println(luminance); 97 | delay(500); 98 | } 99 | -------------------------------------------------------------------------------- /BH1750autoadjust/BH1750autoadjust.ino: -------------------------------------------------------------------------------- 1 | /* 2 | 3 | Example of BH1750 library usage. 4 | 5 | This example initialises the BH1750 object using the default high resolution 6 | one shot mode and then makes a light level reading every five seconds. 7 | 8 | After the measurement the MTreg value is changed according to the result: 9 | lux > 40000 ==> MTreg = 32 10 | lux < 40000 ==> MTreg = 69 (default) 11 | lux < 10 ==> MTreg = 138 12 | Remember to test your specific sensor! Maybe the MTreg value range from 32 13 | up to 254 is not applicable to your unit. 14 | 15 | Connection: 16 | 17 | VCC -> 3V3 or 5V 18 | GND -> GND 19 | SCL -> SCL (A5 on Arduino Uno, Leonardo, etc or 21 on Mega and Due, on esp8266 free selectable) 20 | SDA -> SDA (A4 on Arduino Uno, Leonardo, etc or 20 on Mega and Due, on esp8266 free selectable) 21 | ADD -> (not connected) or GND 22 | 23 | ADD pin is used to set sensor I2C address. If it has voltage greater or equal to 24 | 0.7VCC voltage (e.g. you've connected it to VCC) the sensor address will be 25 | 0x5C. In other case (if ADD voltage less than 0.7 * VCC) the sensor address will 26 | be 0x23 (by default). 27 | 28 | */ 29 | 30 | #include 31 | #include 32 | 33 | BH1750 lightMeter; 34 | 35 | void setup(){ 36 | 37 | Serial.begin(115200); 38 | 39 | // Initialize the I2C bus (BH1750 library doesn't do this automatically) 40 | Wire.begin(); 41 | // On esp8266 you can select SCL and SDA pins using Wire.begin(D4, D3); 42 | 43 | lightMeter.begin(BH1750::ONE_TIME_HIGH_RES_MODE); 44 | //lightMeter.setMTreg(69); // not needed, only mentioning it 45 | 46 | Serial.println(F("BH1750 Test begin")); 47 | 48 | } 49 | 50 | void loop() { 51 | //we use here the maxWait option due fail save 52 | float lux = lightMeter.readLightLevel(true); 53 | Serial.print(F("Light: ")); 54 | Serial.print(lux); 55 | Serial.println(F(" lx")); 56 | 57 | if (lux < 0) { 58 | Serial.println(F("Error condition detected")); 59 | } 60 | else { 61 | if (lux > 40000.0) { 62 | // reduce measurement time - needed in direct sun light 63 | if (lightMeter.setMTreg(32)) { 64 | Serial.println(F("Setting MTReg to low value for high light environment")); 65 | } 66 | else { 67 | Serial.println(F("Error setting MTReg to low value for high light environment")); 68 | } 69 | } 70 | else { 71 | if (lux > 10.0) { 72 | // typical light environment 73 | if (lightMeter.setMTreg(69)) { 74 | Serial.println(F("Setting MTReg to default value for normal light environment")); 75 | } 76 | else { 77 | Serial.println(F("Error setting MTReg to default value for normal light environment")); 78 | } 79 | } 80 | else { 81 | if (lux <= 10.0) { 82 | //very low light environment 83 | if (lightMeter.setMTreg(138)) { 84 | Serial.println(F("Setting MTReg to high value for low light environment")); 85 | } 86 | else { 87 | Serial.println(F("Error setting MTReg to high value for low light environment")); 88 | } 89 | } 90 | } 91 | } 92 | 93 | } 94 | Serial.println(F("--------------------------------------")); 95 | delay(1000); 96 | } 97 | -------------------------------------------------------------------------------- /APDS9960_GestureTest/APDS9960_GestureTest.ino: -------------------------------------------------------------------------------- 1 | /**************************************************************** 2 | GestureTest.ino 3 | APDS-9960 RGB and Gesture Sensor 4 | Shawn Hymel @ SparkFun Electronics 5 | May 30, 2014 6 | https://github.com/sparkfun/APDS-9960_RGB_and_Gesture_Sensor 7 | 8 | Tests the gesture sensing abilities of the APDS-9960. Configures 9 | APDS-9960 over I2C and waits for gesture events. Calculates the 10 | direction of the swipe (up, down, left, right) and displays it 11 | on a serial console. 12 | 13 | To perform a NEAR gesture, hold your hand 14 | far above the sensor and move it close to the sensor (within 2 15 | inches). Hold your hand there for at least 1 second and move it 16 | away. 17 | 18 | To perform a FAR gesture, hold your hand within 2 inches of the 19 | sensor for at least 1 second and then move it above (out of 20 | range) of the sensor. 21 | 22 | Hardware Connections: 23 | 24 | IMPORTANT: The APDS-9960 can only accept 3.3V! 25 | 26 | Arduino Pin APDS-9960 Board Function 27 | 28 | 3.3V VCC Power 29 | GND GND Ground 30 | A4 SDA I2C Data 31 | A5 SCL I2C Clock 32 | 2 INT Interrupt 33 | 34 | Resources: 35 | Include Wire.h and SparkFun_APDS-9960.h 36 | 37 | Development environment specifics: 38 | Written in Arduino 1.0.5 39 | Tested with SparkFun Arduino Pro Mini 3.3V 40 | 41 | This code is beerware; if you see me (or any other SparkFun 42 | employee) at the local, and you've found our code helpful, please 43 | buy us a round! 44 | 45 | Distributed as-is; no warranty is given. 46 | ****************************************************************/ 47 | 48 | #include 49 | #include 50 | 51 | // Pins 52 | #define APDS9960_INT 14 // Needs to be an interrupt pin 53 | 54 | // Constants 55 | 56 | // Global Variables 57 | SparkFun_APDS9960 apds = SparkFun_APDS9960(); 58 | int isr_flag = 0; 59 | 60 | void setup() { 61 | 62 | // Set interrupt pin as input 63 | pinMode(APDS9960_INT, INPUT_PULLUP); 64 | 65 | // Initialize Serial port 66 | Serial.begin(115200); 67 | Serial.println(); 68 | Serial.println(F("--------------------------------")); 69 | Serial.println(F("SparkFun APDS-9960 - GestureTest")); 70 | Serial.println(F("--------------------------------")); 71 | 72 | // Initialize interrupt service routine 73 | attachInterrupt(APDS9960_INT, interruptRoutine, FALLING); 74 | 75 | // Initialize APDS-9960 (configure I2C and initial values) 76 | if ( apds.init() ) { 77 | Serial.println(F("APDS-9960 initialization complete")); 78 | } else { 79 | Serial.println(F("Something went wrong during APDS-9960 init!")); 80 | } 81 | 82 | // Start running the APDS-9960 gesture sensor engine 83 | if ( apds.enableGestureSensor(true) ) { 84 | Serial.println(F("Gesture sensor is now running")); 85 | } else { 86 | Serial.println(F("Something went wrong during gesture sensor init!")); 87 | } 88 | } 89 | 90 | void loop() { 91 | if( isr_flag == 1 ) { 92 | detachInterrupt(0); 93 | handleGesture(); 94 | isr_flag = 0; 95 | attachInterrupt(APDS9960_INT, interruptRoutine, FALLING); 96 | } 97 | } 98 | 99 | void interruptRoutine() { 100 | isr_flag = 1; 101 | } 102 | 103 | void handleGesture() { 104 | if ( apds.isGestureAvailable() ) { 105 | switch ( apds.readGesture() ) { 106 | case DIR_UP: 107 | Serial.println("BACKWARDS"); 108 | break; 109 | case DIR_DOWN: 110 | Serial.println("FORWARDS"); 111 | break; 112 | case DIR_LEFT: 113 | Serial.println("LEFT"); 114 | break; 115 | case DIR_RIGHT: 116 | Serial.println("RIGHT"); 117 | break; 118 | case DIR_NEAR: 119 | Serial.println("NEAR"); 120 | break; 121 | case DIR_FAR: 122 | Serial.println("FAR"); 123 | break; 124 | default: 125 | Serial.println("NONE"); 126 | } 127 | } 128 | } 129 | -------------------------------------------------------------------------------- /TSL2561/TSL2561.ino: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | 5 | /* This driver uses the Adafruit unified sensor library (Adafruit_Sensor), 6 | which provides a common 'type' for sensor data and some helper functions. 7 | 8 | To use this driver you will also need to download the Adafruit_Sensor 9 | library and include it in your libraries folder. 10 | 11 | You should also assign a unique ID to this sensor for use with 12 | the Adafruit Sensor API so that you can identify this particular 13 | sensor in any data logs, etc. To assign a unique ID, simply 14 | provide an appropriate value in the constructor below (12345 15 | is used by default in this example). 16 | 17 | Connections 18 | =========== 19 | Connect SCL to I2C SCL Clock 20 | Connect SDA to I2C SDA Data 21 | Connect VCC/VDD to 3.3V or 5V (depends on sensor's logic level, check the datasheet) 22 | Connect GROUND to common ground 23 | 24 | I2C Address 25 | =========== 26 | The address will be different depending on whether you leave 27 | the ADDR pin floating (addr 0x39), or tie it to ground or vcc. 28 | The default addess is 0x39, which assumes the ADDR pin is floating 29 | (not connected to anything). If you set the ADDR pin high 30 | or low, use TSL2561_ADDR_HIGH (0x49) or TSL2561_ADDR_LOW 31 | (0x29) respectively. 32 | 33 | History 34 | ======= 35 | 2013/JAN/31 - First version (KTOWN) 36 | */ 37 | 38 | Adafruit_TSL2561_Unified tsl = Adafruit_TSL2561_Unified(TSL2561_ADDR_FLOAT, 12345); 39 | 40 | /**************************************************************************/ 41 | /* 42 | Displays some basic information on this sensor from the unified 43 | sensor API sensor_t type (see Adafruit_Sensor for more information) 44 | */ 45 | /**************************************************************************/ 46 | void displaySensorDetails(void) 47 | { 48 | sensor_t sensor; 49 | tsl.getSensor(&sensor); 50 | Serial.println("------------------------------------"); 51 | Serial.print ("Sensor: "); Serial.println(sensor.name); 52 | Serial.print ("Driver Ver: "); Serial.println(sensor.version); 53 | Serial.print ("Unique ID: "); Serial.println(sensor.sensor_id); 54 | Serial.print ("Max Value: "); Serial.print(sensor.max_value); Serial.println(" lux"); 55 | Serial.print ("Min Value: "); Serial.print(sensor.min_value); Serial.println(" lux"); 56 | Serial.print ("Resolution: "); Serial.print(sensor.resolution); Serial.println(" lux"); 57 | Serial.println("------------------------------------"); 58 | Serial.println(""); 59 | delay(500); 60 | } 61 | 62 | /**************************************************************************/ 63 | /* 64 | Configures the gain and integration time for the TSL2561 65 | */ 66 | /**************************************************************************/ 67 | void configureSensor(void) 68 | { 69 | /* You can also manually set the gain or enable auto-gain support */ 70 | // tsl.setGain(TSL2561_GAIN_1X); /* No gain ... use in bright light to avoid sensor saturation */ 71 | // tsl.setGain(TSL2561_GAIN_16X); /* 16x gain ... use in low light to boost sensitivity */ 72 | tsl.enableAutoRange(true); /* Auto-gain ... switches automatically between 1x and 16x */ 73 | 74 | /* Changing the integration time gives you better sensor resolution (402ms = 16-bit data) */ 75 | tsl.setIntegrationTime(TSL2561_INTEGRATIONTIME_13MS); /* fast but low resolution */ 76 | // tsl.setIntegrationTime(TSL2561_INTEGRATIONTIME_101MS); /* medium resolution and speed */ 77 | // tsl.setIntegrationTime(TSL2561_INTEGRATIONTIME_402MS); /* 16-bit data but slowest conversions */ 78 | 79 | /* Update these values depending on what you've set above! */ 80 | Serial.println("------------------------------------"); 81 | Serial.print ("Gain: "); Serial.println("Auto"); 82 | Serial.print ("Timing: "); Serial.println("13 ms"); 83 | Serial.println("------------------------------------"); 84 | } 85 | 86 | /**************************************************************************/ 87 | /* 88 | Arduino setup function (automatically called at startup) 89 | */ 90 | /**************************************************************************/ 91 | void setup(void) 92 | { 93 | Serial.begin(115200); 94 | Serial.println("Light Sensor Test"); Serial.println(""); 95 | 96 | /* Initialise the sensor */ 97 | //use tsl.begin() to default to Wire, 98 | //tsl.begin(&Wire2) directs api to use Wire2, etc. 99 | if(!tsl.begin()) 100 | { 101 | /* There was a problem detecting the TSL2561 ... check your connections */ 102 | Serial.print("Ooops, no TSL2561 detected ... Check your wiring or I2C ADDR!"); 103 | while(1); 104 | } 105 | 106 | /* Display some basic information on this sensor */ 107 | displaySensorDetails(); 108 | 109 | /* Setup the sensor gain and integration time */ 110 | configureSensor(); 111 | 112 | /* We're ready to go! */ 113 | Serial.println(""); 114 | } 115 | 116 | /**************************************************************************/ 117 | /* 118 | Arduino loop function, called once 'setup' is complete (your own code 119 | should go here) 120 | */ 121 | /**************************************************************************/ 122 | void loop(void) 123 | { 124 | /* Get a new sensor event */ 125 | sensors_event_t event; 126 | tsl.getEvent(&event); 127 | 128 | /* Display the results (light is measured in lux) */ 129 | if (event.light) 130 | { 131 | Serial.print(event.light); Serial.println(" lux"); 132 | } 133 | else 134 | { 135 | /* If event.light = 0 lux the sensor is probably saturated 136 | and no reliable data could be generated! */ 137 | Serial.println("Sensor overload"); 138 | } 139 | delay(250); 140 | } 141 | -------------------------------------------------------------------------------- /TCS34725/TCS34725.ino: -------------------------------------------------------------------------------- 1 | #include 2 | #include "Adafruit_TCS34725.h" 3 | 4 | // 5 | // An experimental wrapper class that implements the improved lux and color temperature from 6 | // TAOS and a basic autorange mechanism. 7 | // 8 | // Written by ductsoup, public domain 9 | // 10 | 11 | // RGB Color Sensor with IR filter and White LED - TCS34725 12 | // I2C 7-bit address 0x29, 8-bit address 0x52 13 | // 14 | // http://www.adafruit.com/product/1334 15 | // http://learn.adafruit.com/adafruit-color-sensors/overview 16 | // http://www.adafruit.com/datasheets/TCS34725.pdf 17 | // http://www.ams.com/eng/Products/Light-Sensors/Color-Sensor/TCS34725 18 | // http://www.ams.com/eng/content/view/download/265215 <- DN40, calculations 19 | // http://www.ams.com/eng/content/view/download/181895 <- DN39, some thoughts on autogain 20 | // http://www.ams.com/eng/content/view/download/145158 <- DN25 (original Adafruit calculations) 21 | // 22 | // connect LED to digital 4 or GROUND for ambient light sensing 23 | // connect SCL to analog 5 24 | // connect SDA to analog 4 25 | // connect Vin to 3.3-5V DC 26 | // connect GROUND to common ground 27 | 28 | // some magic numbers for this device from the DN40 application note 29 | #define TCS34725_R_Coef 0.136 30 | #define TCS34725_G_Coef 1.000 31 | #define TCS34725_B_Coef -0.444 32 | #define TCS34725_GA 1.0 33 | #define TCS34725_DF 310.0 34 | #define TCS34725_CT_Coef 3810.0 35 | #define TCS34725_CT_Offset 1391.0 36 | 37 | // Autorange class for TCS34725 38 | class tcs34725 { 39 | private: 40 | struct tcs_agc { 41 | tcs34725Gain_t ag; 42 | tcs34725IntegrationTime_t at; 43 | uint16_t mincnt; 44 | uint16_t maxcnt; 45 | }; 46 | static const tcs_agc agc_lst[]; 47 | uint16_t agc_cur; 48 | 49 | void setGainTime(void); 50 | Adafruit_TCS34725 tcs; 51 | 52 | public: 53 | tcs34725(void); 54 | 55 | boolean begin(void); 56 | void getData(void); 57 | 58 | boolean isAvailable, isSaturated; 59 | uint16_t againx, atime, atime_ms; 60 | uint16_t r, g, b, c; 61 | uint16_t ir; 62 | uint16_t r_comp, g_comp, b_comp, c_comp; 63 | uint16_t saturation, saturation75; 64 | float cratio, cpl, ct, lux, maxlux; 65 | }; 66 | // 67 | // Gain/time combinations to use and the min/max limits for hysteresis 68 | // that avoid saturation. They should be in order from dim to bright. 69 | // 70 | // Also set the first min count and the last max count to 0 to indicate 71 | // the start and end of the list. 72 | // 73 | const tcs34725::tcs_agc tcs34725::agc_lst[] = { 74 | { TCS34725_GAIN_60X, TCS34725_INTEGRATIONTIME_700MS, 0, 20000 }, 75 | { TCS34725_GAIN_60X, TCS34725_INTEGRATIONTIME_154MS, 4990, 63000 }, 76 | { TCS34725_GAIN_16X, TCS34725_INTEGRATIONTIME_154MS, 16790, 63000 }, 77 | { TCS34725_GAIN_4X, TCS34725_INTEGRATIONTIME_154MS, 15740, 63000 }, 78 | { TCS34725_GAIN_1X, TCS34725_INTEGRATIONTIME_154MS, 15740, 0 } 79 | }; 80 | tcs34725::tcs34725() : agc_cur(0), isAvailable(0), isSaturated(0) { 81 | } 82 | 83 | // initialize the sensor 84 | boolean tcs34725::begin(void) { 85 | tcs = Adafruit_TCS34725(agc_lst[agc_cur].at, agc_lst[agc_cur].ag); 86 | if ((isAvailable = tcs.begin())) 87 | setGainTime(); 88 | return (isAvailable); 89 | } 90 | 91 | // Set the gain and integration time 92 | void tcs34725::setGainTime(void) { 93 | tcs.setGain(agc_lst[agc_cur].ag); 94 | tcs.setIntegrationTime(agc_lst[agc_cur].at); 95 | atime = int(agc_lst[agc_cur].at); 96 | atime_ms = ((256 - atime) * 2.4); 97 | switch (agc_lst[agc_cur].ag) { 98 | case TCS34725_GAIN_1X: 99 | againx = 1; 100 | break; 101 | case TCS34725_GAIN_4X: 102 | againx = 4; 103 | break; 104 | case TCS34725_GAIN_16X: 105 | againx = 16; 106 | break; 107 | case TCS34725_GAIN_60X: 108 | againx = 60; 109 | break; 110 | } 111 | } 112 | 113 | // Retrieve data from the sensor and do the calculations 114 | void tcs34725::getData(void) { 115 | // read the sensor and autorange if necessary 116 | tcs.getRawData(&r, &g, &b, &c); 117 | while (1) { 118 | if (agc_lst[agc_cur].maxcnt && c > agc_lst[agc_cur].maxcnt) 119 | agc_cur++; 120 | else if (agc_lst[agc_cur].mincnt && c < agc_lst[agc_cur].mincnt) 121 | agc_cur--; 122 | else break; 123 | 124 | setGainTime(); 125 | delay((256 - atime) * 2.4 * 2); // shock absorber 126 | tcs.getRawData(&r, &g, &b, &c); 127 | break; 128 | } 129 | 130 | // DN40 calculations 131 | ir = (r + g + b > c) ? (r + g + b - c) / 2 : 0; 132 | r_comp = r - ir; 133 | g_comp = g - ir; 134 | b_comp = b - ir; 135 | c_comp = c - ir; 136 | cratio = float(ir) / float(c); 137 | 138 | saturation = ((256 - atime) > 63) ? 65535 : 1024 * (256 - atime); 139 | saturation75 = (atime_ms < 150) ? (saturation - saturation / 4) : saturation; 140 | isSaturated = (atime_ms < 150 && c > saturation75) ? 1 : 0; 141 | cpl = (atime_ms * againx) / (TCS34725_GA * TCS34725_DF); 142 | maxlux = 65535 / (cpl * 3); 143 | 144 | lux = (TCS34725_R_Coef * float(r_comp) + TCS34725_G_Coef * float(g_comp) + TCS34725_B_Coef * float(b_comp)) / cpl; 145 | ct = TCS34725_CT_Coef * float(b_comp) / float(r_comp) + TCS34725_CT_Offset; 146 | } 147 | 148 | tcs34725 rgb_sensor; 149 | 150 | void setup(void) { 151 | Serial.begin(115200); 152 | rgb_sensor.begin(); 153 | pinMode(4, OUTPUT); 154 | digitalWrite(4, LOW); // @gremlins Bright light, bright light! 155 | } 156 | 157 | void loop(void) { 158 | rgb_sensor.getData(); 159 | 160 | float green = rgb_sensor.g; 161 | float red = rgb_sensor.r; 162 | float blue = rgb_sensor.b; 163 | 164 | float max = green; 165 | if (red > max) max = red; 166 | if (blue > max) max = blue; 167 | 168 | red = 100.0 * red / max; 169 | green = 100.0 * green / max; 170 | blue = 100.0 * blue / max; 171 | 172 | /* Serial.print(F("Gain:")); 173 | Serial.print(rgb_sensor.againx); 174 | Serial.print(F("x ")); 175 | Serial.print(F("Time:")); 176 | Serial.print(rgb_sensor.atime_ms); 177 | Serial.print(F("ms (0x")); 178 | Serial.print(rgb_sensor.atime, HEX); 179 | Serial.print(F(")")); 180 | 181 | Serial.print(F("Raw R:")); 182 | Serial.print(rgb_sensor.r); 183 | Serial.print(F(" G:")); 184 | Serial.print(rgb_sensor.g); 185 | Serial.print(F(" B:")); 186 | Serial.print(rgb_sensor.b); 187 | Serial.print(F(" C:")); 188 | Serial.println(rgb_sensor.c); 189 | */ 190 | Serial.print(" Red : "); 191 | Serial.print(red) ; 192 | Serial.print(" Green : "); 193 | Serial.print(green); 194 | Serial.print(" Blue : "); 195 | Serial.print(blue) ; 196 | 197 | /* Serial.print(F("IR:")); 198 | Serial.print(rgb_sensor.ir); 199 | Serial.print(F(" CRATIO:")); 200 | Serial.print(rgb_sensor.cratio); 201 | Serial.print(F(" Sat:")); 202 | Serial.print(rgb_sensor.saturation); 203 | Serial.print(F(" Sat75:")); 204 | Serial.print(rgb_sensor.saturation75); 205 | Serial.print(F(" ")); 206 | Serial.println(rgb_sensor.isSaturated ? "*SATURATED*" : ""); 207 | 208 | Serial.print(F("CPL:")); 209 | Serial.print(rgb_sensor.cpl); 210 | Serial.print(F(" Max lux:")); 211 | Serial.println(rgb_sensor.maxlux); 212 | 213 | Serial.print(F("Compensated R:")); 214 | Serial.print(rgb_sensor.r_comp); 215 | Serial.print(F(" G:")); 216 | Serial.print(rgb_sensor.g_comp); 217 | Serial.print(F(" B:")); 218 | Serial.print(rgb_sensor.b_comp); 219 | Serial.print(F(" C:")); 220 | Serial.println(rgb_sensor.c_comp); 221 | 222 | Serial.print(F("Lux:")); 223 | Serial.print(rgb_sensor.lux); 224 | Serial.print(F(" CT:")); 225 | Serial.print(rgb_sensor.ct); 226 | Serial.println(F("K")); 227 | */ 228 | Serial.println(); 229 | 230 | delay(1000); 231 | } 232 | --------------------------------------------------------------------------------