BH1750FVI I2C Digital Light Intensity Sensor

3

TheBH1750FVI device is a digital light intensity sensor which uses the I2C interface. This allows it to be connected to the Raspberry Pi with only four wires.

The module allows quick and cheap ambient light level measurement and the light level can be read from it as a digital number due to the built in 16-bit analogue-to-digital converter. The device itself is commonly used in mobile phones, LCD TVs and digital cameras.

The module I’ve got measures only 32 x 16mm. I soldered a five pin header onto the PCB and this allowed me to plug it onto a piece of breadboard.

BH1750 Light Sensor Module

The BH1750 Ambient Light Sensor IC datasheet provides all the technical details some of which are used in my example Python script.

Configure I2C Interface

In order to use this module you must enable the I2C interface on the Raspberry Pi as it is not enabled by default. This is a fairly easy process and is described in my Enabling The I2C Interface On The Raspberry Pi tutorial.

Connecting Hardware

The following table shows how I connected the module pins on the PCB to the Pi’s GPIO header (P1). Please refer to my GPIO header guide for a diagram.

Module PCB Desc GPIO Header Pins
GND Ground P1-06
ADD Address select P1-06
SDA I2C SDA P1-03
SCL I2C SCL P1-05
VCC 3.3V P1-01

Here is a diagram of a breadboard setup. If you are connecting the module’s five pins directly to the Pi you only need five female-female wires.

BH1750 Module Setup

This Fritzing diagram uses a custom part I created for my module. Other modules may have a different pin arrangement so make sure you are connecting the correct pins to the Pi if yours is slightly different.

With the device connected and the Pi powered up the “i2cdetect” command should show the device with address 0x23.

Example Python Code

Here is a simple Python script to read the light level from the module :

#!/usr/bin/python
import smbus
import time

# Define some constants from the datasheet

DEVICE     = 0x23 # Default device I2C address

POWER_DOWN = 0x00 # No active state
POWER_ON   = 0x01 # Power on
RESET      = 0x07 # Reset data register value

# Start measurement at 4lx resolution. Time typically 16ms.
CONTINUOUS_LOW_RES_MODE = 0x13
# Start measurement at 1lx resolution. Time typically 120ms
CONTINUOUS_HIGH_RES_MODE_1 = 0x10
# Start measurement at 0.5lx resolution. Time typically 120ms
CONTINUOUS_HIGH_RES_MODE_2 = 0x11
# Start measurement at 1lx resolution. Time typically 120ms
# Device is automatically set to Power Down after measurement.
ONE_TIME_HIGH_RES_MODE_1 = 0x20
# Start measurement at 0.5lx resolution. Time typically 120ms
# Device is automatically set to Power Down after measurement.
ONE_TIME_HIGH_RES_MODE_2 = 0x21
# Start measurement at 1lx resolution. Time typically 120ms
# Device is automatically set to Power Down after measurement.
ONE_TIME_LOW_RES_MODE = 0x23

#bus = smbus.SMBus(0) # Rev 1 Pi uses 0
bus = smbus.SMBus(1)  # Rev 2 Pi uses 1

def convertToNumber(data):
  # Simple function to convert 2 bytes of data
  # into a decimal number
  return ((data[1] + (256 * data[0])) / 1.2)

def readLight(addr=DEVICE):
  data = bus.read_i2c_block_data(addr,ONE_TIME_HIGH_RES_MODE_1)
  return convertToNumber(data)

def main():

  while True:
    print "Light Level : " + str(readLight()) + " lx"
    time.sleep(0.5)
  
if __name__=="__main__":
   main()

If you want download this script directly to your Pi you can use the following command :

wget https://bitbucket.org/MattHawkinsUK/rpispy-misc/raw/master/python/bh1750.py

or use this link in a browser.

In order to run it you can use :

sudo python bh1750.py

The output should look something like :

BH1750 Example Output

The while loop keeps taking readings every 200ms until you press CTRL-C.

Script Notes :

  • The import statements import Python libraries used in the rest of the code including smbus which handles the I2C interface.
  • All I2C devices must have an address. In this example I tied the ADDR pin to ground so the address used by the device is 0x23. If the ADDR pin is tied to 3.3V the address is 0x5C.
  • The block of constants are listed in the datasheet and define the different modes the device can operate in. I define all of them but only use “ONE_TIME_HIGH_RES_MODE_1”. The other modes are only of any real interest if you need to take high speed readings.
  • The “smbus.SMBus(1)” function setups the I2C interface.
  • The “read_i2c_block_data” function is used to read 2 bytes of data from the device using the ONE_TIME_HIGH_RES_MODE_1.
  • The convertToNumber function is then used to convert those 2 bytes of data into a number. The 1.2 value in the calculation is defined in the datasheet.

The module is available from eBay, Amazon and many other online electronics shops.

Share.

3 Comments

  1. Great tutorial, works fine, I was wondering if you can mash this up with your BMP180 Pressure Sensor to work and log together.

    Many thanks

    Derek

    • Unfortunately you can’t change the I2C pins. But if your screen is an I2C device you can add multiple I2C devices to the same pins if they have different internal addresses.

Leave A Reply