Multiple Devices on I2C bus

Thanks Tom. Some great advice there. Should make making my mutlisensor easier!

Btw, do I still need the resistors as I see it says on the I2C page of ESPHome:

Please note the ESP will enable its internal 10kΩ pullup resistors for these pins, so you usually don’t need to put on external ones.

? If I do need them, how do you figure out what rating the resistor needs to be?

Thanks again in advance. Learning lots here!

Try it with only the internal resistors. You should be good.

1 Like

Will do, thanks man!

Hi, I might need to open this up again:

I have an AM2320 and a BH1750 both use i2c. The board I use is ESP-WROOM-32 38Pin Layout.

I created the following config for ESPHome:

captive_portal:

i2c:
  - id: bus_a
    sda: 21
    scl: 22
    scan: true
  - id: bus_b
    sda: 13
    scl: 16
    scan: true

sensor:
#Helligkeit
  - platform: bh1750
    i2c_id: bus_b
    name: "Lux Garage"
    address: 0x23
    #address: 0xc5
    update_interval: 30s
    filters:
      - sliding_window_moving_average:
          window_size: 10
          send_every: 10
          send_first_at: 1
      - or:
          - throttle: 60s
          - delta: 10

#Temperatur
  - platform: am2320
    setup_priority: -100
    i2c_id: bus_a
    address: 0x5c
    temperature:
      name: "Garage Temperature"
    humidity:
      name: "Garage Humidity"
    update_interval: 30s

But the issue I have is only the BH1750 is detected and also working. The AM2320 is found but communication fails. If I disconnect the BH1750 the AM2320 is working fine…

Any hints on what I am doing wrong here?

Do you have the BH1750 ADDR pin connected to +3.3V?

If so it will be trying to use address 0x5C which conflicts with the AM2320 address.

You have to leave the BH1750 ADDR pin floating/unconnected (or preferably shorted to GND) to get it to use address 0x23.

The AM2320 address cannot be changed from 0x5C.

With the AM2320 disconnected look at the esp device logs. What address is the BH1750 using?

Hey,
BH1750 ADDR is not connected.

The Log says the following:

INFO Starting log output from esphome-garage.local using esphome API
INFO Successfully connected to esphome-garage.local
[11:07:48][I][app:102]: ESPHome version 2022.12.3 compiled on Jan 11 2023, 09:06:15
[11:07:48][C][wifi:504]: WiFi:
[11:07:48][C][wifi:362]:   Local MAC: 24:D7:EB:xx:xx:xx
[11:07:48][C][wifi:363]:   SSID: [redacted]
[11:07:48][C][wifi:364]:   IP Address: 192.168.2.90
[11:07:48][C][wifi:366]:   BSSID: [redacted]
[11:07:48][C][wifi:367]:   Hostname: 'esphome-garage'
[11:07:48][C][wifi:369]:   Signal strength: -69 dB ▂▄▆█
[11:07:48][C][wifi:373]:   Channel: 13
[11:07:48][C][wifi:374]:   Subnet: 255.255.255.0
[11:07:48][C][wifi:375]:   Gateway: 192.168.2.2
[11:07:48][C][wifi:376]:   DNS1: 192.168.2.2
[11:07:48][C][wifi:377]:   DNS2: 0.0.0.0
[11:07:48][C][logger:293]: Logger:
[11:07:48][C][logger:294]:   Level: DEBUG
[11:07:48][C][logger:295]:   Log Baud Rate: 115200
[11:07:48][C][logger:296]:   Hardware UART: UART0
[11:07:48][C][i2c.arduino:052]: I2C Bus:
[11:07:48][C][i2c.arduino:053]:   SDA Pin: GPIO21
[11:07:48][C][i2c.arduino:054]:   SCL Pin: GPIO22
[11:07:48][C][i2c.arduino:055]:   Frequency: 50000 Hz
[11:07:48][C][i2c.arduino:058]:   Recovery: bus successfully recovered
[11:07:48][I][i2c.arduino:068]: Results from i2c bus scan:
[11:07:48][I][i2c.arduino:070]: Found no i2c devices!
[11:07:49][C][i2c.arduino:052]: I2C Bus:
[11:07:49][C][i2c.arduino:053]:   SDA Pin: GPIO13
[11:07:49][C][i2c.arduino:054]:   SCL Pin: GPIO16
[11:07:49][C][i2c.arduino:055]:   Frequency: 50000 Hz
[11:07:49][C][i2c.arduino:058]:   Recovery: bus successfully recovered
[11:07:49][I][i2c.arduino:068]: Results from i2c bus scan:
[11:07:49][I][i2c.arduino:074]: Found i2c device at address 0x23
[11:07:49][C][gpio.binary_sensor:015]: GPIO Binary Sensor 'Regensensor'
[11:07:49][C][gpio.binary_sensor:015]:   Device Class: 'window'
[11:07:49][C][gpio.binary_sensor:016]:   Pin: GPIO14
[11:07:49][C][gpio.binary_sensor:015]: GPIO Binary Sensor 'Regenmenge'
[11:07:49][C][gpio.binary_sensor:015]:   Device Class: 'window'
[11:07:49][C][gpio.binary_sensor:016]:   Pin: GPIO26
[11:07:49][C][status:034]: Status Binary Sensor 'Wetter Status'
[11:07:49][C][status:034]:   Device Class: 'connectivity'
[11:07:49][C][bh1750.sensor:118]: BH1750 'Lux Garage'
[11:07:49][C][bh1750.sensor:118]:   Device Class: 'illuminance'
[11:07:49][C][bh1750.sensor:118]:   State Class: 'measurement'
[11:07:49][C][bh1750.sensor:118]:   Unit of Measurement: 'lx'
[11:07:49][C][bh1750.sensor:118]:   Accuracy Decimals: 1
[11:07:49][C][bh1750.sensor:119]:   Address: 0x23
[11:07:49][C][bh1750.sensor:124]:   Update Interval: 30.0s
[11:07:49][C][restart:076]: Restart Switch 'Garage Restart'
[11:07:49][C][restart:078]:   Icon: 'mdi:restart'
[11:07:49][C][restart:099]:   Restore Mode: restore defaults to OFF
[11:07:49][C][bluetooth_proxy:065]: Bluetooth Proxy:
[11:07:49][C][bluetooth_proxy:066]:   Active: YES
[11:07:49][C][esp32_ble_tracker:870]: BLE Tracker:
[11:07:49][C][esp32_ble_tracker:871]:   Scan Duration: 300 s
[11:07:49][C][esp32_ble_tracker:872]:   Scan Interval: 320.0 ms
[11:07:49][C][esp32_ble_tracker:873]:   Scan Window: 30.0 ms
[11:07:49][C][esp32_ble_tracker:874]:   Scan Type: ACTIVE
[11:07:49][C][esp32_ble_tracker:875]:   Continuous Scanning: True
[11:07:49][C][captive_portal:088]: Captive Portal:
[11:07:49][C][mdns:103]: mDNS:
[11:07:49][C][mdns:104]:   Hostname: esphome-garage
[11:07:49][C][ota:093]: Over-The-Air Updates:
[11:07:49][C][ota:094]:   Address: esphome-garage.local:3232
[11:07:49][C][api:138]: API Server:
[11:07:49][C][api:139]:   Address: esphome-garage.local:6053
[11:07:49][C][api:143]:   Using noise encryption: NO
[11:07:49][D][am2320:047]: AM2320:
[11:07:49][C][am2320:048]:   Address: 0x5C
[11:07:49][E][am2320:050]: Communication with AM2320 failed!
[11:07:49][C][am2320:052]:   Temperature 'Garage Temperature'
[11:07:49][C][am2320:052]:     Device Class: 'temperature'
[11:07:49][C][am2320:052]:     State Class: 'measurement'
[11:07:49][C][am2320:052]:     Unit of Measurement: '°C'
[11:07:49][C][am2320:052]:     Accuracy Decimals: 1
[11:07:49][C][am2320:053]:   Humidity 'Garage Humidity'
[11:07:49][C][am2320:053]:     Device Class: 'humidity'
[11:07:49][C][am2320:053]:     State Class: 'measurement'
[11:07:49][C][am2320:053]:     Unit of Measurement: '%'
[11:07:49][C][am2320:053]:     Accuracy Decimals: 1
[11:08:12][D][bh1750.sensor:159]: 'Lux Garage': Got illuminance=21.1lx

Yep. The log has the BH1750 at 0x23 which is good.

It’s not finding the AM2320 at all. Check the wiring. Or was that a log from when it was disconnected?

If so connect it up and post the log again.

From memory, i2s requires that the wiring legs for each device are symmetrical. It is mentioned earlier in this thread that the daisy chain method of connection needs to be used. If you do a deep search there are tutorials out there for correct wiring. I think this is to do with i2s being a clock based protocol, anything which messes with the ability to lock a the correct clock/ frequency will make the device fail. So I had a device fail on a 7m long cat6 cable; I had read that capacitance is a problem; I found the solution was to swap the data line to another twisted pair but leave the 2nd wire of the pair unconnected (unlike an analogue line where you might use ground for the 2nd twisted pair). Also I could NOT put another i2s device on that line, because it would never be balanced! Anyway good luck!

A datasheet of the AM2320 I just read actually lists it’s address as 0xB8, https://cdn-shop.adafruit.com/product-files/3721/AM2320.pdf

Yeah, you definitely can’t use SDA and SCL on the same twisted pair without crosstalk introducing errors. See how I know: Outdoor Lux Sensor

2 Likes

@Tom_in_HI - good article. Wish I had seen it earlier! Would have saved me time and some hair!

1 Like

AM2320 was disconnected in the previous log.

Here is one with both connected:

INFO Reading configuration /config/esphome/esphome-garage.yaml...
INFO Starting log output from esphome-garage.local using esphome API
INFO Successfully connected to esphome-garage.local
[12:02:54][I][app:102]: ESPHome version 2022.12.3 compiled on Jan 11 2023, 12:00:25
[12:02:54][C][wifi:504]: WiFi:
[12:02:54][C][wifi:362]:   Local MAC: 24:D7:EB:xx:xx:xx
[12:02:54][C][wifi:363]:   SSID: [redacted]
[12:02:54][C][wifi:364]:   IP Address: 192.168.2.90
[12:02:54][C][wifi:366]:   BSSID: [redacted]
[12:02:54][C][wifi:367]:   Hostname: 'esphome-garage'
[12:02:54][C][wifi:369]:   Signal strength: -69 dB ▂▄▆█
[12:02:54][C][wifi:373]:   Channel: 13
[12:02:54][C][wifi:374]:   Subnet: 255.255.255.0
[12:02:54][C][wifi:375]:   Gateway: 192.168.2.2
[12:02:54][C][wifi:376]:   DNS1: 192.168.2.2
[12:02:54][C][wifi:377]:   DNS2: 0.0.0.0
[12:02:54][C][logger:293]: Logger:
[12:02:54][C][logger:294]:   Level: DEBUG
[12:02:54][C][logger:295]:   Log Baud Rate: 115200
[12:02:54][C][i2c.arduino:052]: I2C Bus:
[12:02:54][C][i2c.arduino:053]:   SDA Pin: GPIO21
[12:02:54][C][i2c.arduino:055]:   Frequency: 50000 Hz
[12:02:54][I][i2c.arduino:068]: Results from i2c bus scan:
[12:02:54][I][i2c.arduino:070]: Found no i2c devices!
[12:02:54][C][i2c.arduino:052]: I2C Bus:
[12:02:54][C][i2c.arduino:053]:   SDA Pin: GPIO13
[12:02:54][C][i2c.arduino:054]:   SCL Pin: GPIO16
[12:02:54][C][i2c.arduino:055]:   Frequency: 50000 Hz
[12:02:54][C][i2c.arduino:058]:   Recovery: bus successfully recovered
[12:02:54][I][i2c.arduino:068]: Results from i2c bus scan:
[12:02:54][I][i2c.arduino:074]: Found i2c device at address 0x23
[12:02:54][C][gpio.binary_sensor:015]: GPIO Binary Sensor 'Regensensor'
[12:02:54][C][gpio.binary_sensor:015]:   Device Class: 'window'
[12:02:54][C][gpio.binary_sensor:016]:   Pin: GPIO14
[12:02:54][C][gpio.binary_sensor:015]: GPIO Binary Sensor 'Regenmenge'
[12:02:54][C][gpio.binary_sensor:015]:   Device Class: 'window'
[12:02:54][C][gpio.binary_sensor:016]:   Pin: GPIO26
[12:02:54][C][status:034]: Status Binary Sensor 'Wetter Status'
[12:02:54][C][bh1750.sensor:118]: BH1750 'Lux Garage'
[12:02:54][C][bh1750.sensor:118]:   Accuracy Decimals: 1
[12:02:54][C][bh1750.sensor:119]:   Address: 0x23
[12:02:54][C][bh1750.sensor:124]:   Update Interval: 30.0s
[12:02:54][C][restart:076]: Restart Switch 'Garage Restart'
[12:02:54][C][restart:078]:   Icon: 'mdi:restart'
[12:02:54][C][restart:099]:   Restore Mode: restore defaults to OFF
[12:02:54][C][bluetooth_proxy:065]: Bluetooth Proxy:
[12:02:54][C][bluetooth_proxy:066]:   Active: YES
[12:02:54][C][esp32_ble_tracker:870]: BLE Tracker:
[12:02:54][C][esp32_ble_tracker:871]:   Scan Duration: 300 s
[12:02:54][C][esp32_ble_tracker:872]:   Scan Interval: 320.0 ms
[12:02:54][C][esp32_ble_tracker:873]:   Scan Window: 30.0 ms
[12:02:54][C][esp32_ble_tracker:874]:   Scan Type: ACTIVE
[12:02:54][C][esp32_ble_tracker:875]:   Continuous Scanning: True
[12:02:54][C][captive_portal:088]: Captive Portal:
[12:02:54][C][mdns:103]: mDNS:
[12:02:54][C][mdns:104]:   Hostname: esphome-garage
[12:02:54][C][ota:093]: Over-The-Air Updates:
[12:02:54][C][ota:094]:   Address: esphome-garage.local:3232
[12:02:54][C][api:138]: API Server:
[12:02:54][C][api:139]:   Address: esphome-garage.local:6053
[12:02:55][C][api:143]:   Using noise encryption: NO
[12:02:55][D][am2320:047]: AM2320:
[12:02:55][C][am2320:048]:   Address: 0x5C
[12:02:55][E][am2320:050]: Communication with AM2320 failed!
[12:02:55][C][am2320:052]:   Temperature 'Garage Temperature'
[12:02:55][C][am2320:052]:     Device Class: 'temperature'
[12:02:55][C][am2320:052]:     State Class: 'measurement'
[12:02:55][C][am2320:052]:     Unit of Measurement: '°C'
[12:02:55][C][am2320:052]:     Accuracy Decimals: 1
[12:02:55][C][am2320:053]:   Humidity 'Garage Humidity'
[12:02:55][C][am2320:053]:     Device Class: 'humidity'
[12:02:55][C][am2320:053]:     State Class: 'measurement'
[12:02:55][C][am2320:053]:     Unit of Measurement: '%'
[12:02:55][C][am2320:053]:     Accuracy Decimals: 1

BH1750 and AM2320 are directly connected to the board to the GPIOs 13/16(BH1750 to 3.3v) and 21/22(AM2320 to 5v)

Somehow Communication now fails with AM2320… I tested multiple (3) from that I know they work…

I’ll change the AM2320 to 0xB8 let’s see :slight_smile:

For me it was the wiring in the end … it worked … it didn’t work … it did work …it didn’t work

so… wire was the magic word… i switches the wiring and tada… now everything works like a charm. thx for the hint!

1 Like

If you dont mind, would you share the wiring diagram of your setup. And the final working yaml code. I am a newbie and trying to build a multi-sensor for my diy project. Thanks.

1 Like

@goeste Could you share your setup please?
Best regards :smiley:

@KaherdinTristan @animeshp1: sry for coming back only now… also i got rid of the AM… and switched to a bme280

this is my complete config: unfortunately there is no diagram, but according to the pins you should be able to figure it all out. I connected nearly all grounds to one single GND though

esphome:
  name: "esphome-garage"

esp32:
  board: esp32dev
  framework:
    type: arduino

# Enable logging
logger:

# Enable Home Assistant API
api:

ota:

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

  # Enable fallback hotspot (captive portal) in case wifi connection fails
  ap:
    ssid: "Esphome-Web-xxxxxx"
    password: "xxxxxx"
###
# Los geht's
###
captive_portal:

###
# I2C
###
i2c:
  - id: bus_a
    sda: 16
    scl: 17
    scan: true

###
# BinarySensors
###
binary_sensor:
  - platform: gpio
    name: Regensensor
    device_class: moisture
    pin:
      number: GPIO14
      inverted: True
      mode:
        input: True
        pullup: True
#Regenmenge
  - platform: gpio
    name: "Regenmenge"
    device_class: window
    pin:
      number: GPIO26
      inverted: True
      mode:
        input: True
        pullup: True
###
# Sensors
###
sensor:
#Helligkeit
#  - platform: bh1750
#    i2c_id: bus_b
#    name: "Lux Garage"
#    address: 0x23
#    #address: 0xc5
#    update_interval: 30s
#    filters:
#      - sliding_window_moving_average:
#          window_size: 10
#          send_every: 10
#          send_first_at: 1
#      - or:
#          - throttle: 60s
#          - delta: 10
#Temperatur
  - platform: bme280
    i2c_id: bus_a
    temperature:
      name: "Garage Temperature"
      oversampling: 16x
    pressure:
      name: "Garage Pressure"
    humidity:
      name: "Garage Humidity"
    address: 0x76
    update_interval: 5s
#Windsensor
  - platform: pulse_meter
    pin: 
      number: GPIO20
      mode: INPUT_PULLUP
    name: 'Garage Windgeschwindigkeit'
    icon: 'mdi:weather-windy'
    unit_of_measurement: 'm/s'
    internal_filter: 13us
    timeout: 5s
    filters:
      - multiply: 0.005560619
      - sliding_window_moving_average:
          window_size: 5
          send_every: 5

###
# Switches
###
switch:
  - platform: restart
    name: "Garage Restart"
1 Like

I think this comment just saved my day. Thanks alot @tom_l I was really starting to get crazy…

1 Like

Hello,

I’m facing quite same problem handling multiple I2C elements linked to my ESP32 using espHome.

In fact I would like both BH1750 and esp32cam to work together on same board.

For now I suceeded making only one working at a time.

I found the Pinout of the board Here https://github.com/Freenove/Freenove_Basic_Starter_Kit_for_ESP32/blob/5e168c36288cd304ef906464573b25e59ed3ef6a/Datasheet/ESP32-Pinout.pdf

Here is my yaml file

esphome:
  name: illuminance-mezzanine
  friendly_name: illuminance-mezzanine

esp32:
  board: esp32dev
  framework:
    type: arduino

# .....

captive_portal:

i2c:
  - sda: 26
    scl: 27
    id: bus_a
    scan: True

sensor:
  - platform: bh1750
    name: "BH1750 illuminance"
    update_interval: 1s
    i2c_id: bus_a
    #address: 0x5c


switch:
  - platform: restart
    name: "ESP32 Restart"

As for now Camera seams to be found using the scan parameter but not accessible.

When I tried to add the esp32cam component it obviously failed due to same pin usage.

Is someone can help me find out a way to share the pins or maybe to find some other I2C pins that are not used by the camera ?

Thanks