Using a MOSFET (2N7000) as a logic level converter - Page 1
Forum section: electronics / projects

author: pini.grigio
author_rank: Contributor
timestamp: September 21, 2022, 12:30:09 pm
content: 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:However, I am curious at to the behaviour I'm seeing at higher speeds (100KHz):And the same image with the two channels at the same offset: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!!

author: langwadt
author_rank: Super Contributor
timestamp: September 21, 2022, 12:43:17 pm
content: the RP output drives the output most of the way to 3.3V via body diode of the FET74HCT works with 3.3V input

author: gamalot
author_rank: Super Contributor
timestamp: September 21, 2022, 12:59:33 pm
content: 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.

author: T3sl4co1l
author_rank: Super Contributor
timestamp: September 21, 2022, 01:20:35 pm
content: 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

author: Peabody
author_rank: Super Contributor
timestamp: September 21, 2022, 03:20:18 pm
content: 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.

author: Zero999
author_rank: Super Contributor
timestamp: September 21, 2022, 09:01:30 pm
content: 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

author: Peabody
author_rank: Super Contributor
timestamp: September 22, 2022, 02:31:40 pm
content: Quote from: Zero999 on September 21, 2022, 09:01:30 pmI 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.

author: mikerj
author_rank: Super Contributor
timestamp: September 22, 2022, 04:09:33 pm
content: Quote from: Peabody on September 22, 2022, 02:31:40 pmA BJT works backwards well enough, that's what is being shown in the simulation that Zero999 posted.

author: Zero999
author_rank: Super Contributor
timestamp: September 22, 2022, 09:22:22 pm
content: 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.

author: Peabody
author_rank: Super Contributor
timestamp: September 23, 2022, 03:12:31 am
content: 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.

author: ledtester
author_rank: Super Contributor
timestamp: September 23, 2022, 03:26:01 am
content: Quote from: Peabody on September 23, 2022, 03:12:31 amCheck out the blue trace in Zero999's ltspice simulation. At 1.5us the switch S2 closes and the blue trace goes to 0 volts.

author: Peabody
author_rank: Super Contributor
timestamp: September 23, 2022, 03:36:00 am
content: 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.

author: ledtester
author_rank: Super Contributor
timestamp: September 23, 2022, 04:43:15 am
content: 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/transistorsIn 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 mAIe = (3.3-0.017)/2k = 1.641 mAIb+Ie = 2.194 mAwhich agrees with the reading from the graph of Ic.The 0.017 is the Vce saturation voltage and also the voltage at IO1.

author: Nominal Animal
author_rank: Super Contributor
timestamp: September 23, 2022, 06:41:33 am
content: Quote from: pini.grigio on September 21, 2022, 12:30:09 pmI'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 forISOUSB211chips 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 like74LVC1T45in 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 toTXU0202(which you can still find in VSSOP at Mouser right now). For SPI, I'm switching toTXU0304(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:TXU0102has two channels in the same direction, TXU0202 one channel in either direction,TXU0104has all four channels in the same direction,TXU0204has 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, likeTXB0102(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!

author: Zero999
author_rank: Super Contributor
timestamp: September 23, 2022, 07:40:56 pm
content: 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.pdfhttps://www.mouser.com/datasheet/2/68/pn4391-93-541430.pdf

author: JDubU
author_rank: Frequent Contributor
timestamp: September 23, 2022, 08:17:57 pm
content: Here's one already built using the BSS138:https://www.sparkfun.com/products/12009Don't know how fast it is though.

author: Zero999
author_rank: Super Contributor
timestamp: September 23, 2022, 09:25:10 pm
content: Quote from: ledtester on September 23, 2022, 04:43:15 amYuck, that webpage has a nice magic smoke generator.https://cdn.sparkfun.com/assets/learn_tutorials/1/9/3/h-bridge-circuit-600w.gifQuote from: JDubU on September 23, 2022, 08:17:57 pmIt 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

author: langwadt
author_rank: Super Contributor
timestamp: September 24, 2022, 09:17:59 am
content: Quote from: Zero999 on September 23, 2022, 09:25:10 pmor if you only need one direction,https://www.mouser.com/datasheet/2/916/74HC_HCT2G17_Q100-2937325.pdf

author: Zero999
author_rank: Super Contributor
timestamp: September 24, 2022, 02:06:34 pm
content: Quote from: langwadt on September 24, 2022, 09:17:59 amThe 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.