0.2.9 BH1750FVI_RT

This commit is contained in:
rob tillaart 2021-12-14 10:53:34 +01:00
parent 433e8acdd9
commit be3624d7c4
6 changed files with 83 additions and 68 deletions

View File

@ -1,7 +1,7 @@
//
// FILE: BH1750FVI.cpp
// AUTHOR: Rob Tillaart
// VERSION: 0.2.8
// VERSION: 0.2.9
// PURPOSE: library for BH1750FVI lux sensor Arduino
// URL: https://github.com/RobTillaart/BH1750FVI
//
@ -21,6 +21,7 @@
// 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"
@ -187,22 +188,22 @@ void BH1750FVI::setOnceLowRes()
// measurement timing
//
// P11 datasheet
void BH1750FVI::changeTiming(uint8_t val)
void BH1750FVI::changeTiming(uint8_t time)
{
val = constrain(val, 31, 254);
_sensitivityFactor = val;
time = constrain(time, 31, 254);
_sensitivityFactor = time;
// P5 instruction set table
uint8_t Hbits = 0x40 | (val >> 5);
uint8_t Lbits = 0x60 | (val & 0x1F);
uint8_t Hbits = 0x40 | (time >> 5);
uint8_t Lbits = 0x60 | (time & 0x1F);
command(Hbits);
command(Lbits);
}
uint8_t BH1750FVI::setCorrectionFactor(float f)
uint8_t BH1750FVI::setCorrectionFactor(float factor)
{
// 31 .. 254 are range P11 - constrained in changeTIming call
uint8_t timingValue = round(BH1750FVI_REFERENCE_TIME * f);
uint8_t timingValue = round(BH1750FVI_REFERENCE_TIME * factor);
changeTiming(timingValue);
return _sensitivityFactor;
}

View File

@ -2,7 +2,7 @@
//
// FILE: BH1750FVI.h
// AUTHOR: Rob dot Tillaart at gmail dot com
// VERSION: 0.2.8
// VERSION: 0.2.9
// PURPOSE: Arduino library for BH1750FVI (GY-30) lux sensor
// HISTORY: See BH1750FVI.cpp
//
@ -28,34 +28,34 @@
#include "Arduino.h"
#define BH1750FVI_LIB_VERSION (F("0.2.8"))
#define BH1750FVI_LIB_VERSION (F("0.2.9"))
#define BH1750FVI_DEFAULT_ADDRESS 0x23
#define BH1750FVI_ALT_ADDRESS 0x5C
#define BH1750FVI_DEFAULT_ADDRESS 0x23
#define BH1750FVI_ALT_ADDRESS 0x5C
// COMMANDS P5
#define BH1750FVI_POWER_ON 0x00
#define BH1750FVI_POWER_OFF 0x01
#define BH1750FVI_RESET 0x07
#define BH1750FVI_CONT_HIGH 0x10
#define BH1750FVI_CONT_HIGH2 0x11
#define BH1750FVI_CONT_LOW 0x13
#define BH1750FVI_ONCE_HIGH 0x20
#define BH1750FVI_ONCE_HIGH2 0x21
#define BH1750FVI_ONCE_LOW 0x23
// COMMANDS P5
#define BH1750FVI_POWER_ON 0x00
#define BH1750FVI_POWER_OFF 0x01
#define BH1750FVI_RESET 0x07
#define BH1750FVI_CONT_HIGH 0x10
#define BH1750FVI_CONT_HIGH2 0x11
#define BH1750FVI_CONT_LOW 0x13
#define BH1750FVI_ONCE_HIGH 0x20
#define BH1750FVI_ONCE_HIGH2 0x21
#define BH1750FVI_ONCE_LOW 0x23
#define BH1750FVI_REFERENCE_TIME 0x45 // 69
#define BH1750FVI_REFERENCE_TIME 0x45 // 69
#define BH1750FVI_MODE_LOW 0x00
#define BH1750FVI_MODE_HIGH 0x01
#define BH1750FVI_MODE_HIGH2 0x02
#define BH1750FVI_MODE_LOW 0x00
#define BH1750FVI_MODE_HIGH 0x01
#define BH1750FVI_MODE_HIGH2 0x02
// ERROR CODES
#define BH1750FVI_OK 0
#define BH1750FVI_ERROR_WIRE_REQUEST -10
#define BH1750FVI_OK 0
#define BH1750FVI_ERROR_WIRE_REQUEST -10
class BH1750FVI
@ -68,49 +68,59 @@ public:
#endif
BH1750FVI(const uint8_t address, TwoWire *wire = &Wire);
// retuns true if isConnected()
// returns true if isConnected()
bool begin(); // resets to constructor defaults. (use with care)
bool isConnected(); // returns true if address is on I2C bus
float getRaw(); // no HIGH2 mode + no sensitivity factor.
float getLux();
int getError();
void powerOn() { command(BH1750FVI_POWER_ON); };
void powerOff() { command(BH1750FVI_POWER_OFF); };
void reset() { command(BH1750FVI_RESET); };
// MODE TIME RESOLUTION
// 2 HIGH2 120 ms 0.5 lux // recommended max * 1.5 = 180 ms
// 1 HIGH 120 ms 1.0 lux
// 0 LOW 16 ms 4.0 lux
uint8_t getMode() { return _mode; };
void setContHighRes();
void setContHigh2Res();
void setContLowRes();
void setOnceHighRes();
void setOnceHigh2Res();
void setOnceLowRes();
bool isReady(); // only after setOnce...Res();
// read datasheet P11 about details of the correction or sensitivity factor
// to be used for very high and very low brightness
// or to correct for e.g. transparancy
void changeTiming(uint8_t val); // 69 is default = BH1750FVI_REFERENCE_TIME
// returns changeTiming() param
uint8_t setCorrectionFactor(float f = 1); // 0.45 .. 3.68
// returns percentage set .
// or to correct for e.g. transparency
void changeTiming(uint8_t time = BH1750FVI_REFERENCE_TIME); // 69 is default
// returns changeTiming() parameter
uint8_t setCorrectionFactor(float factor = 1); // 0.45 .. 3.68
// returns percentage set.
float getCorrectionFactor();
// read datasheet P3 and check figure 4 and 5.
// setAngle is constrained to -89..+89
// returns the angle correction factor
float setAngle(int degrees = 0);
int getAngle() { return _angle; };
// datasheet P3 figure 7
// Effect of temperature is about 3% / 60°C ~~ 1% / 20°C
// to be used if temp is really hot or cold.
@ -127,6 +137,7 @@ public:
float setWaveLength(int waveLength = 580);
int getWaveLength() { return _waveLength; };
private:
uint16_t readData();
void command(uint8_t value);
@ -136,6 +147,7 @@ private:
int _error;
uint8_t _sensitivityFactor;
uint8_t _mode;
uint32_t _requestTime = 0;
float _angleFactor = 1;
int _angle = 0;
@ -147,4 +159,5 @@ private:
TwoWire* _wire;
};
// -- END OF FILE --

View File

@ -1,6 +1,6 @@
MIT License
Copyright (c) 2020-2021 Rob Tillaart
Copyright (c) 2020-2022 Rob Tillaart
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal

View File

@ -8,21 +8,21 @@
# BH1750FVI_RT
Arduino library for BH1750FVI (GY-30) 16 bit I2C Lux sensor
Arduino library for BH1750FVI (GY-30) 16 bit I2C Lux sensor.
## Description
The BH1750FVI is a 16 bit lux sensor with an I2C interface
The BH1750FVI is a 16 bit lux sensor with an I2C interface.
It is possible to detect a wide range from 0.11 - 100000 lux.
To be able to support this wide range, the sensor can operate in three modi.
| ID | Mode | Integration time | Resolution | Notes |
|:----:|:-----:|:----------------:|:----------:|:------|
| 0 | LOW | 16 ms | 4.0 Lux | to measure very bright light |
| 1 | HIGH | 120 ms | 1.0 lux | default |
| 2 | HIGH2 | 120 ms | 0.5 lux | to measure very dim light |
| ID | Mode | Integration time | Resolution | Notes |
|:----:|:-----:|:----------------:|:----------:|:--------------------------|
| 0 | LOW | 16 ms | 4.0 Lux | measure very bright light |
| 1 | HIGH | 120 ms | 1.0 lux | default |
| 2 | HIGH2 | 120 ms | 0.5 lux | measure very dim light |
Furthermore one can set a correction factor to reduce / increase the
integration time of the sensor.
@ -54,43 +54,43 @@ Library was tested with a breakout board.
// 1 = 0x5C
//
```
The sensor works on 2.4 - 3.6 volt so be careful not to connect directly to 5.0 volt.
(Note: the breakout board was 5 volt tolerant)
The sensor works on 2.4 - 3.6 volt so be careful not to connect directly to 5.0 volt.
Note: the breakout board was 5 volt tolerant.
## Interface
### Constructor
- **BH1750FVI(uint8_t address, uint8_t dataPin, uint8_t clockPin)** ESP constructor with I2C parameters
- **BH1750FVI(uint8_t address, TwoWire \*wire = &Wire)** constructor for other platforms
- **BH1750FVI(uint8_t address, uint8_t dataPin, uint8_t clockPin)** ESP constructor with I2C parameters.
- **BH1750FVI(uint8_t address, TwoWire \*wire = &Wire)** constructor for other platforms.
- **bool begin()** resets some internal variables to default. Use with care.
- **bool isConnected()** returns true if address is on I2C bus.
### Base
- **float getRaw()** reads the lux sensor,
- **float getRaw()** reads the lux sensor.
- **float getLux()** reads the lux sensor and corrects for correctionFactor, mode, temperature and angle.
### management
- **int getError()** get the latest error code, mainly for debugging,
- **void powerOn()** wakes up the sensor,
- **void powerOff()** set sensor to sleep,
- **int getError()** get the latest error code, mainly for debugging.
- **void powerOn()** wakes up the sensor.
- **void powerOff()** set sensor to sleep.
- **void reset()** resets the data register to 0, effectively removing last measurement.
### Mode operators
- **uint8_t getMode()** gets the mode set by one of the set functions. See table above.
- **void setContHighRes()** continuous mode in HIGH resolution
- **void setContHigh2Res()** continuous mode in HIGH2 resolution
- **void setContLowRes()** continuous mode in LOW resolution
- **void setOnceHighRes()** single shot mode in HIGH resolution
- **void setOnceHigh2Res()** single shot mode in HIGH2 resolution
- **void setOnceLowRes()** single shot mode in LOW resolution
- **void setContHighRes()** continuous mode in HIGH resolution.
- **void setContHigh2Res()** continuous mode in HIGH2 resolution.
- **void setContLowRes()** continuous mode in LOW resolution.
- **void setOnceHighRes()** single shot mode in HIGH resolution.
- **void setOnceHigh2Res()** single shot mode in HIGH2 resolution.
- **void setOnceLowRes()** single shot mode in LOW resolution.
### CorrectionFactor
@ -101,8 +101,8 @@ Please read datasheet P11 about details of the correction factor.
This is based on a calculated time, the sensor does not have a means to indicate ready directly.
Needed only for the single shot modi.
The function **isReady()** takes the correction factor into account.
- **void changeTiming(uint8_t val)** 69 is default = BH1750FVI_REFERENCE_TIME
- **uint8_t setCorrectionFactor(float f = 1)** preferred wrapper around changeTiming f = 0.45 .. 3.68.
- **void changeTiming(uint8_t time = BH1750FVI_REFERENCE_TIME)** 69 is default.
- **uint8_t setCorrectionFactor(float factor = 1)** preferred wrapper around changeTiming factor = 0.45 .. 3.68.
Returns changeTiming() parameter.
- **float getCorrectionFactor()** returns the correction factor.
Note this can differ as it is stores as an integer internally.
@ -167,18 +167,18 @@ Values outside the range will be mapped upon 400 or 715.
Default wavelength will be 580 as that gives 100%
## Ideas
## Operation
See samples...
## Future
- **Intelligent isReady()**
After a **getLux()** call one can clean the data register explicitly with
**reset()**. Then a call to **isReady()** fetches data and as long as
data equals zero the sensor is not ready.
- **DVI interface**
To investigate, sort of external reset?
## Operation
See samples...

View File

@ -15,8 +15,9 @@
"type": "git",
"url": "https://github.com/RobTillaart/BH1750FVI_RT.git"
},
"version": "0.2.8",
"version": "0.2.9",
"license": "MIT",
"frameworks": "arduino",
"platforms": "*"
"platforms": "*",
"headers": "BH1750FVI.h"
}

View File

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