BH1750FVI sample code available?


Closed Thread
Results 1 to 15 of 15
  1. #1
    Join Date
    Feb 2013
    Posts
    1,078

    Default BH1750FVI sample code available?

    Hello.

    I want to use this module with picbasic pro to read light intensity. Datasheet lists ideas how should this IC should be treated, however, it is quite hard to understand, since I never used I2C devices before. And reference manual lists only simple I2C code, without showing how to use these square and figure data. Nor samples included with compiler provide any useful I2C usage clues.

    As I understand from datasheet, I need to write some value into device, then wait some time, and then read measured light amount. But how exactly - remains unclear. There's picaxe tread on this, but their syntax appears to be completely different:

    http://www.picaxeforum.co.uk/showthr...-with-a-Picaxe

  2. #2
    Join Date
    Feb 2013
    Posts
    1,078


    Did you find this post helpful? Yes | No

    Default Re: criteria percussionist ugg boots shop

    For example, i2cmast.pbp exempt:

    Code:
    mainloop:
       I2CWrite SDA,SCL,$02,[127], bogus    ' Write offset to slave
       Pause 500               
    
       I2CRead SDA,SCL,$02,[STR a\8], bogus ' Read string from slave
          
       LCDOut $fe, 1, STR a\5,SDEC a[5]     ' Display result, preceded by 5 character
                                            ' string received from slave.
       Pause 500
       GoTo mainloop                        ' Do it forever
    
    bogus:
       LCDOut $fe,1, "timed out"            ' I2C command timed out
       Pause 1000
       GoTo mainloop
    
       End
    what is "bogus" here and what it does?

  3. #3
    Join Date
    Feb 2013
    Posts
    1,078


    Did you find this post helpful? Yes | No

    Default Re: criteria percussionist ugg boots shop

    OK, got clear with "bogus" but still no idea how to interpret datasheet to I2CWRITE/READ .

  4. #4
    Join Date
    Feb 2013
    Posts
    1,078


    Did you find this post helpful? Yes | No

    Default Re: BH1750FVI sample code available?

    8 years passed and now I'm giving it 2nd try, but it still does not works (returns 58317 constantly). Here's code:

    Code:
    ldta  var porta.6 
    lclk  var porta.3        
    
    
    x var word
    adr var byte
    adr=23
    
    
    i2cwrite ldta, lclk, adr, %0100011
    pause 200
    i2cwrite ldta, lclk, adr, %00010001
    pause 200
    maik:
    i2cread ldta, lclk, adr, %0100011, x 
    pause 200
    lcdout $fe, $1, dec x
    pause 200
    goto maik
    Here's the module https://www.addicore.com/BH1750FVI-p/ad290.htm
    With arduino it works fine, tested.

    Here's screenshot from datasheet, based on which I wrote the above code.

    Name:  datasheet.jpg
Views: 547
Size:  240.3 KB

  5. #5
    Join Date
    May 2013
    Location
    australia
    Posts
    2,383


    Did you find this post helpful? Yes | No

    Default Re: BH1750FVI sample code available?

    each and every i2c command has incorrect syntax
    Warning I'm not a teacher

  6. #6
    Join Date
    Feb 2013
    Posts
    1,078


    Did you find this post helpful? Yes | No

    Default Re: BH1750FVI sample code available?

    Well, I tried to use what I already have working for DS3231
    Manual is not very clear about I2C....

  7. #7
    Join Date
    Feb 2013
    Posts
    1,078


    Did you find this post helpful? Yes | No

    Default Re: BH1750FVI sample code available?

    Changed syntax, still getting zeroes, what I'm doing wrong?

    Code:
    ldta  var porta.6 
    lclk  var porta.3        
    
    x var word
    y var byte
    z var byte
    cnt var byte 'control word
    adr var byte
    sda var portc.0
    scl var portc.1
    lcdout $fe, $01, " "
    adr=$23
    x=0
    
    cnt=%0100011
    i2cwrite ldta, lclk, cnt, adr
    pause 200
    cnt=%00010001
    i2cwrite ldta, lclk, cnt, adr 
    pause 200
    maik:
    cnt=%0100011
    i2cread ldta, lclk, cnt, adr, x 
    pause 200
    lcdout $fe, $1, dec x
    pause 200
    goto maik

  8. #8
    Join Date
    Nov 2005
    Location
    Bombay, India
    Posts
    947


    Did you find this post helpful? Yes | No

    Default Re: BH1750FVI sample code available?

    1 - use the appropriate I2C address for the device based on the hardware pin state
    2) Slave Address
    Slave Address is 2 types, it is determined by ADDR Terminal
    ADDR = ‘H’ ( ADDR ≧ 0.7VCC ) → “1011100“
    ADDR = 'L' ( ADDR ≦ 0.3VCC ) → “0100011“


    2 - the device address for case 1 is $B8 and case 2 is $46 (use one of these values in cnt)
    When you put the address as shown in your code, the leading bit is 0 and the address will be $5c or $23. This is definitely wrong.

    3 - cnt remains the same for I2CWrite and I2CRead. the least significant bit is changed internally by the compiler depending on whether the command is a read or write

  9. #9
    Join Date
    Feb 2013
    Posts
    1,078


    Did you find this post helpful? Yes | No

    Default Re: BH1750FVI sample code available?

    Thanks!
    Tried both, none works, so I guess, I'm missing something else too...

  10. #10
    Join Date
    Nov 2005
    Location
    Bombay, India
    Posts
    947


    Did you find this post helpful? Yes | No

    Default Re: BH1750FVI sample code available?

    You ought to be sending this way

    Code:
    Assuming ADDR pin is low
    
        i2cwrite ldta, lclk, $46, adr,  dat
    
    and read by
    
        i2cread ldat, lclk, $46, adr, dat
    make sure the adr and dat match with the register of the device and dat relevant to the register. It should work.

  11. #11
    Join Date
    May 2013
    Location
    australia
    Posts
    2,383


    1 out of 1 members found this post helpful. Did you find this post helpful? Yes | No

    Default Re: BH1750FVI sample code available?

    You ought to be sending this way
    not in my view
    addr var byte
    addr= $46
    i2cwrite sda,sck,addr,[opecode]
    i2cread sda,sck,addr,[dat.highbyte,dat.lowbyte]
    Last edited by richard; - 17th May 2022 at 03:56. Reason: got rid of ridiculous alias names
    Warning I'm not a teacher

  12. #12
    Join Date
    Feb 2013
    Posts
    1,078


    1 out of 1 members found this post helpful. Did you find this post helpful? Yes | No

    Default Re: BH1750FVI sample code available?

    Big thanks Richard! now it works!

    here's the code:
    Code:
    adr=$46
    x=0
    cnt=%0100011
    i2cwrite ldta, lclk, adr, cnt
    pause 200
    cnt=%00010001
    i2cwrite ldta, lclk, adr, cnt 
    pause 200
    maik:
    cnt=%00010001
    i2cwrite ldta, lclk, adr, cnt 
    pause 200
    cnt=%0100011
    i2cread ldta, lclk, adr, [x.highbyte, x.lowbyte] 
    pause 200
    lcdout $fe, $1, dec x
    pause 200
    goto maik

  13. #13
    Join Date
    May 2013
    Location
    australia
    Posts
    2,383


    Did you find this post helpful? Yes | No

    Default Re: BH1750FVI sample code available?

    Big thanks Richard! now it works!
    as maybe but you are still incorrect , if you use a logic analyzer you will see the transaction ends abnormally



    wrong way
    i2cwrite ldta, lclk, adr, cnt
    correct way
    i2cwrite ldta, lclk, adr, [ cnt ]
    Warning I'm not a teacher

  14. #14
    Join Date
    Feb 2013
    Posts
    1,078


    Did you find this post helpful? Yes | No

    Default Re: BH1750FVI sample code available?

    Well, just added these square brackets - see no difference in readings.

  15. #15
    Join Date
    May 2013
    Location
    australia
    Posts
    2,383


    Did you find this post helpful? Yes | No

    Default Re: BH1750FVI sample code available?

    its best to be correct for future reference , not all i2c devices are so forgiving
    Warning I'm not a teacher

Similar Threads

  1. please who can help me for sample code
    By jasem700 in forum mel PIC BASIC Pro
    Replies: 6
    Last Post: - 22nd February 2009, 21:41
  2. USBJADEM sample code
    By SterlingY in forum mel PIC BASIC Pro
    Replies: 2
    Last Post: - 14th March 2007, 01:57
  3. sample code for M25P32
    By Pedro Santos in forum mel PIC BASIC Pro
    Replies: 1
    Last Post: - 9th January 2007, 03:37
  4. Sample code for pwm
    By Md.Shah in forum mel PIC BASIC
    Replies: 1
    Last Post: - 10th October 2006, 17:59
  5. 12F675 code sample
    By marad73 in forum mel PIC BASIC Pro
    Replies: 9
    Last Post: - 23rd May 2006, 14:53

Members who have read this thread : 2

You do not have permission to view the list of names.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts