OPT3001 read not working

Posted on
  • Hello,

    I'm not sure about how to read the OPT3001 sensor's data and i didn't find a lot of documentation. Here my code and the error i have on output.

    My code :

    var i2c = new I2C();
    i2c.setup({sda:B9,scl:B8});
    var opt = require("OPT3001").connectI2C(i2c);
    // default adresse (0x44) and pins are ok, connection works
    
    var data = opt.read();
    console.log(JSON.stringify(data,null,2))­;
    

    Output :

    >new Uint8Array([84, 73])
    21577
    new Uint8Array([48, 1])
    12289
    >Uncaught TypeError: Expecting a number or something iterable, got undefined
     at line 1 col 14
    a.writeTo(e,c);c=a.readFrom(e,2);print(c­);return c[1]|c[0]<<...
                 ^
    in function "r" called from line 1 col 17
    this.r(a.RESULT0)
                    ^
    in function "read" called from line 1 col 21
    var data = opt.read();
                        ^
    > 
    

    The module is right connected and seems ready (led on). Can someone help me ?

  • It looks like a bug in the OPT3001 module, which I have now fixed. If you re-upload the code hopefully you won't get that error any more.

    However it looks like the module itself hasn't been tested, so I'm afraid it may not work for you even it it does run.

    Let us know what happens though - it looks promising from the numbers it is printing, so I may be able to make some tweaks at this end to fix it if it is broken.

  • I looked for the librairie for the OPT3001 module (https://www.espruino.com/modules/OPT3001­.js), and i saw a strange think :

    /* Copyright (c) 2018 Gordon Williams, Pur3 Ltd. See the file LICENSE for copying permission. */
    var REG = {
      RESULT: 0x00,
      CONFIG: 0x01,
      LOWLIMIT: 0x02,
      HIGHLIMIT: 0x03,
      MANUFACTUREID: 0x7E,
      DEVICEID: 0x7F
    };
    function OPT3001(options,r,w) {
      this.r=r;
      this.w=w;
      this.w(REG.CONFIG,0b1100110000010000);
      print(this.r(REG.MANUFACTUREID)); // 0x5449
      print(this.r(REG.DEVICEID));  // 0x3001
    }
    OPT3001.prototype.off = function() {
      this.w(REG.CONFIG,0xc810);
    };
    OPT3001.prototype.read = function() {
      // return this.r(REG.RESULT0); REG.RESULT0 doesn't exit right ?
      return this.r(REG.RESULT);  // better like this ?
    };
    
    exports.OPT3001 = OPT3001;
    
    exports.connectI2C = function(i2c, options) {
      var a = (options&&options.addr)||0x44; // or 0x45?
      return (new OPT3001(options, function(reg) { // read 16 bits
        i2c.writeTo(a, reg);
        var r = i2c.readFrom(a,2);
        print(r);
        return r[1] | r[0]<<8;
      }, function(reg, data) { // write
        i2c.writeTo(a, [reg, data]);
      }));
    };
    

    EDIT : 2 minutes too late =D

  • Ok, so i tried with the following code (the result is the same with your library now) but, in deed, it's seems that it doesn't work. The output is always "0" even when i put some light on the sensor. The output for manufacterID and deviceID seems ok and i tried to change the modul sensor with an other one without more success so i don't think the problem come from the hardware.

    Code :

    var REG = {
      RESULT: 0x00,
      CONFIG: 0x01,
      LOWLIMIT: 0x02,
      HIGHLIMIT: 0x03,
      MANUFACTURERID: 0x7E,
      DEVICEID: 0x7F
    };
    
    var adr = 0x44;
    
    var i2c = new I2C();
    i2c.setup({sda:B9,scl:B8});
    
    // Config (same as the library - maybe don't work)
    i2c.writeTo(adr, [REG.CONFIG, 0b1100110000010000]);
    
    i2c.writeTo(adr, REG.MANUFACTURERID);
    print(i2c.readFrom(adr, 1));
    
    i2c.writeTo(adr, REG.DEVICEID);
    print(i2c.readFrom(adr, 1));
    
    i2c.writeTo(adr, REG.RESULT);
    print(i2c.readFrom(adr, 2));
    

    Output :

    >new Uint8Array([84])
    >new Uint8Array([48])
    >new Uint8Array(2)
    

    I think the output is "uninitialized-like" because the values are "0" and "0". We can verifie that by reading 3 bytes (in place of two) with the following code

    Code :

    i2c.writeTo(adr, REG.RESULT);
    print(i2c.readFrom(adr, 3));
    

    Output :

    >new Uint8Array([0, 0, 255])
    

    There is maybe a problem with the configuration. I'm sorry, i don't have time now to more investigations but I'll let you know if I find something in the next few days.

    If it helps, here is the result of reading of all the registers

    Code :

    i2c.writeTo(adr, REG.RESULT);
    print(i2c.readFrom(adr, 2));
    
    i2c.writeTo(adr, REG.CONFIG);
    print(i2c.readFrom(adr, 2));
    
    i2c.writeTo(adr, REG.LOWLIMIT);
    print(i2c.readFrom(adr, 2));
    
    i2c.writeTo(adr, REG.HIGHLIMIT);
    print(i2c.readFrom(adr, 2));
    
    i2c.writeTo(adr, REG.MANUFACTURERID);
    print(i2c.readFrom(adr, 2));
    
    i2c.writeTo(adr, REG.DEVICEID);
    print(i2c.readFrom(adr, 2));
    

    Output :

    >new Uint8Array(2) // means "0"
    >new Uint8Array([200, 16])
    >new Uint8Array(2) // means "0"
    >new Uint8Array([191, 255])
    >new Uint8Array([84, 73])
    >new Uint8Array([48, 1])
    

    In addition, if i look in the OPT data sheet, the result value is, in deed, on 16 bits but divided as follow :
    12 bit (LSB) : real value data
    4 bit (MSB) : exposent

    Sadly, your library doesn't take care of that and just return the 16 bits value without treatment. It would be nice to have direct the value ready to be read :)

    datasheet i found : http://www.ti.com/lit/ds/symlink/opt3001­.pdf

  • Hi, Looks like the module itself is definitely not good then! Thanks for poking around.

    This won't work, because it's just writing 8 bits:

    i2c.writeTo(adr, [REG.CONFIG, 0b1100110000010000]);
    

    Try

    i2c.writeTo(adr, [REG.CONFIG, 0b11001100, 0b00010000]);
    

    It might work after that?

    Getting the result data should be easy enough, something like:

    var lux =  0.01 * ((d&0x0FFF) << (d>>12))
    

    might work?

  • You might want to try pull-up resistors or set the pins to pull up in the module?

  • Pull-ups are not the problem, in this case. If this was the case, a timeout would occur on I2C (both if pull-ups are missing or if the slave did not respond) and would therefore be notified in the console

  • I think the issue is the fact I wasn't handling 16 bit writes correctly. I have just uploaded some new code - can you try now and see if it's better?

  • It works! (tested by my colleague)

  • \o/ great news!

  • I confirm : It's working, thank for support !

  • Post a reply
    • Bold
    • Italics
    • Link
    • Image
    • List
    • Quote
    • code
    • Preview
About

OPT3001 read not working

Posted by Avatar for Sestre @Sestre

Actions