2N7000 Logic Level Shifter Arduino

Thread Starter

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.
1652381166992.png

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
34,285
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.
 

MrChips

Joined Oct 2, 2009
30,712
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.

1652383372072.png
 

eetech00

Joined Jun 8, 2013
3,859
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,845
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 ...

1652389415566.png
 

Thread Starter

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.
 

Thread Starter

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:

Thread Starter

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?
 

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.
 

Thread Starter

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.
 

Thread Starter

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?
1652405228016.png




#include <Wire.h>
#include <SPI.h>
#include <Adafruit_Sensor.h>
#include <Adafruit_BME280.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.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; // I2C
//Adafruit_BME280 bme(BME_CS); // hardware SPI
//Adafruit_BME280 bme(BME_CS, BME_MOSI, BME_MISO, BME_SCK); // software SPI

Adafruit_SSD1306 display = Adafruit_SSD1306(128, 64, &Wire);
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
display.print("H "); display.print(bme.readHumidity(),1); display.println(" %RH");
Serial.println();
display.display();
delay(10000);
analogWriteRange(1032);
analogWriteFreq(100);
uint16_t hum = ((bme.readHumidity()/100)*1032);
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
}
 
Top