Wednesday, July 27, 2016

Mailbag - BH1750FVI - I2C Light Sensor Driver




//YOUTUBE VIDEO WILL FOLLOW HERE //

   In the process of choosing of some Light Sensors for one of the new projects that will involve the nEXT EVO Board and also the MPDMv4 Mains Dimmer  somebody suggested to give a try also to the ROHM BH1750FVI Sensor.

Just received some BH1750FVI modules in my mail yesterday and must say that it's a pretty impressive little board for only about 2.5USD, even less in a pack of 10!

This is how are looking the received modules:

BH1750FVI Module - TOP

   As you can see on the picture above, on the tiny board, you have the BH1750FVI Sensor, a LDO regulator some filtering caps and pull-up resistors and that's it!



 
BH1750FVI Module - Bottom
     The bottom side is bringing us more good news, the Module interface connector is fully compatible with the nEXT EVO  Bus!



BH1750FVI Overview

  BH1750FVI is an digital Ambient Light Sensor IC for I2C bus interface. This IC is the mostly used to obtain the ambient light data for adjusting LCD and Keypad backlight power of Mobile phone. 
 This is great as we are looking forward to use it for Automatic light adjustment using the MPDMv4 AC Dimmer board :)





Features

  • I2C bus Interface ( f / s Mode Support )
  • Spectral responsibility is approximately human eye response
  • Illuminance to Digital Converter
  • Wide range and High resolution. ( 1 - 65535 lx )
  • Low Current by power down function
  • 50Hz / 60Hz Light noise reject-function
  • 1.8V Logic input interface
  • Native 3.3V Device
  • No need any external parts
  • Light source dependency is little. ( ex. Incandescent Lamp. Fluorescent Lamp. Halogen Lamp. White LED. Sun Light )
  • It is possible to select 2 type of I2C slave-address.
  • Adjustable measurement result for influence of optical window ( It is possible to detect min. 0.11 lx, max. 100000 lx by using this function. )
  • Small measurement variation (+/- 20%)
  • The influence of infrared is very small.


Internal Diagram
BH1750FVI - Internal Diagram

  • PD - Photo diode with approximately human eye response.
  • AMP - Integration-OPAMP for converting from PD current to Voltage.
  • ADC - AD converter for obtainment Digital 16bit data.
  • Logic + I2C Interface :
                 - Ambient Light Calculation and I2C BUS Interface. It is including below register.
                 - Data Register - This is for registration of Ambient Light Data. Initial Value is
                       "0000_0000_0000_0000".
                 - Measurement Time Register - This is for registration of measurement time. Initial Value
                          is  "0100_0101".
  • OSC - Internal Oscillator ( typ. 320kHz ). It is CLK for internal logic.

For more details please take a look at the BH1750FVI Datasheet.



What we will need:

    Connection with the ESP8266 nEXT EVO Board is very easy, as MPU6050 GY-521 Module connector is fully compatible with the nEXT Bus connector. Depending on how to you choose you socket type, you can install it on TOP or Bottom of the ESP8266 nEXT EVO Board :


BH1750FVI Module directly connected to the ESP8266 nEXT EVO Board







BH1750FVI Driver implementation


1. Init I2C bus/interface


 Standard I2C Bus Initialisation function:

function init_I2C()
    i2c.setup(bus, sda, scl, i2c.SLOW)
end



2. WRITE Function - Set BH1750FVI Register config

      From BH1750FVI Datasheet :
   

  • No active state : pwr_down 0x00 
  • Wating for measurment command : pwr_on=0x01 
  • Reset data register value - not accepted in POWER_DOWN mode : reset=0x07

    Continuous modes, no pwr down:
  •  Start measurement at 1lx resolution. Measurement time is approx 120ms:
            hi_res_m1=0x10 
  • Start measurement at 0.5lx resolution. Measurement time is approx 120ms. 
           hi_res_m2=0x11
  • Start measurement at 4lx resolution. Measurement time is approx 16ms.
           lo_res_m1=0x13


    Device is automatically set to Power Down after measurement:
  • Start measurement at 1lx resolution. Measurement time is approx 120ms.
          hi_res_m3=0x20
  • Start measurement at 0.5lx resolution. Measurement time is approx 120ms.
           hi_res_m4=0x21
  • Start measurement at 1lx resolution. Measurement time is approx 120ms.
          lo_res_m2=0x23
 

function setcfg(cfg) 
    i2c.start(id)
    i2c.address(id, dev_addr ,i2c.TRANSMITTER)
    i2c.write(id,cfg)
    i2c.stop(id)
end


3. READ Sensor Input Function


Measurement Procedure

function read_input(dev_addr)
      i2c.start(id)
      i2c.address(id, dev_addr,i2c.RECEIVER)
      c = i2c.read(id,2)
      i2c.stop(id)
      --print("RAW H: "..string.byte(c,1))
      --print("RAW L: "..string.byte(c,2))
      rawl = (bit.lshift(string.byte(c, 1), 8) + string.byte(c, 2))
      lux = rawl/1.2
      print(string.format("\nLight Level: %0.2f lux",lux))
      --print(string.format("             %d raw",rawl))
      return rawl
end


4. MAIN PROGRAM


id = 0                    --I2C Bus ID
sda=2                     --GPIO4
scl=1                      --GPIO5
dev_addr = 0x23   --BH1750FVI I2C Address
i2c_init()                          --init I2C Bus
setcfg(hi_res_m1)            --SET config register
read_input(dev_addr)     --Read sensor data and compute LUX Light level value





No comments:

Post a Comment