adding the BH1750 from their downloads
This commit is contained in:
parent
f92231dbff
commit
064a8ffd26
16 changed files with 1023 additions and 0 deletions
95
BH1750/examples/BH1750advanced/BH1750advanced.ino
Normal file
95
BH1750/examples/BH1750advanced/BH1750advanced.ino
Normal file
|
@ -0,0 +1,95 @@
|
|||
/*
|
||||
|
||||
Advanced BH1750 library usage example
|
||||
|
||||
This example has some comments about advanced usage features.
|
||||
|
||||
Connection:
|
||||
|
||||
VCC -> 3V3 or 5V
|
||||
GND -> GND
|
||||
SCL -> SCL (A5 on Arduino Uno, Leonardo, etc or 21 on Mega and Due, on esp8266 free selectable)
|
||||
SDA -> SDA (A4 on Arduino Uno, Leonardo, etc or 20 on Mega and Due, on esp8266 free selectable)
|
||||
ADD -> (not connected) or GND
|
||||
|
||||
ADD pin is used to set sensor I2C address. If it has voltage greater or equal to
|
||||
0.7VCC voltage (e.g. you've connected it to VCC) the sensor address will be
|
||||
0x5C. In other case (if ADD voltage less than 0.7 * VCC) the sensor address will
|
||||
be 0x23 (by default).
|
||||
|
||||
*/
|
||||
|
||||
#include <Wire.h>
|
||||
#include <BH1750.h>
|
||||
|
||||
/*
|
||||
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 uses 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(9600);
|
||||
|
||||
// Initialize the I2C bus (BH1750 library doesn't do this automatically)
|
||||
Wire.begin();
|
||||
// On esp8266 you can select SCL and SDA pins using Wire.begin(D4, D3);
|
||||
|
||||
/*
|
||||
|
||||
BH1750 has six different measurement modes. They are divided in two groups;
|
||||
continuous and one-time measurements. In continuous mode, sensor continuously
|
||||
measures lightness value. In one-time mode the sensor makes only one
|
||||
measurement and then goes into Power Down mode.
|
||||
|
||||
Each mode, has three different precisions:
|
||||
|
||||
- Low Resolution Mode - (4 lx precision, 16ms measurement time)
|
||||
- High Resolution Mode - (1 lx precision, 120ms measurement time)
|
||||
- High Resolution Mode 2 - (0.5 lx precision, 120ms measurement time)
|
||||
|
||||
By default, the library uses Continuous High Resolution Mode, but you can
|
||||
set any other mode, by passing it to BH1750.begin() or BH1750.configure()
|
||||
functions.
|
||||
|
||||
[!] Remember, if you use One-Time mode, your sensor will go to Power Down
|
||||
mode each time, when it completes a measurement and you've read it.
|
||||
|
||||
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
|
||||
|
||||
*/
|
||||
|
||||
// begin returns a boolean that can be used to detect setup problems.
|
||||
if (lightMeter.begin(BH1750::CONTINUOUS_HIGH_RES_MODE)) {
|
||||
Serial.println(F("BH1750 Advanced begin"));
|
||||
}
|
||||
else {
|
||||
Serial.println(F("Error initialising BH1750"));
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
||||
void loop() {
|
||||
if (lightMeter.measurementReady()) {
|
||||
float lux = lightMeter.readLightLevel();
|
||||
Serial.print("Light: ");
|
||||
Serial.print(lux);
|
||||
Serial.println(" lx");
|
||||
}
|
||||
|
||||
}
|
98
BH1750/examples/BH1750autoadjust/BH1750autoadjust.ino
Normal file
98
BH1750/examples/BH1750autoadjust/BH1750autoadjust.ino
Normal file
|
@ -0,0 +1,98 @@
|
|||
/*
|
||||
|
||||
Example of BH1750 library usage.
|
||||
|
||||
This example initialises the BH1750 object using the default high resolution
|
||||
one shot mode and then makes a light level reading every five seconds.
|
||||
|
||||
After the measurement the MTreg value is changed according to the result:
|
||||
lux > 40000 ==> MTreg = 32
|
||||
lux < 40000 ==> MTreg = 69 (default)
|
||||
lux < 10 ==> MTreg = 138
|
||||
Remember to test your specific sensor! Maybe the MTreg value range from 32
|
||||
up to 254 is not applicable to your unit.
|
||||
|
||||
Connection:
|
||||
|
||||
VCC -> 3V3 or 5V
|
||||
GND -> GND
|
||||
SCL -> SCL (A5 on Arduino Uno, Leonardo, etc or 21 on Mega and Due, on esp8266 free selectable)
|
||||
SDA -> SDA (A4 on Arduino Uno, Leonardo, etc or 20 on Mega and Due, on esp8266 free selectable)
|
||||
ADD -> (not connected) or GND
|
||||
|
||||
ADD pin is used to set sensor I2C address. If it has voltage greater or equal to
|
||||
0.7VCC voltage (e.g. you've connected it to VCC) the sensor address will be
|
||||
0x5C. In other case (if ADD voltage less than 0.7 * VCC) the sensor address will
|
||||
be 0x23 (by default).
|
||||
|
||||
*/
|
||||
|
||||
#include <Wire.h>
|
||||
#include <BH1750.h>
|
||||
|
||||
BH1750 lightMeter;
|
||||
|
||||
void setup(){
|
||||
|
||||
Serial.begin(9600);
|
||||
|
||||
// Initialize the I2C bus (BH1750 library doesn't do this automatically)
|
||||
Wire.begin();
|
||||
// On esp8266 you can select SCL and SDA pins using Wire.begin(D4, D3);
|
||||
|
||||
lightMeter.begin(BH1750::ONE_TIME_HIGH_RES_MODE);
|
||||
//lightMeter.setMTreg(69); // not needed, only mentioning it
|
||||
|
||||
Serial.println(F("BH1750 Test begin"));
|
||||
|
||||
}
|
||||
|
||||
void loop() {
|
||||
//we use here the maxWait option due fail save
|
||||
if (lightMeter.measurementReady(true)) {
|
||||
float lux = lightMeter.readLightLevel();
|
||||
Serial.print(F("Light: "));
|
||||
Serial.print(lux);
|
||||
Serial.println(F(" lx"));
|
||||
|
||||
if (lux < 0) {
|
||||
Serial.println(F("Error condition detected"));
|
||||
}
|
||||
else {
|
||||
if (lux > 40000.0) {
|
||||
// reduce measurement time - needed in direct sun light
|
||||
if (lightMeter.setMTreg(32)) {
|
||||
Serial.println(F("Setting MTReg to low value for high light environment"));
|
||||
}
|
||||
else {
|
||||
Serial.println(F("Error setting MTReg to low value for high light environment"));
|
||||
}
|
||||
}
|
||||
else {
|
||||
if (lux > 10.0) {
|
||||
// typical light environment
|
||||
if (lightMeter.setMTreg(69)) {
|
||||
Serial.println(F("Setting MTReg to default value for normal light environment"));
|
||||
}
|
||||
else {
|
||||
Serial.println(F("Error setting MTReg to default value for normal light environment"));
|
||||
}
|
||||
}
|
||||
else {
|
||||
if (lux <= 10.0) {
|
||||
//very low light environment
|
||||
if (lightMeter.setMTreg(138)) {
|
||||
Serial.println(F("Setting MTReg to high value for low light environment"));
|
||||
}
|
||||
else {
|
||||
Serial.println(F("Error setting MTReg to high value for low light environment"));
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
Serial.println(F("--------------------------------------"));
|
||||
}
|
||||
delay(5000);
|
||||
}
|
44
BH1750/examples/BH1750onetime/BH1750onetime.ino
Normal file
44
BH1750/examples/BH1750onetime/BH1750onetime.ino
Normal file
|
@ -0,0 +1,44 @@
|
|||
/*
|
||||
|
||||
Example of BH1750 library usage.
|
||||
|
||||
This example initialises the BH1750 object using the high resolution
|
||||
one-time mode and then makes a light level reading every second.
|
||||
|
||||
The BH1750 component starts up in default mode when it next powers up.
|
||||
The BH1750 library automatically reconfigures the one-time mode in
|
||||
preparation for the next measurement.
|
||||
|
||||
*/
|
||||
|
||||
#include <Wire.h>
|
||||
#include <BH1750.h>
|
||||
|
||||
BH1750 lightMeter;
|
||||
|
||||
void setup(){
|
||||
|
||||
Serial.begin(9600);
|
||||
|
||||
// Initialize the I2C bus (BH1750 library doesn't do this automatically)
|
||||
Wire.begin();
|
||||
// On esp8266 you can select SCL and SDA pins using Wire.begin(D4, D3);
|
||||
|
||||
lightMeter.begin(BH1750::ONE_TIME_HIGH_RES_MODE);
|
||||
|
||||
Serial.println(F("BH1750 One-Time Test"));
|
||||
|
||||
}
|
||||
|
||||
|
||||
void loop() {
|
||||
|
||||
while (!lightMeter.measurementReady(true)) {
|
||||
yield();
|
||||
}
|
||||
float lux = lightMeter.readLightLevel();
|
||||
Serial.print("Light: ");
|
||||
Serial.print(lux);
|
||||
Serial.println(" lx");
|
||||
lightMeter.configure(BH1750::ONE_TIME_HIGH_RES_MODE);
|
||||
}
|
54
BH1750/examples/BH1750test/BH1750test.ino
Normal file
54
BH1750/examples/BH1750test/BH1750test.ino
Normal file
|
@ -0,0 +1,54 @@
|
|||
/*
|
||||
|
||||
Example of BH1750 library usage.
|
||||
|
||||
This example initialises the BH1750 object using the default high resolution
|
||||
continuous mode and then makes a light level reading every second.
|
||||
|
||||
Connection:
|
||||
|
||||
VCC -> 3V3 or 5V
|
||||
GND -> GND
|
||||
SCL -> SCL (A5 on Arduino Uno, Leonardo, etc or 21 on Mega and Due, on esp8266 free selectable)
|
||||
SDA -> SDA (A4 on Arduino Uno, Leonardo, etc or 20 on Mega and Due, on esp8266 free selectable)
|
||||
ADD -> (not connected) or GND
|
||||
|
||||
ADD pin is used to set sensor I2C address. If it has voltage greater or equal to
|
||||
0.7VCC voltage (e.g. you've connected it to VCC) the sensor address will be
|
||||
0x5C. In other case (if ADD voltage less than 0.7 * VCC) the sensor address will
|
||||
be 0x23 (by default).
|
||||
|
||||
*/
|
||||
|
||||
|
||||
#include <Wire.h>
|
||||
#include <BH1750.h>
|
||||
|
||||
BH1750 lightMeter;
|
||||
|
||||
|
||||
void setup(){
|
||||
|
||||
Serial.begin(9600);
|
||||
|
||||
// Initialize the I2C bus (BH1750 library doesn't do this automatically)
|
||||
Wire.begin();
|
||||
// On esp8266 you can select SCL and SDA pins using Wire.begin(D4, D3);
|
||||
// For Wemos / Lolin D1 Mini Pro and the Ambient Light shield use Wire.begin(D2, D1);
|
||||
|
||||
lightMeter.begin();
|
||||
|
||||
Serial.println(F("BH1750 Test begin"));
|
||||
|
||||
}
|
||||
|
||||
|
||||
void loop() {
|
||||
|
||||
float lux = lightMeter.readLightLevel();
|
||||
Serial.print("Light: ");
|
||||
Serial.print(lux);
|
||||
Serial.println(" lx");
|
||||
delay(1000);
|
||||
|
||||
}
|
73
BH1750/examples/BH1750two_i2c/BH1750two_i2c.ino
Normal file
73
BH1750/examples/BH1750two_i2c/BH1750two_i2c.ino
Normal file
|
@ -0,0 +1,73 @@
|
|||
/*
|
||||
|
||||
Example of BH1750 library usage.
|
||||
|
||||
This example initialises two BH1750 objects using different TwoWire
|
||||
instances (Wire and Wire1) and then makes a light level reading every second.
|
||||
This is the case for boards such as the ESP8266 and ESP32
|
||||
|
||||
Connection:
|
||||
|
||||
BH1750 A:
|
||||
VCC -> 3V3 or 5V
|
||||
GND -> GND
|
||||
SCL -> SCL (19 in this example)
|
||||
SDA -> SDA (18 in this example)
|
||||
ADD -> (not connected) or GND
|
||||
|
||||
BH1750 B:
|
||||
VCC -> 3V3 or 5V
|
||||
GND -> GND
|
||||
SCL -> SCL (22 in this example)
|
||||
SDA -> SDA (21 in this example)
|
||||
ADD -> (not connected) or GND
|
||||
|
||||
ADD pin is used to set sensor I2C address. If it has voltage greater or equal to
|
||||
0.7VCC voltage (e.g. you've connected it to VCC) the sensor address will be
|
||||
0x5C. In other case (if ADD voltage less than 0.7 * VCC) the sensor address will
|
||||
be 0x23 (by default).
|
||||
|
||||
*/
|
||||
|
||||
#include "BH1750.h"
|
||||
#include "Wire.h"
|
||||
|
||||
BH1750 bh1750_a;
|
||||
BH1750 bh1750_b;
|
||||
|
||||
void setup() {
|
||||
Serial.begin(115200);
|
||||
Wire.begin(18, 19);
|
||||
Wire1.begin(21, 22);
|
||||
bh1750_a.begin(BH1750::CONTINUOUS_HIGH_RES_MODE, 0x23, &Wire);
|
||||
bh1750_b.begin(BH1750::CONTINUOUS_HIGH_RES_MODE, 0x23, &Wire1);
|
||||
}
|
||||
|
||||
int error_counter_1_a = 0;
|
||||
int error_counter_2_a = 0;
|
||||
int error_counter_1_b = 0;
|
||||
int error_counter_2_b = 0;
|
||||
|
||||
void loop() {
|
||||
float light_level_a;
|
||||
if (bh1750_a.measurementReady()) { light_level_a = bh1750_a.readLightLevel(); }
|
||||
float light_level_b;
|
||||
if (bh1750_b.measurementReady()) { light_level_b = bh1750_b.readLightLevel(); }
|
||||
|
||||
if (lround(light_level_a) == -1) {
|
||||
error_counter_1_a++;
|
||||
}
|
||||
if (lround(light_level_a) == -2) {
|
||||
error_counter_2_a++;
|
||||
}
|
||||
if (lround(light_level_b) == -1) {
|
||||
error_counter_1_b++;
|
||||
}
|
||||
if (lround(light_level_b) == -2) {
|
||||
error_counter_2_b++;
|
||||
}
|
||||
Serial.printf("A: %.0f lux %d:%d :: B: %.0f lux %d:%d\n", light_level_a,
|
||||
error_counter_1_a, error_counter_2_a, light_level_b,
|
||||
error_counter_1_b, error_counter_2_b);
|
||||
delay(1000);
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue