Mosfet Bidirectional Level Shifter
Ever wanted to connect 5V logic to 3.3V logic? Annoying, isn't it? The voltages are so close... sometimes, a 5V logic pin even recognizes 3.3V as high. (Tip: don't rely on this as an acceptable solution. Fried chips happen.)
There are a few ways we can deal with this. We can use a buffer chip. We can use resistors or diodes to drop voltage. Blah, blah, blah. Here's the approach I like:
So how does it work? Let's find out after the jump. The basic theory of operation is the opposite of what you usually expect - a high voltage causes the transistor to turn off and not conduct!
First, let's talk about how to use it. We'll first examine what happens when the device floats (power is connected, but no signals are sent), and what that means for an input on a pin; then we'll go on to how to use it, and how not to use it.
Recall that a pin on your microcontroller can be thought of as a tri-state output: the value can be 0, 1, or Z. Now, 0 means LOW or 0 volts, 1 means HIGH (usually the supply voltage, or a specified IO voltage - commonly 5V or 3.3V, hence the need for this level shifter.) In addition, a 0 can often sink current, and a 1 can source current - usually just a little, so be careful!
But what's a Z? Z means high-impedance. Remember how a low or high output can pass current? Z means little to no current can flow through the pin. While a 0 or 1 are output signals, Z is an input signal, and we know that a voltage measuring device should be high impedance so it doesn't change the voltage it measures!
Let's quickly recall some transistor physics. The transistor doesn't turn on until VGS > VTH
, where VGS is the voltage difference between the gate and the source, and VTH is the threshold voltage of the transistor found in its specifications. Its on behavior is slightly more complex than its off behavior, but in this case we can simply say that when it's on, it conducts. I'll hand-wave a little and say for this purpose that's enough. In this case, we see that VGS
is 3.3V - LO (input)
.
Knowing that, we can start looking at how this device works. Consider the case where nothing is hooked up to the high and low sides of the shifter. There will be no current through the resistors, so the resistors have no voltage loss, and therefore the voltage at either end is equal; therefore, the LO side is 3.3V and the HI side is 5V. (Note that the gate-source voltage is 0, so the transistor is firmly non-conducting, so there is no short from 5V to 3.3V.)
So when the device floats, both sides are pulled HIGH. That means that if we attach two inputs to either side, neither of them driving a signal, both will read high!
Now, let's consider the proper use case. One of the sides will be an input, and one will be an output.
Low Side | High side |
1 | Z |
0 | Z |
Z | 1 |
Z | 0 |
If we do the math out, we can see how this works. Since there are only four cases, we can work it out exhaustively:
- Low side is 3.3V, high side is Z. Gate-source voltage is 0 (both gate and source are at 3.3V), so the transistor is off. Therefore no current flows through the high-side resistor, therefore the high side sees 5V. Low side put in logic 1, high side sees logic 1.
- Low side is 0V, high side is Z. Gate-source voltage is 3.3 (gate is 3.3, source is 0), so the transistor is on, so the high side is shorted to the low side, which is 0. Low side puts in logic 0, high side sees logic 0.
- Low side is Z, high side is 5V. Because the pin can sink no current, no current flows through the low side resistor, so it exhibits no voltage drop, so the low side is tied to 3.3V. Gate-source voltage is zero, so the transistor does not conduct, so there is no 5V to 3.3V short. High side puts in logic 1, low side sees logic 1.
- Low side is Z, high side is 0V. Current will go the other way, so the drain and source are flipped; gate-source voltage is 3.3V, the device conducts, and the low side gets shorted to the high side which is zero. High side puts in logic 0, low side sees logic 0.
There you have it: a very simple, bidirectional mosfet level shifter. This costs about 20 cents to build in small quantities using surface mount components. It's robust, simple, and has a small footprint.
I'll leave this topic with two more things. First: you may have figured this out, but nevertheless: two inputs are fine, an input and an output are fine, but two outputs will cause shorting. Make sure at least one side is hooked up to a high-impedance pin. Otherwise, here's an example: if you were to hook up the low side to a logic 0, and the high side to a logic 1, the transistor would turn on and you would short the 5V to 0V.
Ches Koblents
February 12, 2014