A Quick Guide on Logic Level Shifting

5,758

30

6

About: Hey everyone! My name is Brian and thanks for checking my Instructables. I'm a software developer by trade but I've recently gotten into Arduino development after discovering the esp8266 chip, a WiFi enab...

Hello everyone!

I've introduced some of my friends into the wonderful world of Arduino and making and a lot of them have a hard time understanding working with different logic levels, so I thought it would be a good idea to make a simple guide about it. So if you ever struggled with logic level shifting this is the guide for you!

I'll show two really simple ways of doing logic level shifting and explain why or when you might need to do it.

Checkout the two minute video where I go through it and check out my YouTube channel for other videos on ESP8266 projects and tips.

Lets get to it!

Step 1: What Are Logic Levels?

So what are logic levels?

Logic levels normally refers to the HIGH or LOW state of your arduino pins, but not all logic levels are the same.

More traditional Arduino boards such as an UNO or a Nano have a 5V logic level, which means when you set a pin HIGH, it will measure as 5V.

But other common boards such the ESP8266 have a 3.3V logic level, which, as you can probably guess, means that when you set a pin HIGH it will measure as 3.3V.

5v and 3.3V are by far the most common logic levels that you will come across in hobby electronics.

It's also important to note that sensors often have these logic levels too. An Ultrasonic sensor is a 5V sensor, while most screens run at 3.3V logic levels

Step 2: Ok, But Why or When Do We Need to Shift Them?

If you are using a micro controller with a different logic level than a sensor you may have to consider logic level shifting, but not always.

3.3V signals going to a 5V device:

Normally if you have a 3.3V device sending signals to a 5V device you will not have any problems, so even though the 3.3V device will only measure at 3.3V when set to HIGH, this is normally over the threshold of what a 5V device considered HIGH. The first picture shows a visual representation of this.

There will be some exceptions to this, such as sometimes you will see Neopixels require logic level shifting as the 3.3V signal doesn't always reliably work with Neopixels running at 5V (My Google Maps Arduino Commute Tracker project didn't work with the 3.3v signal of my ESP8266 for example)

5V signals going to a 3.3V device:

This is where you will run into problems! Normally the max voltage a 3.3V device can accept on its pins is around 3.6V, so a 5V signal could potentially damage your device.

A good real world example of the above is connecting to an Ultrasonic sensor (5V) with an ESP8266 (3.3V). How an ultrasonic sensor works is when it receives a HIGH signal on its trigger pin it will emit an ultrasonic signal and when that signal bounces back it will send a HIGH signal on the echo pin and the microcontroller calculates the distance of the object based on how long there was between the trigger pin and the echo. The 3.3V signal on the trigger pin will work fine, but the 5V response on the echo pin will need to be shifted.

Step 3: Method 1: a Voltage Divider

The simplest and most common method of logic level shifting is a voltage divider.

In the circuit shown as in the first picture, the output of the divider is the line coming out from the middle. There is a total of 5V dropped across the circuit, but we can can calculate how much voltage is dropped across each resistor. Meaning with specific resistor values, we can get the divider to output a signal that is safe for our 3.3V device.

The equation to calculate the voltage drop across a resistor is

(R2 / (R1 + R2) ) * V

So if we use 10K Ohm resistor for R1, and a 22K Ohm resistor for R2 you would get 3.4V on the output of the divider, which would be a safe value to use on our ESP8266.

If you look at the last image it might show a clearer image of how you would wire this up.

Additional Tip (can skip if you want!):
Someone asked before could they build it with a very limited values of resistors and the answer is yes! If you have 3 of any type of resistor, if you place 3 of them in series and take the output of the circuit between R1 and R2 you will get a perfect 3.3V signal! It is recommended you use higher value of resistors so your circuit uses less current. (at least 1K i'd say)

The equation for calculating the voltage drop across the last two resistors would be
(R2 + R3 / (R1 + R2 + R3) ) * V

But if all the resistor values are the same it cancels down to

2/3 * V

Step 4: Method 2: a Bi-Directional Logic Level Shifter

This is my preferred method of doing Logic Level shifting, mainly because i'm terrible at organizing my resistors and find it hard to find the values I'm looking for! I also like them as you often need to shift several pins at once, so this is a good option for that.

It is a device called a Bi-Directional Logic Level Shifter, and they can be bought for about 30c delivered.

Basically how you use them is you connect your higher voltage (normally 5V) to the HV pin and then your lower voltage (normally 3.3V) to the LV pin, and then any signal received on any of the pins will be output on the equivalently numbered pin on the other side at the appropriate voltage.

So an example of this would be if a 5V signal was received on HV1, it would be output at 3.3V on LV1. And then also if a 3.3V signal was received on LV4, it would be output on HV4 at 5V

You can buy the Level shifters from Aliexpress for $1.20 for 5*

They are also available on Amazon.com* and Amazon.co.uk* but are much more expensive!

* = Affiliate links

Step 5: Conclusion

So hopefully you now understand a little bit more about level shifting, why and when you need to do it and how to do it.

If you have any questions please let me know I'd be happy to try help!

Thanks a lot!

Share

    Recommendations

    • Epilog X Contest

      Epilog X Contest
    • Warm and Fuzzy Contest

      Warm and Fuzzy Contest
    • Paper Contest

      Paper Contest

    6 Discussions

    0
    None
    David De Sloovere

    Question 2 months ago

    Thanks for the explanation and the diagram.
    There's one thing I curious about, why should the LV pin w/ 3.3V run back to the 3.3V pin on the Wemos? Does the Wemos need power on both 5V and 3.3V? Are both pin for power going into the board?

    2 answers

    Got an answer on the Arduino forum. The 5v pin can be both input and output, in this case it's input. The 3.3v pin is output only. So we have both 5v and 3.3v coming into the logic shifter.

    0
    None

    I don’t think that’s correct. These LLCs are bi-directional so both the LV and HV pins can be input or output depending on which is getting the signal. That’s how they should work at least....

    0
    None
    LondonSi72

    1 day ago

    I’ve just got a couple of these and thought I’d test the voltages in the pins before I wire up to my NodeMCU. I’m confused, as when I power up the board with 3.3v on low and 5v in the high side, *all* the pins seem to read high on my multi-meter 3.3v in the Lab pins and 5v on the HV pins). I’ve tried two different boards and they both seem to do this. I would have thought the meter would read 0v on the input and output pins until a 3.3v voltage is applied on, say a pin on the LV side, and 5v would then appear in the equivalent HV side. Is this a quirk if reading the pins with a multimeter or is this wrong? I don’t want to fry my NodeMCU by applying 3.3v to a pin that’s supposed to be providing an output from the NodeMCU.

    0
    None
    NathanR124

    Question 12 months ago on Step 5

    If I connect 3.3 Volt signal through these 3v3 logic shifters, will the voltage coming out be 3.3 volts? I am asking because I am curious how Adafruit creates boards that are both 3.3V and 5V logic level safe. Won't the 3.3V decrease to 1.65V?

    1 answer
    0
    None
    witnessmenowNathanR124

    Answer 12 months ago

    Yes i believe so, I'm open for correction but I don't this it manipulates the signal in at all, I believe it uses the signal in as an input to a transistor once the input is high enough to trigger the transistor you should get LV lever signal (3.3 in our case) out

    I haven't tested this but I'm 90% sure that's how it would work

    The great thing about the adafruit stuff is it is all open source so you could actually take a look at the schematic to see what they use to make a sensor or display tolerant