# 2N7000 Logic Level Shifter Arduino

#### loca5790

Joined May 12, 2022
16
All,

Using a 2N7000 and analog write function in arduino to convert 3.3v to 5v and then using an LP RC Filter.

I have tried simulating it and probing the circuit but I cannot get greater than 3.3V out of it. I am wondering if my analog write frequency is too high?

Example of circuit.

Code:

analogWriteRange(1032);
uint16_t temp = ((((1.8 * bme.readTemperature() + 32)+20)/140)*1032);
analogWrite(D7, temp);

Am I missing something? 100% new to electronics as of 4 months ago and trying to learn. Appreciate the help.

#### crutschow

Joined Mar 14, 2008
33,980
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.

#### dl324

Joined Mar 30, 2015
16,658
Welcome to AAC!
I am wondering if my analog write frequency is too high?
What are you trying to do, and what is the write frequency?

Are you trying to scale a 0-3.3V signal to 0-5V?

#### Ya’akov

Joined Jan 27, 2019
8,964
analogWrite(pin, value) is software PWM

#### MrChips

Joined Oct 2, 2009
30,433
If your signal out is PWM, then you can try this circuit.
Put 1kΩ resistor in series with the 3.3V signal and the base of Q1.
R1 can be 10kΩ.
R2 should be 10 times lower than the resistance in your RC LP filter.

#### eetech00

Joined Jun 8, 2013
3,814
All,

Using a 2N7000 and analog write function in arduino to convert 3.3v to 5v and then using an LP RC Filter.

I have tried simulating it and probing the circuit but I cannot get greater than 3.3V out of it. I am wondering if my analog write frequency is too high?

Example of circuit.
View attachment 267039

Code:

analogWriteRange(1032);
uint16_t temp = ((((1.8 * bme.readTemperature() + 32)+20)/140)*1032);
analogWrite(D7, temp);

Am I missing something? 100% new to electronics as of 4 months ago and trying to learn. Appreciate the help.
What is the PWM frequency? and voltage level?

#### Irving

Joined Jan 30, 2016
3,791
Assuming a 3.3v GPIO output at 490Hz (standard Arduino) then you can't get more than 3v out as the time constant of the 10k at R2 is too high to allow C1 to charge higher than the 3v drive from the Arduino through the MOSFET body diode. Reduce R2 to 1k or so, see lower circuit/trace.

This isn't the best way to do this. It would bebetter to have an active pull-up ...

#### loca5790

Joined May 12, 2022
16
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.

Analog write is a software PWM from Arduino that Im trying to step up to 5v and then filter to try and mimic analog.

#### loca5790

Joined May 12, 2022
16
Welcome to AAC!
What are you trying to do, and what is the write frequency?

Are you trying to scale a 0-3.3V signal to 0-5V?

Thanks!

I am trying to read temp/press on an I2C chip and scale it to output for reading into my controller for a fan controller and for density corrections. It doesn't need to be perfect just good. I'm reading the I2C scaling to a PWM analog write function and using that as the input labeled "temperature" which is a 0-3.3v PWM at 1kHz at a 1032 range size (divisor).

Yes I am trying to scale 0-3.3 to 0-5v. The analog write frequency is at the default of the ESP8266 so it is at 1kHz.

Last edited:

#### loca5790

Joined May 12, 2022
16
What is the PWM frequency? and voltage level?

PWM frequency is 1kHz. Voltage is 0-3.3v

#### loca5790

Joined May 12, 2022
16
If your signal out is PWM, then you can try this circuit.
Put 1kΩ resistor in series with the 3.3V signal and the base of Q1.
R1 can be 10kΩ.
R2 should be 10 times lower than the resistance in you RC LP filter.

View attachment 267047
Where would my signal go in?

#### loca5790

Joined May 12, 2022
16
Assuming a 3.3v GPIO output at 490Hz (standard Arduino) then you can't get more than 3v out as the time constant of the 10k at R2 is too high to allow C1 to charge higher than the 3v drive from the Arduino through the MOSFET body diode. Reduce R2 to 1k or so, see lower circuit/trace.

This isn't the best way to do this. It would bebetter to have an active pull-up ...

View attachment 267055
Thanks!

It's at 1kHz for the base default of the ESP8266 in the library.

Going this route to be cheap wanting to make it low cost and small footprint. Active was more expensive by alot.

I can change the frequency and lower it down signficantly to say 10 Hz?

#### MrChips

Joined Oct 2, 2009
30,433
Output from the Arduino goes to the base of Q1 via a 1kΩ series resistor.

#### eetech00

Joined Jun 8, 2013
3,814

#### MrSalts

Joined Apr 2, 2020
2,767
Like this you have a cap and resistor that are screwing things up

#### loca5790

Joined May 12, 2022
16

#### MrSalts

Joined Apr 2, 2020
2,767
I was trying to use the cap and resistor as a LP RC setup. Is that not ok?
No, not with the level shifters like this are for digital systems. If your temp sensor outputs an analog signal, you should use an operational amplifier to adjust the dynamic range of the sensor, not a digital level shifter.

#### loca5790

Joined May 12, 2022
16
No, not with the level shifters like this are for digital systems. If your temp sensor outputs an analog signal, you should use an operational amplifier to adjust the dynamic range of the sensor, not a digital level shifter.
The sensor is I2C and I was using the analog write function to do a SW PWM that I was trying to shift from 3.3 to 5v and then filter.

#### MrSalts

Joined Apr 2, 2020
2,767
The sensor is I2C and I was using the analog write function to do a SW PWM that I was trying to shift from 3.3 to 5v and then filter.
I2C is a digital protocol and there is no need for PWM. Can you post your whole circuit and code?

#### loca5790

Joined May 12, 2022
16
I2C is a digital protocol and there is no need for PWM. Can you post your whole circuit and code?

#include <Wire.h>
#include <SPI.h>
/*#include <SPI.h>
#define BME_SCK 18
#define BME_MISO 19
#define BME_MOSI 23
#define BME_CS 5*/
#define SEALEVELPRESSURE_HPA (1013.25)
//Adafruit_BME280 bme(BME_CS, BME_MOSI, BME_MISO, BME_SCK); // software SPI

unsigned long delayTime;
void setup() {
Serial.begin(9600);
Serial.println(F("BME280 test"));

// by default, we'll generate the high voltage from the 3.3v line internally! (neat!)
display.begin(SSD1306_SWITCHCAPVCC, 0x3C); // initialize with the I2C addr 0x3C (for the 128x64)
// init done
display.display();
delay(100);
display.clearDisplay();
display.display();
display.setTextSize(2);
display.setTextColor(WHITE);

bool status;
// default settings
// (you can also pass in a Wire library object like &Wire2)
status = bme.begin(0x76);
if (!status) {
Serial.println("Could not find a valid BME280 sensor, check wiring!");
while (1);
}
Serial.println("-- Default Test --");
delayTime = 1000;
Serial.println();
}

void loop() {

display.setCursor(0,0);
display.clearDisplay();

// Serial.print("Altitude = "); Serial.print(bme.readAltitude(SEALEVELPRESSURE_HPA) * 3.28); Serial.println(" ft");
// display.print("Altitude: "); display.print(bme.readAltitude(SEALEVELPRESSURE_HPA) * 3.28); display.println(" ft");
Serial.print("Temperature = "); Serial.print(1.8 * bme.readTemperature() + 32); Serial.println(" *F");
// display.print("Temperature: "); display.print(1.8 * bme.readTemperature() + 32); display.println(" *F"); //.91 Display
display.print("T "); display.print(1.8 * bme.readTemperature() + 32,1); display.println(" *F");
// Serial.print("Temperature = "); Serial.print(bme.readTemperature()); Serial.println(" *C");
// display.print("Temperature: "); display.print(bme.readTemperature()); display.println(" *C");
Serial.print("Pressure = "); Serial.print(bme.readPressure() / 1000.0F); Serial.println(" kPa");
// display.print("Pressure: "); display.print(bme.readPressure() / 1000.0F); display.println(" kPa"); //.91 Display
display.print("P "); display.print(bme.readPressure() / 1000.0F,1); display.println(" kPa");
Serial.print("Humidity = "); Serial.print(bme.readHumidity()); Serial.println(" %RH");
// display.print("Humidity: "); display.print(bme.readHumidity()); display.println(" %RH"); //.91 Display
Serial.println();
display.display();
delay(10000);
analogWriteRange(1032);
analogWriteFreq(100);
uint16_t pres = ((((bme.readPressure() / 1000.0F)-50)/60)*1032);
uint16_t temp = ((((1.8 * bme.readTemperature() + 32)+20)/140)*1032);

// Serial.print(hum);
// Serial.print(pres);
// Serial.print(temp);
Serial.print(da);

analogWrite(D3, hum);
// humidity range 0-100% RH
analogWrite(D6, pres);
// pressure range 50-110 kPa
analogWrite(D7, temp);
// temp range -20 to 120F
}