From b7c9286b725bcbd40284b3142bae638276a24af7 Mon Sep 17 00:00:00 2001 From: coelner Date: Wed, 3 Jan 2018 06:22:11 +0100 Subject: [PATCH 01/14] rebase from upstream --- BH1750.cpp | 0 1 file changed, 0 insertions(+), 0 deletions(-) mode change 100755 => 100644 BH1750.cpp diff --git a/BH1750.cpp b/BH1750.cpp old mode 100755 new mode 100644 From 7d2647d937833a3b0615233c9c5ddfa1c61e97f3 Mon Sep 17 00:00:00 2001 From: coelner Date: Wed, 7 Mar 2018 22:04:34 +0100 Subject: [PATCH 02/14] change readlightlevel to float return value --- BH1750.cpp | 28 +++++++++++++++++++--------- BH1750.h | 2 +- 2 files changed, 20 insertions(+), 10 deletions(-) diff --git a/BH1750.cpp b/BH1750.cpp index ab76d6c..e5aabe3 100644 --- a/BH1750.cpp +++ b/BH1750.cpp @@ -131,17 +131,19 @@ bool BH1750::configure(Mode mode) { /** * Read light level from sensor - * @return Light level in lux (0 ~ 65535) + * @return Light level in lux (0.0 ~ 54612,5) + * -1 : no valid return value + * -2 : sensor not configured */ -uint16_t BH1750::readLightLevel(bool maxWait) { +float BH1750::readLightLevel(bool maxWait) { if (BH1750_MODE == UNCONFIGURED) { Serial.println(F("[BH1750] Device is not configured!")); - return 0; + return -2; } // Measurement result will be stored here - uint16_t level=65535; + float level = -1; // Send mode to sensor Wire.beginTransmission(BH1750_I2CADDR); @@ -176,19 +178,26 @@ uint16_t BH1750::readLightLevel(bool maxWait) { // Read two bytes from the sensor, which are low and high parts of the sensor // value - Wire.requestFrom(BH1750_I2CADDR, 2); - if (Wire.available() == 2) { - level = __wire_read(); - level <<= 8; - level |= __wire_read(); + if (2 == Wire.requestFrom(BH1750_I2CADDR, 2)) { + uint16_t tmp = 0; + //if (Wire.available() == 2) { + tmp = __wire_read(); + tmp <<= 8; + tmp |= __wire_read(); + level = tmp; + //} } + if (level != -1) { // Print raw value if debug enabled #ifdef BH1750_DEBUG Serial.print(F("[BH1750] Raw value: ")); Serial.println(level); #endif + if (BH1750_MODE == BH1750::ONE_TIME_HIGH_RES_MODE_2 || BH1750_MODE == BH1750::CONTINUOUS_HIGH_RES_MODE_2) { + level /= 2; + } // Convert raw value to lux level /= 1.2; @@ -197,6 +206,7 @@ uint16_t BH1750::readLightLevel(bool maxWait) { Serial.print(F("[BH1750] Converted value: ")); Serial.println(level); #endif + } return level; diff --git a/BH1750.h b/BH1750.h index a28d533..4ef839f 100644 --- a/BH1750.h +++ b/BH1750.h @@ -60,7 +60,7 @@ class BH1750 { BH1750(byte addr = 0x23); bool begin(Mode mode = CONTINUOUS_HIGH_RES_MODE); bool configure(Mode mode); - uint16_t readLightLevel(bool maxWait = false); + float readLightLevel(bool maxWait = false); private: int BH1750_I2CADDR; From 33fa12dd2587ba1121e9dda1b5bc2f03af3f79da Mon Sep 17 00:00:00 2001 From: coelner Date: Wed, 7 Mar 2018 22:09:32 +0100 Subject: [PATCH 03/14] HRes Mode 2 implemented --- BH1750.cpp | 2 -- 1 file changed, 2 deletions(-) diff --git a/BH1750.cpp b/BH1750.cpp index e5aabe3..bda24b2 100644 --- a/BH1750.cpp +++ b/BH1750.cpp @@ -180,12 +180,10 @@ float BH1750::readLightLevel(bool maxWait) { // value if (2 == Wire.requestFrom(BH1750_I2CADDR, 2)) { uint16_t tmp = 0; - //if (Wire.available() == 2) { tmp = __wire_read(); tmp <<= 8; tmp |= __wire_read(); level = tmp; - //} } if (level != -1) { From 8310b279a57d2a557e9a2d9ed487b9a58bf913dd Mon Sep 17 00:00:00 2001 From: coelner Date: Wed, 7 Mar 2018 22:39:07 +0100 Subject: [PATCH 04/14] added behaviour description into README and change examples for float as return value --- README.md | 27 +++++++++++++++------- examples/BH1750advanced/BH1750advanced.ino | 2 +- examples/BH1750onetime/BH1750onetime.ino | 2 +- examples/BH1750test/BH1750test.ino | 2 +- 4 files changed, 22 insertions(+), 11 deletions(-) diff --git a/README.md b/README.md index 3ec4327..7a3f6cd 100644 --- a/README.md +++ b/README.md @@ -39,6 +39,17 @@ back into One-Time mode. This library has been implemented to automatically reconfigure the sensor when you next attempt a measurement so you should not have to worry about such low level details. +Usually you will get a float value which represent the lux equivalent. + - Low Resolution Mode - (0.0 up to 54612.5 lux) + - High Resolution Mode - (0.0 up to 54612.5 lux) + - High Resolution Mode 2 - (0.0 up to 27306.25 lux) + +The sensor returns a 16 bit unsigned integer. Therefore the maximum value is limited in general. +The standard conversion between the so called 'counts' to lux is 1/1.2, that means you get a smaller value. +If a error occurs you will get a negative value. + - -1 no valid data was transmitted from the sensor + - -2 device is not configured + The datasheet for the BH1750 chip can be obtained [here](http://www.elechouse.com/elechouse/images/product/Digital%20light%20Sensor/bh1750fvi-e.pdf) @@ -117,7 +128,7 @@ void setup(){ void loop() { - uint16_t lux = lightMeter.readLightLevel(); + float lux = lightMeter.readLightLevel(); Serial.print("Light: "); Serial.print(lux); Serial.println(" lx"); @@ -131,12 +142,12 @@ void loop() { Moving the sensor to face more light results in the lux measurements increasing. ``` BH1750 Test -Light: 70 lx -Light: 70 lx -Light: 59 lx -Light: 328 lx -Light: 333 lx -Light: 335 lx -Light: 332 lx +Light: 70.0 lx +Light: 70.0 lx +Light: 59.0 lx +Light: 328.0 lx +Light: 333.0 lx +Light: 335.0 lx +Light: 332.0 lx ``` There are more examples in the examples directory. diff --git a/examples/BH1750advanced/BH1750advanced.ino b/examples/BH1750advanced/BH1750advanced.ino index 811ee75..933895d 100644 --- a/examples/BH1750advanced/BH1750advanced.ino +++ b/examples/BH1750advanced/BH1750advanced.ino @@ -87,7 +87,7 @@ void setup(){ void loop() { - uint16_t lux = lightMeter.readLightLevel(); + float lux = lightMeter.readLightLevel(); Serial.print("Light: "); Serial.print(lux); Serial.println(" lx"); diff --git a/examples/BH1750onetime/BH1750onetime.ino b/examples/BH1750onetime/BH1750onetime.ino index 116f5d8..2988a85 100644 --- a/examples/BH1750onetime/BH1750onetime.ino +++ b/examples/BH1750onetime/BH1750onetime.ino @@ -33,7 +33,7 @@ void setup(){ void loop() { - uint16_t lux = lightMeter.readLightLevel(); + float lux = lightMeter.readLightLevel(); Serial.print("Light: "); Serial.print(lux); Serial.println(" lx"); diff --git a/examples/BH1750test/BH1750test.ino b/examples/BH1750test/BH1750test.ino index a47e99e..19a7a42 100644 --- a/examples/BH1750test/BH1750test.ino +++ b/examples/BH1750test/BH1750test.ino @@ -43,7 +43,7 @@ void setup(){ void loop() { - uint16_t lux = lightMeter.readLightLevel(); + float lux = lightMeter.readLightLevel(); Serial.print("Light: "); Serial.print(lux); Serial.println(" lx"); From bce523c75b90dddcf33533c5f3631b9def34edab Mon Sep 17 00:00:00 2001 From: coelner Date: Mon, 16 Jul 2018 20:01:13 +0200 Subject: [PATCH 05/14] backport of #ifdef BH1750_float case --- BH1750.cpp | 86 ++++++++++++++++++++++++++++++++++++++++++++++ BH1750.h | 5 +++ library.properties | 2 +- 3 files changed, 92 insertions(+), 1 deletion(-) diff --git a/BH1750.cpp b/BH1750.cpp index bda24b2..66db0e3 100644 --- a/BH1750.cpp +++ b/BH1750.cpp @@ -128,7 +128,92 @@ bool BH1750::configure(Mode mode) { } +#ifndef BH1750_FLOAT +/** + * Read light level from sensor + * @return Light level in lux (0.0 ~ 54613) + * 65534 : no valid return value + * 65533 : sensor not configured + */ +uint16_t BH1750::readLightLevel(bool maxWait) { + if (BH1750_MODE == UNCONFIGURED) { + Serial.println(F("[BH1750] Device is not configured!")); + return 65535-2; + } + + // Measurement result will be stored here + uint16_t level = 65535; + + // Send mode to sensor + Wire.beginTransmission(BH1750_I2CADDR); + __wire_write((uint8_t)BH1750_MODE); + Wire.endTransmission(); + + // Wait for measurement to be taken. + // Measurements have a maximum measurement time and a typical measurement + // time. The maxWait argument determines which measurement wait time is + // used when a one-time mode is being used. The typical (shorter) + // measurement time is used by default and if maxWait is set to True then + // the maximum measurement time will be used. See data sheet pages 2, 5 + // and 7 for more details. + // A continuous mode measurement can be read immediately after re-sending + // the mode command. + + switch (BH1750_MODE) { + + case BH1750::ONE_TIME_LOW_RES_MODE: + case BH1750::ONE_TIME_HIGH_RES_MODE: + case BH1750::ONE_TIME_HIGH_RES_MODE_2: + + if (BH1750_MODE == BH1750::ONE_TIME_LOW_RES_MODE) { + maxWait ? _delay_ms(24) : _delay_ms(16); + } + else { + maxWait ? _delay_ms(180) :_delay_ms(120); + } + break; + default: break; + } + + // Read two bytes from the sensor, which are low and high parts of the sensor + // value + if (2 == Wire.requestFrom(BH1750_I2CADDR, 2)) { + uint16_t tmp = 0; + tmp = __wire_read(); + tmp <<= 8; + tmp |= __wire_read(); + level = tmp; + + // Print raw value if debug enabled + #ifdef BH1750_DEBUG + Serial.print(F("[BH1750] Raw value: ")); + Serial.println(level); + #endif + + if (BH1750_MODE == BH1750::ONE_TIME_HIGH_RES_MODE_2 || BH1750_MODE == BH1750::CONTINUOUS_HIGH_RES_MODE_2) { + level /= 2; + } + // Convert raw value to lux + level /= 1.2; + + // Print converted value if debug enabled + #ifdef BH1750_DEBUG + Serial.print(F("[BH1750] Converted value: ")); + Serial.println(level); + #endif + + return level; + } + else { + return 65535-1; + } + + +} +#endif + +#ifdef BH1750_FLOAT /** * Read light level from sensor * @return Light level in lux (0.0 ~ 54612,5) @@ -209,3 +294,4 @@ float BH1750::readLightLevel(bool maxWait) { return level; } +#endif \ No newline at end of file diff --git a/BH1750.h b/BH1750.h index 4ef839f..1715f54 100644 --- a/BH1750.h +++ b/BH1750.h @@ -60,7 +60,12 @@ class BH1750 { BH1750(byte addr = 0x23); bool begin(Mode mode = CONTINUOUS_HIGH_RES_MODE); bool configure(Mode mode); + #ifndef BH1750_FLOAT + uint16_t readLightLevel(bool maxWait = false); + #endif + #ifdef BH1750_FLOAT float readLightLevel(bool maxWait = false); + #endif private: int BH1750_I2CADDR; diff --git a/library.properties b/library.properties index ef0d140..abf5a97 100644 --- a/library.properties +++ b/library.properties @@ -1,5 +1,5 @@ name=BH1750 -version=1.1.3 +version=1.1.4 author=Christopher Laws maintainer=Christopher Laws sentence=Digital light sensor breakout boards containing the BH1750FVI IC From 680cebb95656221dd7c3baeadeee0d2e0ba5df9e Mon Sep 17 00:00:00 2001 From: coelner Date: Mon, 16 Jul 2018 21:25:46 +0200 Subject: [PATCH 06/14] backports of #ifdef BH1750_FLOAT usage --- README.md | 26 +++++++++++++--------- examples/BH1750advanced/BH1750advanced.ino | 4 ++++ examples/BH1750onetime/BH1750onetime.ino | 4 ++++ examples/BH1750test/BH1750test.ino | 5 +++++ 4 files changed, 28 insertions(+), 11 deletions(-) diff --git a/README.md b/README.md index 7a3f6cd..850ad3c 100644 --- a/README.md +++ b/README.md @@ -39,13 +39,17 @@ back into One-Time mode. This library has been implemented to automatically reconfigure the sensor when you next attempt a measurement so you should not have to worry about such low level details. -Usually you will get a float value which represent the lux equivalent. - - Low Resolution Mode - (0.0 up to 54612.5 lux) - - High Resolution Mode - (0.0 up to 54612.5 lux) - - High Resolution Mode 2 - (0.0 up to 27306.25 lux) +Usually you will get an integer value which represent the lux equivalent. + - Low Resolution Mode - (generic range: 0.0 up to 54612.5 lux) + - High Resolution Mode - (generic range: 0.0 up to 54612.5 lux) + - High Resolution Mode 2 - (generic range: 0.0 up to 27306.25 lux) The sensor returns a 16 bit unsigned integer. Therefore the maximum value is limited in general. The standard conversion between the so called 'counts' to lux is 1/1.2, that means you get a smaller value. +If a error occurs you will get a positive value closely to the max. + - 65534 (65535-1) no valid data was transmitted from the sensor + - 65533 (65535-2) device is not configured +If you use the float, you get: If a error occurs you will get a negative value. - -1 no valid data was transmitted from the sensor - -2 device is not configured @@ -142,12 +146,12 @@ void loop() { Moving the sensor to face more light results in the lux measurements increasing. ``` BH1750 Test -Light: 70.0 lx -Light: 70.0 lx -Light: 59.0 lx -Light: 328.0 lx -Light: 333.0 lx -Light: 335.0 lx -Light: 332.0 lx +Light: 70 lx +Light: 70 lx +Light: 59 lx +Light: 328 lx +Light: 333 lx +Light: 335 lx +Light: 332 lx ``` There are more examples in the examples directory. diff --git a/examples/BH1750advanced/BH1750advanced.ino b/examples/BH1750advanced/BH1750advanced.ino index 933895d..3792620 100644 --- a/examples/BH1750advanced/BH1750advanced.ino +++ b/examples/BH1750advanced/BH1750advanced.ino @@ -23,7 +23,11 @@ #include /* + Remove the following if you want to use float +*/ +//#define BH1750_FLOAT 1 +/* 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) diff --git a/examples/BH1750onetime/BH1750onetime.ino b/examples/BH1750onetime/BH1750onetime.ino index 2988a85..b2a923c 100644 --- a/examples/BH1750onetime/BH1750onetime.ino +++ b/examples/BH1750onetime/BH1750onetime.ino @@ -10,6 +10,10 @@ preparation for the next measurement. */ +/* + Remove the following if you want to use float +*/ +//#define BH1750_FLOAT 1 #include #include diff --git a/examples/BH1750test/BH1750test.ino b/examples/BH1750test/BH1750test.ino index 19a7a42..c3b810e 100644 --- a/examples/BH1750test/BH1750test.ino +++ b/examples/BH1750test/BH1750test.ino @@ -20,6 +20,11 @@ */ +/* + Remove the following if you want to use float +*/ +//#define BH1750_FLOAT 1 + #include #include From 13dfed8a12f23b859dcbbc20031bf2bbb85f8485 Mon Sep 17 00:00:00 2001 From: cm Date: Tue, 31 Jul 2018 08:43:51 +0200 Subject: [PATCH 07/14] fix github hickhup --- BH1750.cpp | 194 ++++++++++++++++++++++++++++++++++++++++------------- 1 file changed, 148 insertions(+), 46 deletions(-) diff --git a/BH1750.cpp b/BH1750.cpp index 66db0e3..e0c42af 100644 --- a/BH1750.cpp +++ b/BH1750.cpp @@ -113,12 +113,16 @@ bool BH1750::configure(Mode mode) { return true; case 1: // too long for transmit buffer Serial.println(F("[BH1750] ERROR: too long for transmit buffer")); + break; case 2: // received NACK on transmit of address Serial.println(F("[BH1750] ERROR: received NACK on transmit of address")); + break; case 3: // received NACK on transmit of data Serial.println(F("[BH1750] ERROR: received NACK on transmit of data")); + break; case 4: // other error Serial.println(F("[BH1750] ERROR: other error")); + break; default: Serial.println(F("[BH1750] ERROR: undefined error")); break; @@ -128,22 +132,90 @@ bool BH1750::configure(Mode mode) { } +/** + * Configure BH1750 MTreg value + * MT reg = Measurement Time register + * @param MTreg a value between 32 and 254. Default: 69 + * @return bool true if MTReg successful set + * false if MTreg not changed or parameter out of range + */ +bool BH1750::setMTreg(byte MTreg) { + //Bug: lowest value seems to be 32! + if (MTreg <= 31 || MTreg > 254) { + Serial.println(F("[BH1750] ERROR: MTreg out of range")); + return false; + } + byte ack = 5; + // Send MTreg and the current mode to the sensor + // High bit: 01000_MT[7,6,5] + // Low bit: 011_MT[4,3,2,1,0] + Wire.beginTransmission(BH1750_I2CADDR); + __wire_write((0b01000 << 3) | (MTreg >> 5)); + ack = Wire.endTransmission(); + Wire.beginTransmission(BH1750_I2CADDR); + __wire_write((0b011 << 5 ) | (MTreg & 0b111)); + ack = ack | Wire.endTransmission(); + Wire.beginTransmission(BH1750_I2CADDR); + __wire_write(BH1750_MODE); + ack = ack | Wire.endTransmission(); + + // Wait a few moments to wake up + _delay_ms(10); + + // Check result code + switch (ack) { + case 0: + BH1750_MTreg = MTreg; + //Delay for specific continious mode to get valid values + switch (BH1750_MODE) { + case BH1750::CONTINUOUS_LOW_RES_MODE: + _delay_ms(24 * BH1750_MTreg/(byte)BH1750_DEFAULT_MTREG); + break; + case BH1750::CONTINUOUS_HIGH_RES_MODE: + case BH1750::CONTINUOUS_HIGH_RES_MODE_2: + _delay_ms(180 * BH1750_MTreg/(byte)BH1750_DEFAULT_MTREG); + break; + default: break; + } + return true; + case 1: // too long for transmit buffer + Serial.println(F("[BH1750] ERROR: too long for transmit buffer")); + break; + case 2: // received NACK on transmit of address + Serial.println(F("[BH1750] ERROR: received NACK on transmit of address")); + break; + case 3: // received NACK on transmit of data + Serial.println(F("[BH1750] ERROR: received NACK on transmit of data")); + break; + case 4: // other error + Serial.println(F("[BH1750] ERROR: other error")); + break; + default: + Serial.println(F("[BH1750] ERROR: undefined error")); + break; + } + + return false; +} + #ifndef BH1750_FLOAT /** * Read light level from sensor - * @return Light level in lux (0.0 ~ 54613) - * 65534 : no valid return value - * 65533 : sensor not configured + * bool maxWait : Wait maximum measurement delay time or not + * bool hundredth: Multiply internally with 100 and return this + * @return Light level in lux (0 ~ 117758 [11775800/100]) + * -1 : no valid return value + * -2 : sensor not configured */ -uint16_t BH1750::readLightLevel(bool maxWait) { +int32_t BH1750::readLightLevel(bool maxWait, bool hundredth) { if (BH1750_MODE == UNCONFIGURED) { Serial.println(F("[BH1750] Device is not configured!")); - return 65535-2; + return -2; } // Measurement result will be stored here - uint16_t level = 65535; + int32_t level = 0; // Send mode to sensor Wire.beginTransmission(BH1750_I2CADDR); @@ -163,23 +235,25 @@ uint16_t BH1750::readLightLevel(bool maxWait) { switch (BH1750_MODE) { case BH1750::ONE_TIME_LOW_RES_MODE: + maxWait ? _delay_ms(24 * BH1750_MTreg/(byte)BH1750_DEFAULT_MTREG) : _delay_ms(16 * BH1750_MTreg/(byte)BH1750_DEFAULT_MTREG); + break; case BH1750::ONE_TIME_HIGH_RES_MODE: case BH1750::ONE_TIME_HIGH_RES_MODE_2: - - if (BH1750_MODE == BH1750::ONE_TIME_LOW_RES_MODE) { - maxWait ? _delay_ms(24) : _delay_ms(16); - } - else { - maxWait ? _delay_ms(180) :_delay_ms(120); - } + maxWait ? _delay_ms(180 * BH1750_MTreg/(byte)BH1750_DEFAULT_MTREG) :_delay_ms(120 * BH1750_MTreg/(byte)BH1750_DEFAULT_MTREG); + + + + + + break; default: break; } // Read two bytes from the sensor, which are low and high parts of the sensor // value - if (2 == Wire.requestFrom(BH1750_I2CADDR, 2)) { - uint16_t tmp = 0; + if (2 == Wire.requestFrom((int)BH1750_I2CADDR, (int)2)) { + unsigned int tmp = 0; tmp = __wire_read(); tmp <<= 8; tmp |= __wire_read(); @@ -190,23 +264,41 @@ uint16_t BH1750::readLightLevel(bool maxWait) { Serial.print(F("[BH1750] Raw value: ")); Serial.println(level); #endif + + //use .decimals in the calculation + level *= 100; + + if (BH1750_MTreg != (byte)BH1750_DEFAULT_MTREG) { + level *= (byte)BH1750_DEFAULT_MTREG; + level /= (byte)BH1750_MTreg; + // Print MTreg factor if debug enabled + #ifdef BH1750_DEBUG + Serial.print(F("[BH1750] MTreg factor: ")); + Serial.print(BH1750_MTreg); Serial.print(F("/")); + Serial.println((byte)BH1750_DEFAULT_MTREG); + #endif + } + // Convert raw value to lux + level *= 10; + level /= BH1750_CONV_FACTOR; if (BH1750_MODE == BH1750::ONE_TIME_HIGH_RES_MODE_2 || BH1750_MODE == BH1750::CONTINUOUS_HIGH_RES_MODE_2) { level /= 2; } - // Convert raw value to lux - level /= 1.2; - + if (!hundredth) { + level /= 100; + } + // Print converted value if debug enabled #ifdef BH1750_DEBUG - Serial.print(F("[BH1750] Converted value: ")); + Serial.print(F("[BH1750] Converted int value: ")); Serial.println(level); #endif return level; } else { - return 65535-1; + return -1; } @@ -216,7 +308,7 @@ uint16_t BH1750::readLightLevel(bool maxWait) { #ifdef BH1750_FLOAT /** * Read light level from sensor - * @return Light level in lux (0.0 ~ 54612,5) + * @return Light level in lux (0.0 ~ 54612,5 [121556,85]) * -1 : no valid return value * -2 : sensor not configured */ @@ -248,23 +340,25 @@ float BH1750::readLightLevel(bool maxWait) { switch (BH1750_MODE) { case BH1750::ONE_TIME_LOW_RES_MODE: + maxWait ? _delay_ms(24 * BH1750_MTreg/(byte)BH1750_DEFAULT_MTREG) : _delay_ms(16 * BH1750_MTreg/(byte)BH1750_DEFAULT_MTREG); + break; case BH1750::ONE_TIME_HIGH_RES_MODE: case BH1750::ONE_TIME_HIGH_RES_MODE_2: - - if (BH1750_MODE == BH1750::ONE_TIME_LOW_RES_MODE) { - maxWait ? _delay_ms(24) : _delay_ms(16); - } - else { - maxWait ? _delay_ms(180) :_delay_ms(120); - } + maxWait ? _delay_ms(180 * BH1750_MTreg/(byte)BH1750_DEFAULT_MTREG) :_delay_ms(120 * BH1750_MTreg/(byte)BH1750_DEFAULT_MTREG); + + + + + + break; default: break; } // Read two bytes from the sensor, which are low and high parts of the sensor // value - if (2 == Wire.requestFrom(BH1750_I2CADDR, 2)) { - uint16_t tmp = 0; + if (2 == Wire.requestFrom((int)BH1750_I2CADDR, (int)2)) { + unsigned int tmp = 0; tmp = __wire_read(); tmp <<= 8; tmp |= __wire_read(); @@ -272,26 +366,34 @@ float BH1750::readLightLevel(bool maxWait) { } if (level != -1) { - // Print raw value if debug enabled - #ifdef BH1750_DEBUG - Serial.print(F("[BH1750] Raw value: ")); - Serial.println(level); - #endif + // Print raw value if debug enabled + #ifdef BH1750_DEBUG + Serial.print(F("[BH1750] Raw value: ")); + Serial.println(level); + #endif - if (BH1750_MODE == BH1750::ONE_TIME_HIGH_RES_MODE_2 || BH1750_MODE == BH1750::CONTINUOUS_HIGH_RES_MODE_2) { - level /= 2; - } - // Convert raw value to lux - level /= 1.2; + if (BH1750_MTreg != BH1750_DEFAULT_MTREG) { + level *= (float)((byte)BH1750_DEFAULT_MTREG/(float)BH1750_MTreg); + // Print MTreg factor if debug enabled + #ifdef BH1750_DEBUG + Serial.print(F("[BH1750] MTreg factor: ")); + Serial.println( String((float)((byte)BH1750_DEFAULT_MTREG/(float)BH1750_MTreg)) ); + #endif + } + if (BH1750_MODE == BH1750::ONE_TIME_HIGH_RES_MODE_2 || BH1750_MODE == BH1750::CONTINUOUS_HIGH_RES_MODE_2) { + level /= 2; + } + // Convert raw value to lux + level /= BH1750_CONV_FACTOR; - // Print converted value if debug enabled - #ifdef BH1750_DEBUG - Serial.print(F("[BH1750] Converted value: ")); - Serial.println(level); - #endif + // Print converted value if debug enabled + #ifdef BH1750_DEBUG + Serial.print(F("[BH1750] Converted float value: ")); + Serial.println(level); + #endif } return level; } -#endif \ No newline at end of file +#endif From 95f46e599effe3102204d27b11cbd3f3353fbf1f Mon Sep 17 00:00:00 2001 From: cm Date: Tue, 31 Jul 2018 09:07:45 +0200 Subject: [PATCH 08/14] fix broken header --- BH1750.h | 17 +++++- README.md | 25 ++++----- .../BH1750autoadjust/BH1750autoadjust.ino | 52 +++++++++++++++++++ keywords.txt | 3 +- 4 files changed, 82 insertions(+), 15 deletions(-) create mode 100644 examples/BH1750autoadjust/BH1750autoadjust.ino diff --git a/BH1750.h b/BH1750.h index 1715f54..c9aa54d 100644 --- a/BH1750.h +++ b/BH1750.h @@ -36,6 +36,9 @@ // Reset data register value - not accepted in POWER_DOWN mode #define BH1750_RESET 0x07 +// Default MTreg value +#define BH1750_DEFAULT_MTREG 69 + class BH1750 { public: @@ -60,15 +63,25 @@ class BH1750 { BH1750(byte addr = 0x23); bool begin(Mode mode = CONTINUOUS_HIGH_RES_MODE); bool configure(Mode mode); + bool setMTreg(byte MTreg); #ifndef BH1750_FLOAT - uint16_t readLightLevel(bool maxWait = false); + int32_t readLightLevel(bool maxWait = false, bool hundredth = false); #endif #ifdef BH1750_FLOAT float readLightLevel(bool maxWait = false); #endif private: - int BH1750_I2CADDR; + byte BH1750_I2CADDR; + byte BH1750_MTreg = (byte)BH1750_DEFAULT_MTREG; + // Correction factor used to calculate lux. Typical value is 1.2 but can range from 0.96 to 1.44. + // See data sheet (p.2, Measurement Accuracy) for more information. + #ifndef BH1750_FLOAT + const uint8_t BH1750_CONV_FACTOR = 12; + #endif + #ifdef BH1750_FLOAT + const float BH1750_CONV_FACTOR = 1.2; + #endif Mode BH1750_MODE = UNCONFIGURED; }; diff --git a/README.md b/README.md index 850ad3c..9dfda6b 100644 --- a/README.md +++ b/README.md @@ -46,13 +46,14 @@ Usually you will get an integer value which represent the lux equivalent. The sensor returns a 16 bit unsigned integer. Therefore the maximum value is limited in general. The standard conversion between the so called 'counts' to lux is 1/1.2, that means you get a smaller value. -If a error occurs you will get a positive value closely to the max. - - 65534 (65535-1) no valid data was transmitted from the sensor - - 65533 (65535-2) device is not configured -If you use the float, you get: -If a error occurs you will get a negative value. +As we use signed integer or float, if an error occurs you will get a negative value. - -1 no valid data was transmitted from the sensor - -2 device is not configured + +As the sensor counts impact of light in a specific time frame you could change this time frame. +This is needed if you use an overlay windows or compensate environmental influence like darkness. +This time frame is defined by a register which is called MTreg. Therefore you could choose a value between 32 and 254. +The default value is 69; keep in mind that the measurement time is changed accordingly. The datasheet for the BH1750 chip can be obtained [here](http://www.elechouse.com/elechouse/images/product/Digital%20light%20Sensor/bh1750fvi-e.pdf) @@ -146,12 +147,12 @@ void loop() { Moving the sensor to face more light results in the lux measurements increasing. ``` BH1750 Test -Light: 70 lx -Light: 70 lx -Light: 59 lx -Light: 328 lx -Light: 333 lx -Light: 335 lx -Light: 332 lx +Light: 70.0 lx +Light: 70.0 lx +Light: 59.0 lx +Light: 328.0 lx +Light: 333.0 lx +Light: 335.0 lx +Light: 332.0 lx ``` There are more examples in the examples directory. diff --git a/examples/BH1750autoadjust/BH1750autoadjust.ino b/examples/BH1750autoadjust/BH1750autoadjust.ino new file mode 100644 index 0000000..19a7a42 --- /dev/null +++ b/examples/BH1750autoadjust/BH1750autoadjust.ino @@ -0,0 +1,52 @@ +/* + + 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 +#include + +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(); + + Serial.println(F("BH1750 Test begin")); + +} + + +void loop() { + + float lux = lightMeter.readLightLevel(); + Serial.print("Light: "); + Serial.print(lux); + Serial.println(" lx"); + delay(1000); + +} diff --git a/keywords.txt b/keywords.txt index 2813d17..fa9a921 100644 --- a/keywords.txt +++ b/keywords.txt @@ -14,9 +14,9 @@ BH1750 KEYWORD1 begin KEYWORD2 configure KEYWORD2 +setMTreg KEYWORD2 readLightLevel KEYWORD2 - ####################################### # Instances (KEYWORD2) ####################################### @@ -31,3 +31,4 @@ BH1750_CONTINUOUS_LOW_RES_MODE LITERAL1 BH1750_ONE_TIME_HIGH_RES_MODE LITERAL1 BH1750_ONE_TIME_HIGH_RES_MODE_2 LITERAL1 BH1750_ONE_TIME_LOW_RES_MODE LITERAL1 +BH1750_DEFAULT_MTREG LITERAL1 From a30562d2783f6bfafb75190a06fd950f9b58a68f Mon Sep 17 00:00:00 2001 From: cm Date: Tue, 31 Jul 2018 09:18:40 +0200 Subject: [PATCH 09/14] example added --- .../BH1750autoadjust/BH1750autoadjust.ino | 39 ++++++++++++++----- 1 file changed, 30 insertions(+), 9 deletions(-) diff --git a/examples/BH1750autoadjust/BH1750autoadjust.ino b/examples/BH1750autoadjust/BH1750autoadjust.ino index 19a7a42..09ccc2c 100644 --- a/examples/BH1750autoadjust/BH1750autoadjust.ino +++ b/examples/BH1750autoadjust/BH1750autoadjust.ino @@ -25,16 +25,16 @@ BH1750 lightMeter; - void setup(){ - Serial.begin(9600); + Serial.begin(115200); // Initialize the I2C bus (BH1750 library doesn't do this automatically) - Wire.begin(); + Wire.begin(D2, D1); // On esp8266 you can select SCL and SDA pins using Wire.begin(D4, D3); - lightMeter.begin(); + lightMeter.begin(BH1750::ONE_TIME_HIGH_RES_MODE); + //lightMeter.setMTreg(69); //not needed, only mentioning it Serial.println(F("BH1750 Test begin")); @@ -42,11 +42,32 @@ void setup(){ void loop() { - - float lux = lightMeter.readLightLevel(); - Serial.print("Light: "); + //we use here the maxWait option due fail save and we want reduced information loss due missing decimal part + float lux = lightMeter.readLightLevel(true, true)/100.0; + if (lux > 40000.0) { + //reduce time slot - needed in direct sun light + if(lightMeter.setMTreg(32)) { + Serial.println(F("[DEBUG]: MTReg low")); + lux = lightMeter.readLightLevel(true, true)/100.0; + } + } + else { + if(lux > 10.0 && lightMeter.setMTreg(69)) { + Serial.println(F("[DEBUG]: MTReg default")); + lux = lightMeter.readLightLevel(true, true)/100.0; + } + } + if (lux <= 10.0) { + //very low light environment + if(lightMeter.setMTreg(254)) { + Serial.println(F("[DEBUG]: MTReg high")); + lux = lightMeter.readLightLevel(true, true)/100.0; + } + } + Serial.print(F("Light: ")); Serial.print(lux); - Serial.println(" lx"); - delay(1000); + Serial.println(F(" lx")); + Serial.println(F("--------------------------------------")); + delay(5000); } From 5a2c940f8c93eebbd14067daab3e28a188c087c5 Mon Sep 17 00:00:00 2001 From: coelner Date: Fri, 28 Sep 2018 07:26:52 +0200 Subject: [PATCH 10/14] fix wrong Wire.begin() in example sketch, move #BH1750_FLOAT to header file --- BH1750.cpp | 22 +++++-------------- BH1750.h | 3 +++ .../BH1750autoadjust/BH1750autoadjust.ino | 2 +- 3 files changed, 9 insertions(+), 18 deletions(-) diff --git a/BH1750.cpp b/BH1750.cpp index e0c42af..d3befc4 100644 --- a/BH1750.cpp +++ b/BH1750.cpp @@ -239,13 +239,7 @@ int32_t BH1750::readLightLevel(bool maxWait, bool hundredth) { break; case BH1750::ONE_TIME_HIGH_RES_MODE: case BH1750::ONE_TIME_HIGH_RES_MODE_2: - maxWait ? _delay_ms(180 * BH1750_MTreg/(byte)BH1750_DEFAULT_MTREG) :_delay_ms(120 * BH1750_MTreg/(byte)BH1750_DEFAULT_MTREG); - - - - - - + maxWait ? _delay_ms(180 * BH1750_MTreg/(byte)BH1750_DEFAULT_MTREG) :_delay_ms(120 * BH1750_MTreg/(byte)BH1750_DEFAULT_MTREG); break; default: break; } @@ -316,11 +310,11 @@ float BH1750::readLightLevel(bool maxWait) { if (BH1750_MODE == UNCONFIGURED) { Serial.println(F("[BH1750] Device is not configured!")); - return -2; + return -2.0; } // Measurement result will be stored here - float level = -1; + float level = -1.0; // Send mode to sensor Wire.beginTransmission(BH1750_I2CADDR); @@ -344,13 +338,7 @@ float BH1750::readLightLevel(bool maxWait) { break; case BH1750::ONE_TIME_HIGH_RES_MODE: case BH1750::ONE_TIME_HIGH_RES_MODE_2: - maxWait ? _delay_ms(180 * BH1750_MTreg/(byte)BH1750_DEFAULT_MTREG) :_delay_ms(120 * BH1750_MTreg/(byte)BH1750_DEFAULT_MTREG); - - - - - - + maxWait ? _delay_ms(180 * BH1750_MTreg/(byte)BH1750_DEFAULT_MTREG) :_delay_ms(120 * BH1750_MTreg/(byte)BH1750_DEFAULT_MTREG); break; default: break; } @@ -365,7 +353,7 @@ float BH1750::readLightLevel(bool maxWait) { level = tmp; } - if (level != -1) { + if (level != -1.0) { // Print raw value if debug enabled #ifdef BH1750_DEBUG Serial.print(F("[BH1750] Raw value: ")); diff --git a/BH1750.h b/BH1750.h index c9aa54d..aa58501 100644 --- a/BH1750.h +++ b/BH1750.h @@ -27,6 +27,9 @@ // Uncomment, to enable debug messages // #define BH1750_DEBUG +// Uncomment, to enable float values +// #define BH1750_FLOAT + // No active state #define BH1750_POWER_DOWN 0x00 diff --git a/examples/BH1750autoadjust/BH1750autoadjust.ino b/examples/BH1750autoadjust/BH1750autoadjust.ino index 09ccc2c..5d993bf 100644 --- a/examples/BH1750autoadjust/BH1750autoadjust.ino +++ b/examples/BH1750autoadjust/BH1750autoadjust.ino @@ -30,7 +30,7 @@ void setup(){ Serial.begin(115200); // Initialize the I2C bus (BH1750 library doesn't do this automatically) - Wire.begin(D2, D1); + Wire.begin(); // On esp8266 you can select SCL and SDA pins using Wire.begin(D4, D3); lightMeter.begin(BH1750::ONE_TIME_HIGH_RES_MODE); From c2e98dbbda1ace1d695f2bc33d1a6c57c41eafa8 Mon Sep 17 00:00:00 2001 From: coelner Date: Sat, 29 Sep 2018 19:30:44 +0200 Subject: [PATCH 11/14] fix examples, note to global maximum value --- BH1750.cpp | 3 ++- examples/BH1750advanced/BH1750advanced.ino | 5 ----- examples/BH1750onetime/BH1750onetime.ino | 4 ---- examples/BH1750test/BH1750test.ino | 4 ---- 4 files changed, 2 insertions(+), 14 deletions(-) diff --git a/BH1750.cpp b/BH1750.cpp index d3befc4..39dbc27 100644 --- a/BH1750.cpp +++ b/BH1750.cpp @@ -302,7 +302,8 @@ int32_t BH1750::readLightLevel(bool maxWait, bool hundredth) { #ifdef BH1750_FLOAT /** * Read light level from sensor - * @return Light level in lux (0.0 ~ 54612,5 [121556,85]) + * The return value differs if the MTreg value is changed. The global maximum value is noted in the square brackets + * @return Light level in lux (0.0 ~ 54612,5 [117758,203]) * -1 : no valid return value * -2 : sensor not configured */ diff --git a/examples/BH1750advanced/BH1750advanced.ino b/examples/BH1750advanced/BH1750advanced.ino index 3792620..0d9851e 100644 --- a/examples/BH1750advanced/BH1750advanced.ino +++ b/examples/BH1750advanced/BH1750advanced.ino @@ -22,11 +22,6 @@ #include #include -/* - Remove the following if you want to use float -*/ -//#define BH1750_FLOAT 1 - /* BH1750 can be physically configured to use two I2C addresses: - 0x23 (most common) (if ADD pin had < 0.7VCC voltage) diff --git a/examples/BH1750onetime/BH1750onetime.ino b/examples/BH1750onetime/BH1750onetime.ino index b2a923c..2988a85 100644 --- a/examples/BH1750onetime/BH1750onetime.ino +++ b/examples/BH1750onetime/BH1750onetime.ino @@ -10,10 +10,6 @@ preparation for the next measurement. */ -/* - Remove the following if you want to use float -*/ -//#define BH1750_FLOAT 1 #include #include diff --git a/examples/BH1750test/BH1750test.ino b/examples/BH1750test/BH1750test.ino index c3b810e..7b6b272 100644 --- a/examples/BH1750test/BH1750test.ino +++ b/examples/BH1750test/BH1750test.ino @@ -20,10 +20,6 @@ */ -/* - Remove the following if you want to use float -*/ -//#define BH1750_FLOAT 1 #include #include From 73606f1a1cc9e6bb573b8882346bd56b2e3af1a1 Mon Sep 17 00:00:00 2001 From: coelner Date: Mon, 1 Oct 2018 07:00:34 +0200 Subject: [PATCH 12/14] remove int32 calculation --- BH1750.cpp | 103 ----------------------------------------------------- BH1750.h | 13 ------- 2 files changed, 116 deletions(-) diff --git a/BH1750.cpp b/BH1750.cpp index 39dbc27..056ce13 100644 --- a/BH1750.cpp +++ b/BH1750.cpp @@ -198,108 +198,6 @@ bool BH1750::setMTreg(byte MTreg) { return false; } -#ifndef BH1750_FLOAT -/** - * Read light level from sensor - * bool maxWait : Wait maximum measurement delay time or not - * bool hundredth: Multiply internally with 100 and return this - * @return Light level in lux (0 ~ 117758 [11775800/100]) - * -1 : no valid return value - * -2 : sensor not configured - */ -int32_t BH1750::readLightLevel(bool maxWait, bool hundredth) { - - if (BH1750_MODE == UNCONFIGURED) { - Serial.println(F("[BH1750] Device is not configured!")); - return -2; - } - - // Measurement result will be stored here - int32_t level = 0; - - // Send mode to sensor - Wire.beginTransmission(BH1750_I2CADDR); - __wire_write((uint8_t)BH1750_MODE); - Wire.endTransmission(); - - // Wait for measurement to be taken. - // Measurements have a maximum measurement time and a typical measurement - // time. The maxWait argument determines which measurement wait time is - // used when a one-time mode is being used. The typical (shorter) - // measurement time is used by default and if maxWait is set to True then - // the maximum measurement time will be used. See data sheet pages 2, 5 - // and 7 for more details. - // A continuous mode measurement can be read immediately after re-sending - // the mode command. - - switch (BH1750_MODE) { - - case BH1750::ONE_TIME_LOW_RES_MODE: - maxWait ? _delay_ms(24 * BH1750_MTreg/(byte)BH1750_DEFAULT_MTREG) : _delay_ms(16 * BH1750_MTreg/(byte)BH1750_DEFAULT_MTREG); - break; - case BH1750::ONE_TIME_HIGH_RES_MODE: - case BH1750::ONE_TIME_HIGH_RES_MODE_2: - maxWait ? _delay_ms(180 * BH1750_MTreg/(byte)BH1750_DEFAULT_MTREG) :_delay_ms(120 * BH1750_MTreg/(byte)BH1750_DEFAULT_MTREG); - break; - default: break; - } - - // Read two bytes from the sensor, which are low and high parts of the sensor - // value - if (2 == Wire.requestFrom((int)BH1750_I2CADDR, (int)2)) { - unsigned int tmp = 0; - tmp = __wire_read(); - tmp <<= 8; - tmp |= __wire_read(); - level = tmp; - - // Print raw value if debug enabled - #ifdef BH1750_DEBUG - Serial.print(F("[BH1750] Raw value: ")); - Serial.println(level); - #endif - - //use .decimals in the calculation - level *= 100; - - if (BH1750_MTreg != (byte)BH1750_DEFAULT_MTREG) { - level *= (byte)BH1750_DEFAULT_MTREG; - level /= (byte)BH1750_MTreg; - // Print MTreg factor if debug enabled - #ifdef BH1750_DEBUG - Serial.print(F("[BH1750] MTreg factor: ")); - Serial.print(BH1750_MTreg); Serial.print(F("/")); - Serial.println((byte)BH1750_DEFAULT_MTREG); - #endif - } - // Convert raw value to lux - level *= 10; - level /= BH1750_CONV_FACTOR; - - if (BH1750_MODE == BH1750::ONE_TIME_HIGH_RES_MODE_2 || BH1750_MODE == BH1750::CONTINUOUS_HIGH_RES_MODE_2) { - level /= 2; - } - if (!hundredth) { - level /= 100; - } - - // Print converted value if debug enabled - #ifdef BH1750_DEBUG - Serial.print(F("[BH1750] Converted int value: ")); - Serial.println(level); - #endif - - return level; - } - else { - return -1; - } - - -} -#endif - -#ifdef BH1750_FLOAT /** * Read light level from sensor * The return value differs if the MTreg value is changed. The global maximum value is noted in the square brackets @@ -385,4 +283,3 @@ float BH1750::readLightLevel(bool maxWait) { return level; } -#endif diff --git a/BH1750.h b/BH1750.h index aa58501..0bcb48c 100644 --- a/BH1750.h +++ b/BH1750.h @@ -27,9 +27,6 @@ // Uncomment, to enable debug messages // #define BH1750_DEBUG -// Uncomment, to enable float values -// #define BH1750_FLOAT - // No active state #define BH1750_POWER_DOWN 0x00 @@ -67,24 +64,14 @@ class BH1750 { bool begin(Mode mode = CONTINUOUS_HIGH_RES_MODE); bool configure(Mode mode); bool setMTreg(byte MTreg); - #ifndef BH1750_FLOAT - int32_t readLightLevel(bool maxWait = false, bool hundredth = false); - #endif - #ifdef BH1750_FLOAT float readLightLevel(bool maxWait = false); - #endif private: byte BH1750_I2CADDR; byte BH1750_MTreg = (byte)BH1750_DEFAULT_MTREG; // Correction factor used to calculate lux. Typical value is 1.2 but can range from 0.96 to 1.44. // See data sheet (p.2, Measurement Accuracy) for more information. - #ifndef BH1750_FLOAT - const uint8_t BH1750_CONV_FACTOR = 12; - #endif - #ifdef BH1750_FLOAT const float BH1750_CONV_FACTOR = 1.2; - #endif Mode BH1750_MODE = UNCONFIGURED; }; From 96febfa15d8ba0b5750b31a3dcde153c959d3983 Mon Sep 17 00:00:00 2001 From: coelner Date: Tue, 2 Oct 2018 14:23:18 +0200 Subject: [PATCH 13/14] fix autoadjust example and clarify README --- README.md | 5 +++-- examples/BH1750autoadjust/BH1750autoadjust.ino | 10 +++++----- 2 files changed, 8 insertions(+), 7 deletions(-) diff --git a/README.md b/README.md index 9dfda6b..b58d867 100644 --- a/README.md +++ b/README.md @@ -44,11 +44,12 @@ Usually you will get an integer value which represent the lux equivalent. - High Resolution Mode - (generic range: 0.0 up to 54612.5 lux) - High Resolution Mode 2 - (generic range: 0.0 up to 27306.25 lux) -The sensor returns a 16 bit unsigned integer. Therefore the maximum value is limited in general. +The sensor itself returns a 16 bit unsigned integer. Therefore the maximum value is limited in general. The standard conversion between the so called 'counts' to lux is 1/1.2, that means you get a smaller value. -As we use signed integer or float, if an error occurs you will get a negative value. +As we use float, if an error occurs you will get a negative value. - -1 no valid data was transmitted from the sensor - -2 device is not configured +Otherwise the measured counts are converted to lux and returned. If no advanced parameters are changed the maximum lux value is 54612.5 lx. As the sensor counts impact of light in a specific time frame you could change this time frame. This is needed if you use an overlay windows or compensate environmental influence like darkness. diff --git a/examples/BH1750autoadjust/BH1750autoadjust.ino b/examples/BH1750autoadjust/BH1750autoadjust.ino index 5d993bf..7152e91 100644 --- a/examples/BH1750autoadjust/BH1750autoadjust.ino +++ b/examples/BH1750autoadjust/BH1750autoadjust.ino @@ -42,26 +42,26 @@ void setup(){ void loop() { - //we use here the maxWait option due fail save and we want reduced information loss due missing decimal part - float lux = lightMeter.readLightLevel(true, true)/100.0; + //we use here the maxWait option due fail save + float lux = lightMeter.readLightLevel(true); if (lux > 40000.0) { //reduce time slot - needed in direct sun light if(lightMeter.setMTreg(32)) { Serial.println(F("[DEBUG]: MTReg low")); - lux = lightMeter.readLightLevel(true, true)/100.0; + lux = lux = lightMeter.readLightLevel(true); } } else { if(lux > 10.0 && lightMeter.setMTreg(69)) { Serial.println(F("[DEBUG]: MTReg default")); - lux = lightMeter.readLightLevel(true, true)/100.0; + lux = lux = lightMeter.readLightLevel(true); } } if (lux <= 10.0) { //very low light environment if(lightMeter.setMTreg(254)) { Serial.println(F("[DEBUG]: MTReg high")); - lux = lightMeter.readLightLevel(true, true)/100.0; + lux = lux = lightMeter.readLightLevel(true); } } Serial.print(F("Light: ")); From e75ccea0d8be504275716638058877ac435e7cb1 Mon Sep 17 00:00:00 2001 From: coelner Date: Wed, 3 Oct 2018 16:04:30 +0200 Subject: [PATCH 14/14] Release Candidate 1. minor textual changes --- .../BH1750autoadjust/BH1750autoadjust.ino | 71 ++++++++++++------- 1 file changed, 46 insertions(+), 25 deletions(-) diff --git a/examples/BH1750autoadjust/BH1750autoadjust.ino b/examples/BH1750autoadjust/BH1750autoadjust.ino index 7152e91..c9aeec7 100644 --- a/examples/BH1750autoadjust/BH1750autoadjust.ino +++ b/examples/BH1750autoadjust/BH1750autoadjust.ino @@ -3,7 +3,12 @@ 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. + one shot mode and then makes a light level reading every five second. + 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: @@ -27,7 +32,7 @@ BH1750 lightMeter; void setup(){ - Serial.begin(115200); + Serial.begin(9600); // Initialize the I2C bus (BH1750 library doesn't do this automatically) Wire.begin(); @@ -40,34 +45,50 @@ void setup(){ } - void loop() { //we use here the maxWait option due fail save float lux = lightMeter.readLightLevel(true); - if (lux > 40000.0) { - //reduce time slot - needed in direct sun light - if(lightMeter.setMTreg(32)) { - Serial.println(F("[DEBUG]: MTReg low")); - lux = lux = lightMeter.readLightLevel(true); - } - } - else { - if(lux > 10.0 && lightMeter.setMTreg(69)) { - Serial.println(F("[DEBUG]: MTReg default")); - lux = lux = lightMeter.readLightLevel(true); - } - } - if (lux <= 10.0) { - //very low light environment - if(lightMeter.setMTreg(254)) { - Serial.println(F("[DEBUG]: MTReg high")); - lux = lux = lightMeter.readLightLevel(true); - } - } Serial.print(F("Light: ")); Serial.print(lux); Serial.println(F(" lx")); - Serial.println(F("--------------------------------------")); - delay(5000); + + if (lux < 0) { + Serial.println(F("[DEBUG]: 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 default value for normal 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); }