0.1.0 - inital version BH1750FVI library

This commit is contained in:
RobTillaart 2020-02-02 17:30:48 +01:00
parent e0e23a36e8
commit 3d83c34785
9 changed files with 404 additions and 0 deletions

View File

@ -0,0 +1,95 @@
//
// FILE: BH1750FVI.cpp
// AUTHOR: Rob Tillaart
// VERSION: 0.1.0
// PURPOSE: library for BH1750FVI lux sensor Arduino
// URL: https://github.com/RobTillaart/Arduino/tree/master/libraries
//
// Released to the public domain
//
// 0.1.0 2020-02-02 - initial version
#include "BH1750FVI.h"
#if defined(ESP8266) || defined(ESP32)
BH1750FVI::BH1750FVI(const uint8_t address, const uint8_t dataPin, const uint8_t clockPin)
{
_address = address;
_data = 0;
_error = BH1750FVI_OK;
_factor = 69;
_wire = &Wire;
if ((dataPin < 255) && (clockPin < 255))
{
_wire->begin(dataPin, clockPin);
} else {
_wire->begin();
}
}
#endif
BH1750FVI::BH1750FVI(const uint8_t address, TwoWire *wire)
{
_address = address;
_data = 0;
_error = BH1750FVI_OK;
_factor = 69;
_wire = wire;
_wire->begin();
}
float BH1750FVI::getLux(void)
{
return readData() / 1.2;
}
int BH1750FVI::getError()
{
int e = _error;
_error = BH1750FVI_OK;
return e;
}
void BH1750FVI::changeTiming(uint8_t val)
{
_factor = val;
uint8_t Hbits = 0x40 | (val >> 5);
uint8_t Lbits = 0x60 | (val & 0x1F);
command(Hbits);
command(Lbits);
}
void BH1750FVI::setCorrectionFactor(float f)
{
uint8_t timingValue = round(69 * constrain(f, 0.01, 3.68));
changeTiming(timingValue);
}
///////////////////////////////////////////////////////////
//
// PRIVATE
//
uint16_t BH1750FVI::readData()
{
if (_wire->requestFrom(_address, (uint8_t) 2) != 2)
{
_error = BH1750FVI_ERROR_WIRE_REQUEST;
return _data; // last value
}
_data = _wire->read();
_data <<= 8;
_data += _wire->read();
return _data;
}
void BH1750FVI::command(uint8_t value)
{
_wire->beginTransmission(_address);
_wire->write(value);
_error = _wire->endTransmission();
}
// --- END OF FILE ---

View File

@ -0,0 +1,101 @@
#ifndef BH1750FVI_H
#define BH1750FVI_H
//
// FILE: BH1750FVI_H.h
// AUTHOR: Rob dot Tillaart at gmail dot com
// VERSION: 0.1.0
// PURPOSE: library for BH1750FVI lux sensor Arduino
// HISTORY: See BH1750FVI.cpp
//
// Released to the public domain
//
// breakout BH1750FVI / GY-30
//
// +-----------------------+
// GND |o |
// ADD |o |
// SDA |o |
// SCL |o |
// VCC |o |
// +-----------------------+
//
// ADDRESS:
// 0 = 0x23
// 1 = 0x5C
//
#include "Wire.h"
#include "Arduino.h"
#define BH1750FVI_LIB_VERSION "0.1.0"
#define BH1750FVI_DEFAULT_ADDRESS 0x23
#define BH1750FVI_ALT_ADDRESS 0x5C
// COMMANDS
#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
// ERROR CODES
#define BH1750FVI_OK 0
#define BH1750FVI_ERROR_WIRE_REQUEST -10
class BH1750FVI
{
public:
#if defined(ESP8266) || defined(ESP32)
// dataPin and clockPin can be used for ESP8266
BH1750FVI(const uint8_t address, const uint8_t dataPin, const uint8_t clockPin);
#endif
BH1750FVI(const uint8_t address, TwoWire *wire = &Wire);
float getLux();
int getError();
void powerOn() { command(BH1750FVI_POWER_ON); };
void powerOff() { command(BH1750FVI_POWER_OFF); };
void reset() { command(BH1750FVI_RESET); };
// MODE TIME RESOLUTION
// High 120 ms 0.5 lux // recommended
// High2 120 ms 1 lux
// Low 16 ms 4 lux
void setContHighRes() { command(BH1750FVI_CONT_HIGH); };
void setContHigh2Res() { command(BH1750FVI_CONT_HIGH2); };
void setContLowRes() { command(BH1750FVI_CONT_LOW); };
void setOnceHighRes() { command(BH1750FVI_ONCE_HIGH); };
void setOnceHigh2Res() { command(BH1750FVI_ONCE_HIGH2); };
void setOnceLowRes() { command(BH1750FVI_ONCE_LOW); };
// read datasheet about details of the correction 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
void setCorrectionFactor(float f); // 0.01 .. 3.68
float getCorrectionFactor() { return _factor / 69.0; };
private:
uint16_t readData();
void command(uint8_t value);
uint8_t _address;
uint16_t _data;
int _error;
uint8_t _factor;
TwoWire* _wire;
};
#endif
// END OF FILE

View File

@ -0,0 +1,43 @@
//
// FILE: BH1750FVI_cont_high_res.ino
// AUTHOR: Rob Tillaart
// VERSION: 0.1.0
// PURPOSE: demo of BH1750FVI lux scanner library
// DATE: 2020-02-02
//
// Released to the public domain
//
#include "BH1750FVI.h"
BH1750FVI myLux(0x23);
uint32_t lastUpdate = 0;
void setup()
{
Serial.begin(115200);
Serial.println();
Serial.print(__FILE__);
Serial.println();
Wire.begin();
myLux.powerOn();
myLux.setContHighRes();
}
void loop()
{
int interval = 100;
if (millis() - lastUpdate >= interval)
{
lastUpdate += interval;
float val = myLux.getLux();
Serial.println(val, 1);
}
}
// END OF FILE

View File

@ -0,0 +1,43 @@
//
// FILE: BH1750FVI_cont_low_res.ino
// AUTHOR: Rob Tillaart
// VERSION: 0.1.0
// PURPOSE: demo of BH1750FVI lux scanner library
// DATE: 2020-02-02
//
// Released to the public domain
//
#include "BH1750FVI.h"
BH1750FVI myLux(0x23);
uint32_t lastUpdate = 0;
void setup()
{
Serial.begin(115200);
Serial.println();
Serial.print(__FILE__);
Serial.println();
Wire.begin();
myLux.powerOn();
myLux.setContLowRes();
}
void loop()
{
int interval = 100;
if (millis() - lastUpdate >= interval)
{
lastUpdate += interval;
float val = myLux.getLux();
Serial.println(val, 1);
}
}
// END OF FILE

View File

@ -0,0 +1,53 @@
//
// FILE: setCorrectionFactor.ino
// AUTHOR: Rob Tillaart
// VERSION: 0.1.0
// PURPOSE: demo of BH1750FVI lux scanner library
// DATE: 2020-02-02
//
// Released to the public domain
//
#include "BH1750FVI.h"
BH1750FVI myLux(0x23);
uint32_t lastUpdate = 0;
float correctionFactor = 0.01;
void setup()
{
Serial.begin(115200);
Serial.println();
Serial.print(__FILE__);
Serial.println();
Wire.begin();
myLux.powerOn();
myLux.setContHighRes();
}
void loop()
{
int interval = 100;
if (millis() - lastUpdate >= interval)
{
lastUpdate += interval;
float val = myLux.getLux();
Serial.print(val, 1);
Serial.print("\t");
Serial.print(myLux.getCorrectionFactor(), 3);
Serial.print("\t");
Serial.println(val / myLux.getCorrectionFactor(), 1);
// note correctionfactor are steps of 1/69 internally, see datasheet
myLux.setCorrectionFactor(correctionFactor); // 0.01 .. 3.68
correctionFactor += 0.01;
if (correctionFactor > 3.68) correctionFactor = 0.01;
}
}
// END OF FILE

View File

@ -0,0 +1,26 @@
#######################################
# Syntax Coloring Map For BH1750FVI
#######################################
#######################################
# Datatypes (KEYWORD1)
#######################################
BH1750FVI KEYWORD1
#######################################
# Methods and Functions (KEYWORD2)
#######################################
getLux KEYWORD2
#######################################
# Instances (KEYWORD2)
#######################################
#######################################
# Constants (LITERAL1)
#######################################

View File

@ -0,0 +1,24 @@
{
"name": "BH1750FVI",
"keywords": "BH1750FVI, Lux, light, GY-30",
"description": "library for BH1750FVI Lux sensor Arduino.",
"authors":
[
{
"name": "Rob Tillaart",
"email": "Rob.Tillaart@gmail.com",
"maintainer": true
}
],
"repository":
{
"type": "git",
"url": "https://github.com/RobTillaart/Arduino.git"
},
"version": "0.1.0",
"frameworks": "arduino",
"platforms": "*",
"export": {
"include": "libraries/BH1750FVI"
}
}

View File

@ -0,0 +1,9 @@
name=BH1750FVI
version=0.1.0
author=Rob Tillaart <rob.tillaart@gmail.com>
maintainer=Rob Tillaart <rob.tillaart@gmail.com>
sentence=Library for BH1750FVI lux sensor Arduino
paragraph=
category=Sensors
url=https://github.com/RobTillaart/Arduino/tree/master/libraries/
architectures=*

View File

@ -0,0 +1,10 @@
# BH1750FVI I2C LUX sensor
library for BH1750FVI (GY-30) Lux sensor for Arduino.
## Examples
## Notes