Library for digital ambient light sensor BH1750 with two-wire (also known as I2C) bus interface usually on board GY-302.
- Sensor address is
0x23for ADDR pin state with voltage <= 0.3 Vcc (digital LOW) or floating. - Sensor address is
0x5Cfor ADDR pin state with voltage >= 0.7 Vcc (digital HIGH).
- Connect microcontroller's pin
D0to sensor's pin SDA (Serial Data). - Connect microcontroller's pin
D1to sensor's pin SCL (Serial Clock).
- Connect microcontroller's pin
A4to sensor's pin SDA (Serial Data). - Connect microcontroller's pin
A5to sensor's pin SCL (Serial Clock).
- Connect microcontroller's pin
D2to sensor's pin SDA (Serial Data). - Connect microcontroller's pin
D1to sensor's pin SCL (Serial Clock).
Connect sensor's pin ADDR (Addressing) either to VCC (power supply) rail, or GND (ground) rail, or leave it unconnected in order to set its address. It is neccessary for using two sensors on the same I2C bus.
- Particle.h: Includes alternative (C++) data type definitions.
- Arduino.h: Main include file for the Arduino SDK version greater or equal to 100.
- inttypes.h: Integer type conversions. This header file includes the exact-width integer definitions and extends them with additional facilities provided by the implementation.
- TwoWire: I2C system library loaded from the file
Wire.h.
- gbjTwoWire: I2C custom library loaded from the file
gbj_twowire.h, which provides common bus functionality.
The library does not have specific error codes. Error codes as well as result code are inherited from the parent library only. The result code and error codes can be tested in the operational code with its method getLastResult(), isError() or isSuccess().
- Addresses::ADDRESS_GND: Sensor's address at grounded ADDR pin.
- Addresses::ADDRESS_VCC: Sensor's address at ADDR pin connected to power supply voltage Vcc.
- Addresses::ADDRESS_FLOAT: Sensor's address at floating ADDR pin. Always same as for grounded address pin, but present just for hardware configuration in a sketch.
- Modes::CONTINUOUS_HIGH: Start measurement at
1 lxresolution. Measurement time is typically120 ms. - Modes::CONTINUOUS_HIGH2: Start measurement at
0.5 lxresolution. Measurement time is typically120 ms. - Modes::CONTINUOUS_LOW: Start measurement at
4 lxresolution. Measurement time is typically16 ms. - Modes::ONETIME_HIGH: Start measurement at
1 lxresolution. Measurement time is typically120 ms. The sensor is automatically set to Power Down mode after measurement. - Modes::ONETIME_HIGH2: Start measurement at
0.5 lxresolution. Measurement time is typically120 ms. The sensor is automatically set to Power Down mode after measurement. - Modes::ONETIME_LOW: Start measurement at
4 lxresolution. Measurement time is typically16 ms. The sensor is automatically set to Power Down mode after measurement.
The library increases calculated measurement time from datasheet values for particular measurement mode by safety margin 5% in order to provide a sensor sufficient time for conversion. Without it the measurement is not reliable.
In a sketch the constants can be referenced in following forms:
- Static constant in the form
gbj_bh1750::<enumeration>::<constant>or shortlygbj_bh1750::<constant>, e.g., gbj_bh1750::Addresses::ADDRESS_GND or gbj_bh1750::ADDRESS_GND. - Instance constant in the form
<object>.<constant>, e.g., sensor.ADDRESS_GND.
gbj_bh1750 sensor = gbj_bh1750(sensor.CLOCK_400KHZ);
String getModeName()
{
String measurementType;
switch (sensor.getMode())
{
case sensor.MODE_CONTINUOUS_HIGH:
measurementType = "Continuous High";
break;
...
}
return measurementType;
}- gbj_bh1750()
- begin()
- powerOn()
- powerOff()
- reset()
- measureLight()
- measureLightTyp()
- measureLightMin()
- measureLightMax()
- setAddress()
- setMode()
- setTimingTyp()
- setTimingMax()
- setResolutionTyp()
- setResolutionMin()
- setResolutionMax()
- getMode()
- getSenseCoef()
- getLightResult()
- getLightTyp()
- getLightMin()
- getLightMax()
- getTimingTyp()
- getTimingMax()
- getMeasurementTime()
- getMeasurementTimeTyp()
- getMeasurementTimeMax()
- getResolutionTyp()
- getResolutionMin()
- getResolutionMax()
- getSensitivityTyp()
- getSensitivityMin()
- getSensitivityMax()
- getAccuracyTyp()
- getAccuracyMin()
- getAccuracyMax()
Other possible setters and getters are inherited from the parent library gbjTwoWire and described there.
The library does not need special constructor and destructor, so that the inherited ones are good enough and there is no need to define them in the library, just use it with default or specific parameters as defined at constructor of parent library gbjTwoWire.
- Constructor sets parameters specific to the two-wire bus in general.
- All the constructor parameters can be changed dynamically with corresponding setters later in a sketch.
gbj_bh1750(ClockSpeeds clockSpeed, bool busStop, uint8_t pinSDA, uint8_t pinSCL)
-
clockSpeed: Two-wire bus clock frequency in Hertz.
- Valid values:ClockSpeeds::CLOCK_100KHZ, ClockSpeeds::CLOCK_400KHZ
- Default value: ClockSpeeds::CLOCK_100KHZ
-
busStop: Logical flag about releasing bus after end of transmission.
- Valid values: true, false
- true: Releases the bus after data transmission and enables other master devices to control the bus.
- false: Keeps connection to the bus and enables to begin further data transmission immediately.
- Default value: true
- Valid values: true, false
-
pinSDA: Microcontroller's pin for serial data. It is not a board pin but GPIO number. For hardware two-wire bus platforms it is irrelevant and none of methods utilizes this parameter for such as platforms for communication on the bus. On the other hand, for those platforms the parameters might be utilized for storing some specific attribute in the class instance object.
- Valid values: positive integer
- Default value: 4 (GPIO4, D2)
-
pinSCL: Microcontroller's pin for serial clock. It is not a board pin but GPIO number. For hardware two-wire bus platforms it is irrelevant and none of methods utilizes this parameter for such as platforms. On the other hand, for those platforms the parameters might be utilized for storing some specific attribute in the class instance object.
- Valid values: positive integer
- Default value: 5 (GPIO5, D1)
Object performing the sensor management. The constructor cannot return a result or error code directly, however, it stores them in the instance object.
The method has all arguments defaulted and calling without any parameters is equivalent to the calling with all arguments set by corresponding constant with default value:
gbj_bh1750 sensor = gbj_bh1750(); // It is equivalent to
gbj_bh1750 sensor = gbj_bh1750(gbj_bh1750::CLOCK_100KHZ, true, D2, D1);The method takes, sanitizes, and stores sensor parameters to a class instance object and initiates two-wire bus.
- The method sets parameters specific to the sensor itself.
- All the method parameters can be changed dynamically with corresponding setters later in a sketch.
- The method enables to set the sensor's address according to the ADDR pin, if it is wired to a microcontroller's pin.
ResultCodes begin(Addresses address, Modes mode)
-
address: One of two possible 7 bit addresses of the sensor corresponding to the ADDR pin connection.
- Valid values: Addresses::ADDRESS_GND, Addresses::ADDRESS_VCC, Addresses::ADDRESS_FLOAT
- Default value: _FLOAT::ADDRESS_GND
-
mode: Measurement mode from possible listed ones.
- Valid values: Modes::MODE_CONTINUOUS_HIGH ~ Modes::MODE_ONETIME_LOW
- Default value: Modes::MODE_CONTINUOUS_HIGH
Some of result or error codes.
The method has all arguments defaulted and calling without any parameters is equivalent to the calling with all arguments set by corresponding constant with default value:
gbj_bh1750 sensor = gbj_bh1750();
setup()
{
sensor.begin(); // It is equivalent to
sensor.begin(sensor.ADDRESS_GND, sensor.MODE_CONTINUOUS_HIGH);
}If some argument after some defaulted arguments should have a specific value, use corresponding constants in place of those defaulted arguments, e.g.,
sensor.begin(sensor.ADDRESS_VCC, sensor.MODE_ONETIME_LOW); // Specific measurement modeTypical usage is just with default values without any arguments. Specific values of arguments can be set by corresponding setters.
The particular method either activates (wakes up) or deactivates (sleeps down) a sensor.
- In active state a sensor waits for the measurement command.
- In sleeping state a sensor has minimal power consumption.
ResultCodes powerOn()
ResultCodes powerOff()
None
Some of result or error codes.
The method resets the illuminance data register of the sensor, which removes previous measurement result, and sets previous measurement mode.
ResultCodes reset()
None
Some of result or error codes.
The method measures the ambient light intensity and calculates it in lux at all measurement accuracies (typical, minimal, maximal) with resolution set before exactly or by measurement mode.
- The method stores the measured and calculated values in the class instance object for repeating retrieval without need to measure again. Particular values can be obtain with corresponding getters.
- The method is useful in a sketch when light intensity at all accuracies are utilized for its confidence interval.
ResultCodes measureLight()
None
Some of result or error codes.
measureLightTyp(), measureLightMin(), measureLightMax()
getLightTyp(), getLightMin(), getLightMax()
The particular method measures the ambient light and calculates intensity in lux for corresponding accuracy (typical, minimal, maximal). It subsequently uses the method measureLight() and corresponding method getLightXXX().
- Particular methods is useful in a sketch when the light intensity at just one accuracy is needed.
float measureLightTyp()
float measureLightMin()
float measureLightMax()
None
Current ambient light intensity in lux at corresponding accuracy. The result or error code is set as well.
The method sets the new address of the sensor, e.g., at dynamical changing ADDR pin state by a microcontroller's pin.
ResultCodes setAddress(Addresses address)
- address: The address value of the same range as the same argument in the method begin().
Some of result or error codes.
sensor.setAddress(digitalRead(PIN_BH1750_ADDR) ? sensor.ADDRESS_VCC : sensor.ADDRESS_GND);
sensor.setAddress(sensor.ADDRESS_VCC);The method sets the measurement mode of the sensor. It should be one of defined measuring modes.
- If a low measuring mode is selected, the method sets the measurement time for typical value of measurement time register.
- For selected mode and current resolution the method calculates measurement time and measurement sensitivity.
ResultCodes setMode(Modes mode)
- mode: The mode code of the same range as the same argument in the method begin().
Some of result or error codes.
The method returns the current measurement mode of the sensor stored in the class instance object.
Modes getMode()
None
Current measurement mode of the sensor.
The method returns the current sensitivity coefficient defined by ratio of current set value of the measurement time register and its typical value.
float getSenseCoef()
None
Current sensitivity coefficient.
The particular method retrieves recently measured and calculated light intensity for corresponding accuracy without need to measure again, e.g., for repeating usage without local variable in a sketch.
float getLightTyp()
float getLightMin()
float getLightMax()
None
Recently measured light intensity in lux.
The method provides binary content of the sensor's data register as a raw light intensity value from recent measurement. It is suitable for testing purposes or calibration.
uint16_t getLightResult()
None
Binary word of the recently measured sensor value.
getLightTyp(), getLightMin(), getLightMax()
The particular method sets a flag of the internal library instance determining, whether either typical or maximal measurement time values should be used at measurement by the sensor.
void setTimingTyp()
void setTimingMax()
None
None
getTimingTyp(), getTimingMax()
The particular method returns a flag about currently used typical or maximal measurement time values for measurement by the sensor.
- Although both methods are complement, they are present for enabling better readability in sketches.
bool getTimingTyp()
bool getTimingMax()
None
Boolean flag about used typical or maximal measurement time values.
setTimingTyp(), setTimingMax()
The particular method returns corresponding measurement time in milliseconds for current measurement mode and resolution.
- The methods
getMeasurementTimeTyp()andgetMeasurementTimeMax()are calculated for particular sensor's accuracy and current value of sensor's measurement time register. - The method
getMeasurementTime()provides measurement time used for real light measurement. Its minimal value is limited to the typical measurement time for current measurement mode even if calculated time for lower resolutions is usually shorter. It ensures sufficient time for the sensor for processing measurement.
uint16_t getMeasurementTime()
uint16_t getMeasurementTimeTyp()
uint16_t getMeasurementTimeMax()
None
Measurement time in milliseconds for current setting of the sensor.
getResolutionTyp(), getResolutionMin(), getResolutionMax()
The particular method sets measurement resolution by writing corresponding value to the measurement time register of the sensor.
- Those limits have nothing common with typical, minimal, or maximal values of parameters from a datasheet. They are just minimal, typical (default), and maximal values of the measurement time register.
- The measurement resolution is expressed in
bit count per lux. It determines what binary value of the sensor's data register corresponds to 1 lx of measured ambient illuminance. - The measurement resolution in conjunction with sensor's measurement accuracy determines measurement sensitivity in
lux per bit count. - The typical resolution is determined by the measurement time register value
69. - The minimal resolution is determined by the measurement time register value
31and corresponds to 0.45 (31/69) fraction of typical resolution. This resolution is useful for measuring the highest light intensity up to 100000 lx, theoretically to 151946 lx at minimal datasheet resolution 0.96 bit count per lux and maximal illuminance register value 65535, i.e.,69 / 31 / 0.96 * 65535. - The maximal resolution is determined by the measurement time register value
254and corresponds to 3.68 (254/69) multiple of typical resolution. This resolution is useful for measuring the lowest light intensity (darkness) down to 0.11 lx in conjunction with double high measurement mode, theoretically from 0.09 lx at maximal datasheet resolution 1.44 bit count per lux, double sensitivity, and minimal illuminance register value 1, i.e.,69 / 254 / 2 / 1.44 * 1.
ResultCodes setResolutionTyp()
ResultCodes setResolutionMin()
ResultCodes setResolutionMax()
None
Some of result or error codes.
getResolutionTyp(), getResolutionMin(), getResolutionMax()
The particular method returns currently set measurement resolution in bit count per lux.
float getResolutionTyp()
float getResolutionMin()
float getResolutionMax()
None
Current measurement resolution in bit count per lux.
setResolutionTyp(), setResolutionMin(), setResolutionMax()
The particular method returns current measurement sensitivity.
- The measurement sensitivity is expressed in
lux per bit count. It determines what light intensity corresponds to 1 bit of sensor's data register. - The measurement sensitivity is the reciprocal value to the measurement resolution.
float getSensitivityTyp()
float getSensitivityMin()
float getSensitivityMax()
None
Current measurement sensitivity in lux per bit count.
getResolutionTyp(), getResolutionMin(), getResolutionMax()
The particular method returns corresponding sensor's accuracy (typical, minimal, maximal) as the data sheet them declares in bit count per lux. It is a factory measurement resolution at typical value of measurement time register.
float getAccuracyTyp()
float getAccuracyMin()
float getAccuracyMax()
None
Factory sensor accuracy in bit count per lux.