The GY-302 from CJMCU is an I2C board that allows you to measure the amount of light using the BH1750 photodetector. We will use the measured brightness to construct an ambient lighting quality indicator based on European Standard EN 12464-1. It is very easy to integrate the sensor GY-302 in an Arduino project or ESP8266 using the library developed by Christopher Laws. It is available on this GitHub page. The GY-302 costs less than one euro.
Add the BH1750 library to the Arduino IDE
Download the ZIP archive of the library from GitHub without decompressing it.
From the Arduino IDE, go to the menu and then Add the .ZIP library
Circuit
The GY-302 module communicates via the I2C bus with the Arduino or ESP8266. The wiring is very simple.
Pin | Description |
---|---|
GND | Ground |
ADDR | Select I2C adresse. 0x23 by default. 0x5C if connected to 3V3 |
SDA | I2C SDA |
SCL | I2C SCL |
VCC | 3.3V |
On an Arduino, connect the SDA pin to pin A4 and SCL on pin A5. On the ESP8266 Wemos d1 mini, SDA is in D2 and SCL in D1. For other ESP8266 boards, read this article.
It is possible to manually assign the I2C bus pins using the Wire.h library. At the beginning of the program, the library is declared
#include <Wire.h>
Then in the setup ()
Wire.begin (SDA pin, SCL pin);
Here, all I2C devices are diverted to the new pins.
How to measure the quality of lighting?
In Europe, EN 12464-1 (summary in English and French) defines the minimum lighting levels according to the occupied workplace.
Source: LUX Lighting Review No. 228 May / June 2004 available online.
In a dwelling, there is no specific standard (to my knowledge). Keria, a lighting specialist, has published some common light intensities on his site. Here are excerpts of the recommendations for some rooms of the house (or to obtain a certain atmosphere: intimate, convivial, game, work).
Pièce et ambiance recherchée | Intensité lumineuse | Couleur recommandée |
Salon, salle à manger, chambre d’adulte. Ambiance tamisée, intime. | 25-50 lux | Blanc chaud |
Salon, salle à manger, cuisine, chambre d’adulte, bureau. Ambiance conviviale, moments de détente. | 100-150 lux | Blanc chaud |
Bureau, bibliothèque, chambre d’enfant, salle de jeux. Ambiance de travail, de jeux. | 200-250 lux | Blanc neutre |
Ambiance technique : espaces de forte activité (bureau, atelier) et de circulation (couloirs, entrée) | 350-500 lux | Blanc froid |
Source : https://www.keria.com/guides/comment-calculer-la-quantite-declairage-necessaire-dans-une-piece
On the basis of these different data, I constructed a 5-level indicator (too low, low, ideal, high, too high). You can adjust the values according to your habits and needs.
#define _TOOLOW 25 #define _LOW 50 #define _HIGH 500 #define _TOOHIGH 750 #define LEVEL_TOOLOW "Too low" #define LEVEL_LOW "Low" #define LEVEL_OPTIMAL "Ideal" #define LEVEL_HIGH "High" #define LEVEL_TOOHIGH "Too high"
How to use the BH1750 library
The BH1750 library is used very similar to the BME280 library (or BMP180). At the beginning of the program, the library is called and the lightMeter object is initialized by indicating the address of the BH1750 on the I2C bus. By default the BH1750 is located at address 0x23. If you have a conflict with another component, you can assign the address 0x5C by feeding the addr pin to 3.3V.
#include <BH1750.h>
BH1750 lightMeter (0x23);
The library supports the 6 modes of operation of the sensor. The sensor can measure continuous brightness
- BH1750_CONTINUOUS_LOW_RES_MODE: Fast measurement (16ms) at low resolution (4 lux of precision)
- BH1750_CONTINUOUS_HIGH_RES_MODE (default mode): High resolution (1 lux accuracy). The measurement time is 120ms
- BH1750_CONTINUOUS_HIGH_RES_MODE_2: Very high resolution (0.5 lux accuracy). Measurement time 120ms
These three other modes allow to realize a single measurement (One_Time) and then to put the sensor in energy saving. Accuracy and measurement time are identical.
- BH1750_ONE_TIME_LOW_RES_MODE
- BH1750_ONE_TIME_HIGH_RES_MODE
- BH1750_ONE_TIME_HIGH_RES_MODE_2
In the setup, the lightMeter object is started by using the function begin (uint8_t mode) by passing it as parameter the measurement mode. The configure (uint8_t mode) function is (called by begin) is also exposed.
void setup(){
lightMeter.begin(BH1750_CONTINUOUS_HIGH_RES_MODE);
}
The readLightLevel method reads the light intensity measured by the BH1750 at any time. The function returns the measurement directly to Lux.
uint16_t lux = lightMeter.readLightLevel();
Arduino Code compatible ESP8266
Here is the complete code of the application you just need to upload. It works either on Arduino, ESP8266 or ESP32.
</co/*
Mesurer la qualité d'éclairage de votre habitation à l'aide d'un capteur GY-302 (BH1750)
Measure the lighting quality of your home with a GY-30 (BH1750) sensor
Code basé sur la librairie Arduino de Christopher Laws disponible sur GitHub
Based on the Arduino library of Christopher Laws abailable on GitHub https://github.com/claws/BH1750
Connection:
VCC -> 5V (3V3 on Arduino Due, Zero, MKR1000, etc)
GND -> GND
SCL -> SCL (A5 on Arduino Uno, Leonardo, etc or 21 on Mega and Due)
SDA -> SDA (A4 on Arduino Uno, Leonardo, etc or 20 on Mega and Due)
ADD -> GND or VCC (see below)
ADD pin uses to set sensor I2C address. If it has voltage greater or equal to
0.7VCC voltage (as example, you've connected it to VCC) - sensor address will be
0x5C. In other case (if ADD voltage less than 0.7 * VCC) - sensor address will
be 0x23 (by default).
https://projetsdiy.fr - https://diyprojects.io
*/
#include <Wire.h>
#include <BH1750.h>
/*
* Niveau d'éclairage définit à partir de la norme EN 12464-1
* Lighting level defined from the standard EN 12464-1
* http://www.afe-eclairage.fr/docs/362-ext.pdf
*/
#define _TOOLOW 25
#define _LOW 50
#define _HIGH 500
#define _TOOHIGH 750
#define LEVEL_TOOLOW "Trop bas" // Too low
#define LEVEL_LOW "Bas" // Low
#define LEVEL_OPTIMAL "Idéal" // Ideal
#define LEVEL_HIGH "Elevé" // High
#define LEVEL_TOOHIGH "Trop élevé" // Too High
uint16_t lux = 250;
int luxLevel = 3;
String luxMessage = LEVEL_OPTIMAL;
/*
BH1750 can be physically configured to use two I2C addresses:
- 0x23 (most common) (if ADD pin had < 0.7VCC voltage)
- 0x5C (if ADD pin had > 0.7VCC voltage)
Library use 0x23 address as default, but you can define any other address.
If you had troubles with default value - try to change it to 0x5C.
*/
BH1750 lightMeter(0x23);
void setup(){
Serial.begin(115200);
/*
Each mode, has three different precisions:
- Low Resolution Mode - (4 lx precision, 16ms measurment time)
- High Resolution Mode - (1 lx precision, 120ms measurment time)
- High Resolution Mode 2 - (0.5 lx precision, 120ms measurment time)
Full mode list:
BH1750_CONTINUOUS_LOW_RES_MODE
BH1750_CONTINUOUS_HIGH_RES_MODE (default)
BH1750_CONTINUOUS_HIGH_RES_MODE_2
BH1750_ONE_TIME_LOW_RES_MODE
BH1750_ONE_TIME_HIGH_RES_MODE
BH1750_ONE_TIME_HIGH_RES_MODE_2
*/
lightMeter.begin(BH1750_CONTINUOUS_HIGH_RES_MODE);
Serial.println(F("BH1750 Test"));
}
void loop() {
lux = lightMeter.readLightLevel();
if ( lux <= _TOOLOW ) {
luxLevel = 1;
luxMessage = LEVEL_TOOLOW;
} else if ( lux > _TOOLOW && lux <= _LOW ) {
luxLevel = 2;
luxMessage = LEVEL_LOW;
} else if ( lux > _LOW && lux <= _HIGH ) {
luxLevel = 3;
luxMessage = LEVEL_OPTIMAL;
} else if ( lux > _HIGH && lux < _TOOHIGH ) {
luxLevel = 4;
luxMessage = LEVEL_HIGH;
} else {
luxLevel = 5;
luxMessage = LEVEL_TOOHIGH;
}
Serial.print("Light: ");
Serial.print(lux);
Serial.print(" lx, level: ");
Serial.print(luxLevel);
Serial.print(" , quality: ");
Serial.println(luxMessage);
delay(1000);
}
Here. One more brick finished for our station is monitoring the air quality (and well being).
- Get Started with HC-SR04, measure distance by ultrasound. Arduino code example
- ESP32, GPIO pins and associated functions. I/O, PWM, RTC, I2C, SPI, ADC, DAC
- ESP32-CAM pinout and equipments. ESP-EYE, AI Thinker, TTGO T-Camera, M5Stack Timer Camera …
- ESP32-CAM. Which model to choose? ESP-EYE, AI Thinker, TTGO T-Camera, M5Stack Timer Camera …
- M5Stack Atomic GPS. ESP32 TinyGPS++ tracker, GPX export on SD card, visualization on Google Maps or VSCode
- Home Assistant. Install the snap on Synology NAS on an Ubuntu Virtual Machine