0
\$\begingroup\$

I was wondering if you could set a GPIO pin of an ESP32 to be SDA or SCL even though the pin is not really intended for that.

Here is the pinout of an ESP32 (Disregard the red rectangle):

Pinout ESP32
Source of image

My project would require connecting sensors that would both utilize SCL and SDA pins. Referring to the image above, is it actually possible to interface those two sensors knowing that ESP32 has only one pair of pins dedicated for SCL and SDA?

rzickler
2451 gold badge2 silver badges5 bronze badges
asked Aug 5, 2024 at 11:29
\$\endgroup\$
2
  • \$\begingroup\$ This does not answer your question, but the normal solution is to have multiple sensors on one set of I2C pins. Why can't you do that? \$\endgroup\$ Commented Aug 5, 2024 at 11:35
  • \$\begingroup\$ See Multiplexing signal array for a discussion about connecting large numbers of identical devices via I2C. \$\endgroup\$ Commented Aug 5, 2024 at 21:18

3 Answers 3

2
\$\begingroup\$

As commented already: the I2C bus is a bus that accepts multiple devices on the same bus.

I2C frames contains an address at the start of them. That way, slave devices are able to know if the data sent by the master is for them or for somebody else.

But you must ensure that all your slaves on the bus have different addresses. If you are connecting multiple sensors of the same kind on the same bus, you should check that there is a way the change their address. It's usually done by pulling up of down some pins of the sensors during power-up. You can't have two identical addresses on the same bus, unless you are using an IC that does address translation on the bus, but this starts to become out of scope for that question.

answered Aug 5, 2024 at 11:43
\$\endgroup\$
3
  • 1
    \$\begingroup\$ You can have multiple devices at identical addressed on the same bus. That is a separate thing from communicating to them by dividing the bus into switched segments or changing the address dynamically so only one of the chip is at some other address while all the other chips are at identical addresses. But yeah, out of scope. \$\endgroup\$ Commented Aug 5, 2024 at 11:56
  • \$\begingroup\$ Does it work also on the SDA? \$\endgroup\$ Commented Aug 5, 2024 at 12:24
  • 4
    \$\begingroup\$ "Does it work also on the SDA?" I think you need to do a lot more research into I2C communication. Spending 20-30 minutes reading about how it works will help you understand Blup1980's answer. \$\endgroup\$ Commented Aug 5, 2024 at 15:42
1
\$\begingroup\$

SDA and SDC are digital signals, and you can pretty much duplicate their functionality on any DIO line. When you do stuff like this, we tend to call it "bit-banging", because you're controlling every aspect on the controller side.

That said, to do this can be a bit of a pain, especially to handle some of the nuances of the I2C bus like clock-stretching. Also, you'd be programming stuff that is already handled in silico, so it's a bit of a duplication of effort. It's an exercise that can be avoided by using a microcontroller with two separate I2C busses, or using your address space on one bus to deal with multiple sensors.

Search here for "I2C bit banging" and you'll find a lot of stuff.

answered Aug 5, 2024 at 19:35
\$\endgroup\$
-1
\$\begingroup\$

The answer to your question is yes, you can set most of the GPIOs to be used as the SDA/SCL pins. However, the reason for your question is misguided. When connecting devices over I2C, you connect both the SDA and the SCL lines. One of these is a clock line and one is a data transmission line, so you always need both. You can connect both lines to multiple devices, check out Blup1980's answer for how that works logistically.

So you do not need to set up other pins as SDA or SCL pins, you can use the default pins for whatever sensors you need to use. But as I said in a comment, I would really recommend spending a little time reading about I2C communication until you understand it well enough to answer this question yourself.

answered Aug 5, 2024 at 15:48
\$\endgroup\$
3
  • \$\begingroup\$ OK so how would one use many pins as I2C bus? Would it mux one controller to many sets of pins, are there multiple I2C controllers at different pins, or do you simply mean you can have infinitely many bit-banged GPIOs for I2C? \$\endgroup\$ Commented Aug 5, 2024 at 18:31
  • \$\begingroup\$ @Justme apologies if my answer was not clear. I am not saying that you can use many pins as the I2C bus simultaneously, I'm saying you can choose from many pins to use as the I2C bus. The ESP32 I2C peripheral can be configured in software to use other GPIOs as its SDA/SCL lines. Incidentally there are in fact two I2C peripherals available so yes you could also have multiple I2C controllers at different pins. \$\endgroup\$ Commented Aug 6, 2024 at 13:26
  • \$\begingroup\$ @Justme and this was intended to answer the question that was technically originally asked, which was can you use SDA/SCL on pins that aren't the default ones. As I mention, this isn't really the problem that OP needs to solve, and now that the question has been updated my answer is not as relevant. \$\endgroup\$ Commented Aug 6, 2024 at 13:36

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.