Grovepi+ and digital light sensor

Hello there,

A few days ago I have received my Grovepi+, which is connected to a Raspberry pi, model B+. The sensor is connected to I2C-2.

When I run the included Python script named grove_i2c_digital_light_sensor.py, it looks likes it s reporting random values.
Debug is off and see below for the results:

Can someone tell me what I do wrong?

sudo python grove_i2c_digital_light_sensor.py
Power ON
There is light:
ambient = 41549
IR = 6936
_ambient = 41549
_IR = 6936
Light = 1107.79945921 lux.
Power OFF
There is light:
ambient = 14413
IR = 56087
_ambient = 14413
_IR = 56087
Light = 0 lux.
Power OFF
There is light:
ambient = 59468
IR = 42519
_ambient = 59468
_IR = 42519
Light = 168.3056 lux.
Power OFF
There is light:
ambient = 24396
IR = 26391
_ambient = 24396
_IR = 26391
Light = 13.84188 lux.
Power OFF
There is light:
ambient = 16204
IR = 21015
_ambient = 16204
_IR = 21015
Light = 0.13052 lux.
Power OFF
There is light:
ambient = 37196
IR = 27927
_ambient = 37196
_IR = 27927
Light = 81.2912 lux.
Power OFF
There is light:
ambient = 9293
IR = 43031
_ambient = 9293
_IR = 43031
Light = 0 lux.
Power OFF
There is light:
ambient = 29005
IR = 54551
_ambient = 29005
_IR = 54551
Light = 0 lux.
Power OFF

Regards,

Lex Dane

Hey,
The I2C light sensor is different from a normal light sensor. Can you just double check that you have an I2C light sensor or just a normal one. I think you must be having a normal one and you can use analog read to use it: https://github.com/DexterInd/GrovePi/blob/master/Software/Python/grove_analog_read.py .

Karan

See the attached picture to confirm that it is a Digital light sensor

Hey,
Looks like you are indeed using the I2C sensor rather than the normal digital light sensor. Currently GrovePi does not support it because we don’t have a library for it and this is the first time we have heard about that sensor too.

The arduino library is here: https://github.com/Seeed-Studio/Grove_Digital_Light_Sensor and it should be not too difficult to port it. Do let us know if you want to port the library. We would be happy to help you out.

-Karan

You can tell it’s an I2C sensor using two methods:

Check the pin labels - look for “SCL,SDA,VCC,GND”

SIG,NC,VCC,GND is a digital or analog module. (NC=not connected)
SCL,SDA,VCC,GND is an I2C module.
TX,RX,VCC,GND is a serial module.
DCKI,DI,VCC,GND is a digital module

Run the i2ctools command:
i2cdetect -y 1
if you have an older pi, replace 1 with 0 for the older bus
i2cdetect -y 0

Dear all,
I try to modify the GrovePi firmware in order to be able to read Lux value from this sensor. My trouble is how to send back value from GrovePi to the RaspberryPi. I found the example with the float for the DHT sensor.
The returned value is a long => 4 bytes. Then I was planned to use the code bellow in the GrovePi firmware:

signed long luxValue= TSL2561.readVisibleLux();     
 byte *b1=(byte*)&luxValue;
 for(j=0;j<4;j++)
 {
   b[j+1]=b1[j];
 }

But how to construct back in the python code in the raspberry pi.

Thank you in advance for any advice.

Hey,
The Digital light sensor is an IC2C sensor and can be directly accessed from the Raspberry PI by writing a Python program. You don;t need to modify the firmware and can directly access the sensor.

There is an example for the sensor on out github repo here: https://github.com/DexterInd/GrovePi/blob/master/Software/Python/grove_i2c_digital_light_sensor/grove_i2c_digital_light_sensor.py. Can you try that out and let us know if that works for you.

-Karan

Dear Karan,
Thank you for your answer. I know the digital light sensor could be dirctly interface from the raspberry pi. But in my project it would be better for me to integrate inside the grovepi firmware. By the way I found the solution.

  1. Populate b[1:5]
byte *b1=(byte*)&luxValue;
 for(j=0;j<4;j++)
 {
   b[j+1]=b1[j];
 }

  1. Add en entry for your command in void sendData() as for exemple :
if(cmd[0] == 22 || cmd[0] == 24)
Wire.write(b, 5);

  1. In python :
    'write_i2c_block(address, TSL2561_getLux_cmd + [unused, unused, unused])
    try:
    time.sleep(2)
    read_i2c_byte(address)
    number = read_i2c_block(address)
    if number == -1:
    return [-1,-1]
    except (TypeError, IndexError):
    return [-1,-1]
    f=0
    for element in reversed(number[1:5]):
    #print "element = " + str(element)
    # Converted to hex
    hex_val = hex(element)
    print “element =” + hex_val
    #print hex_val
    try:
    h_val = hex_val[2] + hex_val[3]
    except IndexError:
    h_val = ‘0’ + hex_val[2]
    # Convert to char array
    if f == 0:
    h = h_val
    f = 1
    else:
    h = h + h_val

    convert the temp back to float

    t = struct.unpack(’!l’, h.decode(‘hex’))[0]
    `