BH1750 'no communication' after switching it off and on again

Hello,
I am an ESPHome-User since about a month or so. I built 5 USB-powered devices with ESP32 successfully, also with BH1750. No problems so far.
A week ago I started with a battery powered version of ESP32 with a single Dallas temp sensor and a BH1750. I quickly noticed the high battery consumption when I do not switch both sensors OFF before deep sleep (I use 3xAAA NiMH with an LDO voltage regulator directly powering the ESP via 3.3V, without the transistor I get 2.25d of runtime. However, it does not matter if the ESP32 sleeps for 30min or 1h. Thus, I concluded that something else must be eating current. I was not able to measure the current, with my amperemeter connected the ESP32 would not turn on).

So, now I am using a P-channel IRF9Z34N mosfet in high side configuration (see Low side vs. High side transistor switch - Bald Engineer ) to switch both sensors on and off (logic therefore is inverted, GPIO low switching mosfet on and vice cersa). Yes, this mosfet is an overkill power-wise, but it works (a larger mosfet is also easier to solder :slight_smile: ).

Here comes the strange part: when I do an OTA, the BH1750 and the Dallas work as intended. Then I switch off the Dallas and the BH1750. The ESP32 goes to deep sleep shortly after. When it wakes up again, the mosfet switches both sensors on, I get a ‘Communication with BH1750 failed’ message. The I2C scan also fails (every address is red, so no I2C device is recognized). The Dallas works as intended. I do not get any values from the BH1750. I tried connecting ADDR pin of BH1750, it does not matter. Without the mosfet, this does not occur. Thus, the switching off via mosfet is the reason for this problem.

So here is my current yaml:

esphome:
  name: esphome--6
  on_boot:
   then:
     #- delay: 2s
     - switch.turn_off: mosfet_switch
     #wake_up command BH1750, how to implement??
     - delay: 53s # 180 - 10
     - switch.turn_on: mosfet_switch
     - delay: 2s
     - deep_sleep.enter: deep_sleep_1
esp32:
  board: esp32dev
  framework:
    type: arduino
# Enable logging
logger:
# Enable Home Assistant API
api:
ota:
  password: "XXXXXXXXXXXXXXXXX"
wifi:
  ssid: !secret wifi_ssid
  password: !secret wifi_password
  fast_connect: true
  output_power: 12dB
  manual_ip:
    static_ip: 192.168.178.156
    gateway: 192.168.178.10
    subnet: 255.255.255.0
  # Enable fallback hotspot (captive portal) in case wifi connection fails
  ap:
    ssid: "Esphome--6 Fallback Hotspot"
    password: "XXXXXXXXXX"
captive_portal:
deep_sleep: 
  run_duration: 60s
  sleep_duration: 840s 
  id: deep_sleep_1
i2c:
  sda: 21
  scl: 22
  scan: true #false does not solve BH1750 problem
  id: bus_a
dallas:
  - pin: 5
    update_interval: 22s
sensor:
  - platform: dallas
    address: 0xe300000bfd37a328
    name: "Mobil_1 ESP Dallas Onboard Temperature"
    accuracy_decimals: 2
  - platform: uptime
    name: "Mobil_1 ESP Uptime"
    update_interval: 40s
    filters:
      - lambda: return x / 3600;
    unit_of_measurement: "h"
    accuracy_decimals: 3
  - platform: wifi_signal
    name: "Mobil_1 ESP WiFi Signal"
    update_interval: 45s 
    accuracy_decimals: 1
  - platform: adc
    pin: 34
    name: "Mobil_1 ESP Battery Voltage"
    attenuation: 11dB
    update_interval: 24s
    filters:
      multiply: 1.4545 #voltage divider
  - platform: bh1750
    name: "Mobil_1 ESP BH Illuminance"
    address: 0x23
    id: light_sensor
    update_interval: 45s
    setup_priority: -100.0 #does not solve the BH1750 problem
button:
  - platform: restart
    name: "Mobil_1 Restart"
    id: restart_button
    disabled_by_default: true
    on_press:
      then:
        - logger.log: Button Pressed
switch:
  - platform: gpio
    pin: 13
    name: "Mosfet Switch" #logic inverted!
    id: mosfet_switch
    on_turn_off:
    - delay: 50s
    - switch.turn_on: mosfet_switch
status_led:
  pin:
    number: GPIO2

I found this topic SHTC3: Wake up the sensor during setup by Sizurka · Pull Request #993 · esphome/esphome · GitHub which sounds promising.

If anyone knows how to implement the wake up command in ESPHome, it would be very much appreciated (is it possible in the yaml? I do not know how to access the mentioned file in ESPHome). Maybe there is another workaround? I’m stuck at the moment, I already can see in the voltage curve that the mosfet really helps prolonging battery life, but without any signs of life of the BH1750.

Anyway, thanks in advance, maybe I have made a stupid mistake somewhere,

Mario

I tried to replicate what is done for SHTC3 in above link, I found the .cpp file for BH1750 using the File Editor in:

/config/esphome/.esphome/build/esphome--6/src/esphome/components/bh1750/bh1750.cpp

However, I don’t seem to be able to edit the file. File editor tells me ‘Saved’ but when I look up the file next time, the inserted line (…wake_up) is gone.

void BH1750Sensor::setup() {
  ESP_LOGCONFIG(TAG, "Setting up BH1750 '%s'...", this->name_.c_str());
  uint8_t turn_on = BH1750_COMMAND_POWER_ON;
    this->wake_up();
  if (this->write(&turn_on, 1) != i2c::ERROR_OK) {
    this->mark_failed();
    return;
  }
}

Any help appreciated,

Mario.

EDIT: I didn’t read the README. It says:


THIS DIRECTORY IS AUTO-GENERATED, DO NOT MODIFY

ESPHome automatically populates the build directory, and any
changes to this directory will be removed the next time esphome is
run.

For modifying esphome's core files, please use a development esphome install,
the custom_components folder or the external_components feature.

Ok, I was on the right track the whole time. I seem to have solved the problem with a lower level command found here:

The BH1750 now is switchable, like it should be.
I was not aware that such commands can be used in the yaml, this opens up a lot of possibilities:

esphome:
  name: esphome--6
  on_boot:
   priority: 900
   then:
     - delay: 2s
     - switch.turn_off: mosfet_switch
     - lambda: |-
        Wire.begin(); #solves the problem with BH1750
     #wake_up command BH1750
     - delay: 53s # 180 - 10
     - switch.turn_on: mosfet_switch
     - delay: 2s
     - deep_sleep.enter: deep_sleep_1 

I also tried setting SDA and SCL pins high or low, that had no influence.

However, power consumption is still very high, overall. I do not expect the batteries to hold up for longer than 3 days or so. Maybe some parts on the dev-board need to be removed altogether,

Mario.

1 Like

For completeness, I show some results below which confirm the working BH1750 (the random dots from yesterday are all done by OTAs, the above command was issued late in the evening)

Has this been a reliable solution for you?

Might be a little late, but yes. :slight_smile:

1 Like