Author Topic: Using a MOSFET (2N7000) as a logic level converter  (Read 7171 times)

0 Members and 1 Guest are viewing this topic.

Offline pini.grigioTopic starter

  • Contributor
  • Posts: 12
  • Country: il
Using a MOSFET (2N7000) as a logic level converter
« on: September 21, 2022, 12:30:09 pm »
I need to interface with a MAX7219 chip from an RP2040 via an SPIish protocol (just serial in really).

The RP2040 GPIO is 3.3V and I have a bunch of 2N7000 TO-92's lying around which is great since Im working on a breadboard.
I'm aware the 2N7000's Vgs threshold is kind of borderline for this kind of application, but it seems like I'm not the first one to implement this successfully in real life applications.

I'm following this basic schematic:


and it does work pretty well up to around 10KHz or so, which is fine in my use case:
1596415-1

However, I am curious at to the behaviour I'm seeing at higher speeds (100KHz):
1596421-2
And the same image with the two channels at the same offset:
1596427-3

Of course the higher frequency I go the more distorted the rising edge becomes.

1. The rising edge seems to reach almost 3.3V before starting to taper - I'm assuming this has to do with the drain to source capacitance, but I really don't know enough to take that conclusion any further. I tried reducing the high-side resistance from 10K to 4.7K and it climb to 5V a bit faster (and in a more linear fashion for some reason). I'd love it if someone who understands this better could try and explain why this only happens from 3.3V and up and only on the rising edge.
2. Is there anything I can do to mitigate this while still using the 2N7000?
3. What is the best-practice solution for shifting SPI from 3.3V to 5V? The best I could think of would be to use a non-inverting schmitt trigger buffer like the 74HC7014 or the like. Would appreciate any input on this as well.


Thanks!!




 

Offline langwadt

  • Super Contributor
  • ***
  • Posts: 4778
  • Country: dk
Re: Using a MOSFET (2N7000) as a logic level converter
« Reply #1 on: September 21, 2022, 12:43:17 pm »
the RP output drives the output most of the way to 3.3V via body diode of the FET


74HCT works with 3.3V input
 
The following users thanked this post: pini.grigio

Offline gamalot

  • Super Contributor
  • ***
  • Posts: 1389
  • Country: au
  • Correct my English
    • Youtube
Re: Using a MOSFET (2N7000) as a logic level converter
« Reply #2 on: September 21, 2022, 12:59:33 pm »
If the +5V rail is not used anywhere else, you can change it to as low as 4V, so that the MAX7219 can work with the RP2040 without any level shifting.
I'm a poet, I didn't even know it. |  https://youtube.com/@gamalot | https://github.com/gamalot
 
The following users thanked this post: pini.grigio

Offline T3sl4co1l

  • Super Contributor
  • ***
  • Posts: 22436
  • Country: us
  • Expert, Analog Electronics, PCB Layout, EMC
    • Seven Transistor Labs
Re: Using a MOSFET (2N7000) as a logic level converter
« Reply #3 on: September 21, 2022, 01:20:35 pm »
2N7000 is something of a pig for logic-level work.  Its ~30pF capacitances mean fairly low pull-ups or drive impedances are needed to do much with it.

Like for example, I've made a wideband RF amp kind of thing before using them (just to see), and impedances of ~10 ohms are necessary to get bandwidth in the 10MHz range.  Reponse is roughly proportional below there, for impedances above there, so, if you want 100kHz operation, 100x higher or 1k resistance is roughly what you need, etc.  Maybe less to get sharp enough square waves.

Smaller MOSFETs are not really available, unfortunately.  RF MOSFETs passed away long ago, or moved to far higher frequencies (10s GHz!).  So there's no FET equivalent of your ordinary 2N3904 for example.

BJTs meanwhile, are perfectly fine to use here -- but require more work.  Namely, a resistor to limit base current, and optionally a "speed-up" capacitor in parallel with that resistor.  Typical values would be 1k collector pull-up, 10k base resistor, 33pF cap.

Integrated resistors are available in "pre-biased" or "digital" type transistors, though not with the cap also included.

Tim
Seven Transistor Labs, LLC
Electronic design, from concept to prototype.
Bringing a project to life?  Send me a message!
 
The following users thanked this post: edavid, pini.grigio

Online Peabody

  • Super Contributor
  • ***
  • Posts: 2161
  • Country: us
Re: Using a MOSFET (2N7000) as a logic level converter
« Reply #4 on: September 21, 2022, 03:20:18 pm »
Your schematic is actually a bidirectional translator, which you don't need.  See if it works better if you eliminate R1.  If there's still too much capacitance, then I think Tim is right that a BJT is the way to go.
 
The following users thanked this post: pini.grigio

Offline Zero999

  • Super Contributor
  • ***
  • Posts: 19998
  • Country: gb
  • 0999
Re: Using a MOSFET (2N7000) as a logic level converter
« Reply #5 on: September 21, 2022, 09:01:30 pm »
Use a BJT, such as the BC546. It's much faster and cheaper. I don't know why the MOSFET circuit is more widely used.

Level shift bi-direc BJT 5V faster.asc
 
The following users thanked this post: T3sl4co1l, ledtester, pini.grigio

Online Peabody

  • Super Contributor
  • ***
  • Posts: 2161
  • Country: us
Re: Using a MOSFET (2N7000) as a logic level converter
« Reply #6 on: September 22, 2022, 02:31:40 pm »
Use a BJT, such as the BC546. It's much faster and cheaper. I don't know why the MOSFET circuit is more widely used.

I think it's because the mosfet version allows bidirectional translation.  The 5V side can go low, which draws down the 3.3V side - first through the body diode, which then turns on the mosfet fully.   With the BJT circuit, I think you would need to add a diode to make it bidirectional.
 

Offline mikerj

  • Super Contributor
  • ***
  • Posts: 3339
  • Country: gb
Re: Using a MOSFET (2N7000) as a logic level converter
« Reply #7 on: September 22, 2022, 04:09:33 pm »
Use a BJT, such as the BC546. It's much faster and cheaper. I don't know why the MOSFET circuit is more widely used.

I think it's because the mosfet version allows bidirectional translation.  The 5V side can go low, which draws down the 3.3V side - first through the body diode, which then turns on the mosfet fully.   With the BJT circuit, I think you would need to add a diode to make it bidirectional.

A BJT works backwards well enough, that's what is being shown in the simulation that Zero999 posted.
 

Offline Zero999

  • Super Contributor
  • ***
  • Posts: 19998
  • Country: gb
  • 0999
Re: Using a MOSFET (2N7000) as a logic level converter
« Reply #8 on: September 22, 2022, 09:22:22 pm »
BJTs do work backwards, but with a lower hFE.

The BJT doesn't do much in the reverse mode in my schematic. When IO2 is pulled low, the power to Q1 is cut off and the current through R1 flows through Q1's base into IO2. The only reverse action Q1 performs is discharging IO1's capacitance.

The circuit was designed for a single 5V supply. If 3.3V is available, remove R2 and connect R1 to 3.3V.
« Last Edit: September 22, 2022, 09:41:27 pm by Zero999 »
 

Online Peabody

  • Super Contributor
  • ***
  • Posts: 2161
  • Country: us
Re: Using a MOSFET (2N7000) as a logic level converter
« Reply #9 on: September 23, 2022, 03:12:31 am »
I guess I don't see that.  If IO2 goes low, that grounds the collector of Q1, but that has no effect on anything on the left side.  There's nothing to bring IO1 low.  So the circuit is a unidirectional translator, with IO1 driving IO2.  The mosfet circuit does work in both directions.
 

Offline ledtester

  • Super Contributor
  • ***
  • Posts: 3248
  • Country: us
Re: Using a MOSFET (2N7000) as a logic level converter
« Reply #10 on: September 23, 2022, 03:26:01 am »
I guess I don't see that.  If IO2 goes low, that grounds the collector of Q1, but that has no effect on anything on the left side.  There's nothing to bring IO1 low.  So the circuit is a unidirectional translator, with IO1 driving IO2.  The mosfet circuit does work in both directions.

Check out the blue trace in Zero999's ltspice simulation. At 1.5us the switch S2 closes and the blue trace goes to 0 volts.
 

Online Peabody

  • Super Contributor
  • ***
  • Posts: 2161
  • Country: us
Re: Using a MOSFET (2N7000) as a logic level converter
« Reply #11 on: September 23, 2022, 03:36:00 am »
Well I just hope someone can explain how that happens in terms of the schematic.  How does the collector going low change the voltage on IO1?  Well, I guess it's the backward BJT.  I've just never used one that way.

« Last Edit: September 23, 2022, 03:40:03 am by Peabody »
 

Offline ledtester

  • Super Contributor
  • ***
  • Posts: 3248
  • Country: us
Re: Using a MOSFET (2N7000) as a logic level converter
« Reply #12 on: September 23, 2022, 04:43:15 am »
The transistor is in saturation, that is Vb > Vc and Vb > Ve.

This page goes over the different bjt operating regions:

https://learn.sparkfun.com/tutorials/transistors

In this case current flows between the emitter and collector in either direction.

I added a pull-up resistor to show that current can flow from emitter to collector in this case.

When S2 is closed (and after things settle down) both the base current and the current from R4 will flow out the collector to ground. The calculations are:

Ib = (3.3-0.7)/4.7k = 0.553 mA
Ie = (3.3-0.017)/2k = 1.641 mA
Ib+Ie = 2.194 mA

which agrees with the reading from the graph of Ic.

The 0.017 is the Vce saturation voltage and also the voltage at IO1.

« Last Edit: September 23, 2022, 04:48:30 am by ledtester »
 

Online Nominal Animal

  • Super Contributor
  • ***
  • Posts: 6967
  • Country: fi
    • My home page and email address
Re: Using a MOSFET (2N7000) as a logic level converter
« Reply #13 on: September 23, 2022, 06:41:33 am »
3. What is the best-practice solution for shifting SPI from 3.3V to 5V? The best I could think of would be to use a non-inverting schmitt trigger buffer like the 74HC7014 or the like. Would appreciate any input on this as well.
I'm just a hobbyist and not an EE, but I do quite often interface single board computers running Linux to microcontrollers, using 1.8V, 3.3V, and 5V logic levels.  I even use logic level isolators for both USB (1.1 full speed or 12 Mbit/s currently, using ADuM3160; waiting for ISOUSB211 chips to become available, as they support USB 2.0 high speed, 480 Mbit/s, that my Teensy 4.x are capable of).

I use unidirectional logic level shifters.  For a single pin, configurable direction (using a second pin), I like 74LVC1T45 in SOT-23-6.  There are multi-channel variants, but when Chipageddon started, I started looking at alternatives, since these are now a bit difficult to find.  LCSC does have a few SOT-323-6/SC-70-6 in stock, though.

For UART (RX and TX, one per direction), I'm switching to TXU0202 (which you can still find in VSSOP at Mouser right now).  For SPI, I'm switching to TXU0304 (which is well in stock at Mouser right now).

Either end of a TXU0202 or TXU0x04 can use Vcc on either side between 1.1V and 5.5V, and between 3.3V and 5V, can do up to 200 Mbit/s, so they're fast, too. The direction of the signals is fixed, and the x determines the directions of the channels: TXU0102 has two channels in the same direction, TXU0202 one channel in either direction, TXU0104 has all four channels in the same direction, TXU0204 has two in each direction, and TXU0304 has three in one direction and one in the other direction and is thus best suited for SPI: CS, SCK, MOSI/PICO/DO, plus MISO/POCI/DI.

For SPI isolation, I'd recommend Si8641B* (in SOIC-16), which can support up to 150 Mbit/s per channel, and Vcc between 2.5V and 5.5V on either side.  It is useful when you want to have a SPI- or serial-connected "front end" with its own ground, isolated from your microcontroller.  You obviously need an isolated DC-DC supply or similar to provide current to the isolated side.  As of right now, Mouser does have a couple of hundred of SI8641BC-B-IS1R in stock, for less than 4€ apiece in a lot of ten.

For I²C (and only for I²C and similar protocols like one-wire), do I use bidirectional/autosensing level shifters, like TXB0102 (which is commonly used in I²C break-out boards for interfacing 3.3V and 5V – note that side A can be 1.2V to 3.6V, and side B 1.65V to 5.5V, and you do need to use VccA ≤ VccB).  Bidirectional/autosensing level shifters may cause issues with unidirectional signals, and you can find threads about that here and elsewhere; I have chosen this policy to avoid those problems, and simply have appropriate level shifting chips always at hand.

In all cases, you should use at least 0.1µF=100nF bypass capacitor from each Vcc to ground as close to the chip as possible (both logic level shifters and Si8641B isolators).  Hope this helps, but do remember, me no EE, just another hobbyist!
 
The following users thanked this post: tooki

Offline Zero999

  • Super Contributor
  • ***
  • Posts: 19998
  • Country: gb
  • 0999
Re: Using a MOSFET (2N7000) as a logic level converter
« Reply #14 on: September 23, 2022, 07:40:56 pm »
How about a J-FET?

Unlike the BJT circuit, not any old J-FET will work. It needs to have a gate-source cut-off voltage of -3V or more. Note a negative voltage relative to the source is required to bias a J-FET off, so -4V is no good, but it's confusing as some data sheets use the wrong sign. The minimum saturation current must be greater than the currents through the pull-up resistor which passes the most current, so 5/4.7k = 1.06mA in my circuit. The original post mentioned a breadboard, so it needs to be through hole.

Unfortunately that doesn't leave many choices. There aren't many J-FETs which are sill made, which meet the above criteria and aren't hugely expensive. I had a quick flick through distributors' websites and found the J113 or 2N4393 (use the PN4393 which is in a cheaper TO-92 package) which will do.
https://www.onsemi.com/pdf/datasheet/mmbfj113-d.pdf
https://www.mouser.com/datasheet/2/68/pn4391-93-541430.pdf
« Last Edit: February 29, 2024, 10:50:42 am by Zero999 »
 

Offline JDubU

  • Frequent Contributor
  • **
  • Posts: 450
  • Country: us
Re: Using a MOSFET (2N7000) as a logic level converter
« Reply #15 on: September 23, 2022, 08:17:57 pm »
Here's one already built using the BSS138:

https://www.sparkfun.com/products/12009

Don't know how fast it is though.
 

Offline Zero999

  • Super Contributor
  • ***
  • Posts: 19998
  • Country: gb
  • 0999
Re: Using a MOSFET (2N7000) as a logic level converter
« Reply #16 on: September 23, 2022, 09:25:10 pm »
The transistor is in saturation, that is Vb > Vc and Vb > Ve.

This page goes over the different bjt operating regions:

https://learn.sparkfun.com/tutorials/transistors
Yuck, that webpage has a nice magic smoke generator.
https://cdn.sparkfun.com/assets/learn_tutorials/1/9/3/h-bridge-circuit-600w.gif

Here's one already built using the BSS138:

https://www.sparkfun.com/products/12009

Don't know how fast it is though.
It appears to be similar to the 2N7000.

How about the 4007?
It will make two channels, but a 14 pin package is quite bulky, just to use two transistors.
https://assets.nexperia.com/documents/data-sheet/HEF4007UB.pdf
 

Offline langwadt

  • Super Contributor
  • ***
  • Posts: 4778
  • Country: dk
Re: Using a MOSFET (2N7000) as a logic level converter
« Reply #17 on: September 24, 2022, 09:17:59 am »
The transistor is in saturation, that is Vb > Vc and Vb > Ve.

This page goes over the different bjt operating regions:

https://learn.sparkfun.com/tutorials/transistors
Yuck, that webpage has a nice magic smoke generator.
https://cdn.sparkfun.com/assets/learn_tutorials/1/9/3/h-bridge-circuit-600w.gif

Here's one already built using the BSS138:

https://www.sparkfun.com/products/12009

Don't know how fast it is though.
It appears to be similar to the 2N7000.

How about the 4007?
It will make two channels, but a 14 pin package is quite bulky, just to use two transistors.
https://assets.nexperia.com/documents/data-sheet/HEF4007UB.pdf

or if you only need one direction, https://www.mouser.com/datasheet/2/916/74HC_HCT2G17_Q100-2937325.pdf
 

Offline Zero999

  • Super Contributor
  • ***
  • Posts: 19998
  • Country: gb
  • 0999
Re: Using a MOSFET (2N7000) as a logic level converter
« Reply #18 on: September 24, 2022, 02:06:34 pm »
The transistor is in saturation, that is Vb > Vc and Vb > Ve.

This page goes over the different bjt operating regions:

https://learn.sparkfun.com/tutorials/transistors
Yuck, that webpage has a nice magic smoke generator.
https://cdn.sparkfun.com/assets/learn_tutorials/1/9/3/h-bridge-circuit-600w.gif

Here's one already built using the BSS138:

https://www.sparkfun.com/products/12009

Don't know how fast it is though.
It appears to be similar to the 2N7000.

How about the 4007?
It will make two channels, but a 14 pin package is quite bulky, just to use two transistors.
https://assets.nexperia.com/documents/data-sheet/HEF4007UB.pdf

or if you only need one direction, https://www.mouser.com/datasheet/2/916/74HC_HCT2G17_Q100-2937325.pdf
The original poster wanted through hole, so that would require an adaptor board.

There are plenty of options for 3.3V to 5V single way translation. Any 74HCT logic gate will do. The original poster wants non-inverting. which means the 74HCT04 and 74HCT14 will only give three channels, as two gates will need to be connected in series. The 74HCT08 and 74HCT32 AND/OR gates will give you four channels, the 74HCT7014 as mentioned above six and the 74HCT245 will give eight channels.
« Last Edit: September 24, 2022, 06:03:11 pm by Zero999 »
 


Share me

Digg  Facebook  SlashDot  Delicious  Technorati  Twitter  Google  Yahoo
Smf