I2C picks up BH1750FVI, but can't read data (ESP8266)

Hello everyone!

I've been trying to get my BH1750FVI sensor working on my Wemos D1 R2 board.
At first the code did not see the sensor, neither did the I2C scanner. But after some rewiring I got the
ESP8266 I2C scanner to pick him up. It returns the following:

I2C Scanner
Scanning...
I2C device found at address 0x23  !

The address it find corresponds with the address it should be. (Note: I did not connect the ADDR pin to GND)

After I2c picked it up, I used the BH1750 library to test the sensor, but when I run the following code:

/*
  Example of BH1750 library usage.
  This example initialises the BH1750 object using the default high resolution
  continuous mode and then makes a light level reading every second.
  Connection:
    VCC -> 3V3 or 5V
    GND -> GND
    SCL -> SCL (A5 on Arduino Uno, Leonardo, etc or 21 on Mega and Due, on esp8266 free selectable)
    SDA -> SDA (A4 on Arduino Uno, Leonardo, etc or 20 on Mega and Due, on esp8266 free selectable)
    ADD -> (not connected) or GND
  ADD pin is used to set sensor I2C address. If it has voltage greater or equal to
  0.7VCC voltage (e.g. you've connected it to VCC) the sensor address will be
  0x5C. In other case (if ADD voltage less than 0.7 * VCC) the sensor address will
  be 0x23 (by default).
*/

#include <Wire.h>
#include <BH1750.h>

BH1750 lightMeter;


void setup(){

  Serial.begin(9600);

  // Initialize the I2C bus (BH1750 library doesn't do this automatically)
  Wire.begin();
  // On esp8266 you can select SCL and SDA pins using Wire.begin(D4, D3);

    if (lightMeter.begin()) {
    Serial.println(F("BH1750 initialised"));
  }
  else {
    Serial.println(F("Error initialising BH1750"));
  }

  Serial.println(F("BH1750 Test begin"));

}


void loop() {

  uint16_t lux = lightMeter.readLightLevel();
  Serial.print("Light: ");
  Serial.print(lux);
  Serial.println(" lx");
  delay(1000);

}

I returns this:

[BH1750] ERROR: received NACK on transmit of address
[BH1750] ERROR: received NACK on transmit of data
[BH1750] ERROR: other error
[BH1750] ERROR: undefined error
Error initialising BH1750
BH1750 Test begin
[BH1750] Device is not configured!
Light: 0 lx

This would mean he can't find the sensor on the I2C, even though he can pick it up with the scanner!
Does anyone have any idea how to fix this, or which tests to run/perform to find what's wrong?

Wiring:


VCC -> 3.3v
GND -> GND
SCL -> D1 (SCL pin on ESP8266)
SDA -> D2 (SDA pin on ESP8266)

Please post a wiring diagram.


VCC -> 3.3v
GND -> GND
SCL -> D1 (SCL pin on ESP8266)
SDA -> D2 (SDA pin on ESP8266)

There are a few bugs in that BH1750 code. I can spot a few bugs, but I don't know what triggers the NACK error.
Could you try another library ?
Do you have pullup resistors for SDA and SCL ?

I have made a test to explore the posibilities of the BH1750. It uses normal Arduino code, you could try it.

Thanks for the reply! I do have a pullup resistor between SDA and VCC and SDL and VCC. I use a 4.7k ohm resistor for both (as can be seen in the wiring diagram). I'll try another library, and try the code you sent! It will probably be tommorow before I can test it, so I'll post the results of the test tomorrow.

The test:
Use this library and the code from Koepel.

In my test I wrote that I was not happy with existing code. Guess what ? The Wire.requestFrom() does not need Wire.beginTransmission() or Wire.endTransmission(). · Issue #1 · markbeee/BH1750FVI · GitHub

There seems to be an increase of troubles with the ESP8266 Wire library and there are also more troubles with the Adafruit BMP280 and BME280 libraries. I don't know what is going on, but perhaps something changed.

I don't have a ESP8266 or ESP32. With my Arduino M0 and a BME280 I run into the same troubles with the Adafruit library, but luckely other libraries did work. Adafruit is putting a lot of work into the MicroPython/CircuitPython code, and does not seem to have any interest anymore in improving the existing libraries :cry:

About the BH1750 code by markbeee and my issue with the extra Wire.beginTransmission() and Wire.endTransmission(): it might create an extra I2C bus transaction. That was often no problem in the past, but I don't know the ESP8266 Wire library and I don't understand what is going on at the moment.

You have to test it yourself. You can also fix that issue and remove those two lines after downloading it.

Yes, it didn't suprise me that I can't get both my I2C sensors to work with the ESP8266 layout haha. I also figured out that not soldering your pins could be a reason for tricky connections, which could lead to connection errors (as I have), so I'll have to solder the pins first before actually blaming software/hardware :-[ . I will probably do that on friday/saturday, so expect an update on this issue in the weekend somewhere (then I will try different libraries, codes, etc.)

Atleast for now thanks for all the help!