How to enable 2 I2C devices each one using their own GPIO lines?

2,573 views
Skip to first unread message

Peggy Schafer

unread,
Jan 22, 2022, 1:47:03 AM1/22/22
to TasmotaUsers
I have a BMP280 and AHT10.  Both are wired as I2C.  The AHT10 is connected to I/O 4 and 5 (SDA/SCL).  The BMP280 is connected to I/O 18 and 23 (SDA/SCL).

This is on a Wemos LoLin32 Lite Esp32 Development Board. 

I used the Gitpod.io Tasmo compiler to compile tasmota 10.1.0 for ESP32, i selected a few options (Temp/Hum sensor) and I also added the custom parameter lines for the AHT1x.

I flashed the compiled bin file using ESP-Flasher (exe for windows).

When configuring the ESP32 using its web interface I can set the relevant I/O lines as SDA and SCL, and I notice beside each one is a small drop-down selector where I can select either "1" or "2".  I can select 1 or 2 (and nothing else) when only one or both sensors are connected.  Some examples I see of configuring these sensors shows (5) and (6) which I assume are the numbers I see in the drop-down menu but I never see "5" or "6" - only 1 or 2.

In any case, when I alternate giving setting each sensor to either 1 or 2 or both 1 or both 2, and go back to the main Tasmota page, I can see the sensor data properly for only 1 of the devices, not both.  I can sometimes see the BMP or the AHT but not both at the same time. 

Can anyone explain what's going on here?  How can I have 2 different I2C devices each connected on their own dedicated SDA/SCL pins and what are the numbers in the drop-down menu?

Philip Knowles

unread,
Jan 22, 2022, 2:06:13 AM1/22/22
to Peggy Schafer, TasmotaUsers

ESP32 has 2 I2C interfaces which is why you can choose 1 or 2.

 

You need to read the I2C page as you need to enable/disable drivers for I2C to prevent conflicts

 

Regards

 

Phil K

 

Sent from Mail for Windows

--
You received this message because you are subscribed to the Google Groups "TasmotaUsers" group.
To unsubscribe from this group and stop receiving emails from it, send an email to sonoffusers...@googlegroups.com.
To view this discussion on the web, visit https://groups.google.com/d/msgid/sonoffusers/279afb1c-ee47-486a-923c-ae255888b12bn%40googlegroups.com.

 

Peggy Schafer

unread,
Jan 22, 2022, 8:56:50 AM1/22/22
to TasmotaUsers

From that page:
===========
With the ESP32 you can set almost any pin to have I2C capabilities, you just need to set that in your code.  

When using the ESP32 with the Arduino IDE, use the Wire.h library to communicate with devices using I2C. With this library, you initialize the I2C as follows:

Wire.begin(I2C_SDA, I2C_SCL);

So, you just need to set your desired SDA and SCL GPIOs on the I2C_SDA and I2C_SCL variables.

However, if you’re using libraries to communicate with those sensors, this might not work and it might be a bit tricky to select other pins. That happens because those libraries might overwrite your pins if you don’t pass your own Wire instance when initializing the library.

In those cases, you need to take a closer look at the .cpp library files and see how to pass your own TwoWire parameters.

For example, if you take a closer look at the Adafruit BME280 library, you’ll find out that you can pass your own TwoWire to the begin() method.

(example)
===============

If it were true that you must use specific I/O pins for I2C  Then in the Tasmota template configure screen you should not have the ability to select SDA/SCL *on every I/O pin*  but only the I/O pins that you say are the dedicated I2C pins.

Peggy Schafer

unread,
Jan 22, 2022, 9:11:00 AM1/22/22
to TasmotaUsers
See also this:


==============

Devices with the same I²C-Address cannot be used on the same I²C-bus. So I suggest to implement the use of virtual I²C-buses in Tasmota. Only one common SCL-line/GPIO is needed for all I²C-buses. A SDA-line/GPIO has to be exclusive to a bus.

To be able to use the same device or different devices with the same I²C address multiple times I suggest a user-configurable table to assign libraries and I²C-addresses to buses.

e.g.

GPIO0: I2C-SCL
GPIO2: I2C-SDA-1
GPIO4: I2C-SDA-2
GPIO5: I2C-SDA-3

I2C-SDA-1(0x38) -> lib-VEML6070
I2C-SDA-1(0x39) -> lib-VEML6070
I2C-SDA-2(0x39) -> lib-TSL2561
I2C-SDA-3(0x39) -> lib-APDS9960

=====================

When compiling a custom tasmota version, in what file do the above lines go?

There is this comment in that thread:

======
You need esp32 if you want two physical distinct buses
======

Is there a difference between ESP8266 and ESP32 in this regard?

Peggy Schafer

unread,
Jan 22, 2022, 11:25:17 PM1/22/22
to TasmotaUsers
When I have module parameters set to:

IO GPIO4 I2C SDA 1  (AHT10)
IO GPIO5 I2C SCL 1  (AHT10)

IO GPIO18 I2C SDA 2  (BMP280)
IO GPIO23 I2C SCL 2  (BMP280)

This is what I see on the main menu:

MI ESP32 v0921        0 / 0
AHT1X Temperature        21.4 °C
AHT1X Humidity        41.0 %
AHT1X Dew point        7.6 °C
ESP32 Temperature        81.7 °C

I have no idea what "MI ESP32 v0921        0 / 0" is.

The first few lines of boot log is this:

==========
00:00:00.003 HDW: ESP32-D0WDQ6
00:00:00.038 UFS: FlashFS mounted with 308 kB free
00:00:00.109 CFG: Loaded from File, Count 29
00:00:00.125 QPC: Count 1
00:00:00.138 Project tasmota - Tasmota Version 10.1.0(TasmoCompiler-esp32generic)-2_0_1_1(2022-01-22T06:06:45)
00:00:00.151 SHT: Sensor did not ACK command
00:00:00.167 iBeacon register for advert callbacks
00:00:00.170 M32: init: request callbacks
00:00:00.212 I2C: AHT1X found at 0x38
===========

When I reverse the I2C module parameters (reversing the 1 and 2):

IO GPIO4 I2C SDA 2  (AHT10)
IO GPIO5 I2C SCL 2  (AHT10)

IO GPIO18 I2C SDA 1  (BMP280)
IO GPIO23 I2C SCL 1  (BMP280)

This is what I see on the main menu:

BMP280 Temperature        22.6 °C
BMP280 Pressure        982.4 hPa
MI ESP32 v0921        0 / 0
ESP32 Temperature        81.7 °C

Again I see the strange "MI ESP32 v0921        0 / 0".

The boot log now shows this:

============
00:00:00.002 HDW: ESP32-D0WDQ6
00:00:00.043 UFS: FlashFS mounted with 308 kB free
00:00:00.138 CFG: Loaded from File, Count 31
00:00:00.154 QPC: Count 1
00:00:00.167 Project tasmota - Tasmota Version 10.1.0(TasmoCompiler-esp32generic)-2_0_1_1(2022-01-22T06:06:45)
00:00:00.179 SHT: Sensor did not ACK command
00:00:00.201 I2C: BMP280 found at 0x76
00:00:00.205 iBeacon register for advert callbacks
00:00:00.208 M32: init: request callbacks
============

I seem to have the sensors wired correctly, but only the sensor configured as #1 is read correctly.  The other one generates "SHT: Sensor did not ACK command" log messages upon bootup.

Philip Knowles

unread,
Jan 23, 2022, 1:17:07 AM1/23/22
to Peggy Schafer, TasmotaUsers
The MI ESP32 is BLE devices


From: sonof...@googlegroups.com <sonof...@googlegroups.com> on behalf of Peggy Schafer <peggyg...@gmail.com>
Sent: Sunday, January 23, 2022 4:25:17 AM
To: TasmotaUsers <sonof...@googlegroups.com>
Subject: Re: How to enable 2 I2C devices each one using their own GPIO lines?
 

Philip Knowles

unread,
Jan 23, 2022, 1:25:55 AM1/23/22
to Peggy Schafer, TasmotaUsers
As I said several posts ago ESP32 has 2 I2C interfaces. Having said that PROVIDED that the I2C devices have DIFFERENT addresses you can have more devices on an ESP8266. The I2CScan will tell you the addresses. If they are different  you then need to use I2CDriver to enable/disable drivers to get the devices to appear.

Phil K
From: Philip Knowles <knowles...@gmail.com>
Sent: Sunday, January 23, 2022 6:17:00 AM
To: Peggy Schafer <peggyg...@gmail.com>; TasmotaUsers <sonof...@googlegroups.com>

Peggy Schafer

unread,
Jan 23, 2022, 9:00:46 PM1/23/22
to TasmotaUsers
CMD: gpio 255

(edited to show only the active or defined pins)

RSL: RESULT = {"GPIO4":{"641":"I2C SDA2"},"GPIO5":{"609":"I2C SCL2"},"GPIO18":{"640":"I2C SDA1"},"GPIO23":{"608":"I2C SCL1"}

What do the numbers 641, 609, 640 and 608 represent or indicate?

Philip Knowles

unread,
Jan 24, 2022, 1:44:16 AM1/24/22
to Peggy Schafer, TasmotaUsers
It's just the internal mapping of the connection that is used in the templates
Have you run I2CScan as I suggested?
Have you also read this as I suggested?
Sent: Monday, January 24, 2022 2:00:46 AM

Peggy Schafer

unread,
Jan 24, 2022, 9:55:12 AM1/24/22
to TasmotaUsers
15:47:41.694 CMD: i2cscan
15:47:41.724 RSL: RESULT = {"I2CScan":"Device(s) found at 0x76"}

Regarding you link to /docs/i2cdevices -
Both the sensors I'm using have drivers and if you read the very first post you will see that both devices do work as connected but not simultaneously.

I have since learned that support for 2 physical independent I2C buses was only recently added to Tasmota (and perhaps only for the ESP32 I'm not sure about that).  The problem is that many device drivers are written to NOT be indexed for 2 buses.  (or at least for more than 1 bus).  Some people have modified existing drivers for certain devices to suit their own needs so that they will operate on the second bus.

Peggy Schafer

unread,
Jan 24, 2022, 10:03:10 AM1/24/22
to TasmotaUsers

See here for an example of a driver modified for the second bus.

https://github.com/techman83/Tasmota/commit/8d3705cdc83dd0e5fb4e8251b1fa1398aee5e9e2

Philip Knowles

unread,
Jan 24, 2022, 11:44:03 AM1/24/22
to Peggy Schafer, TasmotaUsers

And when you connect the other device is its address the same? If it’s not the same it should work on one bus but you need to enable/disable the drivers – with some devices you can also change the address by a jumper on the board.

BMP280 can be wired as SPI too which would solve your problem anyway.

Only the ESP32 supports 2 x I2C natively and you just need to tell the GPIO which I2C it is.

 

Sent from Mail for Windows

 

From: Peggy Schafer
Sent: 24 January 2022 14:55
To: TasmotaUsers
Subject: Re: How to enable 2 I2C devices each one using their own GPIO lines?

 

15:47:41.694 CMD: i2cscan


15:47:41.724 RSL: RESULT = {"I2CScan":"Device(s) found at 0x76"}

Regarding you link to /docs/i2cdevices -
Both the sensors I'm using have drivers and if you read the very first post you will see that both devices do work as connected but not simultaneously.

I have since learned that support for 2 physical independent I2C buses was only recently added to Tasmota (and perhaps only for the ESP32 I'm not sure about that).  The problem is that many device drivers are written to NOT be indexed for 2 buses.  (or at least for more than 1 bus).  Some people have modified existing drivers for certain devices to suit their own needs so that they will operate on the second bus.

On Monday, January 24, 2022 at 1:44:16 AM UTC-5 knowles...@gmail.com wrote:

It's just the internal mapping of the connection that is used in the templates

Have you run I2CScan as I suggested?

Have you also read this as I suggested?

Sent: Monday, January 24, 2022 2:00:46 AM


To: TasmotaUsers <sonof...@googlegroups.com>
Subject: Re: How to enable 2 I2C devices each one using their own GPIO lines?

CMD: gpio 255

(edited to show only the active or defined pins)

RSL: RESULT = {"GPIO4":{"641":"I2C SDA2"},"GPIO5":{"609":"I2C SCL2"},"GPIO18":{"640":"I2C SDA1"},"GPIO23":{"608":"I2C SCL1"}

What do the numbers 641, 609, 640 and 608 represent or indicate?


On Sunday, January 23, 2022 at 1:25:55 AM UTC-5 knowles...@gmail.com wrote:

As I said several posts ago ESP32 has 2 I2C interfaces. Having said that PROVIDED that the I2C devices have DIFFERENT addresses you can have more devices on an ESP8266. The I2CScan will tell you the addresses. If they are different  you then need to use I2CDriver to enable/disable drivers to get the devices to appear.

 

Phil K

From: Philip Knowles <knowles...@gmail.com>
Sent: Sunday, January 23, 2022 6:17:00 AM
To: Peggy Schafer <peggyg...@gmail.com>; TasmotaUsers <sonof...@googlegroups.com>


Subject: Re: How to enable 2 I2C devices each one using their own GPIO lines?

The MI ESP32 is BLE devices

 

Philip Knowles

unread,
Jan 24, 2022, 11:56:44 AM1/24/22
to Peggy Schafer, TasmotaUsers

The AHT10 address is 0x38 or 0x39 and the BMP280 address is 0x76 or 0x77 so they should work on the same bus.

So i2cdriver10 1 and i2cdriver43 1 should enable both drivers to work.

 

Sent from Mail for Windows

 

From: Peggy Schafer
Sent: 24 January 2022 14:55
To: TasmotaUsers

Subject: Re: How to enable 2 I2C devices each one using their own GPIO lines?

 

15:47:41.694 CMD: i2cscan


15:47:41.724 RSL: RESULT = {"I2CScan":"Device(s) found at 0x76"}

Regarding you link to /docs/i2cdevices -
Both the sensors I'm using have drivers and if you read the very first post you will see that both devices do work as connected but not simultaneously.

I have since learned that support for 2 physical independent I2C buses was only recently added to Tasmota (and perhaps only for the ESP32 I'm not sure about that).  The problem is that many device drivers are written to NOT be indexed for 2 buses.  (or at least for more than 1 bus).  Some people have modified existing drivers for certain devices to suit their own needs so that they will operate on the second bus.

On Monday, January 24, 2022 at 1:44:16 AM UTC-5 knowles...@gmail.com wrote:

It's just the internal mapping of the connection that is used in the templates

Have you run I2CScan as I suggested?

Have you also read this as I suggested?

Sent: Monday, January 24, 2022 2:00:46 AM


To: TasmotaUsers <sonof...@googlegroups.com>
Subject: Re: How to enable 2 I2C devices each one using their own GPIO lines?

CMD: gpio 255

(edited to show only the active or defined pins)

RSL: RESULT = {"GPIO4":{"641":"I2C SDA2"},"GPIO5":{"609":"I2C SCL2"},"GPIO18":{"640":"I2C SDA1"},"GPIO23":{"608":"I2C SCL1"}

What do the numbers 641, 609, 640 and 608 represent or indicate?


On Sunday, January 23, 2022 at 1:25:55 AM UTC-5 knowles...@gmail.com wrote:

As I said several posts ago ESP32 has 2 I2C interfaces. Having said that PROVIDED that the I2C devices have DIFFERENT addresses you can have more devices on an ESP8266. The I2CScan will tell you the addresses. If they are different  you then need to use I2CDriver to enable/disable drivers to get the devices to appear.

 

Phil K

From: Philip Knowles <knowles...@gmail.com>
Sent: Sunday, January 23, 2022 6:17:00 AM
To: Peggy Schafer <peggyg...@gmail.com>; TasmotaUsers <sonof...@googlegroups.com>


Subject: Re: How to enable 2 I2C devices each one using their own GPIO lines?

The MI ESP32 is BLE devices

 

Peggy Schafer

unread,
Jan 24, 2022, 9:35:08 PM1/24/22
to TasmotaUsers

Which-ever sensor (AHT10 or BMP280) is on the bus that I'm defining as SDA/SCL 1 is the sensor that gets detected.  I'm not changing how they're wired, just how I'm defining their I2C bus (1 vs 2).

The AHT address is 38, the BMP is 76.  But their address doesn't matter - they're not on the same bus.

I can't put them on the same bus because (and this is right from the Tasmoda documentation) the AHT10 is a very "messy" device and can't share a bus with anything else.  It is not looked on very favorably by the Tasmota community.  I didn't know that at the time I bought them - they were pretty cheap for a combined temp/humidity sensor ($5 each).

The BMP280 is a combined temp/pressure sensor and it was also very cheap for a pressure sensor - I bought 3 for $10.  I've been told that the BMP can be wired for SPI (single wire) so that would solve the problem of getting them both working simultaneously.

The problem with Tasmota and I2C is the way that I2C drivers were originally implimented.   The bus number (1 or 2) is not passed to the driver.  All I2C drivers were originally written as if there is only 1 I2C bus.  Tasmota now supports 2 I2C buses, and the bus number (indicated by a variable named "wire") has been expanded to "wire1" to indicate the second bus.  If you want to assign a specific sensor to the second I2C bus then you have to re-write the driver (replace "wire" with "wire1").

Peggy Schafer

unread,
Jan 24, 2022, 10:07:42 PM1/24/22
to TasmotaUsers
Wait a minute.  Yes, BMP280 can do SPI, but I mistakenly thought that was "1-wire" like the DS18B20 temp sensor.  SPI require maybe 4 wires?  How do you define an SPI connection from the GPIO pin list menu in the GUI, and how many pins do you need to use?

Peggy Schafer

unread,
Jan 26, 2022, 9:06:53 PM1/26/22
to TasmotaUsers

Turns out that the Tasmota driver for the BMP280 only supports I2C, not SPI. 

SPI is very confusing, since on a lot of sensors they will label the pins differently vs how the ESP32 labels the ISP pins.  Some will use I and O vs D and Q.  Also, there seems to be 6 pins or lines assigned to SPI in Tasmota, the pin I don't know the function for is labeled "SPI DC". 

So bottom line, If you want to connect even just two different multi-function sensors that includes combinations of pressure, temperature, and humidity, your choices and ability to do this is very limited on an ESP device running Tasmota compared to other platforms.

 

Philip Knowles

unread,
Jan 27, 2022, 2:08:06 AM1/27/22
to Peggy Schafer, TasmotaUsers
I said that Tasmota doesn't support SPI for that sensor - bear in mind that it uses the Arduino library for a lot of drivers. It doesn't use 6 lines for SPI just MISO, MOSI, Clk and Slave Select. In your case you may be able to use 2 BMP280s on I2C by changing the address of 1 of them as I explained previously
Sent: Thursday, January 27, 2022 2:06:53 AM

To: TasmotaUsers <sonof...@googlegroups.com>
Subject: Re: How to enable 2 I2C devices each one using their own GPIO lines?
Turns out that the Tasmota driver for the BMP280 only supports I2C, not SPI. 

SPI is very confusing, since on a lot of sensors they will label the pins differently vs how the ESP32 labels the ISP pins.  Some will use I and O vs D and Q.  Also, there seems to be 6 pins or lines assigned to SPI in Tasmota, the pin I don't know the function for is labeled "SPI DC". 

So bottom line, If you want to connect even just two different multi-function sensors that includes combinations of pressure, temperature, and humidity, your choices and ability to do this is very limited on an ESP device running Tasmota compared to other platforms.

 

--
You received this message because you are subscribed to the Google Groups "TasmotaUsers" group.
To unsubscribe from this group and stop receiving emails from it, send an email to sonoffusers...@googlegroups.com.
Reply all
Reply to author
Forward
0 new messages