Sponsor Link:

UTSource.net Reviews

It is a trustworthy website for ordering electronic components with cheap price and excellent quality.

Mounting the circuit

To mount the circuit, you would want to start off by disconnecting any source of power going into your Arduino as it may be a hazard when connecting wires to the BH1750 sensor. Now, we can start wiring the sensor to the Arduino. Before starting, make sure you have soldered the 5-pin header to the sensor for your connections. This sensor uses the standard i2c protocol wiring, so it may all be very familiar to you. Firstly, use one of your five jumper wires to connect the VCC (+) pin of the sensor to the 5v (+) pin on your Arduino board. Now, use another jumper wire to connect the GND (-) pin of your BH1750 sensor to any of your GND (-) pins on your Arduino. Next, connect the SCL (Serial Clock) pin of the sensor to A5 (analog pin 5) of your Arduino and the SDA (Serial Data) pin of your sensor to A4 (analog pin 4) of your Arduino. These four connections are all part of the standard i2c hardware connections for any i2c sensor. Finally, with a jumper wire, connect the ADD (address) pin of BH1750 sensor to any of your GND (-) pins of your Arduino. The hardware part of this project is now done!

Arduino BH1750 Light Sensor Project Code

#include <Wire.h>
#include <BH1750.h>
BH1750 lightMeter;

void setup(){
  Serial.begin(9600);
  Wire.begin();
  lightMeter.begin();
  Serial.println(F("BH1750 Test"));
}

void loop() {
  float lux = lightMeter.readLightLevel(true);
  Serial.print("Light: ");
  Serial.print(lux);
  Serial.println(" lx");
  delay(2000);
} 

About the code

This simple sketch composes of a few basic functions involving the sensor's library and some of the Arduino IDE's built-in serial monitor's own functions, making this project perfect for beginners in Arduino. Firstly, we see the Wire library being declared, so that we can use the i2c interface for the communication between the Arduino and the BH1750 sensor. Next, we declare another library, the BH1750 sensor library, which is used later on to control the output of the sensor. Without this library, many of the calculations performed would be complex as you would need to have the knowledge to manipulate functions around, and experiment with various features of the C++ language. Then, we set the BH1750 sensor to be under variable lightMeter for use later on in the other parts of this sketch. Now, we move on to the void setup section where we first start by setting the baud rate for serial data communication to 9600 bauds and in the next line, we start the i2c communication between the sensor and the Arduino. Following that line, we use one of the BH1750 library commands to start up the BH1750 sensor. Lastly for this setup section, we print "BH1750 text" into the serial monitor to start. We move to the void loop section now where we start by declaring a float datatype named lux. This datatype is the variable we will assign for the raw data coming out of the sensor to be fed into. The BH1750 library's command reads the light sensor's data with the command, light.Meter.readLightLevel(true), and puts everything into float datatype lux. The next line commands the serial monitor to display the text "Light:" and the following line prints the data from float datatype lux, which is the lux reading from the sensor, next to "Light:". Finally, the serial monitor prints the base unit, "lx" to the number, giving a reading on the serial monitor to be in the format of "Light: xx lx". The very last line of this sketch is to wait for two seconds until we loop this section again, making the serial monitor print the sensor's data every two seconds. That is it for this sketch!

Amazing opportunities

Also, be sure to check out PCBWay, a leading manufacturer and distributor in PCB design and manufacturing. They have amazing prices...

Read more »