# TwoWire Issue to use 2 differents I2C objects with two LCDs

**URL:** <https://forum.arduino.cc/t/twowire-issue-to-use-2-differents-i2c-objects-with-two-lcds/882893>\
**Category:** Programming\
**Created:** [July 8, 2021, 7:21am UTC](https://forum.arduino.cc/t/twowire-issue-to-use-2-differents-i2c-objects-with-two-lcds/882893 "2021-07-08T07:21:30Z")\
**Posts on this page:** 13\
**Page:** 1

<div class="post-metadata">

**Author:** ![juanpdl](https://avatars.discourse-cdn.com/v4/letter/j/dec6dc/32.png) [@juanpdl](https://forum.arduino.cc/u/juanpdl)\
**Post date:** [July 8, 2021, 7:21am UTC](https://forum.arduino.cc/t/twowire-issue-to-use-2-differents-i2c-objects-with-two-lcds/882893/1 "2021-07-08T07:21:30Z")

</div>

Hi I'm using ESP32 with Arduino IDE. Does anyone know how you can use the TwoWire class of wire.h  
There is an example of how to initialize here: [ESP32 and multiple I2C buses | QuadMeUp](https://quadmeup.com/esp32-and-multiple-i2c-buses/)

I need to use 2 LCD at two differents I2C ports...  
//Set pins for I2C1  
#define I2C1\_SDA\_PIN 21  
#define I2C1\_SCL\_PIN 22

//Set pins for I2C2  
#define I2C2\_SDA\_PIN 18  
#define I2C2\_SCL\_PIN 219

TwoWire I2C1 = TwoWire(0); //I2C1 bus  
TwoWire I2C2 = TwoWire(1); //I2C2 bus

But I don´t know how can I handle two LCD displays

LiquidCrystal\_I2C lcd1(0x3F, 16, 2);  
LiquidCrystal\_I2C lcd2(0x27, 16, 2);

Please tell me what lines of code need to relate Wire with lcd1 and Wire1 with lcd2  
Thanks in advance.

---

<div class="post-metadata">

**Author:** ![Koepel](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/koepel/32/637546_2.png) [@Koepel](https://forum.arduino.cc/u/Koepel)\
**Post date:** [July 8, 2021, 7:32am UTC](https://forum.arduino.cc/t/twowire-issue-to-use-2-differents-i2c-objects-with-two-lcds/882893/2 "2021-07-08T07:32:28Z")

</div>

Is one LCD at 0x3F and the other at 0x27 ? Then you don't need two I2C buses. Put them on the same I2C bus.

The "TwoWire" is a lower level, which you don't have to worry about.  
That example with two I2C buses is from March 2021. That is very recent, so I suppose it works. Most libraries use the Arduino default of "`Wire`" in the source code, and some can select the I2C bus via a pointer to a TwoWire. If you need two I2C buses ( **you don't** ), then you can pass on such a pointer or you have to change the source code of the library.

---

<div class="post-metadata">

**Author:** ![juanpdl](https://avatars.discourse-cdn.com/v4/letter/j/dec6dc/32.png) [@juanpdl](https://forum.arduino.cc/u/juanpdl)\
**Post date:** [July 8, 2021, 8:03am UTC](https://forum.arduino.cc/t/twowire-issue-to-use-2-differents-i2c-objects-with-two-lcds/882893/3 "2021-07-08T08:03:27Z")

</div>

Thanks to answer. Yes, I already know that with only two lines I can connect many devices I2C. My question is How to use the two I2C that are in the ESP32, no how to connect 2 LCDs using the default two lines. Thanks

---

<div class="post-metadata">

**Author:** ![Koepel](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/koepel/32/637546_2.png) [@Koepel](https://forum.arduino.cc/u/Koepel)\
**Post date:** [July 8, 2021, 8:30am UTC](https://forum.arduino.cc/t/twowire-issue-to-use-2-differents-i2c-objects-with-two-lcds/882893/4 "2021-07-08T08:30:32Z")

</div>

Why do you want two I2C buses ?

Since the displays have different I2C addresses, they should be connected to a single I2C bus.  
I think I have already answered your question, use a TwoWire pointer or change the source code of the library.

Explanation: You have to actually tell why you want two I2C buses. If someone asks a question that seems strange, then it might be a [XY-problem](https://xyproblem.info/).

---

<div class="post-metadata">

**Author:** ![hsaul17](https://avatars.discourse-cdn.com/v4/letter/h/47e85d/32.png) [@hsaul17](https://forum.arduino.cc/u/hsaul17)\
**Post date:** [July 8, 2021, 9:24am UTC](https://forum.arduino.cc/t/twowire-issue-to-use-2-differents-i2c-objects-with-two-lcds/882893/5 "2021-07-08T09:24:54Z")

</div>

I agree with [Koepel](https://forum.arduino.cc/u/Koepel)

> [@Koepel](#):
>
> If someone asks a question that seems strange, then it might be a [XY-problem](https://xyproblem.info/)

however, what the OP is asking is how to use two LCD in two different I2C busses, we can assume that both LCDs have the same address making it to need two different I2C busses

[juanpdl](https://forum.arduino.cc/u/juanpdl): the problem is that the " **LiquidCrystal\_I2C**" library is defaulted to the main I2C bus, to change that you need to modify the " **LiquidCrystal\_I2C**" library

```arduino
LiquidCrystal_I2C::LiquidCrystal_I2C(uint8_t lcd_Addr,uint8_t lcd_cols,uint8_t lcd_rows)
{
  _Addr = lcd_Addr;
  _cols = lcd_cols;
  _rows = lcd_rows;
  _backlightval = LCD_NOBACKLIGHT;
}

```

and here

```arduino
void LiquidCrystal_I2C::expanderWrite(uint8_t _data){                                        
	Wire.beginTransmission(_Addr);
	printIIC((int)(_data) | _backlightval);
	Wire.endTransmission();   
}

```

and if that wasn't enough you need to modify the " **wire**" library to be able to create two different objects (this may need to rewrite the whole library) starting here

```arduino
TwoWire Wire = TwoWire();

```

and becasue as you noticed none of this changes actually tells what pins to use for the busses, you also need to modify the " **twi**" library at least here

```arduino
void twi_init(void)
{
  // initialize state
  twi_state = TWI_READY;
  twi_sendStop = true; // default value
  twi_inRepStart = false;
  
  // activate internal pullups for twi.
  digitalWrite(SDA, 1);
  digitalWrite(SCL, 1);

  // initialize twi prescaler and bit rate
  cbi(TWSR, TWPS0);
  cbi(TWSR, TWPS1);
  TWBR = ((F_CPU / TWI_FREQ) - 16) / 2;

  /* twi bit rate formula from atmega128 manual pg 204
  SCL Frequency = CPU Clock Frequency / (16 + (2 * TWBR))
  note: TWBR should be 10 or higher for master mode
  It is 72 for a 16mhz Wiring board with 100kHz TWI */

  // enable twi module, acks, and twi interrupt
  TWCR = _BV(TWEN) | _BV(TWIE) | _BV(TWEA);
}

```

and here

```arduino
void twi_disable(void)
{
  // disable twi module, acks, and twi interrupt
  TWCR &= ~(_BV(TWEN) | _BV(TWIE) | _BV(TWEA));

  // deactivate internal pullups for twi.
  digitalWrite(SDA, 0);
  digitalWrite(SCL, 0);
}

```

but as you can see SDA and SCL are not defined anywhere here, so you need to make sure they match the naming from the **esp** variant " **pins\_arduino.h**"

```arduino
#define PIN_WIRE_SDA (2)
#define PIN_WIRE_SCL (3)

static const uint8_t SDA = PIN_WIRE_SDA;
static const uint8_t SCL = PIN_WIRE_SCL;

```

the question here is: **is it really worth it?**

---

<div class="post-metadata">

**Author:** ![noiasca](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/noiasca/32/1126910_2.png) [@noiasca](https://forum.arduino.cc/u/noiasca)\
**Post date:** [July 8, 2021, 9:44am UTC](https://forum.arduino.cc/t/twowire-issue-to-use-2-differents-i2c-objects-with-two-lcds/882893/6 "2021-07-08T09:44:46Z")

</div>

> [@juanpdl](#):
>
> Please tell me what lines of code need to relate Wire with lcd1 and Wire1 with lcd2

as told by others, you could clone the LCD library to be used with

```arduino
TwoWire I2C2 = TwoWire(1); //I2C2 bus

```

there are 6 (?) Wire. in the LiquidCrystal\_I2C.cpp which need to be modified from Wire. to I2C2.

IMHO a nicer variant is, if you adopt the library so the constructor accepts not only the I2C address but also a reference to your I2C interface.

But the remaining question is  
WHY do you think you need two separate I2C buses for your displays with differing I2C addresses?

---

<div class="post-metadata">

**Author:** ![johnwasser](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/johnwasser/32/214270_2.png) [@johnwasser](https://forum.arduino.cc/u/johnwasser)\
**Post date:** [July 8, 2021, 3:11pm UTC](https://forum.arduino.cc/t/twowire-issue-to-use-2-differents-i2c-objects-with-two-lcds/882893/7 "2021-07-08T15:11:58Z")

</div>

> [@juanpdl](#):
>
> #define I2C2\_SCL\_PIN 219

Pin 219?

---

<div class="post-metadata">

**Author:** ![TomGeorge](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/tomgeorge/32/215261_2.png) [@TomGeorge](https://forum.arduino.cc/u/TomGeorge)\
**Post date:** [July 8, 2021, 3:25pm UTC](https://forum.arduino.cc/t/twowire-issue-to-use-2-differents-i2c-objects-with-two-lcds/882893/8 "2021-07-08T15:25:04Z")

</div>

Hi,  
We haven't received @juanpdl answer to why he/she wants to use 2 x I2C when they know only one bus is needed.

I agree X-Y problem

@juanpdl can you please tell us what the application is?  
What is your project trying to do?

Thanks... Tom... 😃 👍 ☕ 🇦🇺

---

<div class="post-metadata">

**Author:** ![juanpdl](https://avatars.discourse-cdn.com/v4/letter/j/dec6dc/32.png) [@juanpdl](https://forum.arduino.cc/u/juanpdl)\
**Post date:** [July 8, 2021, 10:32pm UTC](https://forum.arduino.cc/t/twowire-issue-to-use-2-differents-i2c-objects-with-two-lcds/882893/9 "2021-07-08T22:32:17Z")

</div>

Hi  
The general development integrates many devices, so to summarize I only gave as an example having to use an LCD for each I2C bus.  
In the default bus I am already using 8 I2C sensors that I cannot modify their addresses to expand them and I had the second bus available.  
I did not know how much trouble I was making here if he asked a seemingly simple question.  
Thanks anyway to the whole community, I put the same question in another group and several Asians could help me without asking so much because I wanted it.  
I close the issue.

---

<div class="post-metadata">

**Author:** ![TomGeorge](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/tomgeorge/32/215261_2.png) [@TomGeorge](https://forum.arduino.cc/u/TomGeorge)\
**Post date:** [July 8, 2021, 10:49pm UTC](https://forum.arduino.cc/t/twowire-issue-to-use-2-differents-i2c-objects-with-two-lcds/882893/10 "2021-07-08T22:49:24Z")

</div>

Hi,  
Thanks for the reply.  
The problem we have found here is if too little information is given, solutions can go off at a tangent.  
UNTIL.  
The full project story comes to light, usually two or three days or 20 or 30 replies have been sacrificed.

> [@juanpdl](#):
>
> In the default bus I am already using 8 I2C sensors that I cannot modify their addresses to expand them and I had the second bus available.

Have you run the I2C Scanner code to identify your I2C addresses?  
[https://create.arduino.cc/projecthub/abdularbi17/how-to-scan-i2c-address-in-arduino-eaadda](https://create.arduino.cc/projecthub/abdularbi17/how-to-scan-i2c-address-in-arduino-eaadda)

Tom... 😃 👍 ☕ 🇦🇺

---

<div class="post-metadata">

**Author:** ![gfvalvo](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/gfvalvo/32/1013044_2.png) [@gfvalvo](https://forum.arduino.cc/u/gfvalvo)\
**Post date:** [July 8, 2021, 11:38pm UTC](https://forum.arduino.cc/t/twowire-issue-to-use-2-differents-i2c-objects-with-two-lcds/882893/11 "2021-07-08T23:38:40Z")

</div>

The [ESP32 API Reference](https://docs.espressif.com/projects/esp-idf/en/stable/esp32/api-reference/peripherals/i2c.html#) (and associated examples) describe how to use both I2C busses. But, it's a completely different paradigm than the Arduino Wire class.

---

<div class="post-metadata">

**Author:** ![TomGeorge](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/tomgeorge/32/215261_2.png) [@TomGeorge](https://forum.arduino.cc/u/TomGeorge)\
**Post date:** [July 9, 2021, 12:12am UTC](https://forum.arduino.cc/t/twowire-issue-to-use-2-differents-i2c-objects-with-two-lcds/882893/12 "2021-07-09T00:12:09Z")

</div>

Hi,  
Opps, forgot you are using ESP32.

> **[ESP32 - I2C Scanner using Arduino IDE (working) - ESP32 Forum](https://www.esp32.com/viewtopic.php?t=4742)**
>
> Espressif ESP32 Official Forum

Tom.. 😃 👍 ☕ 🇦🇺

---

<div class="post-metadata">

**Author:** ![system](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/system/32/1140315_2.png) [@system](https://forum.arduino.cc/u/system)\
**Post date:** [November 6, 2021, 4:52am UTC](https://forum.arduino.cc/t/twowire-issue-to-use-2-differents-i2c-objects-with-two-lcds/882893/14 "2021-11-06T04:52:01Z")

</div>

Closed on November 6, 2021, 4:52am UTC

This topic was automatically closed 120 days after the last reply. New replies are no longer allowed.
