starmbi / hp_bh1750 Goto Github PK
View Code? Open in Web Editor NEWhp_BH1750
License: MIT License
hp_BH1750
License: MIT License
to include the RP2040 fix from ladyada. Thanks!
https://github.com/Starmbi/hp_BH1750/wiki
On the first page of your general usage wiki, you have readLux where I believe you mean getLux, as readLux isn't in your library.
Love this library, thank you for making.
Thanks for the great work with this library.
For me it didn't work, and I'm guessing that this is based on the fact that I'm using an ESP32, and that Wire implementation is slightly different.
I think that based on the beginTransmission, requestFrom does not work in readValue.
After updating that to the following, it worked like a charm.
(main change: removed the beginTransmission/endTransmission, since that's not needed in the requestFrom/read construction)
unsigned int hp_BH1750::readValue()
{
const byte bytesRequested = 2;
byte buff[bytesRequested];
unsigned int req = _wire->requestFrom((int)_address, (int)bytesRequested); // request two bytes
if ((req == 0) || (req > bytesRequested))
{
_time = 999;
_value = 0;
return _value; // Sensor not found or other problem
}
unsigned int bytesRead=0;
while(bytesRead<req) {
if (_wire->available()) {
buff[bytesRead] = _wire->read(); // Receive one byte
bytesRead++;
}
}
_nReads++; // Inc the physically count of reads
_value = ((buff[0] << 8) | buff[1]);
unsigned long mil = millis();
if (_value > 0 && _time == 0)
_time = mil - _startMillis; // Conversion time
if (mil >= (_timeoutMillis) && (_time == 0))
_time = mil - _startMillis;
return _value;
}
For some reason I cannot get this to work on the S2, ill keep testing and see if I can gather any debug or crash data.
my serial out put just stops, so not sure what to do here..
This line is where my code just stops..
unsigned int req = _wire->requestFrom((int)_address, (int)2); // request two bytes
Any ideas welcome, pretty sure it worked on esp32, gonna test again, also all other i2c sensors work fine
I've been doing some tests with your library and noticed that the following occasionally breaks out of the following loop before the sensor has had enough time to gather a reading:
bh1750.adjustSettings(90,true);
bh1750.start();
do{
//...reading other non I2C sensors here & averaging....
} while(bh1750.hasValue(true) == false);
float lux_T1red= bh1750.getLux();
If I take readings every minute, about 1-2 of them per hour will end up with zero in lux_T1red on a ProMini system running at 8mhz (default 100khz I2C bus). When I added a counter to the loop it's clear that the do-while is ending at seemingly random times 'before' the BH1750 has had enough time to gather its next reading. It's rare, but I've seen this behavior on 3 different systems with the same code, but different hardware & BH1750 sensor modules. The sensor modules are just cheap ones from eBay, so there is a possibility of that being a factor, rather than your library (?). Anyway just passing on the info.
Hi,
Using ESP32 nodeMCU-32 board.
I cannot see in any of the examples where 'float luxFactor' is used?
It is specified in the wiki to allow for sensor calibration, however I am obviously using it wrong,
as changing the value does nothing.
I have written my code using the 'non blocking' example and below is my function, which takes a reading, increases luxFactor, prints both values and runs in a loop.
Apologies for posting here, I cannot find a way to contact you directly or a forum for your library.
void lightmeter() {
float luxFactor;
luxFactor = 0.96;
while (digitalRead(Button3) == LOW) {} // wait for button release
delay(50); // short delay for bounce
BH1750.start(); // ask sensor to take a reading
while (digitalRead(Button3) == HIGH) { // wait for button press to return from lightmeter subroutine
if (BH1750.hasValue() == true) { // non blocking reading
float lux = BH1750.getLux();
Serial.print("lux: ");
Serial.print(lux);
BH1750.start(); // ask sensor to take a reading
luxFactor = luxFactor + 0.01;
if (luxFactor > 1.44) { luxFactor = 0.96; }
Serial.print(" cal: ");
Serial.println(luxFactor);
}
lux: 6.67 cal: 1.19
lux: 6.67 cal: 1.20
lux: 6.67 cal: 1.21
lux: 6.67 cal: 1.22
lux: 6.67 cal: 1.23
lux: 6.67 cal: 1.24
lux: 6.67 cal: 1.25
lux: 6.67 cal: 1.26
lux: 6.67 cal: 1.27
lux: 6.67 cal: 1.28
lux: 6.67 cal: 1.29
lux: 6.67 cal: 1.30
lux: 6.67 cal: 1.31
lux: 6.67 cal: 1.32
lux: 6.67 cal: 1.33
lux: 6.67 cal: 1.34
lux: 6.67 cal: 1.35
lux: 6.67 cal: 1.36
lux: 6.67 cal: 1.37
lux: 6.67 cal: 1.38
lux: 6.67 cal: 1.39
lux: 6.67 cal: 1.40
lux: 6.67 cal: 1.41
lux: 6.67 cal: 1.42
lux: 6.67 cal: 1.43
lux: 6.67 cal: 1.44
lux: 6.67 cal: 0.96
Hello @Starmbi ,
This is my code:
#include <hp_BH1750.h> // include the library
hp_BH1750 sensor; // define the sensor
void setup() {
Serial.begin(115200);
Serial.println("start");
bool avail = sensor.begin(BH1750_TO_GROUND); //A: set address and init sensor
if (!avail) {
Serial.println("No BH1750 sensor found!");
while (true) {};
}
Serial.println("calibrating sensor...");
sensor.calibrateTiming(); //B: calibrate the timings, about 855ms with a bad chip
Serial.println("calibration finished");
sensor.start(); //C: start the first measurement
Serial.println("end");
}
void loop() {
if (sensor.hasValue()) { //D: most important function of this library!
float lux = sensor.getLux(); //E: read the result
Serial.println(lux);
sensor.start(); //F: start next measurement
}
delay(1000);
Serial.println("waiting");
}
And it blocks at
20:11:02.892 -> entry 0x400805f0
20:11:02.993 -> start
20:11:02.993 -> calibrating sensor...
My wiring for my ESP32, and the sensor's green light is on.
the I2C scanner returns I2C device found at address 0x23
A declarative, efficient, and flexible JavaScript library for building user interfaces.
๐ Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. ๐๐๐
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google โค๏ธ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.