Multiple Devices on I2C bus

Hi all

I’m trying to see if I can create a multisensor using multiple I2C devices. I know the ESP32 has 2 I2C busses, but I believe you can also “daisy-chain” I2C devices together on the same single bus (as each device is given a “unique” address for the communication between it and the ESP over the I2C bus) and this could be useful to connect two or more devices to a single I2C bus on the ESP8266. I’d like to use a I2C temp and humidity sensor (HTU21D) along with an ambient light level sensor measuring lux (TSL2561 or BH1780) which I’ll then add a PIR sensor to (AM312).

I’ve not seen much on this other than here: https://www.bluedot.space/tutorials/how-many-devices-can-you-connect-on-i2c-bus/

Can ESPhome support this setup?

Yes. As long as each device on the bus has a unique address you can and more devices than you will ever need.

1 Like

Thanks for getting back to me Tom. Do you daisy chain one to the other or do you need to split wires from SDA and SCL with resistors like at the link I sent?

Thanks in advance!

Only two resistors are required, one for the SDA and one for the SCL line, close to the ESP preferably though unless you are using very long cables this isn’t critical.

Try to keep the lines as short as possible. Connect those two lines to every device including the ESP. You can do this by hopping the two lines from board to board (easy) or by connecting a star network where each device connects back to the two resistors at the ESP (can get messy with lots of devices).

1 Like

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