2N7000 Logic Level Shifter Arduino

Thread Starter

loca5790

Joined May 12, 2022
16
We understand that. You can convert the PWM signal to 0-1V analog and then use an opamp to amplify it to 0-5V.

Do you have an example circuit and a good simulator you could point me to so I could try to learn more? Need to walk away too frustrated.... the more you think you know the less you know ;)
 

MrSalts

Joined Apr 2, 2020
2,767
Do you have an example circuit and a good simulator you could point me to so I could try to learn more? Need to walk away too frustrated.... the more you think you know the less you know ;)
Which sensor do you have? Generally, i2C cannot be amplified and you can't increase resolution of the sensor with an amplifier.
 

Thread Starter

loca5790

Joined May 12, 2022
16
Which sensor do you have? Generally, i2C cannot be amplified and you can't increase resolution of the sensor with an amplifier.
BME280 I posted the diagram earlier, but the sensor is a BME280 and its setup to an esp8266 mod. Have a few uses for this setup around the house. Was hoping to general purpose it.
 

MrSalts

Joined Apr 2, 2020
2,767
BME280 I posted the diagram earlier, but the sensor is a BME280 and its setup to an esp8266 mod. Have a few uses for this setup around the house. Was hoping to general purpose it.
So, the esp8266 can directly pull the temperature value from the sensor with I2C digital communication. There is no need for analog values (in or out) and no need for PWM.
Do you understand how I2C communication works?
What are you trying to do with PWM?
 

Thread Starter

loca5790

Joined May 12, 2022
16
So, the esp8266 can directly pull the temperature value from the sensor with I2C digital communication. There is no need for analog values (in or out) and no need for PWM.
Do you understand how I2C communication works?
What are you trying to do with PWM?
I'm using the esp8266 to wifi connect to my home controller for some logic controls.

I'm also wanting to use it to analog output to my climate controller for humidity/fan/temp control. The fan control is 0-5v and the logic control is some relays. The ESP8266 is handling the PID control and some generic sensor averaging and min/max limits.

Additionally, I want to use one for some density control calculations to a computer that needs 0-5v input analog.

On top of that I'm using displays with I2C as well.

The key is the density control caluclations I want to be cheap and in a small box so I'm footprint limited. It must be 0-5v in analog.


Thus to be limited to the esp8266 for the footprint and capability I went with analog write to PWM in hopes I could step up the 3.3v to 5v and the LP RC filter it to smooth it out for better analog mimicking vs increasing frequency. I tried driving it above 1kHz but had issues with compiling.
 

Ian0

Joined Aug 7, 2020
9,817
You can't use an N-MOFET for that, since the Vgs gate voltage must be several volts to turn on, which makes the gate voltage several volts above 5V.

But it's not clear what that circuit is doing.
Explain the "analog write function" and what your code is supposed to do.
It's a common-gate circuit - it should work.
Pin (Source) = 0V - gate = 3.3V - MOSFET turns on - output = 0V
Pin (Source) = 3.3V - gate = 3.3V - MOSFET turns off - output pulls up to 5V via 10k resistor.
I suspect that there is a load on the circuit which is pulling it down.

The time constants are all wrong - 11k in one direction and 1k in the other. I would suggest swapping the 1k and 10k resistors to make it more equal.

But I would really suggest using a 74HCT04 as a buffer - it will level-shift from 3V to 5V, because the high-level threshold is 2.4V
Then you have an output resistance which is the same in both directions.
 

ag-123

Joined Apr 28, 2017
276
for 3.3v to 5v level shifting, I'd normally prefer the 74HCTxxx ICs, no resistors, no nothing, just the chip alone and mcu direct connection. They can switch at very fast speeds possibly in the Mhz ranges.
if you need 'bi-directional' you could even use things like 74HCT245
https://www.nexperia.com/products/a...gic/transceivers/series/74HC245-74HCT245.html
https://www.ti.com/lit/gpn/SN74HCT245

for I2C, my guess is normally a pull-up resistor to 5v would do, as it is deemed 'open collector', so long as the 3.3v side is 5v tolerant at the pin.
 
Last edited:

MrSalts

Joined Apr 2, 2020
2,767
It's a common-gate circuit - it should work.
Pin (Source) = 0V - gate = 3.3V - MOSFET turns on - output = 0V
Pin (Source) = 3.3V - gate = 3.3V - MOSFET turns off - output pulls up to 5V via 10k resistor.
I suspect that there is a load on the circuit which is pulling it down.

The time constants are all wrong - 11k in one direction and 1k in the other. I would suggest swapping the 1k and 10k resistors to make it more equal.

But I would really suggest using a 74HCT04 as a buffer - it will level-shift from 3V to 5V, because the high-level threshold is 2.4V
Then you have an output resistance which is the same in both directions.
After some learning, the OPs title doesn't exactly describe the OP's goal. Re-read the last Qp posts or so. Yes, 2n7000 are used commonly for logic level shifting but thst is not what the OP needs, even though he thought he needed it. But the key was, he is a self-described beginner (post 1).
 

Thread Starter

loca5790

Joined May 12, 2022
16
After some learning, the OPs title doesn't exactly describe the OP's goal. Re-read the last Qp posts or so. Yes, 2n7000 are used commonly for logic level shifting but thst is not what the OP needs, even though he thought he needed it. But the key was, he is a self-described beginner (post 1).
So would the 74HCT04 work for what I need or am I way off base stating logic shift?
 

MrSalts

Joined Apr 2, 2020
2,767
So would the 74HCT04 work for what I need or am I way off base stating logic shift?
You can pass the 3.3v PWM (analogWrite) signal to the inputs of a 74AHTC125 chip that is powered by a 5v source.
this chip does not invert the signal.

you can then add your resistor/capacitor network to the corresponding output pin on the '125 chip. It should get you a full 5v dynamic range.

note that the ESP ADC features are not great near ground or near their supply voltages (not very linear). Fine more most applications but beware if you are looking for linear accuracy over the whole range. This goes for ESP32 as well.

1652459511949.png
 

Irving

Joined Jan 30, 2016
3,887
There does seem to be some confusion here between the i2c sensor and an 'analog' output, probably due to the classic issue of many thread starters of only stating part of the problem in the original post and many responders of jumping to conclusions.

If I read post #28 correctly the TS's aim is to use an ESP8266 to read a BME280 temp/humidity sensor (I2C) and then output the read temperature as a 0 - 5v analog signal to control external systems. This is a perfectly reasonable approach. The ESP8266 being only a 3.3v device the question is then how to get 0 - 5v. The TS surmised a level shifter to +5v and a LP filter would do the trick, but this doesn't really work very well as the time constants are wrong - 'ON' = a slow passive pull up and 'OFF' a fast & hard active pull-down giving an unbalanced output. Solutions proposed using an NPN transistor have a similar issue as well as inverting the signal (ON = pull down) meaning that the higher duty cycle -> lower output voltage.

The correct solution to this is a totem-pole output - active pull-up and pull-down - driving the LP filter either with 3.3v and then amplifying to 5v afterwards or driving it with 5v from a buffer as per @Ian0's suggestion - but you should still follow the LPFwith an opamp buffer to give a low-impedance output to drive subsequent systems - especially if they are more than a few inches away!.

Here is one option, using a rail-to-rail opamp to amplify the filtered output. It must be a rail-to-rail output type to get close to 5v assuming no higher voltage is available - cheap n nasty LM358s etc. won't cut it.. This is configured as a non-inverting amplifier with a gain of 1.666 driven directly from the ESP8266 output. I've adjusted the filter values to make it more responsive to change, but even so it takes a good 50mS to catch up... smaller values of C would respond faster but with more output ripple - here its around 45mV. Or a higher PWM frequency - ESP8266 should be able to go to at least 10kHz and the suggested opamp is good to well above that.

The simulation shows results for 5%, 33%, 66% and 95% duty cycles. Its a bit messy in LTSpice to simulate a variable duty cycle PWM generator without a custom component - the blue text shows whats in the long line of black text defining V1. I do have a custom PWM component but for some reason it runs really slowly in this simulation - despite my top-of-the-range processor which normally breezes through these simple simulations.

1652459669016.png
 

ag-123

Joined Apr 28, 2017
276
actually, if the intent is only to get a 5v output, then a NPN transistor with a resistor at the collector pull up to 5v would do the trick.
the base goes to the pin.
e.g. 2N2222
https://en.wikipedia.org/wiki/2N2222
https://en.wikipedia.org/wiki/Common_emitter
the trouble is this is 'inverting', so either daisy chain 2 stages, or ensure that the transistor is switched on i.e. bring the base high once it is powered up.

the 74hctxxx may make it 'easier' going by component counts. it is chip-to-chip, just wires no resistors etc.

A thing about pwm dac is i think there may be residual ac signals unless C is pretty high, LPF reduces linearly by decade frequencies after all e.g. 10 hz, 100hz, 1000hz etc. the pwm frequencies would likely need to be pretty high as well, So as to fall in the range where it is filtered out.
I've been wanting to 'control' a varicap diode feeding a FM (80mhz-108mhz) colpitts oscillator stage. And i keep having doubts about whether pwm dac would feed signals into into the oscillator and cause things to go off track?
e.g. if one wants to tune 80 mhz, doing pwm dac to control the varicap, would that cause the FM oscillation to go out of control and off track? pwm dac isn't quite 'pure dc'
 
Last edited:

DickCappels

Joined Aug 21, 2008
10,180
These last half dozen posts only drag the thread off into Never-Never Land. The solution presented by @MrChips in post #13 solves the problem neatly and does not require replacing the computing platform or using external components more complicated than a transitor.

I think @loca5790 will be very happy with what that simple circuit accomplishes, and he should ignore attempts to redefine the problem as being more complicated than it is.
 

Irving

Joined Jan 30, 2016
3,887
actually, if the intent is only to get a 5v output, then a NPN transistor with a resistor at the collector pull up to 5v would do the trick.
the base goes to the pin.
e.g. 2N2222
https://en.wikipedia.org/wiki/2N2222
https://en.wikipedia.org/wiki/Common_emitter
the trouble is this is 'inverting', so either daisy chain 2 stages, or ensure that the transistor is switched on i.e. bring the base high once it is powered up.

A thing about pwm dac is i think there may be residual ac signals unless C is pretty high, LPF reduces linearly by decade frequencies after all e.g. 10 hz, 100hz, 1000hz etc
I've been wanting to 'control' a varicap diode feeding a FM (80mhz-108mhz) colpitts oscillator stage. And i keep having doubts about whether pwm dac would feed signals into into the oscillator and cause things to go off track.
e.g. if one wants to tune 80 mhz, doing pwm dac to control the varicap would that cause the FM oscillation to go out of control and off track? pwm dac isn't quite 'pure dc'
Yes, but as I said earlier a single device is inverting which puts the PWM 'upside down', so analogWrite(255) = 0 out and analogWrite(0) = 100% which is counter-intuitive, especially for a beginner! and by the time you've deployed 3 transistors or MOSFETs for a non-inverting push-pull output you might as well use a chip, its less wiring and more reliable.

A pmw DAC with an active multipole filter can be pretty clean on the output. An LC rather than an RC filter is another option. A single pole RC filter is a pretty poor filter.
 

DickCappels

Joined Aug 21, 2008
10,180
Yes, but as I said earlier a single device is inverting which puts the PWM 'upside down', so analogWrite(255) = 0 out and analogWrite(0) = 100% which is counter-intuitive, especially for a beginner! and by the time you've deployed 3 transistors or MOSFETs for a non-inverting push-pull output you might as well use a chip, its less wiring and more reliable.

A pmw DAC with an active multipole filter can be pretty clean on the output. An LC rather than an RC filter is another option. A single pole RC filter is a pretty poor filter.
That is an important point to which I did not pay attention. I suspect that @loca5790 will be able to solve that with one or two lines of code. One philosophy connected with the use of microcontrollers is that using external hardware to solve a problem that can be solved in software (and in this case trivially) is a waste. Now that @Irving has identified a needed modification loca5790 can solve it if they want to. And yes, if they don't want to they can find the hardware solution on this page above. It is their choice.
 

Irving

Joined Jan 30, 2016
3,887
That is an important point to which I did not pay attention. I suspect that @loca5790 will be able to solve that with one or two lines of code. One philosophy connected with the use of microcontrollers is that using external hardware to solve a problem that can be solved in software (and in this case trivially) is a waste. Now that @Irving has identified a needed modification loca5790 can solve it if they want to. And yes, if they don't want to they can find the hardware solution on this page above. It is their choice.
The other issue with the single transistor arrangement is the the default output, at startup, is 5v not 0v. This may not be so convenient for the external systems and not so trivial to resolve without, for example, another GPIO providing some form of secondary interlock.
 

MrSalts

Joined Apr 2, 2020
2,767
These last half dozen posts only drag the thread off into Never-Never Land. The solution presented by @MrChips in post #13 solves the problem neatly and does not require replacing the computing platform or using external components more complicated than a transitor.

I think @loca5790 will be very happy with what that simple circuit accomplishes, and he should ignore attempts to redefine the problem as being more complicated than it is.
The lowest emitter-collector voltage of most small signal NPN transistors is about 0.2 to 0.4V so the bottom 8% of the dynamic range is lost with a simple NPN option - which would be the highest 8% range after the OP inverts his code to accommodate the inversion which may also require a library to be edited if he is using a peripheral. Note, the OP wants to filter the output with an RC networks to turn the level-converted PWM to analog value which is missing from the schematic in post 13.


Thus to be limited to the esp8266 for the footprint and capability I went with analog write to PWM in hopes I could step up the 3.3v to 5v and the LP RC filter it to smooth it out for better analog mimicking vs increasing frequency. I tried driving it above 1kHz but had issues with compiling.
 
Last edited:
Top