PDA

View Full Version : BH1750FVI sample code available?



CuriousOne
- 6th January 2014, 15:10
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/showthread.php?23436-Anyone-used-a-BH1750BVI-light-sensor-with-a-Picaxe

CuriousOne
- 6th January 2014, 15:42
For example, i2cmast.pbp exempt:



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?

CuriousOne
- 6th January 2014, 15:55
OK, got clear with "bogus" but still no idea how to interpret datasheet to I2CWRITE/READ .

CuriousOne
- 11th May 2022, 06:26
8 years passed and now I'm giving it 2nd try, but it still does not works (returns 58317 constantly). Here's 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.

9221

richard
- 11th May 2022, 06:58
each and every i2c command has incorrect syntax

CuriousOne
- 11th May 2022, 07:17
Well, I tried to use what I already have working for DS3231 :)
Manual is not very clear about I2C....

CuriousOne
- 15th May 2022, 16:49
Changed syntax, still getting zeroes, what I'm doing wrong?



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

Jerson
- 16th May 2022, 02:54
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

CuriousOne
- 16th May 2022, 06:09
Thanks!
Tried both, none works, so I guess, I'm missing something else too...

Jerson
- 16th May 2022, 06:36
You ought to be sending this way



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.

richard
- 17th May 2022, 02:52
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]

CuriousOne
- 17th May 2022, 06:57
Big thanks Richard! now it works!

here's the 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

richard
- 17th May 2022, 07:32
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 ]

CuriousOne
- 17th May 2022, 07:36
Well, just added these square brackets - see no difference in readings.

richard
- 17th May 2022, 07:38
its best to be correct for future reference , not all i2c devices are so forgiving