Optocouplers for bidirectional line (I2C SDA)

Yesterday I tried to hook up an i2c barometric sensor @ 3.3V via a level shifter. The i2c communication didn't work, but I probably fried the level shifter, not considering that voltage A should be less than voltage B, and absolutely not above 4.5V, which I found out afterwards, when reading the data sheet. :-)

Oh well. Note to self: read doc first! :-)

Anyways, can I use optocouplers for i2c communication? Guess I'd add pullups on both sides, and I believe having to use dual optocouplers for the SDA line, being bidirectional.

I used those optocouplers for SPI-communication between Raspberry Pi and Arduino some time ago, though it's worth noting that I wrote the SPI software for both sides myself, and clocking was fairly slow, or else it stopped working, which I at the time figured out had to do with delays in the optocoupler, resulting in something like 1000 bits per second max.

Any thoughts on i2c using optocouplers?

I'm using a Nano and powering the sensor side from the 3.3V pin of the nano, and so also sharing GND.

I may have seen something similar done before but optos are not the right tool for the job. There are chips designed to do this exactly, I'd use one.

Another option that might work is to just pull up to 3v3, the low side is happy and the high side will usually see 3v3 as a high (depends on the Vih parameter of the CPU on the 5v side).

Thus far, you have not described any need whatsoever to use optocouplers.

Paul__B: Thus far, you have not described any need whatsoever to use optocouplers.

My "need" is to set up communication between 5V Arduino and 3.3V barometric sensor. Having probably destroyed my level shifter, and feeling general curiousity on the limits of what opto-couplers can be used for, is why I ask.

Of course there exists dedicated hardware, but optocouplers have a sort of simplicity, since I actually understand how they work, being a software developer, and knowing next to nothing about electronics beyond capacitors, resistors and diodes. Level shifters are black boxes to me.

That doesn't mean I will not be able to understand them, if I put my mind to it, but for now, I figured, what happens if I try wiring up optocouplers? Then I thought, perhaps the crowd here could share some insights on why it's a stupid idea, heh heh.

:-)

And that's my point.

You just connect them together with a common ground. That's all.

If you have made some terrible blunder and destroyed a component, what you need is to learn what that blunder was. Otherwise you are equally likely to make the same blunder again despite using the optocouplers.

Paul__B: And that's my point.

You just connect them together with a common ground. That's all.

If you have made some terrible blunder and destroyed a component, what you need is to learn what that blunder was. Otherwise you are equally likely to make the same blunder again despite using the optocouplers.

Why this semi-aggressive tone? Have I stepped on your toes lately?

I was thinking there be thoughts on speed limitations and how to wire the two-way line, but I am sorry for asking, and I am sure I will find it out one way or the other.

Rupert909: Why this semi-aggressive tone? Have I stepped on your toes lately?

No, just succinctly summarising the situation.

It's the XY problem yet again. Happens so often here.

You are asking for help in pursuing the wrong solution to a problem because you did not understand the real problem and are "clutching at straws".

You are not following the instructions; you have not cited what components you were using (which is to say, what sensor, what level shifter, what MCU, what other components, given a connection diagram and a photograph of the troubled setup and explained how you are providing power).

No wonder you have received so few replies - we really have no idea what you are doing, right or wrong and contributors tire of replying again and again trying to pry these all critical pieces of information from you in increments. Without them we cannot even guess what you did wrong.

Paul__B: No, just succinctly summarising the situation.

It's the XY problem yet again. Happens so often here.

You are asking for help in pursuing the wrong solution to a problem because you did not understand the real problem and are "clutching at straws".

You are not following the instructions; you have not cited what components you were using (which is to say, what sensor, what level shifter, what MCU, what other components, given a connection diagram and a photograph of the troubled setup and explained how you are providing power).

No wonder you have received so few replies - we really have no idea what you are doing, right or wrong and contributors tire of replying again and again trying to pry these all critical pieces of information from you in increments. Without them we cannot even guess what you did wrong.

Okay. First off, that I possibly fried the level shifter, indeed taught me a lesson, after all I figured out what I did after reading the specs. Not totally dumb.

Second off, I am curious as to limitations, wiring and discussion for and against the use of optocouplers.

I thought this was clear from the first post.

As to wiring, the reason for asking is that I dont know precisely how to wire up a bidirectional line with optocouplers. My "probem" is that I don't get convinced that it is futile, just from being told in bold font by a single agitated individual, that my question is wrong, even though he has 10K postings.

Being told that I haven't understood anything and are grasping at straws ... what is that? I am curious to learn, not desperate. I have many options available to me, including clocking down and running on lower voltage, so that I don't need a level shifter.

What instructions am I not following? That of not explaining sufficiently what my needs are? Of not accepting the "common GND" response as complete and all-encompassing when coming to the use of optocouplers?

What I am doing is searching for things I know not too much about. I should probably have tagged my initial request as "curious about optocouplers" and never mentioned the litte aside about how I fried my level-shifter, as that was anecdotal only.

I see that I have not been too clear on my intentions, but even as those are explained by me, you still feel your task being to pry information from me in small increments. What do you want, a diagram showing how I2C is wired?? I don't really know what more there is to say than the following:

Pros and cons, technical and programming considerations using optocouplers for SCK and SDA-line of I2C hookup between components of different voltage.

There it is, but obviously you need some more details from me, about my reason for thinking this, and whether I fried my levelshifter with 5V or not, and perhaps even humbling myself some more as to how stupid that was, blah blah?

We clearly don't understand each other.

:-)

Okay I realize this may look like an XY-situation. That's the danger of verbose posts, I guess, that all focus is put on what was really just an anecdote, or introduction as to how I came to think as I did.

For the future I will try to be more precise.

:-)

Graynomad: I may have seen something similar done before but optos are not the right tool for the job. There are chips designed to do this exactly, I'd use one.

Another option that might work is to just pull up to 3v3, the low side is happy and the high side will usually see 3v3 as a high (depends on the Vih parameter of the CPU on the 5v side).

Does that mean there are special considerations when doing I2C, that general level shifters do not meet, typically switching speeds?

All the standard level shifting breakouts are just fine. The speed of I2C is even very low. The only pro for an opto is the total isolation. Can be a thing if one part of the circuit is mains referenced or may contain high voltage. In all other cases just using a level shifter is more convenient. Simple circuit, common ground etc.

Rupert909: Does that mean there are special considerations when doing I2C, that general level shifters do not meet, typically switching speeds?

Are you referring to my first paragraph or second?

Switching speeds are seldom an issue with I2C, I'm not convinced that all level shifters are suitable however as some of them actively drive the lines and that won't play well with an open-collector bus as the slave needs to acknowledge by pulling the SCL line low, no can do if it's being actively driven by the level converter.

This is one reason we need to know the exact ICs being used.

My second paragraph states that you may in fact have to do nothing at all. The ATmega328 has a Vih on the I2C pins of .7Vcc, or 3v5. Higher than the other pins (which are .6Vcc or 3v) so what I said may not work but it costs nothing to try.

Graynomad: Are you referring to my first paragraph or second?

Switching speeds are seldom an issue with I2C, I'm not convinced that all level shifters are suitable however as some of them actively drive the lines and that won't play well with an open-collector bus as the slave needs to acknowledge by pulling the SCL line low, no can do if it's being actively driven by the level converter.

This is one reason we need to know the exact ICs being used.

My second paragraph states that you may in fact have to do nothing at all. The ATmega328 has a Vih on the I2C pins of .7Vcc, or 3v5. Higher than the other pins (which are .6Vcc or 3v) so what I said may not work but it costs nothing to try.

I was referring to your mention of specialized level shifters for i2c.

I realize that I may well be able to have the arduino understand high/low from the sensor, but I2C is neccesarily two-way, and sending 5V to the input pins of a 3.3V device may not be safe?

"It costs nothing to try" you say, but it may cost me my barometric sensor, of which I have only the one.

https://cdn-shop.adafruit.com/datasheets/BST-BMP180-DS000-09.pdf

:-)

Sorry to interrupt, but ,, How do you get Bi-Di through an Optocoupler?

sending 5V to the input pins of a 3.3V device may not be safe?

You don't send 5v to the 3v3 device, you pull the signals to 3v3 and send 3v3 to the 5v device. Bi-directional has nothing to do with it.

The I2C pins are open collector, they don't drive anything high, only low, the pullup resistor determines the high voltage.

123Splat: Sorry to interrupt, but ,, How do you get Bi-Di through an Optocoupler?

That was something I was wondering about, before everybody started telling me how considering optos for i2c was clutching for straws, after doing the mistake of telling that I may have been toasting a level shifter, heh heh. :-)

By the way it turned out I had another lying around, and this time it worked, so now I speak i2c with the barometric sensor.

However, I like optocouplers, as I wrote before, since I understand how they work, contrary to level shifters.

:-)

For completeness sake I may try using optocouplers later. Some arrangement with two optocouplers mounted in each direction and some resistor to avoid short-circuit should provide a toway connection, but I'm not quite sure about this. The beauty of those optos is that apart from when the device is powered by the 3.3V and GND from my uc, there is no need for common GND, as with regular electric connections, since systems are not electrically connected.

I don’t think your idea of opto’s mounted each way is going to work. But, if you really want to know, try it out.

As stated earlier, I2C lines are pulled high by pull ups when in idle state ( so for level matching, you hold one side high at the level for that side (say 5V) and the other side is held high at it’s level (say 3V, or even 1.8V).
when one side changes state (high to low, by pulling that side to a low), it then changes the other side’s state (by pulling it low).

Google Bi-level logic converters and look for an applications note by Philips ele. Good example ckt using BSS138 FETs and good ckt description

Graynomad: I'm not convinced that all level shifters are suitable however as some of them actively drive the lines and that won't play well with an open-collector bus as the slave needs to acknowledge by pulling the SCL line low, no can do if it's being actively driven by the level converter.

The one that I tried before, possibly destroyed, had support for tristate, which I have come to understand and output locking, so that pins are stable when seen from the "other" side, while they may vary at the controlling end. That was an option I was planning to avoid, and perhaps an example of such things that may cause troubles with communications like i2c and spi.

Now I dug out from my parts collection a very simple little converter, spesifically designed for i2c, that I purchased locally, and never really trusted, for some reason. Works like a charm.

Maybe something like this, lets either side pull the signal low.

CrossRoads, Interesting, I'm gonna have to try it out just to see. Thanks.

Rupert, You got your project working by finding a converter that works, But the object here is supposed to be to learn by trying. That's how we keep the Arduino folks employed, by buying more of their stuff to replace the ones which have gone down in flames.... Glad you got it working.