Here to make an IoT ESP8266 Based Lux Monitor or Light Gauge using BH1750 Ambient Light Sensor & Gauge Widget it an Ubidots delivered end-to-end IoT solutions. We can monitor the Light intensity of a particular location from anywhere in the world. For that, we need to use some hardware like NodeMCU ESP8266 & BH1750 Ambient Light Sensor Module, IoT cloud platform.
BH1750 Ambient Light Sensor
BH1750 is a digital ambient light sensor that is used commonly used in mobile phones to manipulate the screen brightness based on the environment lighting. This sensor can accurately measure the LUX value of light up to 65535lx.
BH1750 Features
Power Supply: 2.4V-3.6V (typically 3.0V)
Less current consumption: 0.12mA
Measuring Rang: 1-65535lx
Communication: I2C bus
Accuracy: +/-20%
Built in A/D converter for converting analog illuminance in the digital data.
Very small effect of IR radiation
Highly responsive near to human eye.
The BH1750 is a light intensity sensor that can be used to adjust the brightness of display in mobiles and LCD displays. It can also be used to turn the headlights of cars on/off based on the outdoor lighting. The sensor uses I2C communication protocol so that makes it super easy to use with microcontrollers. The SCL and SDA pins are for I2C. There is no calculation needed to measure the LUX value because the sensor directly gives the lux value. Actually, it measures the intensity according to the amount of light hitting on it. It operates on voltage range of 2.4V-3.6V and consumes really small current of 0.12mA. The results of the sensor does not depends upon the light source used and the influence of IR radiation is very less. There are very less chances of any error because the variation in measurement is as low as +/-20%.
Hardware of the BH1750 is very simple. The main component is BH1750FVI IC. The module works on 3.3V so a voltage regulator is used. For I2C lines a pull up resistor of 4.7K is used.
We get the lux values from BH1750 through I2C bus. The ADC in the IC converts the analog illuminance to the digital lux value. Now this data is transferred to microcontroller with the help of I2C pins SCL and SDA. The SCL is used to provide the clock pulse and the SDA is used to transfer the lux value. The IC uses a photodiode which gives the response equivalent to the human eye. There is also an internal oscillator in the IC which is used for the clock of internal logic of the IC.
Applications
Manipulate brightness in mobile phones/LCD
Automobile Headlights on/off control based on surrounding lighting conditions.
Used as Ambient light sensors to control the brightness of Display screens.
BH1750 NodeMCU ESP8266 Circuit Diagram
Now, let us interface the BH1750 Ambient Light Sensor with NodeMCU ESP8266 Board. The connection is fairly simple. Connect the I2C Pin of BH1750, i.e. SDA & SCL to D1 & D2 of NodeMCU Board Respectively.
Connect the 3.3V pin of BH1750 to NodeMCU 3.3V pin & GND to GND. The circuit can be easily assembled on a breadboard.
Installing Libraries
Download: BH1750 Library
Download : ubidots-nodemcu-master
To install the library navigate to the Sketch > Include Library > Manage Libraries… Wait for Library Manager to download libraries index and update list of installed libraries.
Subscribe and Download code.
arduino Code
#include "UbidotsMicroESP8266.h"
#define TOKEN "BBFF-HYfUHivZQVJKjxO12FkQ6X7SWDpdiy" // Put here your Ubidots TOKEN
#define WIFISSID "TP-Link_3200" // your SSID
#define PASSWORD "9500112137" // your wifi password
Ubidots client(TOKEN);
//unsigned long lastMillis = 0;
#include <Wire.h> // I2C Arduino library
#include <BH1750FVI.h> // BH1750FVI Light sensor library
BH1750FVI LightSensor;
int lux;
void setup() {
Serial.begin(9600);
LightSensor.begin();
LightSensor.setMode(Continuously_High_Resolution_Mode); // see datasheet page 5 for modes
Serial.println("Light sensor BH1750FVI found and running");
client.wifiConnection(WIFISSID, PASSWORD);
}
void loop() {
lux = LightSensor.getAmbientLight();
Serial.print("Ambient light intensity: ");
Serial.print(lux);
Serial.println(" lux");
delay(2000);
client.add("sweetHome", lux);
client.sendAll(true);
}
Then, upload the code to your NodeMCU board. Make sure you have selected the right board and COM port. Also, make sure you’ve inserted your WiFi Credentials in the code.
After a successful upload, open the Serial Monitor at a baud rate of 9600. Press the “EN/RST” button on the ESP8266 board.
UBIDOTS IoT Platform Setup
I created an account on the Ubidots IoT platform. Before setting up the Ubidots IOT platform first, I uploaded both the programs in the Nodemcu modules and turned on the circuit boards. This step is really important, because when we first power up the Nodemcu modules it sends the variables to the Ubidots IoT platform which can be then used. We can display the values on the charts and assign some conditions.
Creating a free Ubidots account and sign in.
After filling the forms and completing other requirements, finally, registered on the Ubidots IoT Platform.As I created a new account so there are no events, no Dashboards, and no devices.
Follow the Demo video, Currently, you can see the value in LUX Gauge widget in dash board.
See Connected device,
You can see variables, as have connected ESP8266.
Currently, you can see the graphical representation value.
That’s pretty much how the code works.
Comments