Logic level shift 5V <->  3.3V by using Zener diode and resistors - Page 1
Forum section: electronics / projects

author: eneuro
author_rank: Super Contributor
timestamp: August 11, 2023, 04:59:59 pm
content: Hello,What do you think about such circuit to perform Logic level shift 5V <-> 3.3Vby using Zener diodes (3.3Vz) and resistors to protect IO digital pinsin case when high level output on one side is connected to low level output on other sidedue to program bug or communication error which will let both sides have its pins defined as outputs ?In simulation it looks quite good even with additional capacitor 1nF @ 100kHz square wave output signals ...

author: wasedadoc
author_rank: Super Contributor
timestamp: August 11, 2023, 05:37:35 pm
content: Quote from: eneuro on August 11, 2023, 04:59:59 pmLogic level shift 5V <-> 3.3V means bidirectional level shift. Your circuit does not increase a 3.3 Volt swing to a 5 Volt swing.For 5 -> 3.3 only, there are better solutions.

author: eneuro
author_rank: Super Contributor
timestamp: August 11, 2023, 05:43:14 pm
content: Now I realized that I can have 3.3V logic signals on communication lines (eg. Rx/Tx pins of HC-05 Bluetooth module @ 5Vcc) ,but it can be connected to MPU powered by 3.6Vcc..3.9Vcc directly from 18650 lithium-ion battery,so it looks like this circuit should be fine @ such quite low frequency communication.It looks like I can put there even 100R resistors and 100nF capacitor @ 10kHz to have smooth I/O waves on input pinsLets this with 5Vcc HC-05 BT module 3.3v Rx/Tx logic and connected by using software serial communication with 3.6Vcc Attiny85 powered directly from 3.9V 18650 lithium-ion battery

author: eneuro
author_rank: Super Contributor
timestamp: August 11, 2023, 05:56:28 pm
content: Quote from: wasedadoc on August 11, 2023, 05:37:35 pmNope, 3.3V is higher voltage than needed for digital inputs on 5V logic systems eg. ATMEL MPU Attiny85 etc.Sometimes you have 5Vcc powered boards like Bluetooth HC-05 module where digital input Rx output Tx pins need to be 3.3v digital logic levels,so it looks like this Zener diode should be fine, because of when you connect this HC-05 chip to Attiny85 3.6Vcc by using available level logic shifters made of mosfets,probably MPU 3.6v I/O pins could be a disaster for 3.3v I/O on HC-05 Rx/Tx serial communication lines

author: eneuro
author_rank: Super Contributor
timestamp: August 11, 2023, 07:45:52 pm
content: Quote from: wasedadoc on August 11, 2023, 05:37:35 pmYes, it is bidirectional level shift with additional protection when 5V side & 3.6V side will try to output digital 1 -> 0 or 0 <- 1 at the same timeBelow it is the proof that it should work while left 5V side outputs its digital 1 first and than waiting for response from right side,than 3.6V right side switches from input to output their own digital 1 to left side.BTW: In this case 1k resistors were used and 10nF capacitor @ 9600Hz test square digital 0-1 outputs from left 5V side and 3.6V right side

author: Siwastaja
author_rank: Super Contributor
timestamp: August 12, 2023, 09:59:43 am
content: Quote from: eneuro on August 11, 2023, 05:56:28 pmIt's really marginal! Do the math and remember the tolerance of the the supplies. As the CMOS input thresholds are some % of the supply voltage (you can see anything from 60%, 65% or even 70%; always check), you have to calculate with the combination of worst case highest value for 5V, and worst case lower value for 3.3V.5.3V for USB supply and 3.2V for typical 3.3V linear regulator would be 60.3%, just barely above the somewhat usual 60% threshold, and totally unsuitable for the 65% threshold.Also remember that time required to charge the parasitic RC network asymptotically approaches infinity when voltage headroom approaches zero.

author: DavidAlfa
author_rank: Super Contributor
timestamp: August 12, 2023, 10:14:42 am
content: HC-05 is not bidirectional, you have TX+RX, also UART signals are relatively slow, so you don't need more than a simple 10K resistor between 5V TX and 3.3V RX, the 3.3V pin ESD diode will clamp the voltage.Or connect a 18K resistor to gnd at the RX side, making a resistor divider outputting 3.2V with 5Vin.3.3V TX can be connected directly into 5V RX, but most 5V logic need 0.7xVCC = 3.5V for high level so it might be unreliable.

author: eneuro
author_rank: Super Contributor
timestamp: August 12, 2023, 11:13:20 am
content: Quote from: DavidAlfa on August 12, 2023, 10:14:42 amHC-05 BT module powered by 5Vcc still has 3.3V logic levels.

author: iMo
author_rank: Super Contributor
timestamp: August 12, 2023, 11:29:23 am
content: As davidalfa wrote - you may simply use couple of kOhm resistors in series with your Rx and Tx wires.The value depends on the speeds - with 9k6 you may go with 10-22k, with 115k2 and up I would go with 1k - 2k2.The resistors create a low pass (with parasitic capacitancies) thus will round your signal's edges.There are clamping diodes in almost all chips, and they will limit the voltage at their inputs.People use this even with sdcards..The current through the clamping diodes will be I = (5V - (3.3V + 0.6V)) / 1k = 1.1mA, and 0.11mA with 10k resistors.

author: Zero999
author_rank: Super Contributor
timestamp: August 12, 2023, 11:32:54 am
content: Why not just use a transistor?The classic circuit uses a MOSFET, but I prefer a BJT.LTSpice Simulation

author: eneuro
author_rank: Super Contributor
timestamp: August 12, 2023, 11:33:34 am
content: Quote from: Siwastaja on August 12, 2023, 09:59:43 amHC-05 BT module will be powered by 5Vcc as well as Attiny85 probably also will run at 5Vcc from L7805C IC with BOD enabled to ensure MPU restart when Vcc supply voltage goes below some level and it is around 4.1Vmin .. 4.5Vmax.I need 5Vcc Attiny85 to be able run this MPU at 16MHz system clock - with 3.3Vcc probably I could only run this MPU @8Mhz, but with BOD enabled typical restart is around 2.7Vcc in this case which is good to power MPU directly from Lion battery, because it means it will discharge cell to aroung 2.7Vbat minimum, but in this case I have HC-05 with 3.3V I/O logic, so MPU could see 3.3V input voltages >> 2.7Vcc when battery based Lion Attiny85 goes close to BOD level to restart and stop working due to battery discharge - similar when powered from small coin 3V battery.So, It could be better to use 2.5Vz Zener diodes instead of 3.3Vz, but my plan is to power both HC-05 and Attiny85 at 5Vcc, so this should work with 3.3Vz Zener diodes as shown above.

author: mikerj
author_rank: Super Contributor
timestamp: August 12, 2023, 11:41:48 am
content: You might want to check just how useless regular low voltage zeners are. They have a very soft "knee" meaning the voltage across them varies significantly with current. They also tend to be quite leaky below the zener voltage.

author: eneuro
author_rank: Super Contributor
timestamp: August 12, 2023, 11:41:58 am
content: Quote from: Zero999 on August 12, 2023, 11:32:54 amYep, BJT could be better option, because of MOSFETS are more exposed to electrostatic discharges,so I decided to try to do it with Zener diodes very resistant to this type of discharge and they are supposed to additionally protect I/O

author: Zero999
author_rank: Super Contributor
timestamp: August 12, 2023, 11:52:17 am
content: Quote from: eneuro on August 12, 2023, 11:41:58 amThe main advantage of using a BJT, over a MOSFET in this circuit is, discrete MOSFETs are designed to switch large currents and have high capacitance, which makes it slow, unless low value resistors are used.A J-FET is even better, but it must have a pinch-off voltage low enough to ensure it cuts off, then the gate is 3V below the source, which makes it more difficult to find a suitable part.LTSpice Simulation

author: Nominal Animal
author_rank: Super Contributor
timestamp: August 12, 2023, 12:06:20 pm
content: Why not use proper voltage level translators?For a single data line, 74LVC1T45 can do 1.65V-5.5V on either side, has a direction control pin (so no glitches caused by direction mis-autodetection), proper ESD protection, and can be bought for 0.10€-0.20€ apiece in a lot of hundred from Mouser et cetera.I like using TITXU0202for UARTs (1.1V to 5.5V on either side; can do 200 Mbits/s from 3.3V to 5.5V), andTXU0304for SPI (similar specs). They do cost 0.60€ or so from Mouser in a lot of hundred, but I've found them to be very robust for my use cases.For UARTs, I also use TIISO6721digital isolators, max. 50 Mbit/s, either side with 1.71V to 5.5V voltage levels (except not 1.89V-2.25V). Not only does it work like a level shifter, as an actual isolator it means I don't need to worry about ground currents or ground loops either, if I connect a separately powered device to the UART. It's especially nice with single board computers with 1.8V logic level UARTs directly connected to the system-on-chip, as they are easily damaged by overvoltage. At 1.5€ in singles and 1€ in lots of hundred at Mouser these are not exactly cheap, but when one protects an SBC from my own bumblery, I definitely think it is worth it. I can make things work worry-free.Why would you open-implement a Zener-based level shifter, when even the cheapest I could find costs more than half the 74LVC1T45 (in lots of hundred at Mouser), and does not work too well here, the Zener voltage not being a brick wall, but more like a varying slope? You'll have all sorts of losses and need to tune it to fit your real needs, when using a jellybean part designed for the purpose will only cost ten cents instead of six and behave much better?I am not claiming you are doing it wrong. I am only asking why you'd want to do that when the proper/known-working solution only costs a few cents more?

author: DavidAlfa
author_rank: Super Contributor
timestamp: August 12, 2023, 12:31:44 pm
content: Why not....? Because it adds unnecessary complexity.I ran 1Mbaud with just a diode and the internal ~80K pullup., seehere.So I would not overthink this, 100Kbaud will do just fine with a 10K resistor.Take your LED-toggling 8-layer PCB with 1000-bga FPGA elsewhere.There's only a catch when using the simplest method (Plain 10K resistor).When you take advantage of the ESD diode, the pin voltage will be clamped to VCC + 0.6V.Some devices go nuts with this, they don't get damaged or anything, but analog functions like the ADC can report funny measurements.But most of the time it won't be a problem.

author: Siwastaja
author_rank: Super Contributor
timestamp: August 12, 2023, 02:02:05 pm
content: Quote from: DavidAlfa on August 12, 2023, 12:31:44 pm... but reduces noise margins significantly. While it seems to work on your lab table, someone else could try to run it with a tiny bit longer wire / PCB trace, or in a more noisy environment (could be just the product itself producing EM fields).UART peripherals do noise filtering in hardware by multi-sampling. If one would use the same practice for SPI, for example, where a single misdetected clock pulse is catastrophic, things would get more demanding.10kOhm is a lot of resistance. Noise will couple to the line much better than a line terminated to a ~20-30 ohms of Rds_on.

author: Nominal Animal
author_rank: Super Contributor
timestamp: August 12, 2023, 02:43:13 pm
content: Quote from: DavidAlfa on August 12, 2023, 12:31:44 pmFuck you too, with your spit-and-bubblegum solutions that will cause endless problems to others, just because you like to do "works fine for me on my workbench when I use my Keithley lab supply!".

author: schmitt trigger
author_rank: Super Contributor
timestamp: August 12, 2023, 03:04:10 pm
content: I don’t understand the people who come here, or other forums, asking for opinions on a particular circuit implementation. Then, when someone points out the pitfalls and caveats, they stubbornly defend their position with: “but it works fine for me”, or “the simulation seems to work”.I mean, you were asking for a opinions?Read them all, gather whatever wisdom nuggets you find, discard -in your head- the things that don’t apply to your particular case, and politely thank everyone.

author: DavidAlfa
author_rank: Super Contributor
timestamp: August 12, 2023, 03:05:54 pm
content: Someone is missing the humour plugin or it's intentionally being a complete jerk.Anyways....![Insert Jeremy Clarkson picture here]

author: DavidAlfa
author_rank: Super Contributor
timestamp: August 12, 2023, 03:29:42 pm
content: This is running the wavegen @ 120KHz through a 15K resistor and a random 1.3m/4ft wire I found, not shielded in any way nor terminated, right in my desk picking all sort of noise, running on myKeithleycrappy Hantek scope (1M load).Nowehere close to real life, way worse than any "dangerous 2-inch trace" unless your pcb skills are ***.Anyone knows UART signal is sampled at the middle of the period, excect led-toggling fpga fanboys (Here it comes!).Yes, it's prety close to the limit! So just don't run it through 15K and the equivalent to 3 Baguettes of wire.Wtf, I don't have a lab of any kind, this is in my flat, next to 2 routers, a computer and a half-fcked earth connection.This is just f*** HC-05 and a Attiny85, probably connected each one right to the other, not a 400ft RS485 bus running on industrial.Schmitt Trigger, who are you talking to? Because I didn't ask for anything and then tried to sell my point.

author: Nominal Animal
author_rank: Super Contributor
timestamp: August 12, 2023, 04:45:33 pm
content: Quote from: DavidAlfa on August 12, 2023, 03:29:42 pmDo you realize the difference between a 10-cent IC designed to do exactly what you wanted to do, and using an FPGA to toggle a LED?One is using an appropriate tool for the job, and the other is some kind of cargo cult nonsense. Why the fuck would you confuse the two?What you yourself suggest, is exactly why the world is so full of shit. You're happy to bodge something up that works for you and fuck you to everyone else who says it is unreliable and silly way to solve the problem,because it works for you. People who follow your advice don't realize the limitations of the solution you suggest, and get bogged up in all sorts of problems.And why? Because using an IC explicitly created to solve the exact problem at hand, is according to you, "using an FPGA to toggle a LED".You're so full of shit in this matter, DavidAlfa, it's not even funny. It's like a kid telling other kids to try what happens if they pour drain cleaner and washing detergent into a toilet bowl; it's cool! And when I did it in a toilet bowl I discovered outside, nothing bad happenedto me, so it's surely safe! Idiot.

author: DavidAlfa
author_rank: Super Contributor
timestamp: August 12, 2023, 05:37:56 pm
content: Sorry for the offtopic, I'm done with this.I'm not full of any shit, 99% of the posts I answer to are hobbists asking for simple solutions, so I give simple approaches that will usually work, this just a HC-05 with an AtTiny, not a Mars Rover dealing with cosmic rays or the CERN collider.I won't start a war here, the led FPGA thing is a stupid joke I often say referring to overthinking, feel free to identify as Woke if you wish.Normally people acting like this over Internet are basically human sh*t, I guess this is how it feels after doing "the right thing". Whatever!

author: Siwastaja
author_rank: Super Contributor
timestamp: August 12, 2023, 05:42:47 pm
content: Also, DavidAlfa, where did the UART pop up? It seems you introduced it out of nowhere. It's an optimistic use case because, as I said, UART peripherals implement digital noise suppression on the peripheral hardware, masking small signal integrity issues. But everyone else discussed level shifting of binary digital signalsin general, not limited to UART.BTW, using a dedicated simple IC is not overthinking; it'soppositeto overthinking; it's simplification. With your series resistor solution, there comes a bunch of stuff that needs to be considered, such as:1) current through the protection diodes and whether it's too much;2) RC time constant, so also parasitic C of the line3) signal integrity against what is now a high-impedance lineAnother pet peeve of mine is the "hobbyist argument"; because someone does things for hobby, they deserve crappier solutions. No, the time of hobbyists is valuable, too.

author: DavidAlfa
author_rank: Super Contributor
timestamp: August 12, 2023, 05:53:38 pm
content: 3rd post? What does a HC-05 do? Is it a plane? Is it a bird?No! It's Suppa.. it's a bluetooth module interfaced using uart, normally using AT commands at 9.6Kbaud!That's why I went with this. If It was a 20MHZ spi bus, of course not.You call it high impedance line, but you place the resistor at the receiver, the entire line would be driven by the TX pin, fighting the noise, capacitance...The last mm of trace between the mcu and the resistor won't matter in reallistic conditions.No, you don't place the resistor at the beginning and drive all the line through it. But you should all know this already.

author: eneuro
author_rank: Super Contributor
timestamp: August 12, 2023, 06:37:23 pm
content: Quote from: Nominal Animal on August 12, 2023, 12:06:20 pmI'll take a look, because of I've ordered a few days ago something like those pcbs for level logic shifting with BSS138 N-Channel Logic Level Enhancement Mode Field Effect Transistors,but look at its schematics in attached PDF and we can easy see, that when this mosfet BSS138 fails than both side are exposed to direct connection between each side and additionally I/O pins are pulled up via 10k resistors to 3.3Vcc & 5VccAnyway, I'm still waiting for ordered logic level shifters based on BSS138, but I do not like this design as mentioned above n-channel mosfet failure could be disaster in some worst case scenariosFor the moment I end up with slightly modified my idea of using 3.3Vz Zener diode and tested in circuit simulator 2.5Vz Zener diodes, also changed resistors to 330R and added 33nF capacitor to have nice smooth I/O signals wave forms on both sides @9600Hz I'm interested inIn attached test circuit simulation we can se one of these unusual cases, where both left and right side outputs digital 1 - 1 and ties to do it at the same time and... everything looks good when I've changed 3.3Vz to 2.5Vz Zener diode - this mistake costs only slightly bigger current draw at Zener diode around 10mA which is fine

author: Nominal Animal
author_rank: Super Contributor
timestamp: August 12, 2023, 06:50:12 pm
content: I admit, the using "FPGA to toggle a LED" got uncomfortably under my skin, and I possibly overreacted.I like simple solutions, when their benefits and limitations are known and easily available. While I am often accused of overengineering, it is definitely not to the level of using an FPGA to toggle a LED; it's more like I may use four instead of one six-and-a-half-cent BJT transistors to spread out the power dissipation and let me adjust the per-LED current in case they're not well balanced (although they almost always are), or a pair of 8.3-cent N-MOSFETs to ensure the tach signal from a fan is safe for a device even if it happened to be shorted to 12V somehow.My current projects are related to 12V PWM fans. One uses an ATtiny85 instead of a 555 to generate the PWM only because I have the ATtiny85 read the tach signal, so that instead of the PWM supplied to the fan, the user controls the RPM of the fan directly. The other is just an adapter between 5V and 12V PWM fans, taking power from a 19V supply, with lots of extra protection on a board whose components cost a few euros (the 19V-to-12V DC-DC converter was the most expensive part), but with the idea that since it will be used to help tone down the noise of an expensive nice display, spending a couple of euros (literally two or three euros) to make sure it won't harm the display hardware. This is my design logic._ _ _I could understand, if the voltage level translators were expensive, but they're not. In singles, the TXU0202 (if this is indeed UART translation) does cost 0.77€ (0.68€ in a lot of ten) at Mouser; to do it "by the book" you can add a 0.1µF=100nF bypass cap between each supply and ground so add about 0.06€. To save money, another option would be to use two N-channel MOSFETs and four resistors. You can get BSS138's (often used for such) at less than 0.10€ apiece in a lot of 100, and they do come in handy (so pick a N-MOSFET that has features you like and often need and use that). With the resistors, say 0.25€ per board if doing only one to maybe dozen boards.Is it really worthwhile to try and save that 0.50€ per board? I mean, we're not talking about industrial production, but hobbyist projects here.I like the idea of doing the MOSFET way as a learning experience, sure. But is is what a hobbyist should do in their own projects?I can see it both ways: just using ICs as Lego without understanding how they work is not real learning; but a Not Invented Here syndrome and looking for your own flavour to solve a problem that has been solved cheaply, efficiently, and robustly already is kinda reinventing the wheel to.To these two questions, I do not have an answer, and hopefully indicated that at the end of Reply #14.But to get a response referring to using such dedicated ICs designed for this exact purpose as "LED-toggling 8-layer PCB with 1000-bga FPGA", I got pretty mad. You can claim it is humor, but it's not funny, because the underlying question is valid and kinda important to us hobbyists. Idon'tlike when hobbyists and learners are given bubblegum solutions that work if the given situation is exactly as described, but burn and break if things change in ways the hobbyist may not be able to understand yet.To me, the perfect answer to OP would have explained the soft knee in Zener diodes, then the classic uni-/one-directional MOSFET level shift (pointing out drain must always be connected to the higher voltage, so two circuits depending on the direction), perhaps mentioning the bidirectional variants, and then describe some of the cheap but robust level shifters (hopefully with the note that on auto-direction sensing bidirectional ones, there is sometimes a glitch in the low state when it misdetects the direction, so unidirectional shifters are better when the signals are one-way and not bidirectional; plus that I2C kinda-sorta requires dedicated level shifters).

author: DavidAlfa
author_rank: Super Contributor
timestamp: August 12, 2023, 07:34:50 pm
content: Sure, $0.50 part, + shipping + handling fees = easily $20 + waiting time.Because the TXthing cannot be found at the groceries store (I asked).Plus you've seen the upper zener abomination.

author: Nominal Animal
author_rank: Super Contributor
timestamp: August 12, 2023, 07:55:23 pm
content: Quote from: DavidAlfa on August 12, 2023, 07:34:50 pmLast Wednesday, I ordered a bunch of components from Mouser. As shipping is free for orders over 50€, I included three ISO6721FBQDRQ1 isolators; I already have four TXU0304's, four TXU0202's, and two TXU0204's from past orders in stock, simply because I know I'lleventuallyneed them for UART or SPI interfacing. Say, I might want my 3.3V logic microcontroller without a built-in DAC to control a 0-5V output voltage: an MCP4801T powered at 5V plus a TXU0304 to level-shift the SPI bus between 3.3V and 5Vwill work. Or I could use the same DAC at 3.3V and use an opamp to amplify and buffer that to 5V range (noting the limitations of rail-to-rail output opamps). UART (and to a lesser degree, SPI) voltage level differences are common in my experience.I had the package in my hands in 43 hours, and Finland isn't near the center of things, shipping-wise. Posted packages from Central Europe tend to take well over a week to arrive here.Just because you are unable to anticipate your future needs, is not any kind of excuse for only recommending bubblegum-and-spit solutions to others.You do them if you have to, but don't try to pretend they're the best way of going about it.

author: DavidAlfa
author_rank: Super Contributor
timestamp: August 12, 2023, 08:27:16 pm
content: First - Go tell elseone. I don't care about your life, projects, what you bought, what you did, you did your magic earlier, now I can only give zero fucks.So now it's $50 for free shipping, 100x more than "just $0.50"Nowhere I said my way was the best, stop that BS. I just said it was enough. Now it's a 3 cent resistor vs $50."Anticipation of future needs"? More BS. You can't planify everything.

author: schmitt trigger
author_rank: Super Contributor
timestamp: August 12, 2023, 08:59:16 pm
content: Quote from: DavidAlfa on August 12, 2023, 03:29:42 pmThis post *wasn’t* specifically aimed at you. *Sorry* if it appears that way. Not my intention.Just a general observation of ubiquitous Internet forum’s behavior.

author: DavidAlfa
author_rank: Super Contributor
timestamp: August 12, 2023, 09:28:54 pm
content: Yeah, no problem

author: eneuro
author_rank: Super Contributor
timestamp: August 12, 2023, 09:30:52 pm
content: Quote from: Zero999 on August 12, 2023, 11:52:17 amThank you very much for this hint of using BJT instead of n-chanel mosfets in level logic shifterI've already made yet another simulation of circuit with BC547C npn transistor you've shown in your first post there and after a few modifications it looks good in my sim with 3.3Vio left side and voltage ranges on right side I'm interested in: 5Vio,3.9Vio,3.3Vio,3Vio,2.7Vio typical BOD voltage for Attiny85 3.3VccIt could be nice protect somehow I/O pins against BJT failure, but there is also no problem in this circuit when left 3.3V side and right 5V-2.7V side will connect to ground their I/O pins at the same time

author: Nominal Animal
author_rank: Super Contributor
timestamp: August 12, 2023, 10:16:42 pm
content: Quote from: eneuro on August 12, 2023, 06:37:23 pmAs I already mentioned, I don't use those bidirectional ones; I find them too glitchy.If I were you, I'd use a TXU0202, perhaps with a VSSOP-8 to DIP adapter (assuming you want breadboardable stuff) and a couple of 100nF bypass caps.Feel free to take a look atan example I just created; it's in public domain.Another option is to get a couple of SN74LVC1T45DVBR's, in SOT-23-6. You can even dead-bug those for a quick solution, and you can find them even on eBay (although I don't buy components off eBay, they're so often fakes or rejects).If you have local components stores, check out their selection of voltage level translator IC's (often called "dual supply transceivers"); just make sure they support the voltages you need, and for UART that there is at least one in each direction. (So, '2T245 won't work, because both signals are in the same direction.) The 74LVC1T45 and TXU0n0mare just what I personally happen to like right now, but there are lots of others. I do recommend using unidirectional and not bidirectional ones for UART and SPI, though, because of the glitches.For a simple but much safer N-channel enhancement mode MOSFET and resistor solution, I attached an image with two further options. The 330Ω resistors are optional, and only limit the spike of current needed to change the MOSFET state between conducting and non-conducting. Their value depends on the microcontroller or IC the gate is connected to, many don't need it at all. In this configuration, a single MOSFET per signal will invert the signal, but most MCUs can invert their UARTs internally. The two MOSFETs per signal invert the signal twice, thus no inversion. The idea here is to convert each output (TX) to an open drain one, with a pull-up to the logic level voltage the other side prefers. (In the two MOSFETs per signal, the first pull-up can be to either VCC; I just picked the other here for symmetry.) The 2.2kΩ value is just a guess as to a good pull-up value, based on it being commonly used in 400 kHz I2C buses (that have similar open drain designs). This configuration has the benefit that the gates are very rugged (NX138AKR tolerates ±20V on the gate).If you used the two MOSFETs per signal, and both pull-ups to the RX side voltage, then either one of the MOSFETs on the same signal going bonkers would not be able to raise the RX side voltage beyond its acceptable rails. Quite rugged, if that sort of thing is what you need.Or you could do like some members here suggest, and just throw some components in a pile until it works for you, and declare that to be the best ever solution for this, and everyone wasting their time not doing the same.

author: eneuro
author_rank: Super Contributor
timestamp: August 12, 2023, 11:25:52 pm
content: Quote from: Nominal Animal on August 12, 2023, 10:16:42 pmIn the case of HC-05 BT module I saw Arduino 5Vcc examples where HC-05 was powered from Arduino 5Vcc, output Tx pin on HC-05 was directly connected to arduino and only HC-05 input Rx pin was connected to Arduino by using.... yes voltage divider 1k/2k @ 5Vcc ArduinoHopefully, as I know HC-05 BT module can also be powered by providing regulated 3.3Vcc power supply (it consumes around <50mA current probably @ 3.3V) and bypass built in 3.3V voltage regulator on HC-05,anyway I'd like to run my ATTiny85 MPU @ 5Vcc to be able run it @16MHz system clock instead of probably it is 2 times slower @ 3.3Vcc 8MHz I needd to check it.I think BJT logic level shifter should be fast enougth at rate UART serial speeds 9600 and even my oryginal idea of using Zener diode 3.3Vz should be fine.I'm waiting for CP2102 USB-TTL module to connect this HC-05 BT dongle to PC or Arduino NANO and reconfigure this device in slave mode for 9600 UART serial speeds.

author: eneuro
author_rank: Super Contributor
timestamp: August 12, 2023, 11:40:13 pm
content: Quote from: DavidAlfa on August 12, 2023, 03:29:42 pmYep, Idea is to install HC-05 BT module on custom designed PCB with Attiny85 and other stuff like I2C air pressure sensors, I2C compass, I2C accelerometers and custom sensors like speed sensors etc,so yes HC-05 working is slave mode will be very close to Attiny85 running 16MHz @ 5Vcc .HC-05 as I know can be powered from 3.3V to bypass built in 3.3V voltage regulator - I think it is better provide own 3.3V regulated voltage to this module HC-05, since for faster logic level shifters I need low voltage side LV and its GND.I think BJT based logic level shifter shown here should be fine in many cases - we'll see

author: DavidAlfa
author_rank: Super Contributor
timestamp: August 13, 2023, 06:30:51 am
content: 9600baud is not a fast signal at all, even a potato shifter will work.

author: Zero999
author_rank: Super Contributor
timestamp: August 13, 2023, 10:13:49 am
content: Quote from: Nominal Animal on August 12, 2023, 10:16:42 pmThe problem with discrete N-MOSFETs is their capacitances tend to be high, compared to BJTs, which means low value resistors are required in order to ensure decent rise times.. The NX138AK appears to be good candidate. I don't have any to test though.Oh and I definitely agree with using a unidirectional level shifter IC, when practical,Quote from: eneuro on August 12, 2023, 09:30:52 pmIdeally V2 should be greater than V1, but it will also work with V1 up to 5V above V2: BJTs can work backwards, albeit with lower hFEand the reverse voltage rating is typically under 7V.QuoteI don't see the problem with the single transistor shifter, safety-wise. The BC547 has a maximum current and voltage rating of 100mA and 45V, yet the circuit I posted only passes 1.1mA and has a maximum voltage of 5V, which is a safety factor of 90 and 9. I've never known a BJT to fail short circuit, when used well-within the ratings. ESD will blow up your microcontroller, before the BJT.

author: Nominal Animal
author_rank: Super Contributor
timestamp: August 13, 2023, 11:31:53 am
content: Quote from: Zero999 on August 13, 2023, 10:13:49 amVery true. I mentioned them because I have 100 of them in SOT-23, and they were cheap (8.30€ for the whole lot at Mouser). I think you might get up to 115200 baud with low value resistors, possibly omitting the inrush current limiting gate resistors, but I wouldn't want to try much higher than that.I'd only use such circuit if I was really suspicious of the UARTs, as anything between -20 V and +20 V on the gate inputs are safe, with the threshold voltage being somewhere between 0.5 V and 1.5 V. Definitely not my first choice; I just wanted to show that there are other possible MOSFET circuits as well, and OP seemed to express worries about safety.Quote from: Zero999 on August 12, 2023, 11:52:17 amGood point. Also, if it matters any, if I couldn't use a voltage shifter here, I'd definitely use that single transistor shifter circuit myself (one per signal, of course). I do have both BC847C,215 (NPN BJT, were 6.50€ for a lot of 100 at Mouser) and NX138AKRs (N-channel enh. MOSFET) in SOT-23, so I'd most likely use one of those. Knowing about the gate capacitance (and how it affects especially the falling edge on the low-voltage side: like a capacitor between the signal and Vcc), the BJT seems to me to be the better option, with fewer downsides.

author: eneuro
author_rank: Super Contributor
timestamp: August 13, 2023, 06:04:50 pm
content: Quote from: DavidAlfa on August 12, 2023, 05:53:38 pmNope, some HC-05 BT modules boards defaults to 38400 baud rate unless you change it back to 9600 by using AT commands in master modeBTW: What do you think, what is inside this today made custom logic level shifter ?!Hint is in second photo made at the end of soldering this custom made (not in China, but in Europe) logic level shifter

author: Zero999
author_rank: Super Contributor
timestamp: August 13, 2023, 06:15:23 pm
content: Quote from: Nominal Animal on August 13, 2023, 11:31:53 amHave you tried an N-channel J-FET? The2SK3666appears to be ideal. I have some J113, but they have quite a high gate capacitance.

author: Nominal Animal
author_rank: Super Contributor
timestamp: August 13, 2023, 07:21:47 pm
content: Quote from: Zero999 on August 13, 2023, 06:15:23 pmNo; my level-shifting use cases usually involve SBCs, and I feel better using the unidirectional level shifters with those; no sense in risking expensive boards to save a few cents.Quote from: eneuro on August 12, 2023, 11:25:52 pm"Geekcreit" etc. HC-05 BT modules, as sold by Banggood and on eBay, with silkscreen on the back reading "LEVEL: 3.3V", "Power: 3.6V-6V", and "ZS-040", uses 3.3V logic levels, and has a low drop-out regulator (from 3.6V-6V to 3.3V) on board. The regulator is the 3-pin SOT-23 chip, with the single pin at top being input, and the two other legs ground and 3.3V output. You can remove the chip, and replace it with a short from the input to output, converting it to a 3.3V only module. Or you could just desolder the castellated sub-board and use it directly, as it works with 1.8V - 3.6V; see e.g.Itead HC-05 module. The actual BT chip on these use 1.8V logic.In any case, that module and its clones use 3.3V logic levels on RX and TX, so you don't need any kind of level shifting to a MCU using 3.3V logic levels.

author: eneuro
author_rank: Super Contributor
timestamp: August 13, 2023, 08:07:07 pm
content: Quote from: Nominal Animal on August 13, 2023, 07:21:47 pmYep, I know RX and TX are 3.3V logic level @ HC-05 BT module, but I do need level shifting while I need to run my ATTiny85 @ 16MHz so in this case I need 5Vcc or so, because of @3.3Vcc probably maximum system clock could be only 8Mhz.Another issue with MPU powered @3.3Vcc directly from rechargeable battery, that for example in the case of Li-on 18650 with BOD enabled for 3.3Vcc typical BOD restart voltage is around 2.7Vcc,so this means, if HC-05 I/O logic level is 3.3V, I'm not going to risk to connect its Tx pin directly to Attiny85 digital input powered from dischaged Li-on battery very close to its BOD 2.7Vcc ,because of we have ~0.6V higher digital output from HC-05 running their I/O @3.3VAnyway, so far so good and no magic smoke, when connected my custom logic level shifters based on 3.3Vz Zener diodes to 18650 Li-on battery @3.9Vbat - advatage of this logic level shifter is that as we can see, we need only 3 wires instead of 4, because of we do not need provide Vcc on both sides, but only ground (GND) and I/O lines

author: Nominal Animal
author_rank: Super Contributor
timestamp: August 13, 2023, 10:18:54 pm
content: It would make more sense to use a switchmode DC-DC buck/step-down converter from the 18650 to 1.8 V for just the castellated sub-board of the HC-05 module (which runs at 1.8-3.6V), and a boost/step-up converter from the 18650 to 4.5 V (minimum Vcc for safe operation at 20 MHz), and either a TXU0202 translator, two 74LVC1T45 translators, or two BJT level shifting circuits, between the 1.8 V and 4.5 V sides. That way you avoid the pretty big losses in linear regulators, and get much more out of your 18650.TP4056, DW01, TPS62A01, TPS63000 would give a very power-efficient solution here – 90%-96% efficient conversions from the 18650 voltages –, with easy 5V USB charging; and they all happen to be available for JLCPCB SMT assembly service, too. (Diodes' 74LVC1T45W6-7 would be the cheapest level shifting option then, at $0.05 USD per signal.)

author: eneuro
author_rank: Super Contributor
timestamp: August 14, 2023, 02:47:53 am
content: Quote from: Nominal Animal on August 13, 2023, 10:18:54 pmProblem with boost/step-up converter from the 18650 3Vbat eg. to 5V is that switching PSU voltage regulators will make Vcc full of switching noise, which is not good for RF circuits and MPUs.Of course, BJT based logic level shifter will be used, but for the moment for serial communication at 9600 baud rates with HC-05/HC-06 BT modules in slave mode, lets keep things simple like linear regulated 3.26VccThere is non PCB version for the moment, because of electronics dinosaurs don't use breadboards to test electrical circuits, but calculators and soldering irons12.16Vin -> LM317T: 690R1 1096R2 -> 3.26Vout

author: eneuro
author_rank: Super Contributor
timestamp: August 14, 2023, 08:37:56 pm
content: Quote from: DavidAlfa on August 12, 2023, 10:14:42 amJust testing HC-05 BT module under Linux using CP2102 USB UART dongle, where we have 5Vcc available and 3.3V available, so I decided to use 3.3V via 1k resistor to connect to HC-05 key, I pressed key button while powering HC-05 from 5Vcc CP2102, than connected HC-05 key pin to CP2102 3.3V output via 1k resistor (because of measured voltage in key pin in HC-05 was only around 3V), then it looks like HC-05 entered command mode, but many manuals says that:The default baud rate of HC-05 in command mode is 38400bps and 9600 in data mode.Anyway, I was not able so far communicate in command mode with HC-05 by using serial port ttyUSB0 provided by CP2102 under Linux, so I decided disconnect HC-05 and connect RX - 1k - TX pins of CP2102 to create loop and I was able to communicateI used 2x470R resistors with 3.3Vz Zener diodes in my custom logic level shifter, so while everything worked fine with 1k resistor on CP2102 in loop more at serial speed 38400bps (both CP2102 Rx Tx diodes flashed during this test), probably it could be nice test my custom logic level shifter by using CP2102 loop Rx - Tx to know exactly serial port speed.Another option is try to use Adruino-NANO, while thera are many examples with HC-05, but of course why not to type those command from Linux box by using CP2102 USB UART converter, but of course probably I need logic level shift between CP2102 UART TX and HC-05 RX, while CP2102 is working at 5V UART I/O I guess

author: eneuro
author_rank: Super Contributor
timestamp: August 14, 2023, 09:10:57 pm
content: Quote from: eneuro on August 14, 2023, 08:37:56 pmOK, It looks like CP2102 USB UART under Linux in loop Rx - custom 3Vz3 Zener diode lls -Tx using my custom logic level shifter: 2x470R +3Vz3 Zener diode work fine even @38400bps, but in this case Rx & Tx pins were connected on the same CP2102 to test CP2102 itself using only Linux command line tools and small program written in C to output something to /dev/ttyUSB0 portNow, lets play with this HC-05 dongle connected to Linux PC via CP2102 USB UART since looks like there is no problem with CP2102 UART <-> USB /dev/ttyUSB0 under Linux.

author: eneuro
author_rank: Super Contributor
timestamp: August 15, 2023, 01:17:30 am
content: Quote from: DavidAlfa on August 12, 2023, 05:53:38 pmNOPE, I can confirm I have now working HC-05 BT modules under Linux in command mode by using Silicon Labs CP2102 USB UART /dev/ttyUSB0 @ speed 38400 baud and now it works, but I've HC-05 powered from 3.9Vbat Li-on 18650, because when tried connect this thing to CP2102 5Vout pin, something was wrong with USB UART - it stoped working and I was unable to setup its baud rate or even open in serial port terminalsSo it looks like my custom 3Vz3 Zener diode based logic level shifter works quite nice @38400bps in HC-05 command mode, but I need to connect it to logic analyser to see real Rx/Tx signal wave forms# stty -a -F /dev/ttyUSB0speed 38400 baud; rows 0; columns 0; line = 0;No problem what so ever, I wrote a few lines of C code without any Arduino, but pure C for Linux, to send AT commands in fully automated way by using /de/ttyUSB0 port eg. to change names of those HC-05 BT modules and read device addresses, etc and it works with my custom logic level shifter and no magic smoke so far so good# hc05_testAT+NAME?AT+ADDR?AT+UART?AT+ROLE?putty serial terminal /dev/ttyUSB0 @ speed 38400 baud :+NAME:BTHC59600sOK+ADDR:0000:00:000000OK+UART:9600,0,0OK+ROLE:0OKBTW: I've made a mistake at the begining and instead of pull down HC-05 key pin to ground... I've connected it to 3.3Vout in CP2102, but hopefully I've did it by using in series 1k resistor, so no problem, however, it looks like HC-05 key pin has around 3V while CP2102 USB UART 3.3Vout

author: Nominal Animal
author_rank: Super Contributor
timestamp: August 15, 2023, 10:21:25 am
content: I know you don't want to hear from me, but just in case:LCSC sells Diodes Inc.74LVC1T45W6-7in SOT-26 for 0.05€ apiece, 1€ for a lot of 20. These can translate an one-directional signal like UART RX or TX, SPI CS, SCK, MOSI, or MISO, and similar, between 1.65V to 5.5V logic levels, up to quite high rates (at least tens of millions of transitions per second).They're tiny, the chip itself is just 3mm × 1.6mm, but the legs are spaced 1mm apart, and you can even dead-bug them (solder in-air or direct to wires), especially if you connect the middle legs, GND and DIR together (locking the direction B(4) input -> A(3) output, and connect them to a shared ground wire.I made a 0.4" × 0.4" (10mm × 10mm)UART level translator boarddesign in Public Domain, so that anyone can easily and cheaply get them made (even assembled) at e.g. JLCPCB for real cheap – less than a buck for ten boards plus shipping. Granted, having a 10mm×10mm "block" encased in heatshrink in the middle of 4 wires is not as neat as your ad-hoc solution, but this one works very reliably even when the supply voltages vary, has ESD protection (JESD 22), and if either side is unpowered, the outputs are in high impedance mode (as if not connected to anything), so you won't accidentally power a small microcontroller like ATtiny85 from the signal lines – UARTs idle high, and that can be enough to power a small MCU.If you look at the attached board images, you'll notice that the board is absolutely symmetric: nothing changes if you rotate it 180°. The bottom side has no signals, and is covered by a ground plane. The triangles indicate the signal direction.

author: eneuro
author_rank: Super Contributor
timestamp: August 15, 2023, 07:32:57 pm
content: Quote from: Nominal Animal on August 15, 2023, 10:21:25 amI'm more interested in bidirectional communication, while there are not too many pins on ATTiny85 and I'd like to run few of them in parallel with custom protocol on one wire with additional bits redundancy and send/received bytes error detection and maybe even bits correction to repair on receiver side errors on byte bits levelAnyway, beauty of using only 2 resistors and 1 Zener diode is simplicity and it can be easy integrated inside wires without any pcbs.However, BJT logic level shifters and those you have shown also can be usable... it depends what you need and what you have available and how much time you have, because of sometimes it doesn't matter that something is 10x cheaper, because of simple solution to similar problems also can be very usefull - keep things simpleI was able connect to those HC-05 BT modules from Linux by using CP2102 USB UART, but also BT adapter inserted into Linux PC, but also from Android to this HC-05 BT module by using some kind of HC-05 app and now my goal is write own custom application for Android for data obtained from this HC-05/06 BT slave module, which can be connected to MPU via UART and we can use mobile phone to display information from sensors connected to MPU, but also control devices connected to MPU without physical connection by BT radio

author: eneuro
author_rank: Super Contributor
timestamp: August 16, 2023, 07:56:41 pm
content: Quote from: Nominal Animal on August 13, 2023, 07:21:47 pmI think it is better to ensure that even when someone (idiot) connects logic level shifter lines to 12V and GND no magic smoke comes(12V-3.3Vz)/470 ~ 19mAAnyway, 2x470R + 1xZPD3v3 Zener diode works fine and I'm able to receive on Android phone data bytes send @9600bps from Linux PC via CP2102 USB UART Tx -> HC05 BT module Rx connected to 3.9Vbat Li-on 18650 with a little help of logic level shift converter I've shown above hidden inside 2 (two) wires - job done

author: eneuro
author_rank: Super Contributor
timestamp: August 27, 2023, 10:22:59 pm
content: Quote from: Nominal Animal on August 15, 2023, 10:21:25 amToday I've connected my custom made logic level shifter 3V3 Zener diode + 2x470R from AVR Attiny85 8MHz@3.9Vcc software UART TX pin to Saleae Logic analyzer powered by 5V USB PC Linux and no problem I've perfect UART frames 0x41 (ASCII A) at baud rate 115200 (114.285kHz) by using this simply lls made of pure Zener diode and two resistorsIt works