Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Frequent calls to readLightLevel prevents measurement update #61

Open
bjarnebuchmann opened this issue Jul 31, 2019 · 2 comments
Open

Comments

@bjarnebuchmann
Copy link

It seems that in CONTINUOUS modes, frequent calls to readLightLevel prevents the bh1750 module to actually update the measurement. Presumably, the measurement is interrupted by the i2c communication and has to be restarted.
Thus, if I (say) set
lightMeter.configure(BH1750::CONTINUOUS_HIGH_RES_MODE);
and then call readLightLevel with only 100 (or even 120) millis delays, then I keep getting the same value back.
If I increase to, say, 180 millis (max time to make measurement) ,then I get an updated value every time.

If there already is a caveat about this usage, then I apologize for reporting.

I do not see an easy way to circumvent this, except storing (in the object) the previous measurement and time-of-measurement, and not then checking time vs previous time-of-measurement to see if an updated measurement should be ready.

Obviosly, I can avoid the problem by not calling readLightLevel() often. I was just surprised by the effect, and want to report it.

Best,

/Bjarne

@coelner
Copy link
Contributor

coelner commented Aug 14, 2019

No, that looks like a bug.

BH1750/BH1750.cpp

Lines 220 to 224 in 0643631

// Send mode to sensor
Wire.beginTransmission(BH1750_I2CADDR);
__wire_write((uint8_t)BH1750_MODE);
Wire.endTransmission();

We should check for continuous mode and do not reconfigure the sensor.

@coelner
Copy link
Contributor

coelner commented Aug 14, 2019

Can you replace it accordingly with the following and test it?

float BH1750::readLightLevel(bool maxWait) {

  if (BH1750_MODE == UNCONFIGURED) {
    Serial.println(F("[BH1750] Device is not configured!"));
    return -2.0;
  }

  // Measurement result will be stored here
  float level = -1.0;

  // 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:
        // Send mode to sensor
  Wire.beginTransmission(BH1750_I2CADDR);
  __wire_write((uint8_t)BH1750_MODE);
  Wire.endTransmission();
      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:
      // Send mode to sensor
  Wire.beginTransmission(BH1750_I2CADDR);
  __wire_write((uint8_t)BH1750_MODE);
  Wire.endTransmission();
      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



coelner pushed a commit to coelner/BH1750 that referenced this issue Aug 14, 2020
@coelner coelner mentioned this issue Aug 20, 2020
coelner pushed a commit to coelner/BH1750 that referenced this issue Sep 24, 2020
coelner pushed a commit to coelner/BH1750 that referenced this issue Oct 13, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants