logo elektroda
logo elektroda
X
logo elektroda

Changing I2C pins in the Arduino Uno using the Wire.h library: are PC5 and PC4 free?

pier 4776 14
ADVERTISEMENT
Treść została przetłumaczona polish » english Zobacz oryginalną wersję tematu
  • #1 19351767
    pier
    Level 24  
    Posts: 2444
    Help: 40
    Rate: 1891
    Hi.

    Does the Wire.h library after changing the standard I2C pins to other pins mix anything else on these standard pins?
    Figuratively, let's take for example an Arduino Uno which has standard I2C on the PC5 PC4 pins I change them via Wire.begin(12,13); and I can already use the PC5 PC4 pins without problems?
  • ADVERTISEMENT
  • #2 19351827
    Anonymous
    Level 1  
  • #3 19351831
    GanzConrad
    Level 25  
    Posts: 934
    Help: 67
    Rate: 202
    I don't play with arduino, but I would look at the library first: if it uses hardware I2C (TWI) then it won't work, but if it generates programmatically then there should be no problem.
  • #4 19351836
    Anonymous
    Level 1  
  • #5 19351958
    pier
    Level 24  
    Posts: 2444
    Help: 40
    Rate: 1891
    A little more detail.
    I am using the Esp8266 not the uno, the uno was given as an example.
    Without any problem I use Wire.begin(); so as to change the I2C pins from standard to others and it works.
    The problem is that under these standard I2C pins in my case I have a gps receiver plugged in.
    And yes, if I use only the gps it is ok and everything works but if I configure I2C with the wire.begin(12,14); command, i.e. on other pins, the gps stops working and I2C works. It's as if even after changing the pins the library still freaks something out on those standard I2C pins.
  • ADVERTISEMENT
  • #6 19352303
    Anonymous
    Level 1  
  • #7 19352753
    krzysiek_krm
    Level 40  
    Posts: 4612
    Help: 716
    Rate: 598
    pier wrote:
    And yes, if I use only gps then it is ok and everything works but if I configure I2C with the wire.begin(12,14); command, i.e. on other pins then gps stops working and I2C works. It's as if even after changing the pins the library still freaks something out on those standard I2C pins.
    .
    You write slightly vaguely. However, I think that when you change the I2C pins it is the bus on those "original" pins that stops working. You need to create a second I2C bus (on those previous pins).
  • ADVERTISEMENT
  • #8 19353097
    pier
    Level 24  
    Posts: 2444
    Help: 40
    Rate: 1891
    I don't know how to describe it better.
    I fired up a homemade board with esp8266 and oled on pins 14,12, to it I connected a gps receiver which is plugged into the standard pins used by esp for I2C. Don't ask why I2C on different pins than it should be. The library for the gps is tinyGPS++ and SoftwareSerial.

    If I fire the gps itself on pins 4,5 it is ok, everything works. If I run the bus to the oled on pins 14,12 the oled works but the gps does not. Why?
  • #9 19353122
    Anonymous
    Level 1  
  • ADVERTISEMENT
  • #10 19353133
    pier
    Level 24  
    Posts: 2444
    Help: 40
    Rate: 1891
    khoam wrote:
    pier wrote:
    The library for gps is tinyGPS++
    .
    Provide a link to this library that you used.

    Added after 1 [minute]: .

    pier wrote:
    If I fire up the bus to the oled on pins 14,12 then the oled works but the gps no longer.
    .
    And to this display too.



    TinyGPS++ .
    And the library for the oled is the one from Adafruit without any quirks. Adafruit_SSD1306.h
  • #11 19353151
    Anonymous
    Level 1  
  • #12 19353329
    pier
    Level 24  
    Posts: 2444
    Help: 40
    Rate: 1891
    khoam wrote:
    pier wrote:
    And the library for the oled is the one from Adafruit without any quirks.
    .
    That's great, now write how you specifically call the Adafruit_SSD1306::begin() command i.e. with all four parameters.
    .

    display.begin(SSD1306_SWITCHCAPVCC, 0x3C);
  • #13 19353356
    Anonymous
    Level 1  
  • #14 19353741
    pier
    Level 24  
    Posts: 2444
    Help: 40
    Rate: 1891
    The code is no secret:

    Code: C / C++
    Log in, to see the code
    .

    I can already see what this is about.
  • #15 19355707
    Anonymous
    Level 1  

Topic summary

✨ The discussion revolves around the use of the Wire.h library for I2C communication on an Arduino Uno, specifically addressing the implications of changing the default I2C pins (PC4 and PC5) to other pins (e.g., 12 and 14). It is clarified that on the Arduino Uno, the I2C pins are hardware-assigned and cannot be changed, meaning that using Wire.begin(12, 13) does not free up PC4 and PC5 for other uses. Users are advised to consider software alternatives like SoftWire or SoftI2CMaster for I2C on different pins. A follow-up reveals that the original poster is using an ESP8266, where they successfully change I2C pins but encounter issues with a GPS receiver when I2C is configured on non-standard pins. The conversation highlights the need for careful management of I2C buses and the potential conflicts with other devices connected to the standard pins.
Generated by the language model.

FAQ

TL;DR: On ESP8266, SSD1306 libraries may re-init I2C to defaults (0x3C) because "periphBegin ... assumes true"; pass periphBegin=false or use the newer constructor to keep custom SDA/SCL and avoid breaking other devices. [Elektroda, khoam, post #19353356]

Why it matters: This helps Arduino/ESP8266 users move I2C pins without accidentally disabling GPS or other peripherals.

Quick Facts

Can I change I2C pins on Arduino Uno using Wire.h?

No. The ATmega328P’s hardware I2C is fixed on PC4/PC5. “The Wire library for the Arduino Uno uses a hardware I2C controller.” Use a software I2C library if you need other pins. [Elektroda, khoam, post #19351836]

How do I move I2C to other pins on ESP8266?

Call Wire.begin(SDA, SCL) with GPIO numbers, for example Wire.begin(14, 12). Ensure any display or sensor library does not call Wire.begin() again internally. Set periphBegin=false when initializing SSD1306 to prevent re-initialization. [Elektroda, khoam, post #19353356]

Why did my GPS stop after setting I2C to GPIO14/12 on ESP8266?

Your SSD1306 begin() likely re-initialized Wire on default pins because periphBegin defaults to true. That toggles the default I2C pins and can disrupt a GPS connected there. “periphBegin … assumes true.” Disable it in begin(). [Elektroda, khoam, post #19353356]

How do I stop SSD1306 begin() from changing my I2C pins?

Use this quick fix:
  1. Initialize I2C first: Wire.begin(SDA, SCL).
  2. Call display.begin(SSD1306_SWITCHCAPVCC, 0x3C, true, false).
  3. Confirm GPS or other peripherals on default pins still respond. This prevents an internal Wire.begin() on defaults. [Elektroda, khoam, post #19353356]

Which SSD1306 constructor should I use on ESP8266?

Use the I2C constructor that accepts TwoWire*: Adafruit_SSD1306(w, h, &Wire, rst_pin, ...). The older single-argument constructor you used is “deprecated.” It offers less control and flexibility on ESP8266. [Elektroda, khoam, post #19355707]

Can I run two I2C buses on Uno or ESP8266?

Uno has one hardware I2C; add another with a software I2C library (SoftWire or SoftI2CMaster). ESP8266 also has one hardware I2C; add a second via software I2C. “The Wire library for the Arduino Uno uses a hardware I2C controller.” [Elektroda, khoam, post #19351836]

Does Wire.h still touch default pins after I reassign I2C on ESP8266?

Wire itself uses the pins you set. However, some libraries call Wire.begin() with no pins by default. Because periphBegin is true, they reconfigure I2C to default pins and may disturb other devices. [Elektroda, khoam, post #19353356]

What I2C address should I use for SSD1306, 0x3C or 0x3D?

0x3C is the default for 32‑pixel‑tall SSD1306 displays. 0x3D is used for other heights. If you omit the address, the library selects an appropriate default. [Elektroda, khoam, post #19353356]

Why is GPIO12 causing conflicts with my GPS on ESP8266?

Your sketch sets SCL to GPIO12 and also uses GPIO12 as SoftwareSerial TX. Sharing one GPIO between I2C SCL and UART TX causes conflicts and breaks GPS communication. Reassign either SCL or TX to a different pin. [Elektroda, pier, post #19353741]

How can I add software I2C on Uno quickly?

Install SoftWire or SoftI2CMaster. Instantiate the software I2C on your chosen SDA/SCL pins. Use it for devices you want off PC4/PC5. Keep the hardware Wire bus free or for other devices as needed. [Elektroda, khoam, post #19351836]

Is calling Wire.begin() without parameters safe after setting custom pins on ESP8266?

No. A parameterless Wire.begin() resets I2C to default pins (GPIO4/5 on ESP8266 variants). That can disrupt peripherals wired to those pins. Always pass SDA/SCL or disable periphBegin in libraries. [Elektroda, khoam, post #19353356]

What I2C speed should I expect with Adafruit SSD1306 on ESP8266?

The recommended constructor uses 400 kHz during transactions and 100 kHz afterward by default. These values are typical for SSD1306 modules and balance speed with reliability. [Elektroda, khoam, post #19355707]

Are PC5 and PC4 free if I move I2C elsewhere on Arduino Uno?

No. You cannot remap Uno’s hardware I2C with Wire.h, so PC4/PC5 remain the I2C pins. If you must move devices, use a software I2C library instead. [Elektroda, khoam, post #19351827]
Generated by the language model.
ADVERTISEMENT