Communication issues with i2c sensors

Hello all,

I am having a bit communication issues with i2c sensors.

Below are some examples.

  1. MPL3115A2 connected to an ESP8266 (Wemos D1 mini).
    Despite the sensor being discovered, the communication fails.
    This happened after upgrading to ESPHome 2022.12.0
[09:15:31][C][i2c.arduino:053]:   SDA Pin: GPIO4
[09:15:31][C][i2c.arduino:054]:   SCL Pin: GPIO5
[09:15:31][C][i2c.arduino:055]:   Frequency: 50000 Hz
[09:15:31][C][i2c.arduino:058]:   Recovery: bus successfully recovered
[09:15:31][I][i2c.arduino:068]: Results from i2c bus scan:
[09:15:31][I][i2c.arduino:074]: Found i2c device at address 0x60
...
[09:15:31][C][mpl3115a2:035]: MPL3115A2:
[09:15:31][C][mpl3115a2:036]:   Address: 0x60
[09:15:31][E][mpl3115a2:040]: Communication with MPL3115A2 failed!
  1. BH1750 and BMP280 connected to ESP32 ( D1 mini ESP32)
    For some reason BH1750 is not even discovered. 0x76 is the address for BMP280
    This happened after upgrading to ESPHome 2022.12.0
[09:17:52][C][i2c.arduino:052]: I2C Bus:
[09:17:52][C][i2c.arduino:053]:   SDA Pin: GPIO21
[09:17:52][C][i2c.arduino:054]:   SCL Pin: GPIO22
[09:17:52][C][i2c.arduino:055]:   Frequency: 50000 Hz
[09:17:52][C][i2c.arduino:058]:   Recovery: bus successfully recovered
[09:17:52][I][i2c.arduino:068]: Results from i2c bus scan:
[09:17:52][I][i2c.arduino:074]: Found i2c device at address 0x76
...
[09:17:52][C][bh1750.sensor:119]:   Address: 0x23
[09:17:52][E][bh1750.sensor:121]: Communication with BH1750 failed!
  1. BMP280 only seems to work for me on ESP32.
    I have never managed to get BMP280 working on an ESP8266 as I always get:
Communication with BMP280 failed!

I canā€™t say that a specific release broke the communication as the issues above have been on and off present in different releases.

I can say that:

  • ESPHome 2022.12.0 presents all the issues I have stated above
  • ESPHome 2022.11.5: only issue (3) was present
  • ESPHome 2022.11.x (where x<5): definitely presented issue (3) but I did see issue (1) at some point not sure if it was between different releases or on the same one it worked one time and didnā€™t the other time.

In summary, my journey with i2c sensors started during some version before ESPHome 2022.11.5

  • First I saw issue (3) after which I replaced BMP280 on ESP8266 with MPL3115A2. That was ESPHome 2022.11.3 or 4 (not sure)
  • BH1750 despite being discovered wouldnā€™t work on ESPHome 2022.11.4 but started working on ESPHome 2022.11.5
  • That brings us to ESPHome 2022.12.0 where all the issues are back.

p.s.

  • I have seen the thread Has ESPHome i2c become unreliable? but I donā€™t think it applies as I have used many different power supplies with no difference in the outcome. As for pulling up the i2c pins (a) the documentation states that ESPs have internal pullups that get activated with i2c configuration and (b) in ESPHome 2022.11.5 the sensors worked reliably and consistently.

  • Given the issues I have seen so far I will avoid i2c sensors if I can but that is not the point.

Iā€™ve been using a BME280 and several BH1750s on nodemcu esp8266 boards for several years and they all work without problems.

That is what I would expect. I am literary using copy-paste code from the documentation. I even set up a brand new ESP8266 with BMP280 and BH1750 on it to rule out that the problem is coming from other parts of the code.

What version or ESPHome are you running?

ESPhome version 2022.12.0

Very odd.
Hope someone else has had the issue and has solved it.

Just recompiled one of my D1mini with a BME280 using ESPhome 2022.12.1 without any problem. And yes board also defined being a nodemcu since thatā€™s sort of a swiss army board type working. Normally Iā€™d ask to post your yaml code but since the sensor was found all I could think of would be lowering the frequency to see is that makes any differnce.
I do remember I once had a sensor asking me to do so since it didnā€™t work with 50000.

On the ESP32 board I have the BH1750 it is not discovered and I have tried to change the frequency as well with no result

Perhaps you could share your YAML code for the D1 mini? Perhaps something strange in there.
I bet you already turned the logger: debuglevel to verbose to check about whatā€™s going on.

I have given up on the D1 and installed a DHT22 instead.
I did get logs for the ESP32 with the missing sensor. I see nothing to sayā€¦ā€œahaā€ :sweat_smile:

Oh yeah, forgot to mention that when I set the BMP280 to report both Temperature and Pressure to Home Assistant I get the ā€œCommunication Failedā€ to the BMP280 as well. Which is why I have it commented out (not sending to HA)
yaml

esphome:
  name: "staircase-sensor"

esp32:
  board: esp32dev
  framework:
    type: arduino

# Enable logging
logger:
  level: VERY_VERBOSE

# Enable Home Assistant API
api:

ota:
  password: [redacted]

wifi:
  ssid: !secret wifi_ssid
  password: !secret wifi_password

  # Enable fallback hotspot (captive portal) in case wifi connection fails
  ap:
    ssid: "Pir-Stairs Fallback Hotspot"
    password: !secret ap_password

captive_portal:

substitutions:
  friendly_name: "PIR"
  motion_1_pin: GPIO14
  motion_2_pin: GPIO05
  motion_3_pin: GPIO23
  motion_4_pin: GPIO34
  motion_5_pin: GPIO19
  motion_6_pin: GPIO33

web_server:
  port: 80

# bluetooth_proxy:
#   active: true

i2c:
  sda: GPIO21
  scl: GPIO22
  scan: true
  #id: bus_a  


binary_sensor:
  - platform: gpio
    pin: 
      number: ${motion_1_pin} #GPIO39
      inverted: false
      mode:
        input: true
    name: "Motion-1"
    device_class: motion

  - platform: gpio
    pin: 
      number: ${motion_2_pin} #GPIO35
      inverted: false
      mode:
        input: true
    name: "Motion-2"
    device_class: motion

  - platform: gpio
    pin: 
      number: ${motion_3_pin} #GPIO33
      inverted: false
      mode:
        input: true
        # pulldown: true
    name: "Motion-3"
    device_class: motion

  - platform: gpio
    pin: 
      number: ${motion_4_pin} #GPIO34
      inverted: false
      mode:
        input: true
    name: "Motion-4"
    device_class: motion

  - platform: gpio
    pin: 
      number: ${motion_5_pin} #GPIO25
      inverted: false
      mode:
        input: true
    name: "Motion-5"
    device_class: motion

  - platform: gpio
    pin: 
      number: ${motion_6_pin} #GPIO32
      inverted: false
      mode:
        input: true
        # pullup: true
    name: "Motion-6"
    device_class: motion

# status_led:
#   pin:
#     number: GPIO02
#     inverted: true
button:
  - platform: restart
    name: "Landing ESP - Restart"

light:
  - platform: status_led
    name: "Landing ESP - status LED"
    pin: GPIO02

sensor:  
  # Uptime Sensor
  - platform: uptime
    name: Uptime Sensor
    id: sensor_uptime
    internal: true
  
  # Uptime timestamp
  - platform: template
    id: sensor_uptime_timestamp
    name: "${friendly_name} - Uptime"
    device_class: "timestamp"
    accuracy_decimals: 0
    update_interval: never
    lambda: |-
      static float timestamp = (
        id(homeassistant_time).utcnow().timestamp - id(sensor_uptime).state
      );
      return timestamp;

  - platform: bmp280
    temperature:
      name: "Landing - Temperature"
      oversampling: 16x
      filters:
        - offset: -0.4
        - delta: 0.09
    # pressure:
    #   name: "Landing - Pressure"
    #   unit_of_measurement: "mmHg"
    #   filters:
    #     - multiply: 0.75006
    address: 0x76
    update_interval: 25s

  - platform: bh1750
    name: "Landing -  Illuminance"
    address: 0x23
    update_interval: 10s
    filters:
      - delta: 0.09
    
# Enable time component to reset energy at midnight
# https://esphome.io/components/time.html#home-assistant-time-source
time:
  - platform: homeassistant
    id: homeassistant_time
    timezone: "Europe/London"
    on_time_sync:
      - component.update: sensor_uptime_timestamp

log

NFO Waiting for result...
INFO OTA successful
INFO Successfully uploaded program.
INFO Starting log output from staircase-sensor.local using esphome API
INFO Successfully connected to staircase-sensor.local
[15:59:18][I][app:102]: ESPHome version 2022.12.1 compiled on Dec 16 2022, 15:57:24
[15:59:18][C][status_led:063]: Status Led Light:
[15:59:18][C][status_led:064]:   Pin: GPIO2
[15:59:18][VV][api.service:595]: on_subscribe_home_assistant_states_request: SubscribeHomeAssistantStatesRequest {}
[15:59:18][VV][api.service:070]: send_binary_sensor_state_response: BinarySensorStateResponse {
  key: 2866486986
  state: NO
  missing_state: NO
}
[15:59:18][C][wifi:504]: WiFi:
[15:59:18][C][wifi:362]:   Local MAC: C8:C9:A3:C8:1F:D8
[15:59:18][C][wifi:363]:   SSID: [redacted]
[15:59:18][C][wifi:364]:   IP Address: 192.168.68.125
[15:59:18][C][wifi:366]:   BSSID: [redacted]
[15:59:18][C][wifi:367]:   Hostname: 'staircase-sensor'
[15:59:18][C][wifi:369]:   Signal strength: -58 dB ā–‚ā–„ā–†ā–ˆ
[15:59:18][V][wifi:371]:   Priority: 0.0
[15:59:18][C][wifi:373]:   Channel: 1
[15:59:18][C][wifi:374]:   Subnet: 255.255.255.0
[15:59:18][C][wifi:375]:   Gateway: 192.168.68.1
[15:59:18][C][wifi:376]:   DNS1: 192.168.1.254
[15:59:18][C][wifi:377]:   DNS2: 192.168.68.1
[15:59:18][VV][api.service:070]: send_binary_sensor_state_response: BinarySensorStateResponse {
  key: 2866486987
  state: NO
  missing_state: NO
}
[15:59:18][C][logger:293]: Logger:
[15:59:18][C][logger:294]:   Level: VERY_VERBOSE
[15:59:18][C][logger:295]:   Log Baud Rate: 115200
[15:59:18][C][logger:296]:   Hardware UART: UART0
[15:59:18][VV][api.service:070]: send_binary_sensor_state_response: BinarySensorStateResponse {
  key: 2866486988
  state: NO
  missing_state: NO
}
[15:59:18][C][i2c.arduino:052]: I2C Bus:
[15:59:18][C][i2c.arduino:053]:   SDA Pin: GPIO21
[15:59:18][C][i2c.arduino:054]:   SCL Pin: GPIO22
[15:59:18][C][i2c.arduino:055]:   Frequency: 50000 Hz
[15:59:18][C][i2c.arduino:058]:   Recovery: bus successfully recovered
[15:59:18][I][i2c.arduino:068]: Results from i2c bus scan:
[15:59:18][I][i2c.arduino:074]: Found i2c device at address 0x76
[15:59:18][VV][api.service:070]: send_binary_sensor_state_response: BinarySensorStateResponse {
  key: 2866486989
  state: NO
  missing_state: NO
}
[15:59:18][VV][api.service:070]: send_binary_sensor_state_response: BinarySensorStateResponse {
  key: 2866486990
  state: NO
  missing_state: NO
}
[15:59:18][C][gpio.binary_sensor:015]: GPIO Binary Sensor 'Motion-1'
[15:59:18][C][gpio.binary_sensor:015]:   Device Class: 'motion'
[15:59:18][C][gpio.binary_sensor:016]:   Pin: GPIO14
[15:59:18][C][gpio.binary_sensor:015]: GPIO Binary Sensor 'Motion-2'
[15:59:18][C][gpio.binary_sensor:015]:   Device Class: 'motion'
[15:59:18][C][gpio.binary_sensor:016]:   Pin: GPIO5
[15:59:18][VV][api.service:122]: send_light_state_response: LightStateResponse {
  key: 3032877358
  state: NO
  brightness: 1
  color_mode: COLOR_MODE_ON_OFF
  color_brightness: 1
  red: 1
  green: 1
  blue: 1
  white: 1
  color_temperature: 0
  cold_white: 1
  warm_white: 1
  effect: ''
}
[15:59:18][C][gpio.binary_sensor:015]: GPIO Binary Sensor 'Motion-3'
[15:59:18][C][gpio.binary_sensor:015]:   Device Class: 'motion'
[15:59:18][C][gpio.binary_sensor:016]:   Pin: GPIO23
[15:59:18][C][gpio.binary_sensor:015]: GPIO Binary Sensor 'Motion-4'
[15:59:18][C][gpio.binary_sensor:015]:   Device Class: 'motion'
[15:59:18][C][gpio.binary_sensor:016]:   Pin: GPIO34
[15:59:18][C][gpio.binary_sensor:015]: GPIO Binary Sensor 'Motion-5'
[15:59:18][C][gpio.binary_sensor:015]:   Device Class: 'motion'
[15:59:18][C][gpio.binary_sensor:016]:   Pin: GPIO19
[15:59:18][VV][api.service:140]: send_sensor_state_response: SensorStateResponse {
  key: 4216381597
  state: 1.67121e+09
  missing_state: NO
}
[15:59:18][C][gpio.binary_sensor:015]: GPIO Binary Sensor 'Motion-6'
[15:59:18][C][gpio.binary_sensor:015]:   Device Class: 'motion'
[15:59:18][C][gpio.binary_sensor:016]:   Pin: GPIO33
[15:59:18][VV][api.service:140]: send_sensor_state_response: SensorStateResponse {
  key: 3424784105
  state: 22.02
  missing_state: NO
}
[15:59:18][C][uptime.sensor:031]: Uptime Sensor 'Uptime Sensor'
[15:59:18][C][uptime.sensor:031]:   Device Class: 'duration'
[15:59:18][C][uptime.sensor:031]:   State Class: 'total_increasing'
[15:59:18][C][uptime.sensor:031]:   Unit of Measurement: 's'
[15:59:18][C][uptime.sensor:031]:   Accuracy Decimals: 0
[15:59:18][C][uptime.sensor:031]:   Icon: 'mdi:timer-outline'
[15:59:18][V][uptime.sensor:031]:   Unique ID: 'c8c9a3c81fd8-uptime'
[15:59:18][VV][api.service:140]: send_sensor_state_response: SensorStateResponse {
  key: 3800293995
  state: nan
  missing_state: YES
}
[15:59:18][C][template.sensor:023]: Template Sensor 'PIR - Uptime'
[15:59:18][C][template.sensor:023]:   Device Class: 'timestamp'
[15:59:18][C][template.sensor:023]:   State Class: ''
[15:59:18][C][template.sensor:023]:   Unit of Measurement: ''
[15:59:18][C][template.sensor:023]:   Accuracy Decimals: 0
[15:59:18][C][template.sensor:024]:   Update Interval: never
[15:59:18][C][light:104]: Light 'Landing ESP - status LED'
[15:59:18][C][restart.button:017]: Restart Button 'Landing ESP - Restart'
[15:59:18][C][bmp280.sensor:098]: BMP280:
[15:59:18][C][bmp280.sensor:099]:   Address: 0x76
[15:59:18][C][bmp280.sensor:111]:   IIR Filter: OFF
[15:59:18][C][bmp280.sensor:112]:   Update Interval: 25.0s
[15:59:18][C][bmp280.sensor:114]:   Temperature 'Landing - Temperature'
[15:59:18][C][bmp280.sensor:114]:     Device Class: 'temperature'
[15:59:18][C][bmp280.sensor:114]:     State Class: 'measurement'
[15:59:18][C][bmp280.sensor:114]:     Unit of Measurement: 'Ā°C'
[15:59:18][C][bmp280.sensor:114]:     Accuracy Decimals: 1
[15:59:18][C][bmp280.sensor:115]:     Oversampling: 16x
[15:59:18][C][bmp280.sensor:117]:     Oversampling: 16x
[15:59:18][C][bh1750.sensor:118]: BH1750 'Landing -  Illuminance'
[15:59:18][C][bh1750.sensor:118]:   Device Class: 'illuminance'
[15:59:18][C][bh1750.sensor:118]:   State Class: 'measurement'
[15:59:18][C][bh1750.sensor:118]:   Unit of Measurement: 'lx'
[15:59:18][C][bh1750.sensor:118]:   Accuracy Decimals: 1
[15:59:18][C][bh1750.sensor:119]:   Address: 0x23
[15:59:18][E][bh1750.sensor:121]: Communication with BH1750 failed!
[15:59:18][C][bh1750.sensor:124]:   Update Interval: 10.0s
[15:59:18][C][homeassistant.time:010]: Home Assistant Time:
[15:59:18][C][homeassistant.time:011]:   Timezone: 'GMT0BST,M3.5.0/1,M10.5.0'
[15:59:18][C][captive_portal:088]: Captive Portal:
[15:59:18][C][web_server:125]: Web Server:
[15:59:18][C][web_server:126]:   Address: staircase-sensor.local:80
[15:59:18][C][mdns:103]: mDNS:
[15:59:18][C][mdns:104]:   Hostname: staircase-sensor
[15:59:18][V][mdns:105]:   Services:
[15:59:18][V][mdns:107]:   - _esphomelib, _tcp, 6053
[15:59:18][V][mdns:109]:     TXT: version = 2022.12.1
[15:59:18][V][mdns:109]:     TXT: mac = c8c9a3c81fd8
[15:59:18][V][mdns:109]:     TXT: platform = ESP32
[15:59:18][V][mdns:109]:     TXT: board = esp32dev
[15:59:18][V][mdns:109]:     TXT: network = wifi
[15:59:18][V][mdns:107]:   - _http, _tcp, 80
[15:59:19][C][ota:093]: Over-The-Air Updates:
[15:59:19][C][ota:094]:   Address: staircase-sensor.local:3232
[15:59:19][C][ota:097]:   Using Password.
[15:59:19][C][api:138]: API Server:
[15:59:19][C][api:139]:   Address: staircase-sensor.local:6053
[15:59:19][C][api:143]:   Using noise encryption: NO
[15:59:21][VV][scheduler:196]: Running interval '' with interval=10000 last_execution=5143 (now=15145)
[15:59:21][VV][scheduler:196]: Running interval 'update' with interval=25000 last_execution=4294957481 (now=15185)
[15:59:21][V][bmp280.sensor:125]: Sending conversion request...
[15:59:21][VV][i2c.arduino:140]: 0x76 TX F4B5
[15:59:21][VV][scheduler:026]: set_timeout(name='data', timeout=76)
[15:59:21][VV][scheduler:196]: Running timeout 'data' with interval=76 last_execution=15205 (now=15282)
[15:59:21][VV][i2c.arduino:140]: 0x76 TX FA
[15:59:21][VV][i2c.arduino:116]: 0x76 RX 7F8FD0
[15:59:21][VV][i2c.arduino:140]: 0x76 TX F7
[15:59:21][VV][i2c.arduino:116]: 0x76 RX 5A3D70
[15:59:21][D][bmp280.sensor:149]: Got temperature=22.4Ā°C pressure=1009.7hPa
[15:59:21][V][sensor:076]: 'Landing - Temperature': Received new state 22.420000
[15:59:21][VV][sensor.filter:014]: Filter(0x3ffb29c8)::input(22.420000)
[15:59:21][VV][sensor.filter:024]: Filter(0x3ffb29c8)::output(22.020000) -> 0x3ffb3534
[15:59:21][VV][sensor.filter:014]: Filter(0x3ffb3534)::input(22.020000)
[15:59:31][VV][scheduler:196]: Running interval '' with interval=10000 last_execution=15143 (now=25143)
[15:59:31][VV][api.service:470]: on_ping_request: PingRequest {}
[15:59:31][VV][api.service:043]: send_ping_response: PingResponse {}
[15:59:32][VV][api.service:470]: on_ping_request: PingRequest {}
[15:59:32][VV][api.service:043]: send_ping_response: PingResponse {}
[15:59:41][VV][scheduler:196]: Running interval '' with interval=10000 last_execution=25143 (now=35147)
[15:59:44][VV][scheduler:196]: Running interval '' with interval=60000 last_execution=4294944638 (now=37342)
[15:59:46][VV][scheduler:196]: Running interval 'update' with interval=25000 last_execution=15185 (now=40187)
[15:59:46][V][bmp280.sensor:125]: Sending conversion request...
[15:59:46][VV][i2c.arduino:140]: 0x76 TX F4B5
[15:59:46][VV][scheduler:026]: set_timeout(name='data', timeout=76)
[15:59:46][VV][api.service:470]: on_ping_request: PingRequest {}
[15:59:46][VV][api.service:043]: send_ping_response: PingResponse {}
[15:59:46][VV][scheduler:196]: Running timeout 'data' with interval=76 last_execution=40209 (now=40285)
[15:59:46][VV][i2c.arduino:140]: 0x76 TX FA
[15:59:46][VV][i2c.arduino:116]: 0x76 RX 7F9040
[15:59:46][VV][i2c.arduino:140]: 0x76 TX F7
[15:59:47][VV][i2c.arduino:116]: 0x76 RX 5A3E00

Working with me ā€¦ and all my other 12c nodes


esp8266:
  board: d1_mini


i2c:
  sda: 04
  scl: 05
  scan: true
  id: bus_a

sensor:
  - platform: bmp280
    i2c_id: bus_a
    temperature:
      name: "Temperature Roof Weather Box"
      oversampling: 16x
      accuracy_decimals: 0
    pressure:
      name: "Pressure"
      accuracy_decimals: 0
    address: 0x77
    update_interval: 120s
1 Like

found this thread because of similar problems.
was able to fix it by lowering the IĀ²C bus frequency.

i2c:
  scl: D2
  sda: D1
  scan: false # set true and watch output to find the BUS ID
  id: bus_a
  frequency: 100kHz

display:
  - platform: ssd1306_i2c
    i2c_id: bus_a
    model: "SH1106 128x64" # with default IĀ²C frequencies it only works as "SH1106 128x32", hangs if set to 64!
    id: "my_oled"

cheers,

I have been working on the ā€œComponent xxx took a long timeā€¦ā€ warning enabled by recent esphome versions. That is sometimes due to low i2c speed settings, like the default 50kHz. (This is unrelated to the OP issues.)

Measuring a D1-mini, with no pullup, the i2C lines rise time is around 4us. That alone would limit the i2C speed to probably no more than 100kHz (from raw signal point of view).

With 22K pullup, the rise time is around 800ns. That would raise the upper limit to no more than 500kHz.

i2c speed depends on a number of things, and pullups have a significant role. Typical sensor modules often have pullups but of various values. Therefore, for example, two bmp280 modules of different made could work differently on i2c speed. Also, long wire length can also reduce the workable speed.

This was very helpful for meā€¦ even if it is an ā€œolderā€ postā€¦ thanks a lot.

I was struggling with two thingsā€¦ finding the right board for my Wemos-D1-Mini and running a BH1750 on it (sounds simple, but took me hours)

1 Like

Does anyone know if I can use any I2C out of the box, or is their a list of devices that are in ESPHome by default?

IE: if I find an interesting device, can I just add it in my YAML?

The short answer is no.

I2C is a low-level protocol for reading and writing bytes from and to devices. What is in those bytes is mostly dependent on what the specific device has implemented. So, to use any particular type of device, you want it to have a supporting component definition in ESPHome.

Now, technically, you can probably cobble together custom C++ code to put into your YAML file as an ESPHome script to read and write the necessary logic, but thatā€™s at best a clumsy way to go about it. For less effort, you could implement it directly as a C++ component that you then merely reference from your YAML.

ESPHome has so many components already implemented that itā€™s usually pretty easy to find something pretty similar (in implementation logic) to a hypothetical new device. It would be a bit beyond copy-and-paste, but an experienced developer could usually dope it out fairly easily.

There is a draft PR that would add the ability to target otherwise unsupported i2c devices with purely YAML config - you could try that out.

Thanks for that, it looks to be bring what I can doā€¦ And I am not sure about others demand for this specific device!

Iā€™ve ended up going with a 0.4 to 20 device that seems to be working quite well so far!