# OPT3001 conversion formula / equation?

I am struggling to find a real conversion formula for converting the RAW value into result that makes sense. I am using an OPT3001 sensor from TI and ATTiny25 as MUC.

I have found one example online, but it does not give the correct value in Lux, the equation used there is this:

/*Convert to LUX*/
//extract result & exponent data from raw readings
result = raw&0x0FFF;
exponent = (raw>>12)&0x000F;
lux=0.01*pow(2,eksponent)*result;


This gives wrong values according to my Lux meter that I have as refference and, for example, lux meter says its 420 lux inside Office.. the equation above gives me the result 110 Lux, which is not realistic. Sensor is working fine as I replaced it new one, in order to be 100% that it is not sensor related issue.

The result is very close if I taked only result = raw&0x0FFF; , but then it fails at higher values (when I put my sensor under the Lamp light bulb).

I do not think it's an error inside my I2C code, but I will attach here also the part of my coude for UpdateLight:

// I2C OPT address

// I2C OPT registers
#define OPT_RESULT_REG  0x00
#define OPT_CONFIG_REG  0x01

// OPT config bits - automatic full scale mode, 100 ms conversion, single-shot
#define OPT_CONFIGURATION_H 0xC2
#define OPT_CONFIGURATION_L 0x00

void UpdateLight(void)
{
uint8_t error = 0;
// start conversion
I2C_Start();
error |= I2C_Write(OPT_CONFIG_REG);
error |= I2C_Write(OPT_CONFIGURATION_H);
error |= I2C_Write(OPT_CONFIGURATION_L);
I2C_Stop();

// wait for conversion
_delay_ms(150);

// set result register
I2C_Start();
error |= I2C_Write(OPT_RESULT_REG);
I2C_Stop();

I2C_Start();
if (error == 0)
{
}
else
{
}
I2C_Stop();
}


And this is my old formula that I came up with, but it gives even more ridiculous results than the above one from the internet:

   // calculate light ( LUX = (MANTISA << EXPONENT) / 100 )
debug_printf("L_VAL: %d\r\n", light);


Adding the output lines of the above print statements:

L: 5460 - 824

L: 5464 - 824

L: 5462 - 824

L: 5462 - 824

The first value should be RAW MSB and LSB, and the second is the converted value to Lux, using the formula above this.

Moreover, I am surprised that inside the Datasheet, TI did not provide a conversion formula ?

Any help is greatly appreaciated.

• What is the raw value you are getting? BTW, it looks(by the conversion example) that the representation is actually a standard floating point one.. – Eugene Sh. Aug 17 '15 at 14:40
• @EugeneSh. Raw1: 5463 , Raw2: 5461 taken at Office Light ambient. The equation above gives me result 148.16Lux, which is incorrect. More closer to real result is if I only take result=raw&0x0FFF ... However, this one then fails at higher brightnes (lamp shining above it). Weird.. – David Kasabji Aug 17 '15 at 14:53
• The numbers look suspicious as 5461 in hex is 0x1555 which are alternating zeros and ones. Could it be that you have swapped the SDA and SCL? – Eugene Sh. Aug 17 '15 at 14:55
• @EugeneSh. hm. Could be, but that I need to check in lab again, wasn't expecting that kind of issue, was hopping it was software oriented. here is one raw example under light: 8437 (hex) – David Kasabji Aug 17 '15 at 14:57
• Oh well, if you have some different values, I guess the connection should be fine. – Eugene Sh. Aug 17 '15 at 14:58

On page 20 of the datasheet the detailed description of the calculation is given. From the given example value from the comments 0x5461 we are getting the 0x5 for the exponent value, which is corresponding to 0.32 lux per LSB. The mantissa is 0x461 or 1121 in decimal. The resulting value is 1121*0.32=358.72lux which is close to the expected range.

The working conversion code:

#include <stdio.h>
#include <stdint.h>

int main(void) {
uint16_t raw = 0x5461;
uint16_t result, exponent;
float lux;

result = raw & 0x0FFF;
exponent = (raw>>12) & 0x000F;

lux=0.01*(1<<exponent)*result;

printf("%f\n", lux);
return 0;
}


Or run it here.

Please pay a close attention to the datatypes used, they might have causing trouble to you as well..

• so you think that the value 5461¸and others are not HEX values ? You consider them decimal values ? I thought these were HEX values – David Kasabji Aug 17 '15 at 15:32
• I'm still wondering where that 110 lux are coming from, if I put in the wrong values I get only 27 lux... But yeah endianness is a big trap in data transfer, I guess everyone stumbles upon that. – Arsenal Aug 17 '15 at 15:32
• I don't know, you tell me. How are you reading them? – Eugene Sh. Aug 17 '15 at 15:32
• I have added the part of the code where I convert the raw value and then print it out. – David Kasabji Aug 17 '15 at 15:36
• @AndroidNFC if there is no indication (like hex or 0x or _h) most people will read them as decimal. If you take those values as hex, you end up with 360 lux and not 148 lux as stated in your comment. – Arsenal Aug 17 '15 at 15:36