Anyone used a BH1750BVI light sensor with a Picaxe?

Jeremy Harris

Senior Member
I recently bought a couple of these sensors, mounted on interface boards with 3.3V regulators, level shifters and 4k7 pull-ups. I assumed they'd be fairly easy to hook up to a Picaxe, but it seems that it isn't as easy as I'd thought. The datasheet isn't great (it's sort of written in Chinglish - see here: http://www.mouser.com/ds/2/348/bh1750fvi-e-186247.pdf ).

AFAICS, using this should be pretty straightforward, just send the right command to put the device into the correct mode, then read the light value register as a pair of bytes, hi byte first.

If only. I've spent an hour or so trying to read data out of this device to no avail. The only clue I have to what might, just possibly, be the problem is that the LSB of the address (the eighth bit) sets read or write, and this seems (unless I've misunderstood the datasheet - quite possible) to be the wrong way around. I've always assumed (and would guess that HI2CIN and HI2COUT use) this eighth bit in the sense that 1 = write, 0 = read, yet the datasheet seems to suggest that this chip uses 0 = write, 1 = read (assuming I've read it correctly).

If anyone has used on of these sensors with a Picaxe and got it working I'd appreciate some insight into driving it. I'm about to revert to bit bashing it if I can't get the HI2C commands to work (BTW, I have set it up correctly, right address, slow I2C, byte size instructions, etc).
 

rq3

Senior Member
I recently bought a couple of these sensors, mounted on interface boards with 3.3V regulators, level shifters and 4k7 pull-ups. I assumed they'd be fairly easy to hook up to a Picaxe, but it seems that it isn't as easy as I'd thought. The datasheet isn't great (it's sort of written in Chinglish - see here: http://www.mouser.com/ds/2/348/bh1750fvi-e-186247.pdf ).

AFAICS, using this should be pretty straightforward, just send the right command to put the device into the correct mode, then read the light value register as a pair of bytes, hi byte first.

If only. I've spent an hour or so trying to read data out of this device to no avail. The only clue I have to what might, just possibly, be the problem is that the LSB of the address (the eighth bit) sets read or write, and this seems (unless I've misunderstood the datasheet - quite possible) to be the wrong way around. I've always assumed (and would guess that HI2CIN and HI2COUT use) this eighth bit in the sense that 1 = write, 0 = read, yet the datasheet seems to suggest that this chip uses 0 = write, 1 = read (assuming I've read it correctly).

If anyone has used on of these sensors with a Picaxe and got it working I'd appreciate some insight into driving it. I'm about to revert to bit bashing it if I can't get the HI2C commands to work (BTW, I have set it up correctly, right address, slow I2C, byte size instructions, etc).
I can see a few potential gotchas. The address of the device depends on the logic state of the address pin. The DVI pin has to be cycled after power up to clear the registers. And the fastest data output is 16 msec per reading, and that's with a 400KHz I2C clock.
 

Jeremy Harris

Senior Member
Thanks, I've managed to sort of reverse engineer the module and it looks like there is a pull down on the address pin, so with no connection it defaults to the low address. The DVI pin seems to have an RC delay on it (as described as an option on the datasheet from what I can work out) that should mean it is delayed enough to allow a proper power-on reset (but I will try looking at this with a 'scope tomorrow to make sure the pin is really going high after an adequate delay).

I've been trying to use it in the high resolution mode, as recommended, which seems to mean sending a "continuous high resolution" command, then reading the high byte and then the low byte. Unfortunately this doesn't seem to work, and all I'm reading back is zero. I allowed 1 second between reads to ensure that the device had time to do its thing, so hopefully I'm allowing plenty of time between readings.

The chip looks useful if I can get it to work, as it gives an output in lux directly, with acceptable accuracy and with a response that looks to be a better match to that of the human eye than many photodiode sensors. I just wish the datasheet was a little easier to understand!

I'm semi-convinced that the problem may be that the eighth bit function of the I2C address is swapped relative to the way the Picaxe commands work, as at first glance that seems that it might be the case. If HI2CIN sends the address plus the eighth bit as zero, when the chip expects this to be a one to read the register, then that might be the problem. If it is then it seems very odd, which makes me wonder whether or not I've misinterpreted things.
 

DamonHD

Senior Member
Is there any confusion from the fact that some of these protocols send MSB first and some LSB first, and in some such as the DS1306 RTC this becomes evident if bitbanging since you can run the chip in either of two serial modes requiring s/w to take account of this?

Rgds

Damon
 

Jeremy Harris

Senior Member
Good point, Damon, yes it might well b that's the case. I'll try bit-banging it later to see if I can get some sort of response that may help work out why it seems reluctant to work.

I've been frustrated by I2C in the past, as despite there being a notional standard it seems that some devices like to go off piste and use something outside the normal protocol. This might be because it's an old standard originally only intended for inter-chip communications in things like old TV sets (I think Phillips originally came up with it to interface Teletext decoders to the semi-analogue dedicated TV chips in the early 80's!).
 

Jeremy Harris

Senior Member
OK, problem fixed - entirely finger trouble on my part from misunderstanding the data sheet!

For anyone who wants to use one of these in future to read the light level directly in lux, as a reasonably well-calibrated figure (given the logarithmic scale), then all you need to do is simple set up the i2c device address as either %01000110 for "ADD" held low (default on the cheap ebay BY-30 module) or %1011100 for "ADD" held high. Setting up the high resolution mode for continuous measurement is then just HI2COUT (%00010000). To read the 16 bit word value (in lux) then just use HI2CIN (b1, b0) and the value in lux will be in w0.

Seems really simple - my problem was misreading the data sheet and assuming that more commands needed to be sent.......................

These modules would make a good basis for a light meter project, especially as they are good match to the human eye response and so read light levels in about the way we perceive them. Being fairly well calibrated makes them even more useful. Search ebay for 1750 light sensor and you'll find several very cheap ones for sale. Having the 5V to 3.3V regulator and circuitry on board makes them even more useful.
 

Puuhaaja

Senior Member
That's timely threasd for me because few weeks ago i ordered some bh 1750 chips from ebay.
I don't know anythin about code, but when connected to pin C.4
HI2COUT (%00010000)
HI2CIN (b1, b0)
Should that store value to w0?
 

Jeremy Harris

Senior Member
That's timely threasd for me because few weeks ago i ordered some bh 1750 chips from ebay.
I don't know anythin about code, but when connected to pin C.4


Should that store value to w0?
Yes, it does store the light value in w0. The chip connects to the SDA and SCL lines on whichever Picaxe you're using.
 
Top