Logic Shift a 12v Data Pin to Control 5v WS2812b with Optocoupler

General project help for Adafruit customers

Moderators: adafruit_support_bill, adafruit

Please be positive and constructive with your questions and comments.
Locked
User avatar
BrookeDot
 
Posts: 35
Joined: Sat Sep 05, 2015 12:34 pm

Logic Shift a 12v Data Pin to Control 5v WS2812b with Optocoupler

Post by BrookeDot »

I'm getting an "early" start on building a custom star PCB build for my holiday light show. I built a test board using WS2811b LEDs. The board has a switch to change the data input from an ATTiny85 to use my Pixel Controller (SanDevices E6804). The problem is that the rest of my pixels use 12v logic but the star will use 5v logic.
I thought of two solutions:

- Use 12v pixels, but these seems to be hard to find, and doesn't solve the second problem of needing a level shifter.
- Use a voltage regulator to switch the voltage to 5v for the pixels/tiny85 then some type of logic shifter for the 12v data pin.

I think I have settled on the PC817 optocoupler. However, I'm new to optocouplers so having a bit of trouble making sure I'm using it right (or if it's even the right solution here)

I tested using 5v power for my test board with the 12v data pin without an optocoupler and as I suspected the pixel did work, but the flickering indicated that the logic level wasn't happy.

The 12v data pin outputs close to 400mA of power (380 or so) when the LEDs on my board are on. You can ignore the logic on the switch as that is just switching the data pin between the tiny85 and the pixel controller. I've just included the entire line into the first pixel for completness.

Could someone look at this schematic and tell me if I'm on the right track here? I'm also not sure if I also need a resistor between the controller's data pin and the pixels data in.
Screenshot 2023-05-20 185714.png
Screenshot 2023-05-20 185714.png (13.67 KiB) Viewed 167 times
Here VCC is the level shifted 5v from the LD117 and the 12v is from an external 12v power supply controlling the pixels.

User avatar
adafruit_support_mike
 
Posts: 67446
Joined: Thu Feb 11, 2010 2:51 pm

Re: Logic Shift a 12v Data Pin to Control 5v WS2812b with Optocoupler

Post by adafruit_support_mike »

An optocoupler probably won't be fast enough to handle pixel data signals. It takes a few microseconds for the output to turn on and off, and NeoPixel signals run at 800kHz (1.25us per bit).

Full-scale level shift circuits use cross-connected transistors:

https://wiki.analog.com/university/cour ... el-shifter

For a more compact solution, you can use a fast comparator like the LM311:

https://www.mouser.com/ProductDetail/Te ... ZxLw%3D%3D

It can handle up to 36V of supply voltage, and can switch from high to low in <200ns. Comparators make good general-purpose level shifters because you can set the reference voltage wherever you want relative to the supply voltage.

User avatar
BrookeDot
 
Posts: 35
Joined: Sat Sep 05, 2015 12:34 pm

Re: Logic Shift a 12v Data Pin to Control 5v WS2812b with Optocoupler

Post by BrookeDot »

Thanks @adafruit_support_mike! I was reading about the speed of an optocoupler and was wondering if it was going to be fast enough. Guess not :D

I am going with the LM311D since it is a surface mount chip. It only supports up to 30v but for my application this seems totally fine. I also looked at the LM311N but that one wasn't as well documented. Let me know your thoughts on if this chip will do what I need.

Do you mind taking a quick glance at the schematic and letting me know if I'm on the right track?
Screenshot 2023-05-31 210717.png
Screenshot 2023-05-31 210717.png (14.42 KiB) Viewed 136 times
Thanks again!

User avatar
adafruit_support_mike
 
Posts: 67446
Joined: Thu Feb 11, 2010 2:51 pm

Re: Logic Shift a 12v Data Pin to Control 5v WS2812b with Optocoupler

Post by adafruit_support_mike »

Bump the negative pin voltage a little above GND. Comparators usually need at least 100mV of extra voltage to switch quickly.

You can make a quick and dirty 0.65V voltage reference with a silicon diode with about 1mA flowing through it. The exact voltage isn't terribly important, as long as your input is clearly above it or below it.

Also keep an eye on your rising edges. Pull-up resistors can be a bit slow, so you may want something like a BJT current source:
current-source.jpg
current-source.jpg (37.45 KiB) Viewed 109 times
The resistor values shown are more for illustration than specific suggestions.

The maximum current flowing through the BJT will be about 100x as much as flows through the resistor between the base and GND. The 4.7k collector resistor is there to keep the BJT from ever turning off completely. Changing the collector current is faster than turning a BJT on and off.

You want the maximum current set by the base resistor to be significantly higher than the idle current that flows through the collector resistor. That will put the transistor into a state called 'saturation', where it's hard to reduce the collector current quickly. When the LM311 shuts off, the PNP will continue to have low resistance long enough to give you a fast rising edge.

User avatar
BrookeDot
 
Posts: 35
Joined: Sat Sep 05, 2015 12:34 pm

Re: Logic Shift a 12v Data Pin to Control 5v WS2812b with Optocoupler

Post by BrookeDot »

This is all fantastic and helpful advice! However, I have to admit I'm a bit lost here :)

I always say that diodes are my kryptonite. I understand the principles here, but struggling a bit with choosing the right components and application.

For the Diode, I'm looking to add a simple Rectifier Diode (CDSUC4148) between VCC and GND of the negative input of the LM311. I think I need a voltage limiting resistor there as well to drop the voltage down from 5v. It's also possible this it the wrong diode to use (so many diode types!).

For the BJT I choose the MMBT2907A somewhat randomly, but looked at the Collector mA and wanted something above my 400mA of my data signal. I was looking for a saturation calculator and did find a few but again got confused so just went with your values (at least for now).
Screenshot 2023-06-08 202047.png
Screenshot 2023-06-08 202047.png (15.73 KiB) Viewed 94 times
Any additional help would be much appreciated! Thanks again.

User avatar
adafruit_support_mike
 
Posts: 67446
Joined: Thu Feb 11, 2010 2:51 pm

Re: Logic Shift a 12v Data Pin to Control 5v WS2812b with Optocoupler

Post by adafruit_support_mike »

BrookeDot wrote: Thu Jun 08, 2023 4:28 pm I always say that diodes are my kryptonite
That's reasonable.. they're annoying little boxes of particle physics. The ideas you need to understand them are things we explicitly ignore at the resistors-and-diodes level of component-based circuit design, like the fact that the term 'current' refers to three different physical processes.

As a quick summary of solid state physics, a single free electron in an infinite crystal behaves kind of like it's in a vacuum: if it hops from one atom to another, there's no change in energy levels. The mathematical description of the post-hop state is identical to the description of the pre-hop state. It's kind of like the problem of describing a single electron moving in a vacuum.. if there's no other particle to define relative motion, how do we prove it's moving at all?

In a silicon crystal, every electron in the outer set of orbitals (the 'valence electrons') is shared between two neighboring atoms (a 'covalent bond') and prefers to stay there. But if you give such an electron 1.25V of kick, it hops into a new energy band where it's easier to hop from one atom to the next than to re-localize around any single atom (the 'conduction band'). You can also think of it as electron-1 hopping to a new atom and localizing, but kicking electron-2 into the conduction band in the process. Since electrons don't have labels and can only be described by the orbital they inhabit, it's impossible to say whether we have a single hopping electron or a cascade of electrons displacing each other. The upshot is that the electron is free to travel through the crystal and obeys most of the same rules as an electron in a vacuum. The only exception is that free electrons in a vacuum move in a straight line, while free electrons in a crystal change direction randomly with each new hop.

The empty orbital left behind when an electron jumps up to the conduction band is called a 'hole', and is also free to wander the crystal by a different mechanism. Since the covalent bond is shared by two atoms, it's impossible to say that one of those atoms lost an electron while the other didn't. And since all four covalent bonds to an atom are identical, it doesn't cost any energy for a hole to move from 'bond with the atom on the left' to 'bond with the atom on the right'. As an observed fact, free electrons move from one atom to another about twice as fast as holes move from one bond to another.

Electrons and holes act like particles with opposite charge, so they attract each other. When a free electron dumps its excess energy and drops back into a hole it's called 'recombination'.

Thermal energy kicks electrons up in the conduction band, and they migrate until they meet a hole and recombine. At any moment, a pure silicon crystal at room temperature has about 1 free electron/hole pair per 1e15 atoms.

If we add atoms with a different number of valence electrons, it changes that balance dramatically. Boron has three valence electrons, so when it forms bonds with its four nearest neighbors there's still one hole that can be filled. An electron that fills that hole makes the crystal's energy balence happy, but leaves the boron atom with an unbalanced negative charge. Aluminum has five valence electrons, so to keep the crystal happy it has to lose an electron and maintain an unbalanced positive charge.

We typically add boron and aluminum at a rate of 1 atom per 1e10 atoms of silicon. In that concentration, a free electron will see 100,000 boron atoms with empty orbitals, while its corresponding hole will have a 1-in-100,000 chance of meeting a free electron. The opposite is true for aluminum, so even very light concentrations of dopant atoms will leave a silicon crystal with an abundance of more or less permanently free electrons (n-type silicon) or holes (p-type silicon).

If you put a piece of n-type silicon against a piece of p-type silicon, you get a large abundance of electrons on the n-side seeing a large abundance of holes on the p-side. They attract each other and recombine, leaving the boron and aluminum atoms with unbalanced charges. The atoms can't move because they're locked into the crystal structure, so they become fixed charges at a distance from each other, creating a fixed electric field.

The tendency for electrons and holes to attract each other is called 'diffusion current'. The fixed electric field pushes on the electrons and holes to create what's known as 'drift current' in both the conduction band (free electrons) and in the valence band (free holes). As long as the attraction between free electrons and free holes is stronger than the fixed electric field created by dopant atoms with unblanced charges, diffusion current will keep pushing electrons into the p-layer and holes into the n-layer. That increases the number of dopant atoms with unbalanced charge, making the fixed electric field stronger. It also moves the remaining free electrons and holes farther apart, making the diffusion current weaker.

Eventually the forces driving diffusion current and drift current become equally strong, and the system reaches equilibrium. In theory all the atoms near the dopant atoms near the junction between the p-layer and the n-layer have permanent unbalanced charge. That zone is called the 'depletion region' because there are no free electrons or holes.

In reality, thermal energy still kicks electrons in the depletion region up into the condution band. That creates something called 'reverse leakage current', at a level of a few femtoamps (1fA is about 6000 electrons per second). The dynamics of the depletion region being what they are, any carrier that does become free will be replaced almost instantly.

If you apply an external electric field, it can be oriented the same direction as the fixed electric field (making drift current stronger), or in the opposing direction (making the drift current weaker). The external field can't change the diffusion current directly, but weakening the drift current makes the diffusion current stronger, and vice versa.

Free electrons and holes can't cross the depletion region as drift current, but they can cross it as diffusion current. An external electric field that makes the drift current weak and the diffusion current strong allows current to flow through the depletion region. The amount of current changes exponentially with the strength of the external electric field, with the reverse leakage current being the base value. The multiplier is e^(Ve/Vt) where Ve is the strength of the external electric field and Vt is the 'thermal voltage' kicking electrons up to the conduction band and creating reverse leakage current (about 26mV at room temperature).

Conduction happens for all external field voltages, but typically becomes noticeable when the multiplier is around e^25 (about 72 billion) with the current passing through the depletion region being about 1mA at Ve=0.65V. The exact value depends on the doping concentration and the size of the pn junction.

Given that small changes in the external voltage have a large effect on the amount of current flowing through a diode, the diode voltage is one of the most stable and predictable values in electronics. All high-quality voltage references are made from them.

A low-quality voltage reference looks like this:
diode-vref.jpg
diode-vref.jpg (24.19 KiB) Viewed 89 times
You had the right general idea, but need a connection to the voltage at the top of the diode.

Locked
Please be positive and constructive with your questions and comments.

Return to “General Project help”