Introduction: Light Intensity Measurement by Using BH1715 and Raspberry Pi

About: We are a group of makers. We work in IoT, IOS app, android app, embedded design, sensor design, raspberry pi, arduino, beaglebone, particle electron, particle photon, Bluetooth.

Yesterday we were working on LCD displays, and while working over them we realized the importance of light intensity computation. Light intensity is not only important in the physical domain of this world but it has its well-said role in the biological domain too. Accurate estimation of light intensity plays a pivotal role in our ecosystem, in the growth of plants, etc. So, for serving this purpose we studied this sensor BH1715, which is a 16-bit serial output type ambient light sensor.

In this tutorial, we are going to demonstrate the working of BH1715 with Raspberry pi, using Java as the programming language.

Hardware that you are going to need for this purpose are as follows:

1. BH1715 - Ambient Light Sensor

2. Raspberryy Pi

3. I2C Cable

4. I2C Shield For Raspberry Pi

5. Ethernet Cable

Step 1: BH1715 Overview:

First of all, we would like to familiarize you with the basic features of the sensor module that is BH1715 and the communication protocol on which it works.

BH1715 is a digital Ambient Light Sensor with an I²C bus interface. The BH1715 is commonly used to obtain the ambient light data for adjusting LCD and Keypad backlight power for mobile devices. This device offers a 16-bit resolution and an adjustable measurement range, allowing detection from .23 to 100,000 lux.

The communication protocol on which the sensor works is I2C. I2C stands for the inter-integrated circuit. It is a communication protocol in which the communication takes place through SDA(serial data) and SCL(serial clock) lines. It allows connecting multiple devices at the same time. It is one of the simplest and most efficient communication protocol.

Step 2: What You Need..!!

The materials that we need for accomplishing our goal includes the following hardware components:

1. BH1715 - Ambient Light Sensor

2. Raspberry Pi

3. I2C Cable

4. I2C Shield For Raspberry Pi

5. Ethernet cable

Step 3: Hardware Hookup:

The hardware hookup section basically explains the wiring connections required between the sensor and the raspberry pi. Ensuring correct connections is the basic necessity while working on any system for the desired output. So, the requisite connections are as follows:

The BH1715 will work over I2C . Here is the example wiring diagram, demonstrating how to wire up each interface of the sensor.

Out-of-the-box, the board is configured for an I2C interface, as such we recommend using this hookup if you’re otherwise agnostic. All you need is four wires!

Only four connections are required Vcc, Gnd, SCL and SDA pins and these are connected with the help of I2C cable.

These connections are demonstrated in the pictures above.

Step 4: Light Intensity Measurement Using Java Code:

The advantage of using raspberry pi is, that provides you the flexibility of the programming language in which you want to program the board in order to interface the sensor with it. Harnessing this advantage of this board, we are demonstrating here it's programming in Java. The Java code for BH1715 can be downloaded from our GitHub community that is Dcube Store.

As well as for the ease of the users, we are explaining the code here also:

As the first step of coding, you need to download the pi4j library in case of java, because this library supports the functions used in the code. So, to download the library you can visit the following link:

http://pi4j.com/install.html

You can copy the working java code for this sensor from here also:

<p>// Distributed with a free-will license.</p><p>// Use it any way you want, profit or free, provided it fits in the licenses of its associated works.</p><p>// BH1715</p><p>// This code is designed to work with the BH1715_I2CS I2C Mini Module available from ControlEverything.com.</p><p>// <a href="https://www.controleverything.com/content/Light?sku=BH1715_I2CS#tabs-0-product_tabset-2"> https://www.controleverything.com/content/Light?s...</a></p><p>import com.pi4j.io.i2c.I2CBus;</p><p>import com.pi4j.io.i2c.I2CDevice;</p><p>import com.pi4j.io.i2c.I2CFactory;</p><p>import java.io.IOException;</p><p>public class BH1715</p><p>{</p><p>public static void main(String args[]) throws Exception	</p><p>{		</p><p>// Create I2C bus	</p><p>I2CBus bus = I2CFactory.getInstance(I2CBus.BUS_1);		</p><p>// Get I2C device, BH1715 I2C address is 0x23(35)		</p><p>I2CDevice device = bus.getDevice(0x23);</p><p>// Send power on command		</p><p>device.write((byte)0x01);		</p><p>// Send continuous measurement command</p><p>device.write((byte)0x10);		</p><p>Thread.sleep(500);
// Read 2 bytes of data		</p><p>// luminance msb, luminance lsb		</p><p>byte[] data = new byte[2];		</p><p>device.read(data, 0, 2);
// Convert data		</p><p>double luminance = ((data[0] & 0xFF) * 256 + (data[1] & 0xFF)) / 1.20;
// Output data to screen		</p><p>System.out.printf("Ambient Light Luminance : %.2f lux %n", luminance);	</p><p>}</p><p>}</p>

The library which facilitates i2c communication between the sensor and the board is pi4j, its various packages I2CBus, I2CDevice and I2CFactory help to establish the connection.

<p>import com.pi4j.io.i2c.I2CBus;<br>import com.pi4j.io.i2c.I2CDevice;
import com.pi4j.io.i2c.I2CFactory;
import java.io.IOException;</p>

This part of the code makes the sensor operate for light intensity measurement by writing the respective commands using the write() function and then the data is read using the read() function.

<p>device.write((byte)0x01);	//  power on command	</p><p>device.write((byte)0x10);	//  continuous measurement command	</p><p>byte[] data = new byte[2];	// Read 2 bytes of data</p><p>device.read(data, 0, 2);</p>

The data received from the sensor is converted to the appropriate format by using the following:

<p>double luminance = ((data[0] & 0xFF) * 256 + (data[1] & 0xFF)) / 1.20;</p>

The output is printed using the System.out.println() function, in the following format.

<p>System.out.printf("Ambient Light Luminance : %.2f lux %n", luminance);</p>

The output of the sensor is shown in the picture above.

Step 5: Applications:

BH1715 is a digital output ambient light sensor which can be incorporated in Mobile phone, LCD TV, NOTE PC etc. It can also be employed in Portable game machine, Digital camera, Digital video camera, PDA, LCD display and many more devices which require efficient light sensing applications.