Skip to content

Commit

Permalink
incorporate changes from
Browse files Browse the repository at this point in the history
https://forum.arduino.cc/index.php?topic=563507.15 and adjust back into a
stock Arduino I2C interface
  • Loading branch information
c- committed Jan 11, 2020
1 parent 628988a commit c7dfa9d
Show file tree
Hide file tree
Showing 2 changed files with 92 additions and 32 deletions.
113 changes: 82 additions & 31 deletions LTR303.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
LTR303 illumination sensor library for Arduino
Lovelesh, thingTronics
MODIFIED to work better by Neil Emiro, Sep 2019
The MIT License (MIT)
Copyright (c) 2015 thingTronics Limited
Expand All @@ -28,7 +30,7 @@ version 0.1
*/

#include <LTR303.h>
#include <Wire.h>
#include <Wire.h> //use this for most Arduinos

LTR303::LTR303(void) {
// LTR303 object
Expand All @@ -48,8 +50,8 @@ boolean LTR303::setPowerUp(void) {
// Returns true (1) if successful, false (0) if there was an I2C error
// (Also see getError() below)

// Write 0x03 (reset = 1 & mode = 1) to command byte (power on)
return(writeByte(LTR303_CONTR,0x03));
// Write 0x01 (reset = 0 & mode = 1) to command byte (power on)
return(writeByte(LTR303_CONTR,0x01));
}

boolean LTR303::setPowerDown(void) {
Expand Down Expand Up @@ -422,7 +424,7 @@ boolean LTR303::getIntrPersist(byte &persist) {
}

// Get the right lux algorithm
boolean LTR303::getLux(byte gain, byte integrationTime, unsigned int CH0, unsigned int CH1, double &lux) {
boolean LTR303::getLux(byte gain, unsigned char IntTime, unsigned int CH0, unsigned int CH1, double &lux) {
// Convert raw data to lux
// gain: 0 (1X) or 7 (96X), see getControl()
// integrationTime: integration time in ms, from getMeasurementRate()
Expand All @@ -431,55 +433,104 @@ boolean LTR303::getLux(byte gain, byte integrationTime, unsigned int CH0, unsign
// returns true (1) if calculation was successful
// returns false (0) AND lux = 0.0 IF EITHER SENSOR WAS SATURATED (0XFFFF)

double ratio, d0, d1;

double ratio, ALS_INT;
int ALS_GAIN;

// Determine if either sensor saturated (0xFFFF)
// If so, abandon ship (calculation will not be accurate)
if ((CH0 == 0xFFFF) || (CH1 == 0xFFFF)) {
lux = 0.0;
return(false);
}

// Convert from unsigned integer to floating point
d0 = CH0; d1 = CH1;


// We will need the ratio for subsequent calculations
ratio = d1 / d0;
ratio = CH1 / (CH0 + CH1);


// Gain can take any value from 0-7, except 4 & 5
// If gain = 4, invalid
// If gain = 5, invalid
switch(gain){
case 0: // If gain = 0, device is set to 1X gain (default)
ALS_GAIN = 1;
break;
case 1: // If gain = 1, device is set to 2X gain
ALS_GAIN = 2;
break;
case 2: // If gain = 2, device is set to 4X gain
ALS_GAIN = 4;
break;
case 3: // If gain = 3, device is set to 8X gain
ALS_GAIN = 8;
break;
case 6: // If gain = 6, device is set to 48X gain
ALS_GAIN = 48;
break;
case 7: // If gain = 7, device is set to 96X gain
ALS_GAIN = 96;
break;
default: // If gain = 0, device is set to 1X gain (default)
ALS_GAIN = 1;
break;
}

// Normalize for integration time
d0 *= (402.0/integrationTime);
d1 *= (402.0/integrationTime);

// Normalize for gain
if (!gain) {
d0 *= 16;
d1 *= 16;
}
switch(IntTime){
case 0: // If integrationTime = 0, integrationTime will be 100ms (default)
ALS_INT = 1;
break;
case 1: // If integrationTime = 1, integrationTime will be 50ms
ALS_INT = 0.5;
break;
case 2: // If integrationTime = 2, integrationTime will be 200ms
ALS_INT = 2;
break;
case 3: // If integrationTime = 3, integrationTime will be 400ms
ALS_INT = 4;
break;
case 4: // If integrationTime = 4, integrationTime will be 150ms
ALS_INT = 1.5;
break;
case 5: // If integrationTime = 5, integrationTime will be 250ms
ALS_INT = 2.5;
break;
case 6: // If integrationTime = 6, integrationTime will be 300ms
ALS_INT = 3;
break;
case 7: // If integrationTime = 7, integrationTime will be 350ms
ALS_INT = 3.5;
break;
default: // If integrationTime = 0, integrationTime will be 100ms (default)
ALS_INT = 1;
break;
}




// Determine lux per datasheet equations:
if (ratio < 0.5) {
lux = 0.0304 * d0 - 0.062 * d0 * pow(ratio,1.4);
if (ratio < 0.45) {
lux = ((1.7743 * CH0) + (1.1059 * CH1))/ALS_GAIN/ALS_INT;
return(true);
}

if (ratio < 0.61) {
lux = 0.0224 * d0 - 0.031 * d1;
if ((ratio < 0.64) && (ratio >= 0.45)){
lux = ((4.2785 * CH0) + (1.9548 * CH1))/ALS_GAIN/ALS_INT;
return(true);
}

if (ratio < 0.80) {
lux = 0.0128 * d0 - 0.0153 * d1;
if ((ratio < 0.85) && (ratio >= 0.64)){
lux = ((0.5926 * CH0) + (0.1185 * CH1))/ALS_GAIN/ALS_INT;
return(true);
}

if (ratio < 1.30) {
lux = 0.00146 * d0 - 0.00112 * d1;
// if (ratio >= 0.85)
else {
lux = 0.0;
return(true);
}

// if (ratio > 1.30)
lux = 0.0;
return(true);
}
}

byte LTR303::getError(void) {
Expand Down Expand Up @@ -582,4 +633,4 @@ boolean LTR303::writeUInt(byte address, unsigned int value) {
return(true);

return(false);
}
}
11 changes: 10 additions & 1 deletion LTR303.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
LTR303 illumination sensor library for Arduino
Lovelesh, thingTronics
MODIFIED to work better by Neil Emiro, Sep 2019
The MIT License (MIT)
Copyright (c) 2015 thingTronics Limited
Expand Down Expand Up @@ -39,10 +41,17 @@ version 0.1
#define LTR303_MEAS_RATE 0x85
#define LTR303_PART_ID 0x86
#define LTR303_MANUFAC_ID 0x87

#define LTR303_DATA_CH1_0 0x88
#define LTR303_DATA_CH1_1 0x89
#define LTR303_DATA_CH0_0 0x8A
#define LTR303_DATA_CH0_1 0x8B
/*
#define LTR303_DATA_CH0_0 0x88
#define LTR303_DATA_CH0_1 0x89
#define LTR303_DATA_CH1_0 0x8A
#define LTR303_DATA_CH1_1 0x8B
*/
#define LTR303_STATUS 0x8C
#define LTR303_INTERRUPT 0x8F
#define LTR303_THRES_UP_0 0x97
Expand Down Expand Up @@ -284,7 +293,7 @@ class LTR303 {
// Returns true (1) if successful, false (0) if there was an I2C error
// (Also see getError() below)

boolean getLux(byte gain, byte integrationTime, unsigned int CH0, unsigned int CH1, double &lux);
boolean getLux(byte gain, unsigned char IntTime, unsigned int CH0, unsigned int CH1, double &lux);
// Convert raw data to lux
// gain: 0 (1X) or 7 (96X), see getControl()
// integrationTime: integration time in ms, from getMeasurementRate()
Expand Down

0 comments on commit c7dfa9d

Please sign in to comment.