0.2.10 BH1750FVI_RT

This commit is contained in:
rob tillaart 2022-10-29 14:25:01 +02:00
parent 11c4e36f6b
commit 4f4650a2fd
8 changed files with 159 additions and 88 deletions

View File

@ -1,3 +1,18 @@
platforms:
rpipico:
board: rp2040:rp2040:rpipico
package: rp2040:rp2040
gcc:
features:
defines:
- ARDUINO_ARCH_RP2040
warnings:
flags:
packages:
rp2040:rp2040:
url: https://github.com/earlephilhower/arduino-pico/releases/download/global/package_rp2040_index.json
compile: compile:
# Choosing to run compilation tests on 2 different Arduino platforms # Choosing to run compilation tests on 2 different Arduino platforms
platforms: platforms:
@ -7,5 +22,7 @@ compile:
# - leonardo # - leonardo
- m4 - m4
- esp32 - esp32
# - esp8266 - esp8266
# - mega2560 # - mega2560
- rpipico

View File

@ -1,27 +1,11 @@
// //
// FILE: BH1750FVI.cpp // FILE: BH1750FVI.cpp
// AUTHOR: Rob Tillaart // AUTHOR: Rob Tillaart
// VERSION: 0.2.9 // VERSION: 0.2.10
// PURPOSE: library for BH1750FVI lux sensor Arduino // PURPOSE: library for BH1750FVI lux sensor Arduino
// URL: https://github.com/RobTillaart/BH1750FVI // URL: https://github.com/RobTillaart/BH1750FVI
// //
// HISTORY // HISTORY: see changelog.md
// 0.1.0 2020-02-02 initial version
// 0.1.1 2020-03-28 refactor
// 0.1.2 2020-03-29 unique name in repo, and new release tag.
// 0.1.3 2020-06-05 fix library.json file
// 0.1.4 2020-08-14 clean up tabs/spaces;
// 0.2.0 2020-08-18 implement logic for LOW & HIGH2;
// implement correction factor; examples;
// 0.2.1 2020-08-31 implement angle factor
// 0.2.2 2020-09-04 implement temperature compensation
// 0.2.3 2020-09-04 implement wavelength compensation
// 0.2.4 2020-11-27 fix #10 rename _sensitivityFactor for ESP32
// 0.2.5 2020-12-12 add Arduino-CI and unit tests
// 0.2.6 2021-01-16 add reset()
// 0.2.7 2021-06-08 add unit tests, improved correction factor code
// 0.2.8 2021-10-19 update Arduino-CI, badges in readme
// 0.2.9 2021-12-14 update library.json, license
#include "BH1750FVI.h" #include "BH1750FVI.h"
@ -73,7 +57,7 @@ bool BH1750FVI::isConnected()
bool BH1750FVI::isReady() bool BH1750FVI::isReady()
{ {
// max times from datasheet P2 + P11; // max times from datasheet P2 + P11;
uint8_t timeout[3] = { 16, 120, 120 }; uint8_t timeout[3] = { 16, 120, 120 };
if (_mode < 3) if (_mode < 3)
{ {
@ -92,25 +76,25 @@ float BH1750FVI::getRaw(void)
float BH1750FVI::getLux(void) float BH1750FVI::getLux(void)
{ {
// lux without mode correction // lux without mode correction
float lux = getRaw(); float lux = getRaw();
// sensitivity factor // sensitivity factor
if (_sensitivityFactor != BH1750FVI_REFERENCE_TIME) if (_sensitivityFactor != BH1750FVI_REFERENCE_TIME)
{ {
lux *= (1.0 * BH1750FVI_REFERENCE_TIME) / _sensitivityFactor; lux *= (1.0 * BH1750FVI_REFERENCE_TIME) / _sensitivityFactor;
} }
// angle compensation // angle compensation
if (_angle != 0) if (_angle != 0)
{ {
lux *= _angleFactor; lux *= _angleFactor;
} }
// temperature compensation. // temperature compensation.
if (_temp != 20) if (_temp != 20)
{ {
lux *= _tempFactor; lux *= _tempFactor;
} }
// wavelength compensation. // wavelength compensation.
if (_waveLength != 580) if (_waveLength != 580)
{ {
lux *= _waveLengthFactor; lux *= _waveLengthFactor;
@ -133,7 +117,7 @@ int BH1750FVI::getError()
//////////////////////////////////////////// ////////////////////////////////////////////
// //
// operational mode // operational mode
// //
void BH1750FVI::setContHighRes() void BH1750FVI::setContHighRes()
{ {
@ -185,14 +169,14 @@ void BH1750FVI::setOnceLowRes()
//////////////////////////////////////////// ////////////////////////////////////////////
// //
// measurement timing // measurement timing
// //
// P11 datasheet // P11 datasheet
void BH1750FVI::changeTiming(uint8_t time) void BH1750FVI::changeTiming(uint8_t time)
{ {
time = constrain(time, 31, 254); time = constrain(time, 31, 254);
_sensitivityFactor = time; _sensitivityFactor = time;
// P5 instruction set table // P5 instruction set table
uint8_t Hbits = 0x40 | (time >> 5); uint8_t Hbits = 0x40 | (time >> 5);
uint8_t Lbits = 0x60 | (time & 0x1F); uint8_t Lbits = 0x60 | (time & 0x1F);
command(Hbits); command(Hbits);
@ -202,7 +186,7 @@ void BH1750FVI::changeTiming(uint8_t time)
uint8_t BH1750FVI::setCorrectionFactor(float factor) uint8_t BH1750FVI::setCorrectionFactor(float factor)
{ {
// 31 .. 254 are range P11 - constrained in changeTIming call // 31 .. 254 are range P11 - constrained in changeTIming call
uint8_t timingValue = round(BH1750FVI_REFERENCE_TIME * factor); uint8_t timingValue = round(BH1750FVI_REFERENCE_TIME * factor);
changeTiming(timingValue); changeTiming(timingValue);
return _sensitivityFactor; return _sensitivityFactor;
@ -219,7 +203,7 @@ float BH1750FVI::getCorrectionFactor()
float BH1750FVI::setTemperature(int temp) float BH1750FVI::setTemperature(int temp)
{ {
_temp = temp; _temp = temp;
// _tempFactor = 1.0f - (_temp - 20.0f) / 2000.0f; // _tempFactor = 1.0f - (_temp - 20.0f) / 2000.0f;
_tempFactor = 1.0f - (_temp - 20.0f) * 0.0005f; _tempFactor = 1.0f - (_temp - 20.0f) * 0.0005f;
return _tempFactor; return _tempFactor;
} }
@ -228,13 +212,13 @@ float BH1750FVI::setTemperature(int temp)
float BH1750FVI::setAngle(int degrees) float BH1750FVI::setAngle(int degrees)
{ {
_angle = constrain(degrees, -89, 89); _angle = constrain(degrees, -89, 89);
// Lamberts Law. // Lamberts Law.
_angleFactor = 1.0f / cos(_angle * (PI / 180.0f)); _angleFactor = 1.0f / cos(_angle * (PI / 180.0f));
return _angleFactor; return _angleFactor;
} }
// interpolation tables uses more RAM (versus progmem) // interpolation tables uses more RAM (versus progmem)
float BH1750FVI::setWaveLength(int waveLength) float BH1750FVI::setWaveLength(int waveLength)
{ {
_waveLength = constrain(waveLength, 400, 715); _waveLength = constrain(waveLength, 400, 715);
@ -254,7 +238,7 @@ float BH1750FVI::setWaveLength(int waveLength)
/////////////////////////////////////////////////////////// ///////////////////////////////////////////////////////////
// //
// PRIVATE // PRIVATE
// //
uint16_t BH1750FVI::readData() uint16_t BH1750FVI::readData()
{ {

View File

@ -1,10 +1,10 @@
#pragma once #pragma once
// //
// FILE: BH1750FVI.h // FILE: BH1750FVI.h
// AUTHOR: Rob dot Tillaart at gmail dot com // AUTHOR: Rob Tillaart
// VERSION: 0.2.9 // VERSION: 0.2.10
// PURPOSE: Arduino library for BH1750FVI (GY-30) lux sensor // PURPOSE: Arduino library for BH1750FVI (GY-30) lux sensor
// HISTORY: See BH1750FVI.cpp // HISTORY: see changelog.md
// //
@ -28,7 +28,7 @@
#include "Arduino.h" #include "Arduino.h"
#define BH1750FVI_LIB_VERSION (F("0.2.9")) #define BH1750FVI_LIB_VERSION (F("0.2.10"))
#define BH1750FVI_DEFAULT_ADDRESS 0x23 #define BH1750FVI_DEFAULT_ADDRESS 0x23
@ -46,7 +46,7 @@
#define BH1750FVI_ONCE_HIGH2 0x21 #define BH1750FVI_ONCE_HIGH2 0x21
#define BH1750FVI_ONCE_LOW 0x23 #define BH1750FVI_ONCE_LOW 0x23
#define BH1750FVI_REFERENCE_TIME 0x45 // 69 #define BH1750FVI_REFERENCE_TIME 0x45 // 69 = default
#define BH1750FVI_MODE_LOW 0x00 #define BH1750FVI_MODE_LOW 0x00
#define BH1750FVI_MODE_HIGH 0x01 #define BH1750FVI_MODE_HIGH 0x01
@ -63,18 +63,18 @@ class BH1750FVI
public: public:
#if defined(ESP8266) || defined(ESP32) #if defined(ESP8266) || defined(ESP32)
// dataPin and clockPin can be used for ESP8266 // dataPin and clockPin can be used for ESP8266
BH1750FVI(const uint8_t address , const uint8_t dataPin, const uint8_t clockPin); BH1750FVI(const uint8_t address , const uint8_t dataPin, const uint8_t clockPin);
#endif #endif
BH1750FVI(const uint8_t address, TwoWire *wire = &Wire); BH1750FVI(const uint8_t address, TwoWire *wire = &Wire);
// returns true if isConnected() // returns true if isConnected()
bool begin(); // resets to constructor defaults. (use with care) bool begin(); // resets to constructor defaults. (use with care)
bool isConnected(); // returns true if address is on I2C bus bool isConnected(); // returns true if address is on I2C bus
float getRaw(); // no HIGH2 mode + no sensitivity factor. float getRaw(); // no HIGH2 mode + no sensitivity factor.
float getLux(); float getLux();
int getError(); int getError();
@ -85,7 +85,7 @@ public:
// MODE TIME RESOLUTION // MODE TIME RESOLUTION
// 2 HIGH2 120 ms 0.5 lux // recommended max * 1.5 = 180 ms // 2 HIGH2 120 ms 0.5 lux // recommended max * 1.5 = 180 ms
// 1 HIGH 120 ms 1.0 lux // 1 HIGH 120 ms 1.0 lux
// 0 LOW 16 ms 4.0 lux // 0 LOW 16 ms 4.0 lux
uint8_t getMode() { return _mode; }; uint8_t getMode() { return _mode; };
@ -99,41 +99,41 @@ public:
void setOnceHighRes(); void setOnceHighRes();
void setOnceHigh2Res(); void setOnceHigh2Res();
void setOnceLowRes(); void setOnceLowRes();
bool isReady(); // only after setOnce...Res(); bool isReady(); // only after setOnce...Res();
// read datasheet P11 about details of the correction or sensitivity factor // read datasheet P11 about details of the correction or sensitivity factor
// to be used for very high and very low brightness // to be used for very high and very low brightness
// or to correct for e.g. transparency // or to correct for e.g. transparency
void changeTiming(uint8_t time = BH1750FVI_REFERENCE_TIME); // 69 is default void changeTiming(uint8_t time = BH1750FVI_REFERENCE_TIME); // 69 is default
// returns changeTiming() parameter
// returns changeTiming() parameter
uint8_t setCorrectionFactor(float factor = 1); // 0.45 .. 3.68 uint8_t setCorrectionFactor(float factor = 1); // 0.45 .. 3.68
// returns percentage set. // returns percentage set.
float getCorrectionFactor(); float getCorrectionFactor();
// read datasheet P3 and check figure 4 and 5. // read datasheet P3 and check figure 4 and 5.
// setAngle is constrained to -89..+89 // setAngle is constrained to -89..+89
// returns the angle correction factor // returns the angle correction factor
float setAngle(int degrees = 0); float setAngle(int degrees = 0);
int getAngle() { return _angle; }; int getAngle() { return _angle; };
// datasheet P3 figure 7 // datasheet P3 figure 7
// Effect of temperature is about 3% / 60°C ~~ 1% / 20°C // Effect of temperature is about 3% / 60°C ~~ 1% / 20°C
// to be used if temp is really hot or cold. // to be used if temp is really hot or cold.
// returns the temperature correction factor // returns the temperature correction factor
float setTemperature(int temp = 20); float setTemperature(int temp = 20);
int getTemperature() { return _temp; }; int getTemperature() { return _temp; };
// datasheet Page 3 figure 1 (experimental correction) // datasheet Page 3 figure 1 (experimental correction)
// Effect of wavelength can be substantial, // Effect of wavelength can be substantial,
// correction is calculated by multiple linear approximations. // correction is calculated by multiple linear approximations.
// wavelength of 580 ==> correction == 1 // wavelength of 580 ==> correction == 1
// returns the wavelength correction factor // returns the wavelength correction factor
float setWaveLength(int waveLength = 580); float setWaveLength(int waveLength = 580);
int getWaveLength() { return _waveLength; }; int getWaveLength() { return _waveLength; };
@ -161,3 +161,4 @@ private:
// -- END OF FILE -- // -- END OF FILE --

View File

@ -0,0 +1,69 @@
# Change Log AD520X
All notable changes to this project will be documented in this file.
The format is based on [Keep a Changelog](http://keepachangelog.com/)
and this project adheres to [Semantic Versioning](http://semver.org/).
## [0.2.10] - 2022-10-28
- Add RP2040 support to build-CI.
- Add CHANGELOG.md
- update unit test
##[0.2.9] - 2021-12-14
- update library.json
- update license
##[0.2.8] - 2021-10-19
- update Arduino-CI
- add badges in readme.md
##[0.2.7] - 2021-06-08
- add unit tests
- improved correction factor code
##[0.2.6] - 2021-01-16
- add reset()
##[0.2.5] - 2020-12-12
- add Arduino-CI and unit tests
##[0.2.4] - 2020-11-27
- fix #10 rename \_sensitivityFactor for ESP32
##[0.2.3] - 2020-09-04
- implement wavelength compensation
##[0.2.2] - 2020-09-04
- implement temperature compensation
##[0.2.1] - 2020-08-31
- implement angle factor
##[0.2.0] - 2020-08-18
- implement logic for LOW & HIGH2
- implement correction factor
- add examples
----
## [0.1.4] - 2020-08-14
- clean up tabs/spaces
## [0.1.3] - 2020-06-05
- fix library.json
## [0.1.2] - 2020-03-29
- unique name in repo
- new release tag
## [0.1.1] - 2020-03-28
- refactor
## [0.1.0] - 2020-02-02
- initial version
----

View File

@ -74,7 +74,7 @@ Note: the breakout board was 5 volt tolerant.
- **float getLux()** reads the lux sensor and corrects for correctionFactor, mode, temperature and angle. - **float getLux()** reads the lux sensor and corrects for correctionFactor, mode, temperature and angle.
### management ### Management
- **int getError()** get the latest error code, mainly for debugging. - **int getError()** get the latest error code, mainly for debugging.
- **void powerOn()** wakes up the sensor. - **void powerOn()** wakes up the sensor.
@ -84,7 +84,8 @@ Note: the breakout board was 5 volt tolerant.
### Mode operators ### Mode operators
- **uint8_t getMode()** gets the mode set by one of the set functions. See table above. - **uint8_t getMode()** gets the mode set by one of the set functions.
See table above.
- **void setContHighRes()** continuous mode in HIGH resolution. - **void setContHighRes()** continuous mode in HIGH resolution.
- **void setContHigh2Res()** continuous mode in HIGH2 resolution. - **void setContHigh2Res()** continuous mode in HIGH2 resolution.
- **void setContLowRes()** continuous mode in LOW resolution. - **void setContLowRes()** continuous mode in LOW resolution.
@ -125,7 +126,8 @@ to angles between -89 - +89 degrees.
If the light is perpendicular on the sensor the angle to use is 0 degrees. If the light is perpendicular on the sensor the angle to use is 0 degrees.
Light coming from the side is 90 degrees. Light coming from the side is 90 degrees.
- **float setAngle(int degrees = 0)** adjust the lux to incoming angle in degrees (-89..89). Returns the angle correction factor. - **float setAngle(int degrees = 0)** adjust the lux to incoming angle in degrees (-89..89).
Returns the angle correction factor.
- **int getAngle()** returns set angle in degrees, 0 by default is perpendicular. - **int getAngle()** returns set angle in degrees, 0 by default is perpendicular.
@ -135,7 +137,8 @@ The reference temperature of the sensor = 20°C.
The effect of temperature is small, about 3% per 60°C ==> 1% per 20°C The effect of temperature is small, about 3% per 60°C ==> 1% per 20°C
so only on either a hot roof or on a icy cold day the effect is substantial. so only on either a hot roof or on a icy cold day the effect is substantial.
- **float setTemperature(int temp = 20)** see datasheet P3 fig7 Returns the temperature correction factor - **float setTemperature(int temp = 20)** see datasheet P3 fig 7.
Returns the temperature correction factor.
- **int getTemperature()** returns temperature set, default = 20°C. - **int getTemperature()** returns temperature set, default = 20°C.
@ -147,8 +150,9 @@ compensate for it by setting the wavelength. It can also be used when using filt
As said it is not tested so use at your own risk, but I am interested in your experiences As said it is not tested so use at your own risk, but I am interested in your experiences
if you do real tests with it. if you do real tests with it.
- **float setWaveLength(int wavelength = 580)** set wavelength, returns the wavelength correction factor. - **float setWaveLength(int wavelength = 580)** set wavelength.
- **int getWaveLength()** returns set wavelength Returns the wavelength correction factor.
- **int getWaveLength()** returns set wavelength.
As the graph (figure 1) is not linear it is approximated by linear interpolation with the As the graph (figure 1) is not linear it is approximated by linear interpolation with the
following six points. following six points.
@ -164,7 +168,7 @@ following six points.
| 715 | 1 | | 715 | 1 |
Values outside the range will be mapped upon 400 or 715. Values outside the range will be mapped upon 400 or 715.
Default wavelength will be 580 as that gives 100% Default wavelength will be 580 as that gives 100%.
## Operation ## Operation
@ -178,7 +182,6 @@ See samples...
After a **getLux()** call one can clean the data register explicitly with After a **getLux()** call one can clean the data register explicitly with
**reset()**. Then a call to **isReady()** fetches data and as long as **reset()**. Then a call to **isReady()** fetches data and as long as
data equals zero the sensor is not ready. data equals zero the sensor is not ready.
- **DVI interface** - **DVI interface**
To investigate, sort of external reset? To investigate, sort of external reset?
- move code to .cpp

View File

@ -15,7 +15,7 @@
"type": "git", "type": "git",
"url": "https://github.com/RobTillaart/BH1750FVI_RT.git" "url": "https://github.com/RobTillaart/BH1750FVI_RT.git"
}, },
"version": "0.2.9", "version": "0.2.10",
"license": "MIT", "license": "MIT",
"frameworks": "arduino", "frameworks": "arduino",
"platforms": "*", "platforms": "*",

View File

@ -1,5 +1,5 @@
name=BH1750FVI_RT name=BH1750FVI_RT
version=0.2.9 version=0.2.10
author=Rob Tillaart <rob.tillaart@gmail.com> author=Rob Tillaart <rob.tillaart@gmail.com>
maintainer=Rob Tillaart <rob.tillaart@gmail.com> maintainer=Rob Tillaart <rob.tillaart@gmail.com>
sentence=Arduino library for BH1750FVI (GY-30) lux sensor sentence=Arduino library for BH1750FVI (GY-30) lux sensor

View File

@ -37,6 +37,7 @@
unittest_setup() unittest_setup()
{ {
fprintf(stderr, "BH1750FVI_LIB_VERSION: %s\n", (char *) BH1750FVI_LIB_VERSION);
} }
@ -47,8 +48,6 @@ unittest_teardown()
unittest(test_constructor) unittest(test_constructor)
{ {
fprintf(stderr, "BH1750FVI_LIB_VERSION: %s\n", (char *) BH1750FVI_LIB_VERSION);
BH1750FVI myLux(0x23); BH1750FVI myLux(0x23);
myLux.setContHigh2Res(); myLux.setContHigh2Res();
@ -73,8 +72,6 @@ unittest(test_constructor)
unittest(test_constants) unittest(test_constants)
{ {
fprintf(stderr, "BH1750FVI_LIB_VERSION: %s\n", (char *) BH1750FVI_LIB_VERSION);
assertEqual(0x23, BH1750FVI_DEFAULT_ADDRESS); assertEqual(0x23, BH1750FVI_DEFAULT_ADDRESS);
assertEqual(0x5C, BH1750FVI_ALT_ADDRESS); assertEqual(0x5C, BH1750FVI_ALT_ADDRESS);
@ -113,11 +110,11 @@ unittest(test_parameters)
{ {
BH1750FVI myLux(0x23); BH1750FVI myLux(0x23);
// 0.45 .. 3.68 // 0.45 .. 3.68
myLux.setCorrectionFactor(3.14); myLux.setCorrectionFactor(3.14);
assertEqualFloat(3.14, myLux.getCorrectionFactor(), 0.01); assertEqualFloat(3.14, myLux.getCorrectionFactor(), 0.01);
// -89 - 89 // -89 - 89
myLux.setAngle(30); myLux.setAngle(30);
assertEqual(30, myLux.getAngle()); assertEqual(30, myLux.getAngle());
@ -125,7 +122,7 @@ unittest(test_parameters)
myLux.setTemperature(42); myLux.setTemperature(42);
assertEqual(42, myLux.getTemperature()); assertEqual(42, myLux.getTemperature());
// 400 - 715 // 400 - 715
myLux.setWaveLength(700); myLux.setWaveLength(700);
assertEqual(700, myLux.getWaveLength()); assertEqual(700, myLux.getWaveLength());
} }
@ -135,7 +132,7 @@ unittest(test_correctionFactor)
{ {
BH1750FVI myLux(0x23); BH1750FVI myLux(0x23);
// 0.45 .. 3.68 // 0.45 .. 3.68
assertEqual( 31, myLux.setCorrectionFactor(0.00) ); assertEqual( 31, myLux.setCorrectionFactor(0.00) );
assertEqual( 31, myLux.setCorrectionFactor(0.44) ); assertEqual( 31, myLux.setCorrectionFactor(0.44) );
assertEqual( 31, myLux.setCorrectionFactor(0.45) ); assertEqual( 31, myLux.setCorrectionFactor(0.45) );
@ -149,7 +146,7 @@ unittest(test_angleFactor)
{ {
BH1750FVI myLux(0x23); BH1750FVI myLux(0x23);
// -89 ..89 // -89 ..89
assertEqualFloat(57.2987, myLux.setAngle(-90), 0.0001); assertEqualFloat(57.2987, myLux.setAngle(-90), 0.0001);
assertEqualFloat(57.2987, myLux.setAngle(-89), 0.0001); assertEqualFloat(57.2987, myLux.setAngle(-89), 0.0001);
assertEqualFloat(2.00000, myLux.setAngle(-60), 0.0001); assertEqualFloat(2.00000, myLux.setAngle(-60), 0.0001);
@ -168,7 +165,7 @@ unittest(test_temperatureFactor)
{ {
BH1750FVI myLux(0x23); BH1750FVI myLux(0x23);
// -20 .. 100 // -20 .. 100
assertEqualFloat(1.020, myLux.setTemperature(-20), 0.001); assertEqualFloat(1.020, myLux.setTemperature(-20), 0.001);
assertEqualFloat(1.015, myLux.setTemperature(-10), 0.001); assertEqualFloat(1.015, myLux.setTemperature(-10), 0.001);
assertEqualFloat(1.010, myLux.setTemperature( 0), 0.001); assertEqualFloat(1.010, myLux.setTemperature( 0), 0.001);
@ -189,7 +186,7 @@ unittest(test_wavelengthFactor)
{ {
BH1750FVI myLux(0x23); BH1750FVI myLux(0x23);
// 400 - 715 // 400 - 715
assertEqualFloat(100.000, myLux.setWaveLength(300), 0.001); assertEqualFloat(100.000, myLux.setWaveLength(300), 0.001);
assertEqualFloat(100.000, myLux.setWaveLength(400), 0.001); assertEqualFloat(100.000, myLux.setWaveLength(400), 0.001);
assertEqualFloat(4.66667, myLux.setWaveLength(450), 0.0001); assertEqualFloat(4.66667, myLux.setWaveLength(450), 0.0001);