Avoid I2C Address Conflicts On ESP32 By Pin Muxing

A breadboard showing a tiny ESP32 board and two HMC5883L sensors connected to it on different pins

Using hardware I2C on an ESP32? Do you need to connect multiple I2C devices with the same address? Normally, you wouldn’t be able to do that without extra parts, but on the ESP32, [BastelBaus] has found a nice hack — just connect your devices to different pins and slightly abuse the ESP32 GPIO muxing, no extra hardware required!

Initially, they tried separating SDA and SCL completely, and after a bit of tinkering, that’s worked out wonders! For this method, [BastelBaus] provides example Arduino code you could easily integrate into your project, and shows logic analyzer captures that demonstrate there’s barely any overhead. Later, they’ve also found out that you could multiplex only one of the pins, specifically, SDA, having the SCL line be common! As far as we see, this could also work out with split SCL, but do let us know if that doesn’t sound right.

Typically, such a problem is solved with an I2C multiplexer, and we’ve highlighted projects with them before. However, this simple method could also work on chips like the RP2040 or even the Raspberry Pi 4 — just a bit more limited, since the GPIO muxing for I2C has less available ports! Also, if you’re not using a chip with such a comfortable GPIO mux and you must use devices with overlapping addresses, check out the comment section under our I2C ecosystem article – there’s a fair few other methods you can use. And, if this method ever malfunctions for you, there’s a bunch of very straightforward ways you could debug your bus!

20 thoughts on “Avoid I2C Address Conflicts On ESP32 By Pin Muxing

  1. Heh. I actually have a patent* on (almost) that :-)
    (Got a buck and a placque for it)
    Mine uses an I2C PIO chip to generate the mux select signals at the far end of the I2C bus. So your master can be located remotely, two wires to your array of identical chips and you simply set the PIO chip to select the one you want to talk to.

    *CSB: Company was on a push to build a patent portfolio, so if you had *anything* novel, they’d write it up and submit it. Mine is fairly trivial and I have no illusion that it would stand up under any rigorous contest

  2. I was awarded a patent under similar circumstances; Company sent in a patent lawyer asking the engineers to describe anything they thought was novel or unique. I went into the process very skeptical. At the end of it I had a new respect for what patent lawyers do; Yet I’m still conflicted about patents causing more harm to innovation than they do protection for the owner.

      1. ” In most patents the ‘innovative’ part is provided by the lawyer, not the engineer!”

        This is an incredibly mis-informed statement. That’s a polite as I can put it (and not have my comment censored).

        1. In fact, the best patent lawyer I met was someone that made a mid-career move FROM engineer TO lawyer. He also explained that it was a lot easier to teach a competent engineer the part of the law necessary to be a patent lawyer, than it is to teach a lawyer enough engineering to competently write a patent.

          I’ve also seen some patents so poorly structured by the lawyer, that they failed to cover significant parts of the invention in the actual “claims” (the only parts that count).

    1. Actually, my experience at Hewlett-Packard was the exact opposite. HP, as a company of it’s size and caliber, has relatively few patents (particularly any of significance). Big companies use patents as playing card for cross-licensing (unfortunately they also screw small companies). At one point HP tried to beef up its portfolio, which resulted in a bunch of low-grade junk patents clogging up the works. They hired some outside patent lawyers, to clear the backlog, who were just pathetic.

      For example, after discussing a proposed patent with the lawyer, he went off and was incommunicado for 6-months. The patent involved RGB-LCD displays (mixing light). The writeup the lawyer returned with was gobble-gook talking about mixing CMY-pigments. Clearly something he’d read about on the internet. By the time they got a competent lawyer on it, I was already leaving the company.

      In another earlier patent, a co-worker got his name added to a patent because (as my first manager explained it) “he was part of the team”. This, despite there being no identifiable part of the patent that could be attributed to any of his contributions. Indeed, HP, AT&T, etc. got in trouble for routinely including project manager names on patents. But the courts ruled “you cannot induce people to invent”. Merely managing a project is not “invention”.

      Fast forward: Years, later, at HP-labs, I was in the opposite situation where, while my contribution was small, they could be identified as part of two patent “claims” out of perhaps 10 claims. The manager lied to me for nearly 4 years concerning my inclusion on the patent. But when it was eventually granted with only a sole inventor, my manager’s excuse was that “he was hoping that the patent would NOT be granted, and thus there’d be no conflict about credit”. Further, he “was as surprised as anyone that it was granted”. This, (and the general chaos, and intellectual fraud at HP-Labs) was the reason I left the company. It wasn’t a surprise to me, when a decade later, HP reorganized, and they really “cleaned house” and gutted most of HP Labs.

      Yeah, and don’t even get me started about HP passing on what became “video DRAM”. “Too obvious”, was the reply. That was a multi-billion-dollar business.

      Funny thing, that manager was a pretty good manager; other that that lying stuff.

    1. Why is absolutely the wrong question. People hack stuff for no other reason than, “I wonder if…” and then they do it. See also: porting Doom to literally everything.

      That said, there absolutely are instance where you may want to have many multiple identical I2C devices on the bus in which case this would help. I’m not saying it’s a good design but if you only have prefabbed widgets to work with then this could save you time and effort.

  3. Want to do the same thing with the UART.

    ESP32 has 3 UARTs, which is awesome, until you need more. IF your requirement is such that you control when there is traffic on the UART (i.e. ESP32 is the master, and knows when the remote end is finished talking), then you can simply “Serial.end(); Serial.begin();” with new parameters. i.e. choose different GPIO’s to mux into the UART hardware peripheral.

    In this way, you should be able to talk to far more serial devices than you would normally be able to do with a simple ESP32, probably as many as 10 or even more! Just timeshare a hardware UART peripheral between them!

    1. yeah, I’ve had to use stuff like the 74HC125 before for exactly this kind of thing… seeing hacks like this reminds me that I might have to recalibrate my brain in terms of what’s possible, and it’s reassuring to know I can get rid of some hardware components in newer designs ^~^

    2. Literally exactly what I was gonna comment! At my work we’re using a Nordic nrf52 which only has 2 UARTs, some other nrf52 variants only have 1. This method means we can have pretty much as many UART peripherals as we want

  4. Here’s a hack: If you are lacking in the free pins department, try swapping SCL & SDA for a free second i2c bus. Like this:

    MCU.GPIO_A – device1.SCL – device2.SDA
    MCU.GPIO_B – device1.SDA – device2.SCL

    Depending on how you configure your i2c pinmux (or bit-banged i2c) you will reach either device1 or device2. This works because many devices can safely ignore pin-swapped i2c traffic on their input. I’ve tried this successfully with the popular VL53 ToF sensors connected to an ESP32.

    1. Now that’s a hack! One thing I’d be concerned about though is corrupting the i2c state machine in the sensor. I2c is finicky enough as it is, and some of the VL53 series have *long* init times when you need to reset them as the whole sensor firmware (~100kb) is sent at boot

Leave a Reply

Please be kind and respectful to help make the comments section excellent. (Comment Policy)

This site uses Akismet to reduce spam. Learn how your comment data is processed.